Skip to content

Commit

Permalink
Move duplicate handler code to super class (#13)
Browse files Browse the repository at this point in the history
* Add ListHandler with UnitTests

* Move duplicate handler code into super class
  • Loading branch information
nopx authored May 12, 2020
1 parent 72f8a2a commit a725113
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 215 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package software.amazon.opsworkscm.server;

import com.amazonaws.util.StringUtils;
import software.amazon.awssdk.services.opsworkscm.OpsWorksCmClient;
import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy;
import software.amazon.cloudformation.proxy.Logger;
import software.amazon.cloudformation.proxy.ProgressEvent;
import software.amazon.cloudformation.proxy.ResourceHandlerRequest;
import software.amazon.cloudformation.resource.IdentifierUtils;

abstract public class BaseOpsWorksCMHandler extends BaseHandler<CallbackContext> {


protected ResourceModel model;
protected ResourceModel oldModel;
protected CallbackContext callbackContext;
protected Logger logger;
protected ResourceHandlerRequest<ResourceModel> request;
protected ClientWrapper client;

protected static int NO_CALLBACK_DELAY = 0;
protected static int CALLBACK_DELAY_SECONDS = 60;
private static final int MAX_LENGTH_CONFIGURATION_SET_NAME = 40;

@Override
abstract public ProgressEvent<ResourceModel, CallbackContext> handleRequest(
final AmazonWebServicesClientProxy proxy,
final ResourceHandlerRequest<ResourceModel> request,
final CallbackContext callbackContext,
final Logger logger);

protected void initialize(final AmazonWebServicesClientProxy proxy,
final ResourceHandlerRequest<ResourceModel> request,
final CallbackContext callbackContext,
final Logger logger) {
this.request = request;
this.model = request.getDesiredResourceState();
this.oldModel = request.getPreviousResourceState();
this.callbackContext = callbackContext;
this.logger = logger;

setModelServerName();
setModelId();

final OpsWorksCmClient opsWorksCmClientclient = ClientBuilder.getClient();
this.client = new ClientWrapper(opsWorksCmClientclient, model, oldModel, proxy, logger);
}

private void setModelServerName() {
if (StringUtils.isNullOrEmpty(model.getServerName())) {
logger.log("RequestModel doesn't have the server name. Setting it using request identifier and client token");
model.setServerName(
IdentifierUtils.generateResourceIdentifier(
request.getLogicalResourceIdentifier(),
request.getClientRequestToken(),
MAX_LENGTH_CONFIGURATION_SET_NAME
)
);
} else if (model.getServerName().length() > MAX_LENGTH_CONFIGURATION_SET_NAME) {
logger.log(String.format("ServerName length was greater than %d characters. Truncating the ServerName", MAX_LENGTH_CONFIGURATION_SET_NAME));
model.setServerName(model.getServerName().substring(0, MAX_LENGTH_CONFIGURATION_SET_NAME));
}
}

private void setModelId() {
if (model.getId() == null) {
logger.log("RequestModel doesn't have the model id. Setting it using request identifier and client token");
model.setId(IdentifierUtils.generateResourceIdentifier(
request.getLogicalResourceIdentifier(),
request.getClientRequestToken()
));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package software.amazon.opsworkscm.server;

import com.amazonaws.util.StringUtils;
import software.amazon.awssdk.services.opsworkscm.OpsWorksCmClient;
import software.amazon.awssdk.services.opsworkscm.model.DescribeServersResponse;
import software.amazon.awssdk.services.opsworkscm.model.InvalidStateException;
import software.amazon.awssdk.services.opsworkscm.model.ResourceAlreadyExistsException;
Expand All @@ -13,20 +11,8 @@
import software.amazon.cloudformation.proxy.Logger;
import software.amazon.cloudformation.proxy.ProgressEvent;
import software.amazon.cloudformation.proxy.ResourceHandlerRequest;
import software.amazon.cloudformation.resource.IdentifierUtils;

public class CreateHandler extends BaseHandler<CallbackContext> {

ResourceModel model;
ResourceModel oldModel;
CallbackContext callbackContext;
Logger logger;
ResourceHandlerRequest<ResourceModel> request;
ClientWrapper client;

private static int NO_CALLBACK_DELAY = 0;
private static int CALLBACK_DELAY_SECONDS = 60;
private static final int MAX_LENGTH_CONFIGURATION_SET_NAME = 40;
public class CreateHandler extends BaseOpsWorksCMHandler {

@Override
public ProgressEvent<ResourceModel, CallbackContext> handleRequest(
Expand All @@ -35,17 +21,7 @@ public ProgressEvent<ResourceModel, CallbackContext> handleRequest(
final CallbackContext callbackContext,
final Logger logger) {

this.request = request;
this.model = request.getDesiredResourceState();
this.oldModel = request.getPreviousResourceState();
this.callbackContext = callbackContext;
this.logger = logger;

setModelServerName();
setModelId();

final OpsWorksCmClient opsWorksCmClientclient = ClientBuilder.getClient();
this.client = new ClientWrapper(opsWorksCmClientclient, model, oldModel, proxy, logger);
initialize(proxy, request, callbackContext, logger);

try {
if (callbackContext.isStabilizationStarted()) {
Expand Down Expand Up @@ -118,31 +94,6 @@ private ProgressEvent<ResourceModel, CallbackContext> handleStabilize() {
}
}

private void setModelServerName() {
if (StringUtils.isNullOrEmpty(model.getServerName())) {
logger.log("RequestModel doesn't have the server name. Setting it using request identifier and client token");
model.setServerName(
IdentifierUtils.generateResourceIdentifier(
request.getLogicalResourceIdentifier(),
request.getClientRequestToken(),
MAX_LENGTH_CONFIGURATION_SET_NAME
)
);
} else if (model.getServerName().length() > MAX_LENGTH_CONFIGURATION_SET_NAME) {
logger.log(String.format("ServerName length was greater than %d characters. Truncating the ServerName", MAX_LENGTH_CONFIGURATION_SET_NAME));
model.setServerName(model.getServerName().substring(0, MAX_LENGTH_CONFIGURATION_SET_NAME));
}
}

private void setModelId() {
if (model.getId() == null) {
logger.log("RequestModel doesn't have the model id. Setting it using request identifier and client token");
model.setId(IdentifierUtils.generateResourceIdentifier(
request.getLogicalResourceIdentifier(),
request.getClientRequestToken()
));
}
}

private ProgressEvent<ResourceModel, CallbackContext> handleServerNotFound(final String serverName) {
logger.log(String.format("Server %s failed to CREATE because it was not found.", serverName));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,31 @@
package software.amazon.opsworkscm.server;

import com.amazonaws.util.StringUtils;
import software.amazon.awssdk.services.opsworkscm.OpsWorksCmClient;
import software.amazon.awssdk.services.opsworkscm.model.DescribeServersResponse;
import software.amazon.awssdk.services.opsworkscm.model.InvalidStateException;
import software.amazon.awssdk.services.opsworkscm.model.ResourceNotFoundException;
import software.amazon.awssdk.services.opsworkscm.model.Server;
import software.amazon.awssdk.services.opsworkscm.model.ServerStatus;
import software.amazon.awssdk.services.opsworkscm.model.ValidationException;
import software.amazon.cloudformation.exceptions.CfnInvalidRequestException;
import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy;
import software.amazon.cloudformation.proxy.HandlerErrorCode;
import software.amazon.cloudformation.proxy.Logger;
import software.amazon.cloudformation.proxy.ProgressEvent;
import software.amazon.cloudformation.proxy.ResourceHandlerRequest;
import software.amazon.cloudformation.resource.IdentifierUtils;

public class DeleteHandler extends BaseHandler<CallbackContext> {
public class DeleteHandler extends BaseOpsWorksCMHandler {

public static final String SERVER_DELETION_FAILED_MESSAGE = "Server %s deletion has failed with reason: %s";
public static final String SERVER_OPERATION_STILL_IN_PROGRESS_MESSAGE = "Cannot delete the server '%s'. The current operation on the server is still in progress\\." +
" \\(Service: AWSOpsWorksCM; Status Code: 400; Error Code: ValidationException; Request ID: .*\\)";

AmazonWebServicesClientProxy proxy;
ResourceHandlerRequest<ResourceModel> request;
CallbackContext callbackContext;
Logger logger;
ResourceModel model;
ResourceModel oldModel;
ClientWrapper client;

private static int NO_CALLBACK_DELAY = 0;
private static int CALLBACK_DELAY_SECONDS = 60;
private static final int MAX_LENGTH_CONFIGURATION_SET_NAME = 40;

@Override
public ProgressEvent<ResourceModel, CallbackContext> handleRequest(
final AmazonWebServicesClientProxy proxy,
final ResourceHandlerRequest<ResourceModel> request,
final CallbackContext callbackContext,
final Logger logger) {

this.request = request;
this.model = request.getDesiredResourceState();
this.oldModel = request.getPreviousResourceState();
this.callbackContext = callbackContext;
this.logger = logger;

setModelServerName();
setModelId();

final OpsWorksCmClient opsWorksCmClientclient = ClientBuilder.getClient();
this.client = new ClientWrapper(opsWorksCmClientclient, model, oldModel, proxy, logger);
initialize(proxy, request, callbackContext, logger);

try {
if (callbackContext.isStabilizationStarted()) {
Expand Down Expand Up @@ -116,29 +90,6 @@ private ProgressEvent<ResourceModel, CallbackContext> handleStabilize() {
}
}

private void setModelServerName() {
if (StringUtils.isNullOrEmpty(model.getServerName())) {
model.setServerName(
IdentifierUtils.generateResourceIdentifier(
request.getLogicalResourceIdentifier(),
request.getClientRequestToken(),
MAX_LENGTH_CONFIGURATION_SET_NAME
)
);
} else if (model.getServerName().length() > MAX_LENGTH_CONFIGURATION_SET_NAME) {
model.setServerName(model.getServerName().substring(0, MAX_LENGTH_CONFIGURATION_SET_NAME));
}
}

private void setModelId() {
if (model.getId() == null) {
model.setId(IdentifierUtils.generateResourceIdentifier(
request.getLogicalResourceIdentifier(),
request.getClientRequestToken()
));
}
}

private ProgressEvent<ResourceModel, CallbackContext> handleServerNotFound(final String serverName) {
logger.log(String.format("Server %s deleted successfully.", serverName));
return ProgressEvent.defaultSuccessHandler(model);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
package software.amazon.opsworkscm.server;

import com.amazonaws.util.StringUtils;
import software.amazon.awssdk.services.opsworkscm.OpsWorksCmClient;
import software.amazon.awssdk.services.opsworkscm.model.DescribeServersResponse;
import software.amazon.awssdk.services.opsworkscm.model.Server;
import software.amazon.cloudformation.exceptions.CfnInvalidRequestException;
import software.amazon.cloudformation.exceptions.ResourceNotFoundException;
import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy;
import software.amazon.cloudformation.proxy.Logger;
import software.amazon.cloudformation.proxy.ProgressEvent;
import software.amazon.cloudformation.proxy.ResourceHandlerRequest;
import software.amazon.cloudformation.resource.IdentifierUtils;
import software.amazon.cloudformation.exceptions.ResourceNotFoundException;

public class ReadHandler extends BaseHandler<CallbackContext> {

ResourceModel model;
ResourceModel oldModel;
CallbackContext callbackContext;
Logger logger;
ResourceHandlerRequest<ResourceModel> request;
ClientWrapper client;

private static final int MAX_LENGTH_CONFIGURATION_SET_NAME = 40;
public class ReadHandler extends BaseOpsWorksCMHandler {

@Override
public ProgressEvent<ResourceModel, CallbackContext> handleRequest(
Expand All @@ -30,17 +17,7 @@ public ProgressEvent<ResourceModel, CallbackContext> handleRequest(
final CallbackContext callbackContext,
final Logger logger) {

this.request = request;
this.model = request.getDesiredResourceState();
this.oldModel = request.getPreviousResourceState();
this.callbackContext = callbackContext;
this.logger = logger;

setModelServerName();
setModelId();

final OpsWorksCmClient opsWorksCmClientclient = ClientBuilder.getClient();
this.client = new ClientWrapper(opsWorksCmClientclient, model, oldModel, proxy, logger);
initialize(proxy, request, callbackContext, logger);

final DescribeServersResponse result;
final String serverName = model.getServerName();
Expand All @@ -59,34 +36,6 @@ public ProgressEvent<ResourceModel, CallbackContext> handleRequest(
}
}

private void setModelServerName() {
if (StringUtils.isNullOrEmpty(model.getServerName())) {
logger.log("RequestModel doesn't have the server name. Setting it using request identifier and client token");
model.setServerName(
IdentifierUtils.generateResourceIdentifier(
request.getLogicalResourceIdentifier(),
request.getClientRequestToken(),
MAX_LENGTH_CONFIGURATION_SET_NAME
)
);
} else if (model.getServerName().length() > MAX_LENGTH_CONFIGURATION_SET_NAME) {
logger.log(String.format("ServerName %s was greater than %d characters", model.getServerName(), MAX_LENGTH_CONFIGURATION_SET_NAME));
throw new CfnInvalidRequestException(String.format("ServerName %s must have less than or equal to %d characters", model.getServerName(), MAX_LENGTH_CONFIGURATION_SET_NAME));
} else {
model.setServerName(model.getServerName());
}
}

private void setModelId() {
if (model.getId() == null) {
logger.log("RequestModel doesn't have the model id. Setting it using request identifier and client token");
model.setId(IdentifierUtils.generateResourceIdentifier(
request.getLogicalResourceIdentifier(),
request.getClientRequestToken()
));
}
}

private void addDescribeServerResponseAttributes(final Server server) {
model.setEndpoint(server.endpoint());
model.setArn(server.serverArn());
Expand Down
Loading

0 comments on commit a725113

Please sign in to comment.