Skip to content

Commit

Permalink
Add ListHandler with UnitTests (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
nopx authored May 11, 2020
1 parent 38a159c commit 9dad938
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 18 deletions.
14 changes: 14 additions & 0 deletions aws-opsworkscm-server/manual-requests/list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"credentials": {
"AccessKeyId": "<TO_BE_ADDED>",
"SecretAccessKey": "<TO_BE_ADDED>",
"SessionToken": "<TO_BE_ADDED>"
},
"action": "LIST",
"request": {
"clientRequestToken": "4b90a7e4-b790-456b-a937-0cfdfa211dfe",
"region": "us-east-1",
"desiredResourceState": {}
},
"callbackContext": {}
}
1 change: 1 addition & 0 deletions aws-opsworkscm-server/resource-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Resources:
- "opsworkscm:CreateReceiptFilter"
- "opsworkscm:DeleteReceiptFilter"
- "opsworkscm:ListReceiptFilters"
- "opsworkscm:UpdateReceiptFilters"
Resource: "*"
Outputs:
ExecutionRoleArn:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public DescribeServersResponse describeServer() {
return proxy.injectCredentialsAndInvokeV2(buildDescribeServerRequest(), client::describeServers);
}

public DescribeServersResponse describeAllServers() {
return proxy.injectCredentialsAndInvokeV2(buildDescribeAllServersRequest(), client::describeServers);
}

public DeleteServerResponse deleteServer() {
return proxy.injectCredentialsAndInvokeV2(buildDeleteServerRequest(), client::deleteServer);
}
Expand Down Expand Up @@ -71,6 +75,9 @@ private DescribeServersRequest buildDescribeServerRequest() {
.serverName(model.getServerName())
.build();
}
private DescribeServersRequest buildDescribeAllServersRequest() {
return DescribeServersRequest.builder().build();
}

private DeleteServerRequest buildDeleteServerRequest() {
return DeleteServerRequest.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,60 @@
package software.amazon.opsworkscm.server;

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.proxy.AmazonWebServicesClientProxy;
import software.amazon.cloudformation.proxy.Logger;
import software.amazon.cloudformation.proxy.ProgressEvent;
import software.amazon.cloudformation.proxy.OperationStatus;
import software.amazon.cloudformation.proxy.ProgressEvent;
import software.amazon.cloudformation.proxy.ResourceHandlerRequest;

import java.util.ArrayList;
import java.util.List;

public class ListHandler extends BaseHandler<CallbackContext> {

private static final int NO_CALLBACK_DELAY = 0;

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

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

final List<ResourceModel> models = new ArrayList<>();
this.logger = logger;

// TODO : put your code here
final OpsWorksCmClient opsWorksCmClientclient = ClientBuilder.getClient();
this.client = new ClientWrapper(opsWorksCmClientclient, request.getDesiredResourceState(), request.getPreviousResourceState(), proxy, logger);

logger.log("Calling Describe Servers with no ServerName");

DescribeServersResponse result = client.describeAllServers();
if (result == null || result.servers() == null) {
logger.log("Describe result is Null. Retrying request.");
return ProgressEvent.defaultInProgressHandler(callbackContext, NO_CALLBACK_DELAY, request.getDesiredResourceState());
}

List<ResourceModel> models = addDescribeServersResponseAttributes(result);
return ProgressEvent.<ResourceModel, CallbackContext>builder()
.resourceModels(models)
.status(OperationStatus.SUCCESS)
.build();
.resourceModels(models)
.status(OperationStatus.SUCCESS)
.build();
}

private List<ResourceModel> addDescribeServersResponseAttributes(final DescribeServersResponse response) {
List<ResourceModel> models = new ArrayList<>();
List<Server> servers = response.hasServers() ? response.servers() : new ArrayList<>();
servers.forEach(server -> models.add(ResourceModel.builder()
.endpoint(server.endpoint())
.serverName(server.serverName())
.build()));
return models;
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package software.amazon.opsworkscm.server;

import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy;
import software.amazon.cloudformation.proxy.Logger;
import software.amazon.cloudformation.proxy.OperationStatus;
import software.amazon.cloudformation.proxy.ProgressEvent;
import software.amazon.cloudformation.proxy.ResourceHandlerRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import software.amazon.awssdk.services.opsworkscm.model.DescribeServersResponse;
import software.amazon.awssdk.services.opsworkscm.model.Server;
import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy;
import software.amazon.cloudformation.proxy.Logger;
import software.amazon.cloudformation.proxy.OperationStatus;
import software.amazon.cloudformation.proxy.ProgressEvent;
import software.amazon.cloudformation.proxy.ResourceHandlerRequest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

@ExtendWith(MockitoExtension.class)
public class ListHandlerTest {

private static final String SERVER_NAME = "server-name";
private static final String ENDPOINT = "endpoint.com";
@Mock
private AmazonWebServicesClientProxy proxy;

Expand All @@ -31,23 +37,47 @@ public void setup() {

@Test
public void handleRequest_SimpleSuccess() {
doReturn(DescribeServersResponse.builder().servers(Server.builder().serverName(SERVER_NAME).endpoint(ENDPOINT).build()).build())
.when(proxy).injectCredentialsAndInvokeV2(any(), any());
final ListHandler handler = new ListHandler();

final ResourceModel model = ResourceModel.builder().build();

final ResourceHandlerRequest<ResourceModel> request = ResourceHandlerRequest.<ResourceModel>builder()
.desiredResourceState(model)
.build();
.desiredResourceState(model)
.build();

final ProgressEvent<ResourceModel, CallbackContext> response =
handler.handleRequest(proxy, request, null, logger);
handler.handleRequest(proxy, request, null, logger);

assertThat(response).isNotNull();
assertThat(response.getStatus()).isEqualTo(OperationStatus.SUCCESS);
assertThat(response.getCallbackContext()).isNull();
assertThat(response.getCallbackDelaySeconds()).isEqualTo(0);
assertThat(response.getResourceModel()).isNull();
assertThat(response.getResourceModels()).isNotNull();
assertThat(response.getResourceModels().size()).isEqualTo(1);
assertThat(response.getMessage()).isNull();
assertThat(response.getErrorCode()).isNull();
}

@Test
public void handleRequest_NullDescribe() {
final ListHandler handler = new ListHandler();

final ResourceModel model = ResourceModel.builder().build();

final ResourceHandlerRequest<ResourceModel> request = ResourceHandlerRequest.<ResourceModel>builder()
.desiredResourceState(model)
.build();

final ProgressEvent<ResourceModel, CallbackContext> response =
handler.handleRequest(proxy, request, null, logger);

assertThat(response).isNotNull();
assertThat(response.getStatus()).isEqualTo(OperationStatus.IN_PROGRESS);
assertThat(response.getCallbackContext()).isNull();
assertThat(response.getCallbackDelaySeconds()).isEqualTo(0);
assertThat(response.getMessage()).isNull();
assertThat(response.getErrorCode()).isNull();
}
Expand Down

0 comments on commit 9dad938

Please sign in to comment.