-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ListHandler with UnitTests (#11)
- Loading branch information
Showing
5 changed files
with
100 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 40 additions & 10 deletions
50
aws-opsworkscm-server/src/main/java/software/amazon/opsworkscm/server/ListHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters