From 06b319ddd752e4f3f73752fdda5f7151d640ff5f Mon Sep 17 00:00:00 2001 From: akash1810 Date: Thu, 26 Sep 2024 08:18:43 +0100 Subject: [PATCH] feat(cloud-formation): Preserve existing tags Update the `cloud-formation` deployment type to preserve any existing tags on the CloudFormation stack. --- .../main/scala/magenta/tasks/ChangeSetTasks.scala | 15 ++++++++++----- .../scala/magenta/tasks/SetStackPolicyTask.scala | 2 +- .../magenta/tasks/UpdateCloudFormationTask.scala | 9 +++++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/magenta-lib/src/main/scala/magenta/tasks/ChangeSetTasks.scala b/magenta-lib/src/main/scala/magenta/tasks/ChangeSetTasks.scala index 9193f9abc..9dd6b9479 100644 --- a/magenta-lib/src/main/scala/magenta/tasks/ChangeSetTasks.scala +++ b/magenta-lib/src/main/scala/magenta/tasks/ChangeSetTasks.scala @@ -42,7 +42,7 @@ class CreateChangeSetTask( ) ) - val (stackName, changeSetType, existingParameters) = + val (stackName, changeSetType, existingParameters, currentTags) = stackLookup.lookup(resources.reporter, cfnClient) val template = processTemplate( @@ -106,12 +106,15 @@ class CreateChangeSetTask( resources.reporter.verbose(s"Using execution role $role") ) + val mergedTags = currentTags ++ stackTags + resources.reporter.info("Tags: " + mergedTags.mkString(", ")) + CloudFormation.createChangeSet( resources.reporter, stackLookup.changeSetName, changeSetType, stackName, - Some(stackTags), + Some(mergedTags), template, awsParameters, maybeExecutionRole, @@ -162,7 +165,7 @@ class CheckChangeSetCreatedTask( ): Unit = { check(resources.reporter, stopFlag) { CloudFormation.withCfnClient(keyRing, region, resources) { cfnClient => - val (stackName, changeSetType, _) = + val (stackName, changeSetType, _, _) = stackLookup.lookup(resources.reporter, cfnClient) val changeSetName = stackLookup.changeSetName val changeSet = CloudFormation.describeChangeSetByName( @@ -240,7 +243,8 @@ class ExecuteChangeSetTask( stopFlag: => Boolean ): Unit = { CloudFormation.withCfnClient(keyRing, region, resources) { cfnClient => - val (stackName, _, _) = stackLookup.lookup(resources.reporter, cfnClient) + val (stackName, _, _, _) = + stackLookup.lookup(resources.reporter, cfnClient) val changeSetName = stackLookup.changeSetName val changeSet = CloudFormation.describeChangeSetByName( @@ -306,7 +310,8 @@ class DeleteChangeSetTask( stopFlag: => Boolean ): Unit = { CloudFormation.withCfnClient(keyRing, region, resources) { cfnClient => - val (stackName, _, _) = stackLookup.lookup(resources.reporter, cfnClient) + val (stackName, _, _, _) = + stackLookup.lookup(resources.reporter, cfnClient) val changeSetName = stackLookup.changeSetName CloudFormation.deleteChangeSet(stackName, changeSetName, cfnClient) diff --git a/magenta-lib/src/main/scala/magenta/tasks/SetStackPolicyTask.scala b/magenta-lib/src/main/scala/magenta/tasks/SetStackPolicyTask.scala index f1a3c8411..0a3bec6d5 100644 --- a/magenta-lib/src/main/scala/magenta/tasks/SetStackPolicyTask.scala +++ b/magenta-lib/src/main/scala/magenta/tasks/SetStackPolicyTask.scala @@ -211,7 +211,7 @@ class SetStackPolicyTask( stopFlag: => Boolean ): Unit = { CloudFormation.withCfnClient(keyRing, region, resources) { cfnClient => - val (stackName, changeSetType, _) = + val (stackName, changeSetType, _, _) = stackLookup.lookup(resources.reporter, cfnClient) val policyDoc = toPolicyDoc(stackPolicy, () => accountResourceTypes(cfnClient)) diff --git a/magenta-lib/src/main/scala/magenta/tasks/UpdateCloudFormationTask.scala b/magenta-lib/src/main/scala/magenta/tasks/UpdateCloudFormationTask.scala index 136b6b6ed..559a14ba2 100644 --- a/magenta-lib/src/main/scala/magenta/tasks/UpdateCloudFormationTask.scala +++ b/magenta-lib/src/main/scala/magenta/tasks/UpdateCloudFormationTask.scala @@ -98,7 +98,7 @@ class CloudFormationStackMetadata( def lookup( reporter: DeployReporter, cfnClient: CloudFormationClient - ): (String, ChangeSetType, List[ExistingParameter]) = { + ): (String, ChangeSetType, List[ExistingParameter], Map[String, String]) = { val existingStack = strategy match { case LookupByName(name) => CloudFormation.describeStack(name, cfnClient) case LookupByTags(tags) => @@ -123,7 +123,12 @@ class CloudFormationStackMetadata( reporter ) - (stackName, changeSetType, stackParameters) + val currentTags: Map[String, String] = existingStack match { + case Some(stack) => stack.tags().asScala.map(t => t.key -> t.value).toMap + case _ => Map.empty + } + + (stackName, changeSetType, stackParameters, currentTags) } }