Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected ProgressEvent<ResourceModel, CallbackContext> handleRequest(
// ManagedExecution update should be separated due to its limitations
.then(progress -> updateManagedExecution(proxy, proxyClient, progress, previousModel, stackSet))
.then(progress -> deleteStackInstances(proxy, proxyClient, progress, placeHolder.getDeleteStackInstances(), logger))
.then(progress -> updateStackSet(proxy, proxyClient, request, progress, previousModel))
.then(progress -> updateStackSet(proxy, proxyClient, request, progress, previousModel, stackSet))
.then(progress -> createStackInstances(proxy, proxyClient, progress, placeHolder.getCreateStackInstances(), logger))
.then(progress -> updateStackInstances(proxy, proxyClient, progress, placeHolder.getUpdateStackInstances(), logger))
.then(progress -> ProgressEvent.defaultSuccessHandler(model));
Expand All @@ -62,15 +62,16 @@ private ProgressEvent<ResourceModel, CallbackContext> updateStackSet(
final ProxyClient<CloudFormationClient> client,
final ResourceHandlerRequest<ResourceModel> handlerRequest,
final ProgressEvent<ResourceModel, CallbackContext> progress,
final ResourceModel previousModel) {
final ResourceModel previousModel,
final StackSet currentStackSet) {

final ResourceModel desiredModel = progress.getResourceModel();
final CallbackContext callbackContext = progress.getCallbackContext();
if (isStackSetConfigEquals(previousModel, desiredModel, handlerRequest.getPreviousResourceTags(), handlerRequest.getDesiredResourceTags())) {
return ProgressEvent.progress(desiredModel, callbackContext);
}
return proxy.initiate("AWS-CloudFormation-StackSet::UpdateStackSet", client, desiredModel, callbackContext)
.translateToServiceRequest(modelRequest -> updateStackSetRequest(modelRequest, handlerRequest.getDesiredResourceTags()))
.translateToServiceRequest(modelRequest -> updateStackSetRequest(modelRequest, handlerRequest.getPreviousResourceTags(), handlerRequest.getDesiredResourceTags(), currentStackSet.tags()))
.backoffDelay(MULTIPLE_OF)
.makeServiceCall((modelRequest, proxyInvocation) -> {
logger.log(String.format("%s [%s] UpdateStackSet request: [%s]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
import software.amazon.awssdk.services.cloudformation.model.ListStackInstancesRequest;
import software.amazon.awssdk.services.cloudformation.model.ListStackSetOperationResultsRequest;
import software.amazon.awssdk.services.cloudformation.model.ListStackSetsRequest;
import software.amazon.awssdk.services.cloudformation.model.Tag;
import software.amazon.awssdk.services.cloudformation.model.UpdateStackInstancesRequest;
import software.amazon.awssdk.services.cloudformation.model.UpdateStackSetRequest;
import software.amazon.cloudformation.stackset.OperationPreferences;
import software.amazon.cloudformation.stackset.ResourceModel;
import software.amazon.cloudformation.stackset.StackInstances;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static software.amazon.awssdk.services.cloudformation.model.StackSetStatus.ACTIVE;
import static software.amazon.cloudformation.stackset.translator.PropertyTranslator.translateToSdkAutoDeployment;
Expand Down Expand Up @@ -106,7 +111,17 @@ public static DeleteStackInstancesRequest deleteStackInstancesRequest(

public static UpdateStackSetRequest updateStackSetRequest(
final ResourceModel model,
final Map<String, String> tags) {
final Map<String, String> previousTags,
final Map<String, String> tags,
final List<Tag> currentTags) {
// Aggregate all current tags
Set<Tag> allTags = new HashSet<>(currentTags);
Set<Tag> currentTagSet = translateToSdkTags(tags);
allTags.addAll(currentTagSet);
// Remove the one's that were in the previousTags but not in tags
Set<Tag> previousTagSet = translateToSdkTags(previousTags);
previousTagSet.removeAll(currentTagSet);
allTags.removeAll(previousTagSet);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little hard to follow, a lot of stuff being added/removed. Do we not just need to set the tags to targetTags + out of band tags? So something like:

outOfBandTags = currentTags - previousTags
tagsToSet = targetTags + outOfBandTags

return UpdateStackSetRequest.builder()
.stackSetName(model.getStackSetId())
.administrationRoleARN(model.getAdministrationRoleARN())
Expand All @@ -118,7 +133,7 @@ public static UpdateStackSetRequest updateStackSetRequest(
.parameters(translateToSdkParameters(model.getParameters()))
.templateURL(model.getTemplateURL())
.templateBody(model.getTemplateBody())
.tags(translateToSdkTags(tags))
.tags(allTags)
.callAs(model.getCallAs())
.build();
}
Expand Down Expand Up @@ -200,16 +215,16 @@ public static GetTemplateSummaryRequest getTemplateSummaryRequest(
}

public static ListStackSetOperationResultsRequest listStackSetOperationResultsRequest(
final String nextToken,
final String stackSetName,
final String operationId,
final String callAs) {
final String nextToken,
final String stackSetName,
final String operationId,
final String callAs) {
return ListStackSetOperationResultsRequest.builder()
.maxResults(LIST_MAX_ITEMS)
.nextToken(nextToken)
.stackSetName(stackSetName)
.operationId(operationId)
.callAs(callAs)
.build();
.maxResults(LIST_MAX_ITEMS)
.nextToken(nextToken)
.stackSetName(stackSetName)
.operationId(operationId)
.callAs(callAs)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import software.amazon.awssdk.awscore.exception.AwsErrorDetails;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
Expand Down Expand Up @@ -50,10 +52,13 @@
import static software.amazon.cloudformation.stackset.util.TestUtils.DELEGATED_ADMIN_SERVICE_MANAGED_MODEL;
import static software.amazon.cloudformation.stackset.util.TestUtils.DELETE_STACK_INSTANCES_RESPONSE;
import static software.amazon.cloudformation.stackset.util.TestUtils.DESCRIBE_SELF_MANAGED_STACK_SET_ME_DISABLED_RESPONSE;
import static software.amazon.cloudformation.stackset.util.TestUtils.DESCRIBE_SELF_MANAGED_STACK_SET_OUT_OF_BAND_TAGS_RESPONSE;
import static software.amazon.cloudformation.stackset.util.TestUtils.DESCRIBE_SELF_MANAGED_STACK_SET_RESPONSE;
import static software.amazon.cloudformation.stackset.util.TestUtils.DESCRIBE_SERVICE_MANAGED_STACK_SET_RESPONSE;
import static software.amazon.cloudformation.stackset.util.TestUtils.DESIRED_RESOURCE_TAGS;
import static software.amazon.cloudformation.stackset.util.TestUtils.EXPECTED_TAGS;
import static software.amazon.cloudformation.stackset.util.TestUtils.LOGICAL_ID;
import static software.amazon.cloudformation.stackset.util.TestUtils.NEW_RESOURCE_TAGS;
import static software.amazon.cloudformation.stackset.util.TestUtils.OPERATION_SUCCEED_RESPONSE;
import static software.amazon.cloudformation.stackset.util.TestUtils.PREVIOUS_RESOURCE_TAGS;
import static software.amazon.cloudformation.stackset.util.TestUtils.REQUEST_TOKEN;
Expand All @@ -73,6 +78,7 @@

@ExtendWith(MockitoExtension.class)
public class UpdateHandlerTest extends AbstractMockTestBase<CloudFormationClient> {
@Spy
private UpdateHandler handler;
private CloudFormationClient client;
private ResourceHandlerRequest<ResourceModel> request;
Expand Down Expand Up @@ -182,6 +188,53 @@ public void handleRequest_SelfManagedSS_SimpleSuccess() {
verify(client, times(4)).describeStackSetOperation(any(DescribeStackSetOperationRequest.class));
}

@Test
public void handleRequest_UpdateSelfManagedSS_RespectOutOfBandTags() {
ArgumentCaptor<UpdateStackSetRequest> updateStackSetRequestArgumentCaptor = ArgumentCaptor.forClass(UpdateStackSetRequest.class);
request = ResourceHandlerRequest.<ResourceModel>builder()
.previousResourceState(SELF_MANAGED_MODEL)
.desiredResourceState(UPDATED_SELF_MANAGED_MODEL)
.previousResourceTags(PREVIOUS_RESOURCE_TAGS)
.desiredResourceTags(NEW_RESOURCE_TAGS)
.build();

when(client.describeStackSet(any(DescribeStackSetRequest.class)))
.thenReturn(DESCRIBE_SELF_MANAGED_STACK_SET_OUT_OF_BAND_TAGS_RESPONSE);
when(client.getTemplateSummary(any(GetTemplateSummaryRequest.class)))
.thenReturn(VALID_TEMPLATE_SUMMARY_RESPONSE);
when(client.updateStackSet(any(UpdateStackSetRequest.class)))
.thenReturn(UPDATE_STACK_SET_RESPONSE);
when(client.createStackInstances(any(CreateStackInstancesRequest.class)))
.thenReturn(CREATE_STACK_INSTANCES_RESPONSE);
when(client.deleteStackInstances(any(DeleteStackInstancesRequest.class)))
.thenReturn(DELETE_STACK_INSTANCES_RESPONSE);
when(client.updateStackInstances(any(UpdateStackInstancesRequest.class)))
.thenReturn(UPDATE_STACK_INSTANCES_RESPONSE);
when(client.describeStackSetOperation(any(DescribeStackSetOperationRequest.class)))
.thenReturn(OPERATION_SUCCEED_RESPONSE);

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

assertThat(response).isNotNull();
assertThat(response.getStatus()).isEqualTo(OperationStatus.SUCCESS);
assertThat(response.getCallbackContext()).isNull();
assertThat(response.getCallbackDelaySeconds()).isEqualTo(0);
assertThat(response.getResourceModel()).isEqualTo(UPDATED_SELF_MANAGED_MODEL);
assertThat(response.getMessage()).isNull();
assertThat(response.getErrorCode()).isNull();



verify(client).getTemplateSummary(any(GetTemplateSummaryRequest.class));
verify(client).updateStackSet(updateStackSetRequestArgumentCaptor.capture());
assertThat(updateStackSetRequestArgumentCaptor.getValue().tags()).hasSameElementsAs(EXPECTED_TAGS);
verify(client).createStackInstances(any(CreateStackInstancesRequest.class));
verify(client).updateStackInstances(any(UpdateStackInstancesRequest.class));
verify(client).deleteStackInstances(any(DeleteStackInstancesRequest.class));
verify(client, times(4)).describeStackSetOperation(any(DescribeStackSetOperationRequest.class));
}

@Test
public void handleRequest_ServiceManagedSS_WithCallAs_SimpleSuccess() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import software.amazon.cloudformation.stackset.ResourceModel;
import software.amazon.cloudformation.stackset.StackInstances;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -157,8 +158,7 @@ public class TestUtils {
"key1", "val1", "key2", "val2", "key3", "val3");
public final static Map<String, String> PREVIOUS_RESOURCE_TAGS = ImmutableMap.of(
"key-1", "val1", "key-2", "val2", "key-3", "val3");
public final static Map<String, String> NEW_RESOURCE_TAGS = ImmutableMap.of(
"key1", "val1", "key2updated", "val2updated", "key3", "val3");
public final static Map<String, String> NEW_RESOURCE_TAGS = ImmutableMap.of("key2updated", "val2updated");

public final static Set<String> REGIONS_1 = new HashSet<>(Arrays.asList(US_WEST_1, US_EAST_1));
public final static Set<String> UPDATED_REGIONS_1 = new HashSet<>(Arrays.asList(US_WEST_1, US_EAST_2));
Expand Down Expand Up @@ -242,6 +242,11 @@ public class TestUtils {
Tag.builder().key("key1").value("val1").build(),
Tag.builder().key("key2").value("val2").build(),
Tag.builder().key("key3").value("val3").build()));
public final static Set<Tag> OUT_OF_BAND_TAGS = new HashSet<>(Arrays.asList(
Tag.builder().key("outOfBandTags").value("outOfBandTagValue").build()));
public final static List<Tag> EXPECTED_TAGS = new ArrayList<>(Arrays.asList(
Tag.builder().key("outOfBandTags").value("outOfBandTagValue").build(),
Tag.builder().key("key2updated").value("val2updated").build()));

public final static AutoDeployment AUTO_DEPLOYMENT_ENABLED = AutoDeployment.builder()
.enabled(true)
Expand Down Expand Up @@ -431,6 +436,11 @@ public class TestUtils {
.managedExecution(MANAGED_EXECUTION_DISABLED_SDK)
.build();

public final static StackSet SELF_MANAGED_STACK_SET_WITH_OUT_OF_BAND_TAGS = SELF_MANAGED_STACK_SET.toBuilder()
.tags(OUT_OF_BAND_TAGS)
.managedExecution(MANAGED_EXECUTION_DISABLED_SDK)
.build();

public final static StackSet NULL_PERMISSION_MODEL_STACK_SET = StackSet.builder()
.stackSetId(STACK_SET_ID)
.stackSetName(STACK_SET_NAME)
Expand Down Expand Up @@ -734,6 +744,11 @@ public class TestUtils {
.stackSet(SELF_MANAGED_STACK_SET_ME_DISABLED)
.build();

public final static DescribeStackSetResponse DESCRIBE_SELF_MANAGED_STACK_SET_OUT_OF_BAND_TAGS_RESPONSE =
DescribeStackSetResponse.builder()
.stackSet(SELF_MANAGED_STACK_SET_WITH_OUT_OF_BAND_TAGS)
.build();

public final static DescribeStackSetResponse DESCRIBE_SERVICE_MANAGED_STACK_SET_RESPONSE =
DescribeStackSetResponse.builder()
.stackSet(SERVICE_MANAGED_STACK_SET)
Expand Down
Loading