Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(cloudformation): share EC2 tag reconciliation (#4057)
Hector Venturae7fe03b69b16
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.javaindex 8045bddb74..448a89e6b6 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java@@ -16,7 +16,6 @@ import org.jboss.logging.Logger;import java.util.ArrayList;import java.util.HashMap;-import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import java.util.Set;@@ -89,7 +88,7 @@ public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner {if (prior != null && !createOnlyChanged(prior, imageId, subnetId, keyName)) {r.setPhysicalId(prior.getInstanceId());publishInstanceAttributes(r, prior);- reconcileTags(prior.getInstanceId(), region, props, engine);+ Ec2Tags.reconcile(ec2Service, region, prior.getInstanceId(), ctx.resolveTags(props, "Tags"));ReplacementCleanup.record(r, ctx, attributesBefore);return;}@@ -219,40 +218,6 @@ public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner {}/** Reconciles the instance tags to the template when the instance is kept in place. */- private void reconcileTags(String instanceId, String region, JsonNode props,- CloudFormationTemplateEngine engine) {- Map<String, String> desired = new LinkedHashMap<>();- JsonNode tagsNode = props != null ? engine.resolveNode(props.get("Tags")) : null;- if (tagsNode != null && tagsNode.isArray()) {- for (JsonNode tag : tagsNode) {- String key = engine.resolve(tag.path("Key"));- if (key != null && !key.isEmpty()) {- String value = engine.resolve(tag.path("Value"));- desired.put(key, value == null ? "" : value);- }- }- }- Map<String, String> current = new LinkedHashMap<>();- for (Map<String, String> entry : ec2Service.describeTags(region, Map.of("resource-id", List.of(instanceId)))) {- current.put(entry.get("key"), entry.get("value"));- }- List<String> stale = ProvisionContext.staleTagKeys(current, desired);- if (!stale.isEmpty()) {- List<Tag> remove = new ArrayList<>();- for (String key : stale) {- remove.add(new Tag(key, current.get(key)));- }- ec2Service.deleteTags(region, List.of(instanceId), remove);- }- if (!desired.isEmpty()) {- List<Tag> add = new ArrayList<>();- for (Map.Entry<String, String> entry : desired.entrySet()) {- add.add(new Tag(entry.getKey(), entry.getValue()));- }- ec2Service.createTags(region, List.of(instanceId), add);- }- }-@Overridepublic boolean hasReplacementUpdate(StackResource resource) {return ReplacementCleanup.hasReplacement(resource);diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2SecurityGroupCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2SecurityGroupCfnProvisioner.javaindex fe477b4626..7a6f8adba7 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2SecurityGroupCfnProvisioner.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2SecurityGroupCfnProvisioner.java@@ -10,7 +10,6 @@ import io.github.hectorvent.floci.services.ec2.model.IpRange;import io.github.hectorvent.floci.services.ec2.model.Ipv6Range;import io.github.hectorvent.floci.services.ec2.model.PrefixListId;import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;-import io.github.hectorvent.floci.services.ec2.model.Tag;import io.github.hectorvent.floci.services.ec2.model.UserIdGroupPair;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;@@ -18,7 +17,6 @@ import org.jboss.logging.Logger;import java.util.ArrayList;import java.util.Arrays;-import java.util.LinkedHashMap;import java.util.LinkedHashSet;import java.util.List;import java.util.Map;@@ -104,7 +102,7 @@ public class Ec2SecurityGroupCfnProvisioner implements CfnResourceProvisioner {EGRESS_RULES_ATTR,perms -> ec2Service.authorizeSecurityGroupEgress(region, sg.getGroupId(), perms),perms -> ec2Service.revokeSecurityGroupEgress(region, sg.getGroupId(), perms));- reconcileTags(props, sg.getGroupId(), region, ctx);+ Ec2Tags.reconcile(ec2Service, region, sg.getGroupId(), ctx.resolveTags(props, "Tags"));}@Override@@ -176,29 +174,6 @@ public class Ec2SecurityGroupCfnProvisioner implements CfnResourceProvisioner {* declared tags and removes any the template dropped. AWS treats SecurityGroup Tags as an* in-place (no interruption) update.*/- private void reconcileTags(JsonNode props, String groupId, String region, ProvisionContext ctx) {- Map<String, String> desired = ctx.resolveTags(props, "Tags");- Map<String, String> current = new LinkedHashMap<>();- for (Map<String, String> entry : ec2Service.describeTags(region, Map.of("resource-id", List.of(groupId)))) {- current.put(entry.get("key"), entry.get("value"));- }- List<String> stale = ProvisionContext.staleTagKeys(current, desired);- if (!stale.isEmpty()) {- List<Tag> remove = new ArrayList<>();- for (String key : stale) {- remove.add(new Tag(key, current.get(key)));- }- ec2Service.deleteTags(region, List.of(groupId), remove);- }- if (!desired.isEmpty()) {- List<Tag> add = new ArrayList<>();- for (Map.Entry<String, String> entry : desired.entrySet()) {- add.add(new Tag(entry.getKey(), entry.getValue()));- }- ec2Service.createTags(region, List.of(groupId), add);- }- }-/*** Resolves a peer group's name to its id, the same lookup {@code Ec2Service} performs when it* stores an authorized rule. Group names are unique per VPC rather than per region, so thediff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2Tags.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2Tags.javanew file mode 100644index 0000000000..e58f930b3b--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2Tags.java@@ -0,0 +1,46 @@+package io.github.hectorvent.floci.services.cloudformation.provisioners;++import io.github.hectorvent.floci.services.ec2.Ec2Service;+import io.github.hectorvent.floci.services.ec2.model.Tag;++import java.util.ArrayList;+import java.util.LinkedHashMap;+import java.util.List;+import java.util.Map;++/**+ * Tag reconciliation shared by the EC2 provisioners ({@code Ec2InstanceCfnProvisioner} and+ * {@code Ec2SecurityGroupCfnProvisioner}). Both drive it from the desired tags resolved via+ * {@link ProvisionContext#resolveTags}; the reconcile itself is identical.+ */+final class Ec2Tags {++ private Ec2Tags() {+ }++ /**+ * Brings a resource's tags to {@code desired}: removes the keys AWS currently has that the+ * template no longer declares, then creates or overwrites the declared ones.+ */+ static void reconcile(Ec2Service ec2Service, String region, String resourceId, Map<String, String> desired) {+ Map<String, String> current = new LinkedHashMap<>();+ for (Map<String, String> entry : ec2Service.describeTags(region, Map.of("resource-id", List.of(resourceId)))) {+ current.put(entry.get("key"), entry.get("value"));+ }+ List<String> stale = ProvisionContext.staleTagKeys(current, desired);+ if (!stale.isEmpty()) {+ List<Tag> remove = new ArrayList<>();+ for (String key : stale) {+ remove.add(new Tag(key, current.get(key)));+ }+ ec2Service.deleteTags(region, List.of(resourceId), remove);+ }+ if (!desired.isEmpty()) {+ List<Tag> add = new ArrayList<>();+ for (Map.Entry<String, String> entry : desired.entrySet()) {+ add.add(new Tag(entry.getKey(), entry.getValue()));+ }+ ec2Service.createTags(region, List.of(resourceId), add);+ }+ }+}