Skip to content
This repository has been archived by the owner on Jan 5, 2022. It is now read-only.

Commit

Permalink
Handling null-values in parameters and tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalwrzeszcz committed Mar 29, 2019
1 parent e72eece commit c8b46bb
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ object.
accountName: "dev"
administratorRoleName: "OrganizationAdmin"
# assume OrganizationUnit is a resource created by lambda-cform-organization-unit handler
ouId: !GetAtt "OrganizationUnit.id"
ouId: !GetAtt "OrganizationUnit.Id"

InvitedAccount:
Type: "AWS::CloudFormation::CustomResource"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
package pl.wrzasq.lambda.cform.stackset.instance.service;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import com.amazonaws.services.cloudformation.AmazonCloudFormation;
Expand Down Expand Up @@ -226,7 +228,10 @@ private String updateStackInstance(StackInstanceRequest input)
*/
private static Collection<Parameter> buildSdkParameters(StackInstanceRequest input)
{
return input.getParameterOverrides().entrySet().stream()
return Optional.ofNullable(input.getParameterOverrides())
.orElse(Collections.emptyMap())
.entrySet()
.stream()
.map(
(Map.Entry<String, String> entry) ->
new Parameter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ object.
Code:
# put your source bucket
S3Bucket: "your-bucket"
S3Key: "lambda-cform-stackset-1.0.6-standalone.jar"
S3Key: "lambda-cform-stackset-instance-1.0.6-standalone.jar"
Handler: "pl.wrzasq.lambda.cform.stackset.instance.Handler::handle"
MemorySize: 256
Description: "AWS CloudFormation stack instance manager deployment."
Expand All @@ -110,7 +110,7 @@ object.
# reference to deploy function
ServiceToken: !GetAtt "StackInstanceManager.Arn"
# reference to resource provisioned by lambda-cform-stackset
stackSetName: !GetAtt "StackSet.Name"
stackSetName: !GetAtt "StackSet.StackSetName"
# reference to resource provisioned by lambda-cform-account
accountId: !GetAtt "Account.Id"
region: !Ref "AWS::Region"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void deployStackInstance()
{
StackInstance stackInstance = new StackInstance();

StackSetInstanceManager manager = new StackSetInstanceManager(this.cloudFormation);
StackSetInstanceManager manager = this.createStackSetInstanceManager();

Mockito
.when(this.cloudFormation.describeStackInstance(this.describeRequest.capture()))
Expand Down Expand Up @@ -227,7 +227,7 @@ public void deployStackInstanceUpdate()
{
StackInstance stackInstance = new StackInstance();

StackSetInstanceManager manager = new StackSetInstanceManager(this.cloudFormation);
StackSetInstanceManager manager = this.createStackSetInstanceManager();

Mockito
.when(this.cloudFormation.describeStackInstance(this.describeRequest.capture()))
Expand Down Expand Up @@ -349,7 +349,7 @@ public void deployStackInstanceUpdate()
@Test
public void deployStackInstanceFailed()
{
StackSetInstanceManager manager = new StackSetInstanceManager(this.cloudFormation);
StackSetInstanceManager manager = this.createStackSetInstanceManager();

Mockito
.when(this.cloudFormation.describeStackInstance(Mockito.any(DescribeStackInstanceRequest.class)))
Expand Down Expand Up @@ -380,10 +380,52 @@ public void deployStackInstanceFailed()
);
}

@Test
public void deployStackSetInstanceNullParameters()
{
StackInstance stackInstance = new StackInstance();

StackSetInstanceManager manager = this.createStackSetInstanceManager();

Mockito
.when(this.cloudFormation.describeStackInstance(this.describeRequest.capture()))
.thenThrow(StackInstanceNotFoundException.class)
.thenReturn(
new DescribeStackInstanceResult()
.withStackInstance(stackInstance)
);
Mockito
.when(this.cloudFormation.createStackInstances(this.createRequest.capture()))
.thenReturn(
new CreateStackInstancesResult()
.withOperationId(StackSetInstanceManagerTest.OPERATION_ID)
);
Mockito
.when(this.cloudFormation.describeStackSetOperation(this.describeOperationRequest.capture()))
.thenReturn(
new DescribeStackSetOperationResult()
.withStackSetOperation(
new StackSetOperation()
.withStatus(StackSetOperationStatus.SUCCEEDED)
)
);

StackInstanceRequest input = StackSetInstanceManagerTest.createStackInstanceRequest();
input.setParameterOverrides(null);

manager.deployStackInstance(input, null);

CreateStackInstancesRequest request = this.createRequest.getValue();
Assertions.assertTrue(
request.getParameterOverrides().isEmpty(),
"StackSetInstanceManager.deployStackInstance() should set empty mapping if no parameters were given."
);
}

@Test
public void deleteStackInstance()
{
StackSetInstanceManager manager = new StackSetInstanceManager(this.cloudFormation);
StackSetInstanceManager manager = this.createStackSetInstanceManager();

manager.deleteStackInstance(null, StackSetInstanceManagerTest.PHYSICAL_ID);

Expand Down Expand Up @@ -415,4 +457,11 @@ private static StackInstanceRequest createStackInstanceRequest()
request.setParameterOverrides(StackSetInstanceManagerTest.PARAMETERS);
return request;
}

private StackSetInstanceManager createStackSetInstanceManager()
{
StackSetInstanceManager manager = new StackSetInstanceManager(this.cloudFormation);
manager.setSleepInterval(1);
return manager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
package pl.wrzasq.lambda.cform.stackset.service;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import com.amazonaws.services.cloudformation.AmazonCloudFormation;
Expand Down Expand Up @@ -202,7 +204,10 @@ private void updateStackSet(StackSetRequest input)
*/
private static Collection<Parameter> buildSdkParameters(StackSetRequest input)
{
return input.getParameters().entrySet().stream()
return Optional.ofNullable(input.getParameters())
.orElse(Collections.emptyMap())
.entrySet()
.stream()
.map(
(Map.Entry<String, String> entry) ->
new Parameter()
Expand All @@ -220,7 +225,10 @@ private static Collection<Parameter> buildSdkParameters(StackSetRequest input)
*/
private static Collection<Tag> buildSdkTags(StackSetRequest input)
{
return input.getTags().entrySet().stream()
return Optional.ofNullable(input.getTags())
.orElse(Collections.emptyMap())
.entrySet()
.stream()
.map(
(Map.Entry<String, String> entry) ->
new Tag()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,38 @@ public void deployStackSetNotExists()
);
}

@Test
public void deployStackSetNullParameters()
{
StackSetManager manager = new StackSetManager(this.cloudFormation);

Mockito
.when(this.cloudFormation.describeStackSet(this.describeRequest.capture()))
.thenThrow(StackSetNotFoundException.class);
Mockito
.when(this.cloudFormation.createStackSet(this.createRequest.capture()))
.thenReturn(
new CreateStackSetResult()
.withStackSetId(StackSetManagerTest.STACK_SET_ID_1)
);

StackSetRequest input = StackSetManagerTest.createStackSetRequest();
input.setParameters(null);
input.setTags(null);

manager.deployStackSet(input, null);

CreateStackSetRequest request = this.createRequest.getValue();
Assertions.assertTrue(
request.getParameters().isEmpty(),
"StackSetManager.deployStackSet() should set empty mapping if no parameters were given."
);
Assertions.assertTrue(
request.getTags().isEmpty(),
"StackSetManager.deployStackSet() should set empty mapping no tags were given."
);
}

@Test
public void deleteStackSet()
{
Expand Down

0 comments on commit c8b46bb

Please sign in to comment.