added subscriptions
This commit is contained in:
parent
84f228c2a1
commit
d09c7623bb
@ -38,6 +38,9 @@ public abstract class AbstractProcess {
|
|||||||
|
|
||||||
public static final String PRIVATE_KEY_APIM_32 = "wso2apim32-pk.pem";
|
public static final String PRIVATE_KEY_APIM_32 = "wso2apim32-pk.pem";
|
||||||
|
|
||||||
|
public static final String ARTIFACT_GROUP_SUBSCRIPTIONS = "SUBSCRIPTIONS";
|
||||||
|
public static final String ARTIFACT_NAME_SUBSCRIPTIONS = "subs.yaml";
|
||||||
|
|
||||||
public static ObjectMapper mapper;
|
public static ObjectMapper mapper;
|
||||||
public static ObjectMapper mapperYaml;
|
public static ObjectMapper mapperYaml;
|
||||||
|
|
||||||
|
|||||||
@ -21,11 +21,11 @@ public class ApiSync {
|
|||||||
StartParameters sp = StartParameters.parse(commandLine);
|
StartParameters sp = StartParameters.parse(commandLine);
|
||||||
log.info("Parsed parameters: " + sp);
|
log.info("Parsed parameters: " + sp);
|
||||||
|
|
||||||
if (sp.getCommand().equals("import")) {
|
if (sp.getCommand().equalsIgnoreCase("wso2ToApicurio")) {
|
||||||
log.info("Import command selected.");
|
log.info("Import command selected.");
|
||||||
ImportToApicurio imp = new ImportToApicurio();
|
ImportToApicurio imp = new ImportToApicurio();
|
||||||
imp.process();
|
imp.process();
|
||||||
} else if (sp.getCommand().equals("export")) {
|
} else if (sp.getCommand().equals("apicurioToWso2")) {
|
||||||
log.info("Export command selected.");
|
log.info("Export command selected.");
|
||||||
ExportToWso2FromV32 exp = new ExportToWso2FromV32();
|
ExportToWso2FromV32 exp = new ExportToWso2FromV32();
|
||||||
exp.process();
|
exp.process();
|
||||||
|
|||||||
@ -25,6 +25,9 @@ import cz.trask.migration.model.FileType;
|
|||||||
import cz.trask.migration.model.HttpResponse;
|
import cz.trask.migration.model.HttpResponse;
|
||||||
import cz.trask.migration.model.TokenResponse;
|
import cz.trask.migration.model.TokenResponse;
|
||||||
import cz.trask.migration.model.ZipEntryData;
|
import cz.trask.migration.model.ZipEntryData;
|
||||||
|
import cz.trask.migration.model.v32.Subscriptions;
|
||||||
|
import cz.trask.migration.model.v32.Subscriptions.ApplicationInfo;
|
||||||
|
import cz.trask.migration.model.v32.Subscriptions.Subscription;
|
||||||
import io.apicurio.registry.rest.client.RegistryClient;
|
import io.apicurio.registry.rest.client.RegistryClient;
|
||||||
import io.apicurio.registry.rest.client.RegistryClientFactory;
|
import io.apicurio.registry.rest.client.RegistryClientFactory;
|
||||||
import io.apicurio.registry.rest.client.exception.VersionAlreadyExistsException;
|
import io.apicurio.registry.rest.client.exception.VersionAlreadyExistsException;
|
||||||
@ -105,8 +108,9 @@ public class ImportToApicurio extends AbstractProcess {
|
|||||||
"Bearer " + tokenResponse.getAccess_token());
|
"Bearer " + tokenResponse.getAccess_token());
|
||||||
|
|
||||||
// 1) Retrieve basic information
|
// 1) Retrieve basic information
|
||||||
HttpResponse apiInfoResp = makeRequest("GET", config.getSource().getDevPortalApiUrl() + "/apis/" + api.getId(),
|
HttpResponse apiInfoResp = makeRequest("GET",
|
||||||
httpHeaders, Collections.emptyMap());
|
config.getSource().getDevPortalApiUrl() + "/apis/" + api.getId(), httpHeaders,
|
||||||
|
Collections.emptyMap());
|
||||||
|
|
||||||
HttpResponse subsResp = makeRequest("GET",
|
HttpResponse subsResp = makeRequest("GET",
|
||||||
config.getSource().getPublisherApiUrl() + "/subscriptions?apiId=" + api.getId(), httpHeaders,
|
config.getSource().getPublisherApiUrl() + "/subscriptions?apiId=" + api.getId(), httpHeaders,
|
||||||
@ -131,7 +135,7 @@ public class ImportToApicurio extends AbstractProcess {
|
|||||||
|
|
||||||
// 3) Deserialize JSON responses
|
// 3) Deserialize JSON responses
|
||||||
Map<String, Object> apiMap = mapper.readValue(apiInfoResp.getResponse(), Map.class);
|
Map<String, Object> apiMap = mapper.readValue(apiInfoResp.getResponse(), Map.class);
|
||||||
Map<String, Object> subsMap = mapper.readValue(subsResp.getResponse(), Map.class);
|
Subscriptions subs = mapper.readValue(subsResp.getResponse(), Subscriptions.class);
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
List<String> tagsList = (List<String>) apiMap.get("tags");
|
List<String> tagsList = (List<String>) apiMap.get("tags");
|
||||||
@ -141,7 +145,7 @@ public class ImportToApicurio extends AbstractProcess {
|
|||||||
props.put("version", api.getVersion());
|
props.put("version", api.getVersion());
|
||||||
props.put("status", status);
|
props.put("status", status);
|
||||||
props.put(PARAM_SOURCE_APIM, VERSION_32);
|
props.put(PARAM_SOURCE_APIM, VERSION_32);
|
||||||
addSubscriptionsToProps(props, subsMap);
|
addSubscriptionsToProps(props, subs);
|
||||||
addEndpointsToProps(props, apiMap);
|
addEndpointsToProps(props, apiMap);
|
||||||
addTagsToProps(props, tagsList);
|
addTagsToProps(props, tagsList);
|
||||||
|
|
||||||
@ -173,12 +177,13 @@ public class ImportToApicurio extends AbstractProcess {
|
|||||||
if (existingArtifacts == null) {
|
if (existingArtifacts == null) {
|
||||||
// Create new artifact
|
// Create new artifact
|
||||||
List<ArtifactReference> references = createReferencesFromZip(zipEntries, group, api);
|
List<ArtifactReference> references = createReferencesFromZip(zipEntries, group, api);
|
||||||
|
addSubscriptionsToReferences(references, subs, api);
|
||||||
|
|
||||||
ArtifactMetaData meta = client.createArtifact(group, mainArtifactId, api.getVersion(), null, null, null,
|
ArtifactMetaData meta = client.createArtifact(group, mainArtifactId, api.getVersion(), null, null, null,
|
||||||
api.getName(), fullDesc, null, null, null,
|
api.getName(), fullDesc, null, null, null,
|
||||||
new ByteArrayInputStream(swaggerObj.toString().getBytes()), references);
|
new ByteArrayInputStream(swaggerObj.toString().getBytes()), references);
|
||||||
|
|
||||||
setArtifactMetaData(meta, props, tagsList);
|
setArtifactMetaData(meta, props);
|
||||||
// Create the three required rules
|
// Create the three required rules
|
||||||
createRule(meta, "NONE", RuleType.COMPATIBILITY);
|
createRule(meta, "NONE", RuleType.COMPATIBILITY);
|
||||||
createRule(meta, "NONE", RuleType.VALIDITY);
|
createRule(meta, "NONE", RuleType.VALIDITY);
|
||||||
@ -195,12 +200,13 @@ public class ImportToApicurio extends AbstractProcess {
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<ArtifactReference> references = createReferencesFromZip(zipEntries, group, api);
|
List<ArtifactReference> references = createReferencesFromZip(zipEntries, group, api);
|
||||||
|
addSubscriptionsToReferences(references, subs, api);
|
||||||
|
|
||||||
if (!versionExists) {
|
if (!versionExists) {
|
||||||
ArtifactMetaData meta = client.updateArtifact(group, mainArtifactId, api.getVersion(),
|
ArtifactMetaData meta = client.updateArtifact(group, mainArtifactId, api.getVersion(),
|
||||||
api.getName(), fullDesc, new ByteArrayInputStream(swaggerObj.toString().getBytes()),
|
api.getName(), fullDesc, new ByteArrayInputStream(swaggerObj.toString().getBytes()),
|
||||||
references);
|
references);
|
||||||
setArtifactMetaData(meta, props, tagsList);
|
setArtifactMetaData(meta, props);
|
||||||
} else {
|
} else {
|
||||||
// Version already exists – no action needed
|
// Version already exists – no action needed
|
||||||
log.warn("API {} with version {} already exists. Skipping import.", api.getContext(),
|
log.warn("API {} with version {} already exists. Skipping import.", api.getContext(),
|
||||||
@ -257,19 +263,15 @@ public class ImportToApicurio extends AbstractProcess {
|
|||||||
swagger.set("servers", servers);
|
swagger.set("servers", servers);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addSubscriptionsToProps(Map<String, String> props, Map<String, Object> subsMap) {
|
private void addSubscriptionsToProps(Map<String, String> props, Subscriptions subs) {
|
||||||
if (subsMap == null || !subsMap.containsKey("list"))
|
if (subs == null || subs.getList() == null || subs.getList().isEmpty())
|
||||||
return;
|
return;
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
List<Map<String, Object>> list = (List<Map<String, Object>>) subsMap.get("list");
|
|
||||||
int i = 1;
|
int i = 1;
|
||||||
for (Map<String, Object> sub : list) {
|
for (Subscription sub : subs.getList()) {
|
||||||
@SuppressWarnings("unchecked")
|
ApplicationInfo appInfo = sub.getApplicationInfo();
|
||||||
Map<String, String> appInfo = (Map<String, String>) sub.get("applicationInfo");
|
|
||||||
if (appInfo == null)
|
if (appInfo == null)
|
||||||
continue;
|
continue;
|
||||||
props.put("subscription" + i,
|
props.put("subscription" + i, appInfo.getName() + " (Owner: " + appInfo.getSubscriber() + ")");
|
||||||
appInfo.getOrDefault("name", "") + " (Owner: " + appInfo.getOrDefault("subscriber", "") + ")");
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -284,7 +286,10 @@ public class ImportToApicurio extends AbstractProcess {
|
|||||||
Map<String, String> urls = (Map<String, String>) env.get("URLs");
|
Map<String, String> urls = (Map<String, String>) env.get("URLs");
|
||||||
if (urls == null)
|
if (urls == null)
|
||||||
continue;
|
continue;
|
||||||
urls.forEach((k, v) -> props.put(k + " Endpoint", v));
|
urls.forEach((k, v) -> {
|
||||||
|
if (v != null)
|
||||||
|
props.put(k + " Endpoint", v);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -306,7 +311,7 @@ public class ImportToApicurio extends AbstractProcess {
|
|||||||
api.getVersion(), is);
|
api.getVersion(), is);
|
||||||
Map<String, String> props = new LinkedHashMap<>();
|
Map<String, String> props = new LinkedHashMap<>();
|
||||||
props.put(PARAM_SOURCE_APIM, VERSION_32);
|
props.put(PARAM_SOURCE_APIM, VERSION_32);
|
||||||
setArtifactMetaData(meta, props, Collections.emptyList());
|
setArtifactMetaData(meta, props);
|
||||||
}
|
}
|
||||||
|
|
||||||
ArtifactReference ref = new ArtifactReference();
|
ArtifactReference ref = new ArtifactReference();
|
||||||
@ -319,14 +324,37 @@ public class ImportToApicurio extends AbstractProcess {
|
|||||||
return references;
|
return references;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setArtifactMetaData(ArtifactMetaData meta, Map<String, String> props, List<String> tags) {
|
private void addSubscriptionsToReferences(List<ArtifactReference> references, Subscriptions subs, APIInfo api)
|
||||||
|
throws Exception {
|
||||||
|
if (subs == null || subs.getList() == null || subs.getList().isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
String artifactId = api.getName() + "/" + api.getVersion() + "/" + ARTIFACT_NAME_SUBSCRIPTIONS;
|
||||||
|
|
||||||
|
byte[] subsBytes = mapperYaml.writeValueAsBytes(subs);
|
||||||
|
|
||||||
|
try (ByteArrayInputStream is = new ByteArrayInputStream(subsBytes)) {
|
||||||
|
ArtifactMetaData meta = client.createArtifactWithVersion(ARTIFACT_GROUP_SUBSCRIPTIONS, artifactId,
|
||||||
|
api.getVersion(), is);
|
||||||
|
Map<String, String> props = new LinkedHashMap<>();
|
||||||
|
props.put(PARAM_SOURCE_APIM, VERSION_32);
|
||||||
|
setArtifactMetaData(meta, props);
|
||||||
|
}
|
||||||
|
|
||||||
|
ArtifactReference ref = new ArtifactReference();
|
||||||
|
ref.setName(ARTIFACT_NAME_SUBSCRIPTIONS);
|
||||||
|
ref.setGroupId(ARTIFACT_GROUP_SUBSCRIPTIONS);
|
||||||
|
ref.setArtifactId(artifactId);
|
||||||
|
ref.setVersion(api.getVersion());
|
||||||
|
references.add(ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setArtifactMetaData(ArtifactMetaData meta, Map<String, String> props) {
|
||||||
EditableMetaData metaData = new EditableMetaData();
|
EditableMetaData metaData = new EditableMetaData();
|
||||||
metaData.setName(meta.getName());
|
metaData.setName(meta.getName());
|
||||||
metaData.setDescription(meta.getDescription());
|
metaData.setDescription(meta.getDescription());
|
||||||
if (props != null)
|
if (props != null)
|
||||||
metaData.setProperties(props);
|
metaData.setProperties(props);
|
||||||
if (tags != null)
|
|
||||||
metaData.setLabels(tags);
|
|
||||||
|
|
||||||
client.updateArtifactMetaData(meta.getGroupId(), meta.getId(), metaData);
|
client.updateArtifactMetaData(meta.getGroupId(), meta.getId(), metaData);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
package cz.trask.migration.model;
|
package cz.trask.migration.model;
|
||||||
|
|
||||||
public enum FileType {
|
public enum FileType {
|
||||||
APIDEF, OPENAPI, WSDL, POLICY_IN, POLICY_OUT, POLICY_FAULT, CERTIFICATE, UNKNOWN
|
APIDEF, OPENAPI, WSDL, POLICY_IN, POLICY_OUT, POLICY_FAULT, CERTIFICATE, SUBSCRIPTIONS, UNKNOWN
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,31 @@
|
|||||||
|
package cz.trask.migration.model.v32;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public class Subscriptions {
|
||||||
|
private List<Subscription> list;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public static class Subscription {
|
||||||
|
private String subscriptionId;
|
||||||
|
private ApplicationInfo applicationInfo;
|
||||||
|
private String throttlingPolicy;
|
||||||
|
private String subscriptionStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public static class ApplicationInfo {
|
||||||
|
private String applicationId;
|
||||||
|
private String name;
|
||||||
|
private String subscriber;
|
||||||
|
private String description;
|
||||||
|
private int subscriptionCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user