Fix 'Export/Import of a plugin with policies does not work because of its ID'
Closes
MODIFY
manager/api/beans/src/main/java/io/apiman/manager/api/beans/policies/PolicyDefinitionBean.java
manager/api/beans/src/main/java/io/apiman/manager/api/beans/policies/PolicyDefinitionBean.java
@@ -66,7 +66,7 @@ public class PolicyDefinitionBean implements Serializable {
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "pd_templates", joinColumns = @JoinColumn(name = "policydef_id"))
private Set<PolicyDefinitionTemplateBean> templates = new HashSet<>();
- @Column(name = "plugin_id", updatable=false, nullable=true)
+ @Column(name = "plugin_id", updatable=true, nullable=true)
private Long pluginId;
@Column(name = "form_type", updatable=true, nullable=true)
@Enumerated(EnumType.STRING)

MODIFY
manager/api/export-import/src/main/java/io/apiman/manager/api/exportimport/manager/StorageImportDispatcher.java
manager/api/export-import/src/main/java/io/apiman/manager/api/exportimport/manager/StorageImportDispatcher.java
@@ -51,6 +51,7 @@ import io.apiman.manager.api.exportimport.read.IImportReaderDispatcher;
import io.apiman.manager.api.gateway.IGatewayLink;
import io.apiman.manager.api.gateway.IGatewayLinkFactory;
+import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -79,6 +80,7 @@ public class StorageImportDispatcher implements IImportReaderDispatcher {
private IGatewayLinkFactory gatewayLinks;
private Map<String, PolicyDefinitionBean> policyDefIndex = new HashMap<>();
+ private Map<Long, Map.Entry<String, String>> pluginBeanIdMap = new HashMap<>();
private OrganizationBean currentOrg;
private PlanBean currentPlan;
@@ -183,6 +185,7 @@ public class StorageImportDispatcher implements IImportReaderDispatcher {
public void plugin(PluginBean plugin) {
try {
logger.info(Messages.i18n.format("StorageImportDispatcher.ImportingPlugin") + plugin.getGroupId() + '/' + plugin.getArtifactId() + '/' + plugin.getVersion()); //$NON-NLS-1$
+ mapPluginIds(plugin);
plugin.setId(null);
storage.createPlugin(plugin);
} catch (StorageException e) {
@@ -190,6 +193,10 @@ public class StorageImportDispatcher implements IImportReaderDispatcher {
}
}
+ private void mapPluginIds(PluginBean plugin) {
+ pluginBeanIdMap.put(plugin.getId(), new AbstractMap.SimpleEntry<>(plugin.getGroupId(), plugin.getArtifactId()));
+ }
+
/**
* @see io.apiman.manager.api.exportimport.read.IImportReaderDispatcher#gateway(io.apiman.manager.api.beans.gateways.GatewayBean)
*/
@@ -216,6 +223,8 @@ public class StorageImportDispatcher implements IImportReaderDispatcher {
try {
logger.info(Messages.i18n.format("StorageImportDispatcher.ImportingPolicyDef") + policyDef.getName()); //$NON-NLS-1$
+ policyDef = updatePluginIdInPolicyDefinition(policyDef);
+
PolicyDefinitionBean policyDefBean = storage.getPolicyDefinition(policyDef.getId());
if (policyDefBean != null) {
storage.updatePolicyDefinition(policyDef);
@@ -228,6 +237,24 @@ public class StorageImportDispatcher implements IImportReaderDispatcher {
}
}
+ /**
+ * Update the pluginID in the policyDefinition to the new generated pluginID
+ * @param policyDef
+ * @return updated PolicyDefinitionBean policyDef
+ */
+ private PolicyDefinitionBean updatePluginIdInPolicyDefinition(PolicyDefinitionBean policyDef) {
+ if (pluginBeanIdMap.containsKey(policyDef.getPluginId())){
+ try {
+ Map.Entry<String, String> pluginCoordinates = pluginBeanIdMap.get(policyDef.getPluginId());
+ PluginBean plugin = storage.getPlugin(pluginCoordinates.getKey(), pluginCoordinates.getValue());
+ policyDef.setPluginId(plugin.getId());
+ } catch (StorageException e) {
+ error(e);
+ }
+ }
+ return policyDef;
+ }
+
/**
* @see io.apiman.manager.api.exportimport.read.IImportReaderDispatcher#organization(io.apiman.manager.api.beans.orgs.OrganizationBean)
*/

MODIFY
manager/test/api/src/test/resources/test-plan-data/migrate/002_export.resttest
manager/test/api/src/test/resources/test-plan-data/migrate/002_export.resttest
@@ -273,7 +273,7 @@ X-RestTest-ArrayOrdering: any
"description": "Turns an endpoint into a JSONP compatible endpoint.",
"icon": "bars",
"templates": [],
- "pluginId": 79,
+ "pluginId": 2,
"formType": "JsonSchema",
"form": "schemas/jsonp-policyDef.schema",
"deleted": false
@@ -285,7 +285,7 @@ X-RestTest-ArrayOrdering: any
"description": "A policy that logs the headers to std out. Useful to analyse inbound HTTP traffic to the gateway when added as the first policy in the chain or to analyse outbound HTTP traffic from the gateway when added as the last policy in the chain.",
"icon": "fire",
"templates": [],
- "pluginId": 78,
+ "pluginId": 1,
"formType": "JsonSchema",
"form": "schemas/log-policyDef.schema",
"deleted": false

Fix 'Export/Import of a plugin with policies does not work because of its ID'

Closes #660