Skip to content

Commit

Permalink
removed redundant state maintainence (aws-cloudformation#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdebojit authored Apr 17, 2020
1 parent 0f6783c commit 33b5859
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,14 @@ public class CreateHandler extends BaseHandler<CallbackContext> {
private static final String DEFAULT_LOG_GROUP_NAME_PREFIX = "LogGroup";
private static final int MAX_LENGTH_LOG_GROUP_NAME = 512;

private AmazonWebServicesClientProxy proxy;
private ResourceHandlerRequest<ResourceModel> request;
private Logger logger;

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

this.proxy = proxy;
this.request = request;
this.logger = logger;

prepareResourceModel();
prepareResourceModel(request);

final ResourceModel model = request.getDesiredResourceState();

Expand All @@ -47,7 +39,7 @@ public ProgressEvent<ResourceModel, CallbackContext> handleRequest(
logger.log(createMessage);

if (model.getRetentionInDays() != null) {
updateRetentionInDays();
updateRetentionInDays(proxy, request, logger);
}

return ProgressEvent.defaultSuccessHandler(model);
Expand All @@ -62,7 +54,7 @@ public ProgressEvent<ResourceModel, CallbackContext> handleRequest(
* 2. Generating a log name if one is not given. This is a createOnly property,
* but we generate one if one is not provided.
*/
private void prepareResourceModel() {
private void prepareResourceModel(final ResourceHandlerRequest<ResourceModel> request) {
if (request.getDesiredResourceState() == null) {
request.setDesiredResourceState(new ResourceModel());
}
Expand All @@ -83,7 +75,9 @@ private void prepareResourceModel() {
}
}

private void updateRetentionInDays() {
private void updateRetentionInDays(final AmazonWebServicesClientProxy proxy,
final ResourceHandlerRequest<ResourceModel> request,
final Logger logger) {
final ResourceModel model = request.getDesiredResourceState();
proxy.injectCredentialsAndInvokeV2(Translator.translateToPutRetentionPolicyRequest(model),
ClientBuilder.getClient()::putRetentionPolicy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,19 @@

public class ReadHandler extends BaseHandler<CallbackContext> {

private AmazonWebServicesClientProxy proxy;
private ResourceHandlerRequest<ResourceModel> request;
private Logger logger;

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

this.proxy = proxy;
this.request = request;
this.logger = logger;

return fetchLogGroupAndAssertExists();
}

private ProgressEvent<ResourceModel, CallbackContext> fetchLogGroupAndAssertExists() {
final ResourceModel model = request.getDesiredResourceState();

if (model == null || model.getLogGroupName() == null) {
throwNotFoundException(model);
}


DescribeLogGroupsResponse response = null;
try {
response = proxy.injectCredentialsAndInvokeV2(Translator.translateToReadRequest(model),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,28 @@

public class UpdateHandler extends BaseHandler<CallbackContext> {

private AmazonWebServicesClientProxy proxy;
private ResourceHandlerRequest<ResourceModel> request;
private CallbackContext callbackContext;
private Logger logger;

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

this.proxy = proxy;
this.request = request;
this.callbackContext = callbackContext;
this.logger = logger;

// RetentionPolicyInDays is the only attribute that is not createOnly
return updateRetentionPolicy();
}

private ProgressEvent<ResourceModel, CallbackContext> updateRetentionPolicy() {
final ResourceModel model = request.getDesiredResourceState();

if (model.getRetentionInDays() == null) {
deleteRetentionPolicy();
deleteRetentionPolicy(proxy, request, logger);
} else {
putRetentionPolicy();
putRetentionPolicy(proxy, request, logger);
}

return ProgressEvent.defaultSuccessHandler(model);
}

private void deleteRetentionPolicy() {
private void deleteRetentionPolicy(final AmazonWebServicesClientProxy proxy,
final ResourceHandlerRequest<ResourceModel> request,
final Logger logger) {
final ResourceModel model = request.getDesiredResourceState();
final DeleteRetentionPolicyRequest deleteRetentionPolicyRequest =
Translator.translateToDeleteRetentionPolicyRequest(model);
Expand All @@ -62,7 +50,9 @@ private void deleteRetentionPolicy() {
logger.log(retentionPolicyMessage);
}

private void putRetentionPolicy() {
private void putRetentionPolicy(final AmazonWebServicesClientProxy proxy,
final ResourceHandlerRequest<ResourceModel> request,
final Logger logger) {
final ResourceModel model = request.getDesiredResourceState();
final PutRetentionPolicyRequest putRetentionPolicyRequest =
Translator.translateToPutRetentionPolicyRequest(model);
Expand Down

0 comments on commit 33b5859

Please sign in to comment.