in services/onboarding-service/src/main/java/com/amazon/aws/partners/saasfactory/saasboost/OnboardingService.java [868:1017]
public APIGatewayProxyResponseEvent statusEventListener(Map<String, Object> event, Context context) {
if (Utils.isBlank(SAAS_BOOST_EVENT_BUS)) {
throw new IllegalStateException("Missing required environment variable SAAS_BOOST_EVENT_BUS");
}
long startTimeMillis = System.currentTimeMillis();
LOGGER.info("OnboardingService::statusEventListener");
Utils.logRequestEvent(event);
APIGatewayProxyResponseEvent response = null;
if ("aws.codepipeline".equals(event.get("source"))) {
Map<String, Object> detail = (Map<String, Object>) event.get("detail");
OnboardingStatus status = null;
Object pipelineState = detail.get("state");
if ("STARTED".equals(pipelineState)) {
status = OnboardingStatus.deploying;
} else if ("FAILED".equals(pipelineState) || "CANCELED".equals(pipelineState)) {
status = OnboardingStatus.failed;
} else if ("SUCCEEDED".equals(pipelineState)) {
status = OnboardingStatus.deployed;
}
String pipeline = (String) detail.get("pipeline");
String prefix = "tenant-";
if (pipeline != null && pipeline.startsWith(prefix)) {
String tenantId = pipeline.substring(prefix.length());
Onboarding onboarding = dal.getOnboardingByTenantId(tenantId);
if (onboarding != null) {
tenantId = onboarding.getTenantId().toString();
LOGGER.info("OnboardingService::statusEventListener Updating Onboarding status for tenant " + tenantId + " to " + status);
onboarding = dal.updateStatus(onboarding.getId(), status);
response = new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withHeaders(CORS)
.withBody(Utils.toJson(onboarding));
// And update the tenant record
if (OnboardingStatus.deployed == onboarding.getStatus()) {
try {
ObjectNode systemApiRequest = MAPPER.createObjectNode();
systemApiRequest.put("resource", "tenants/" + tenantId + "/onboarding");
systemApiRequest.put("method", "PUT");
systemApiRequest.put("body", "{\"id\":\"" + tenantId + "\", \"onboardingStatus\":\"succeeded\"}");
PutEventsRequestEntry systemApiCallEvent = PutEventsRequestEntry.builder()
.eventBusName(SAAS_BOOST_EVENT_BUS)
.detailType(SYSTEM_API_CALL_DETAIL_TYPE)
.source(SYSTEM_API_CALL_SOURCE)
.detail(MAPPER.writeValueAsString(systemApiRequest))
.build();
PutEventsResponse eventBridgeResponse = eventBridge.putEvents(r -> r
.entries(systemApiCallEvent)
);
for (PutEventsResultEntry entry : eventBridgeResponse.entries()) {
if (entry.eventId() != null && !entry.eventId().isEmpty()) {
LOGGER.info("Put event success {} {}", entry.toString(), systemApiCallEvent.toString());
} else {
LOGGER.error("Put event failed {}", entry.toString());
}
}
} catch (JsonProcessingException ioe) {
LOGGER.error("JSON processing failed");
LOGGER.error(Utils.getFullStackTrace(ioe));
throw new RuntimeException(ioe);
} catch (SdkServiceException eventBridgeError) {
LOGGER.error("events::PutEvents");
LOGGER.error(Utils.getFullStackTrace(eventBridgeError));
throw eventBridgeError;
}
}
} else {
response = new APIGatewayProxyResponseEvent().withHeaders(CORS).withStatusCode(404);
}
}
} else if (event.containsKey("body")) {
Map<String, Object> body = Utils.fromJson((String) event.get("body"), Map.class);
if (null == body) {
return new APIGatewayProxyResponseEvent()
.withStatusCode(400)
.withHeaders(CORS)
.withBody("{\"message\": \"Invalid request body.\"}");
}
String tenantId = (String) body.get("tenantId");
String provisioningStatus = (String) body.get("stackStatus");
OnboardingStatus status = OnboardingStatus.failed;
if ("CREATE_COMPLETE".equals(provisioningStatus)) {
status = OnboardingStatus.provisioned;
} else if ("UPDATE_COMPLETE".equals(provisioningStatus)) {
status = OnboardingStatus.updated;
} else if ("CREATE_FAILED".equals(provisioningStatus) || "DELETE_FAILED".equals(provisioningStatus)) {
status = OnboardingStatus.failed;
} else if ("DELETE_COMPLETE".equals(provisioningStatus)) {
status = OnboardingStatus.deleted;
}
Onboarding onboarding = dal.getOnboardingByTenantId(tenantId);
if (onboarding != null) {
LOGGER.info("OnboardingService::statusEventListener Updating Onboarding status for tenant " + onboarding.getTenantId() + " to " + status);
onboarding = dal.updateStatus(onboarding.getId(), status);
//update the Tenant record status
try {
ObjectNode systemApiRequest = MAPPER.createObjectNode();
systemApiRequest.put("resource", "tenants/" + tenantId + "/onboarding");
systemApiRequest.put("method", "PUT");
systemApiRequest.put("body", "{\"id\":\"" + tenantId + "\", \"onboardingStatus\":\"" + status + "\"}");
PutEventsRequestEntry systemApiCallEvent = PutEventsRequestEntry.builder()
.eventBusName(SAAS_BOOST_EVENT_BUS)
.detailType(SYSTEM_API_CALL_DETAIL_TYPE)
.source(SYSTEM_API_CALL_SOURCE)
.detail(MAPPER.writeValueAsString(systemApiRequest))
.build();
PutEventsResponse eventBridgeResponse = eventBridge.putEvents(r -> r
.entries(systemApiCallEvent)
);
for (PutEventsResultEntry entry : eventBridgeResponse.entries()) {
if (entry.eventId() != null && !entry.eventId().isEmpty()) {
LOGGER.info("Put event success {} {}", entry.toString(), systemApiCallEvent.toString());
} else {
LOGGER.error("Put event failed {}", entry.toString());
}
}
if (status.equals(OnboardingStatus.provisioned)) {
//move the s3 file from the SAAS_BOOST_BUCKET to a key for the tenant and name it config.zip
moveTenantConfigFile(onboarding.getId().toString(), tenantId);
}
} catch (JsonProcessingException ioe) {
LOGGER.error("JSON processing failed");
LOGGER.error(Utils.getFullStackTrace(ioe));
throw new RuntimeException(ioe);
} catch (SdkServiceException eventBridgeError) {
LOGGER.error("events::PutEvents");
LOGGER.error(Utils.getFullStackTrace(eventBridgeError));
throw eventBridgeError;
}
response = new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withHeaders(CORS)
.withBody(Utils.toJson(onboarding));
} else {
response = new APIGatewayProxyResponseEvent().withHeaders(CORS).withStatusCode(404);
}
}
if (response == null) {
response = new APIGatewayProxyResponseEvent()
.withHeaders(CORS)
.withStatusCode(400)
.withBody("{\"message\":\"Empty request body.\"}");
}
long totalTimeMillis = System.currentTimeMillis() - startTimeMillis;
LOGGER.info("OnboardingService::statusEventListener exec " + totalTimeMillis);
return response;
}