application deletion
This commit is contained in:
parent
a1eb3cce42
commit
001bbedced
@ -239,7 +239,7 @@ public abstract class AbstractProcess {
|
|||||||
protected HttpResponse makeRequest(String method, String urlStr, Map<String, String> httpHeaders,
|
protected HttpResponse makeRequest(String method, String urlStr, Map<String, String> httpHeaders,
|
||||||
Map<String, String> params, boolean binary) throws Exception {
|
Map<String, String> params, boolean binary) throws Exception {
|
||||||
|
|
||||||
log.info("Calling URL: " + urlStr);
|
log.info("Making {} request to URL: {}", method, urlStr);
|
||||||
String query = "";
|
String query = "";
|
||||||
if (params != null) {
|
if (params != null) {
|
||||||
for (String key : params.keySet()) {
|
for (String key : params.keySet()) {
|
||||||
|
|||||||
@ -13,6 +13,7 @@ import cz.trask.migration.AbstractProcess;
|
|||||||
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.v32.ApplicationDetail;
|
import cz.trask.migration.model.v32.ApplicationDetail;
|
||||||
|
import cz.trask.migration.model.v32.ApplicationList;
|
||||||
import cz.trask.migration.model.v45.ApplicationCreateRequest;
|
import cz.trask.migration.model.v45.ApplicationCreateRequest;
|
||||||
import cz.trask.migration.model.v45.ApplicationCreateResponse;
|
import cz.trask.migration.model.v45.ApplicationCreateResponse;
|
||||||
import io.apicurio.registry.rest.v2.beans.ArtifactMetaData;
|
import io.apicurio.registry.rest.v2.beans.ArtifactMetaData;
|
||||||
@ -65,8 +66,6 @@ public class ExportAppsToWso2FromV32 extends AbstractProcess {
|
|||||||
private void processApp(SearchedArtifact app, TokenResponse tokenResponse, int index, int total) {
|
private void processApp(SearchedArtifact app, TokenResponse tokenResponse, int index, int total) {
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
String endpoint = config.getTarget().getDevPortalApiUrl() + "/v3/applications";
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
log.info("Processing App {} of {}", index, total);
|
log.info("Processing App {} of {}", index, total);
|
||||||
|
|
||||||
@ -87,20 +86,9 @@ public class ExportAppsToWso2FromV32 extends AbstractProcess {
|
|||||||
byte[] data = mapper.writeValueAsBytes(appCreateRequest);
|
byte[] data = mapper.writeValueAsBytes(appCreateRequest);
|
||||||
log.info(" - Prepared application data for WSO2: {} bytes", data.length);
|
log.info(" - Prepared application data for WSO2: {} bytes", data.length);
|
||||||
|
|
||||||
// Publish the application data to WSO2
|
deleteWso2ApplicationIfExists(appDetail.getName(), tokenResponse);
|
||||||
byte[] decoded = Base64.getDecoder().decode(config.getTarget().getWso2User());
|
|
||||||
String decodedstring = new String(decoded);
|
|
||||||
String[] decodedstringparts = decodedstring.split(":");
|
|
||||||
|
|
||||||
String username = decodedstringparts[0];
|
HttpResponse response = publishAppToWso2(appDetail.getName(), data, tokenResponse);
|
||||||
String password = decodedstringparts[1];
|
|
||||||
Map<String, String> httpHeaders = new HashMap<>();
|
|
||||||
|
|
||||||
httpHeaders.put("Authorization", "Basic ".concat(Base64.getEncoder()
|
|
||||||
.encodeToString(username.concat(":").concat(password).getBytes())));
|
|
||||||
httpHeaders.put("Content-Type", "application/json");
|
|
||||||
|
|
||||||
HttpResponse response = makeDataRequest(endpoint, httpHeaders, data);
|
|
||||||
|
|
||||||
if (response.getResponseCode() == 200 || response.getResponseCode() == 201) {
|
if (response.getResponseCode() == 200 || response.getResponseCode() == 201) {
|
||||||
log.info(" - Application {} imported successfully", appDetail.getName());
|
log.info(" - Application {} imported successfully", appDetail.getName());
|
||||||
@ -115,30 +103,8 @@ public class ExportAppsToWso2FromV32 extends AbstractProcess {
|
|||||||
changeApplicationOwner(createdApp, appDetail.getOwner(), tokenResponse);
|
changeApplicationOwner(createdApp, appDetail.getOwner(), tokenResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (response.getResponseCode() == 409) {
|
|
||||||
log.warn(" - Application {} already exists in WSO2, skipping import", appDetail.getName());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// String fileName = api.getName() + "-" + ver.getVersion() + ".zip";
|
|
||||||
|
|
||||||
// FileOutputStream fos = new FileOutputStream(fileName);
|
|
||||||
// fos.write(data);
|
|
||||||
// fos.flush();
|
|
||||||
// fos.close();
|
|
||||||
// System.exit(0);
|
|
||||||
|
|
||||||
// if (data != null && data.length > 0 && fileName != null &&
|
|
||||||
// !fileName.isEmpty()) {
|
|
||||||
// int responseCode = publishAppToWso2(fileName, data, tokenResponse);
|
|
||||||
// if (responseCode == 200 || responseCode == 201) {
|
|
||||||
// log.info(" - API version {} imported successfully", ver.getVersion());
|
|
||||||
// } else {
|
|
||||||
// log.warn(" - API version {} import failed with response code {}",
|
|
||||||
// ver.getVersion(),
|
|
||||||
// responseCode);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
long end = System.currentTimeMillis();
|
long end = System.currentTimeMillis();
|
||||||
@ -148,6 +114,87 @@ public class ExportAppsToWso2FromV32 extends AbstractProcess {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void deleteWso2ApplicationIfExists(String appName, TokenResponse tokenResponse) {
|
||||||
|
|
||||||
|
// Resolve application id by name first (WSO2 Admin API works with applicationId in paths)
|
||||||
|
String resolvedAppId = getApplicationIdByName(config.getTarget().getAdminApiUrl(), appName, tokenResponse);
|
||||||
|
|
||||||
|
if (resolvedAppId == null) {
|
||||||
|
log.warn(" - Application {} not found in WSO2, cannot delete", appName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String endpoint = config.getTarget().getAdminApiUrl() + "/applications/" + resolvedAppId;
|
||||||
|
|
||||||
|
try {
|
||||||
|
Map<String, String> httpHeaders = new HashMap<>();
|
||||||
|
httpHeaders.put("Authorization", "Bearer ".concat(tokenResponse.getAccess_token()));
|
||||||
|
|
||||||
|
HttpResponse response = makeRequest("DELETE", endpoint, httpHeaders, null);
|
||||||
|
|
||||||
|
if (response.getResponseCode() == 200 || response.getResponseCode() == 204) {
|
||||||
|
log.info(" - Application {} (id={}) deleted successfully from WSO2", appName, resolvedAppId);
|
||||||
|
} else {
|
||||||
|
log.info(" - Application {} (id={}) deletion from WSO2 returned response code {}",
|
||||||
|
appName, resolvedAppId, response.getResponseCode());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("IO error while deleting Application {} (id={}): {}", appName, resolvedAppId,
|
||||||
|
e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getApplicationIdByName(String adminApiUrl, String appName, TokenResponse tokenResponse) {
|
||||||
|
try {
|
||||||
|
String url = adminApiUrl.concat(String.format("/applications"));
|
||||||
|
|
||||||
|
Map<String, String> httpHeaders = new HashMap<>();
|
||||||
|
Map<String, String> params = new HashMap<>();
|
||||||
|
|
||||||
|
httpHeaders.put("Authorization", "Bearer ".concat(tokenResponse.getAccess_token()));
|
||||||
|
|
||||||
|
HttpResponse response = makeRequest("GET", url, httpHeaders, params);
|
||||||
|
|
||||||
|
if (response.getResponseCode() != 200) {
|
||||||
|
log.warn("Cannot list Applications (HTTP {}). Will not attempt delete.", response.getResponseCode());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
ApplicationList listOfApps = mapper.readValue(response.getResponse(), ApplicationList.class);
|
||||||
|
|
||||||
|
if (listOfApps != null && listOfApps.getList() != null) {
|
||||||
|
for (ApplicationList.ApplicationInfo info : listOfApps.getList()) {
|
||||||
|
if (appName.equals(info.getName())) {
|
||||||
|
return info.getApplicationId();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Error while resolving Application id for name {}: {}", appName, e.getMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private HttpResponse publishAppToWso2(String name, byte[] data, TokenResponse tokenResponse) throws Exception {
|
||||||
|
// Publish the application data to WSO2
|
||||||
|
byte[] decoded = Base64.getDecoder().decode(config.getTarget().getWso2User());
|
||||||
|
String decodedstring = new String(decoded);
|
||||||
|
String[] decodedstringparts = decodedstring.split(":");
|
||||||
|
|
||||||
|
String username = decodedstringparts[0];
|
||||||
|
String password = decodedstringparts[1];
|
||||||
|
Map<String, String> httpHeaders = new HashMap<>();
|
||||||
|
|
||||||
|
httpHeaders.put("Authorization", "Basic ".concat(Base64.getEncoder()
|
||||||
|
.encodeToString(username.concat(":").concat(password).getBytes())));
|
||||||
|
httpHeaders.put("Content-Type", "application/json");
|
||||||
|
|
||||||
|
String endpoint = config.getTarget().getDevPortalApiUrl() + "/v3/applications";
|
||||||
|
|
||||||
|
return makeDataRequest(endpoint, httpHeaders, data);
|
||||||
|
}
|
||||||
|
|
||||||
private void changeApplicationOwner(ApplicationCreateResponse createdApp, String origOwner,
|
private void changeApplicationOwner(ApplicationCreateResponse createdApp, String origOwner,
|
||||||
TokenResponse tokenResponse) {
|
TokenResponse tokenResponse) {
|
||||||
String endpoint = config.getTarget().getAdminApiUrl() + "/applications/" + createdApp.getApplicationId()
|
String endpoint = config.getTarget().getAdminApiUrl() + "/applications/" + createdApp.getApplicationId()
|
||||||
@ -179,6 +226,7 @@ public class ExportAppsToWso2FromV32 extends AbstractProcess {
|
|||||||
request.setThrottlingPolicy(appDetail.getTier());
|
request.setThrottlingPolicy(appDetail.getTier());
|
||||||
request.setTokenType(appDetail.getTokenType());
|
request.setTokenType(appDetail.getTokenType());
|
||||||
request.setGroups(List.of(appDetail.getGroupId()));
|
request.setGroups(List.of(appDetail.getGroupId()));
|
||||||
|
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user