From d09ac7ff34257fdac828c617a31977eeacfe5836 Mon Sep 17 00:00:00 2001 From: Jimmy Liu Jia <91264074+JimmyLiuJia@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:06:20 +0800 Subject: [PATCH] fix(codedeploy): match any on-premises deployment tag filter (#8032) --- .../codedeploy/CodeDeployService.java | 30 ++++++++----------- .../codedeploy/CodeDeployServiceTest.java | 25 ++++++++++++++++ 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/floci/aws/services/codedeploy/CodeDeployService.java b/src/main/java/io/floci/aws/services/codedeploy/CodeDeployService.java index e0f56dfd00..f644eb8b89 100644 --- a/src/main/java/io/floci/aws/services/codedeploy/CodeDeployService.java +++ b/src/main/java/io/floci/aws/services/codedeploy/CodeDeployService.java @@ -347,7 +347,7 @@ public class CodeDeployService { if (tags != null && !tags.isEmpty()) { String arn = applicationArn(region, name); - applyTags(arn, tags); + tagResource(arn, tags); } return app; } @@ -443,7 +443,7 @@ public class CodeDeployService { groupStore.put(groupName, group); persistRegion(deploymentGroups, region); if (tags != null && !tags.isEmpty()) { - applyTags(deploymentGroupArn(region, appName, groupName), tags); + tagResource(deploymentGroupArn(region, appName, groupName), tags); } return group; } @@ -773,7 +773,8 @@ public class CodeDeployService { List> tagFilters) { return onPremisesFor(region).values().stream() .filter(i -> registrationStatus == null || registrationStatus.equals(i.getRegistrationStatus())) - .filter(i -> tagFilters == null || tagFilters.isEmpty() || matchesTagFilters(i.getTags(), tagFilters)) + .filter(i -> tagFilters == null || tagFilters.isEmpty() + || matchesTagFilters(i.getTags(), tagFilters, TagFilterMatchMode.ALL)) .map(OnPremisesInstance::getInstanceName) .collect(Collectors.toList()); } @@ -1115,7 +1116,7 @@ public class CodeDeployService { if (onPremFilters != null && !onPremFilters.isEmpty()) { onPremisesFor(region).values().stream() .filter(inst -> "Registered".equals(inst.getRegistrationStatus())) - .filter(inst -> matchesTagFilters(inst.getTags(), onPremFilters)) + .filter(inst -> matchesTagFilters(inst.getTags(), onPremFilters, TagFilterMatchMode.ANY)) .map(OnPremisesInstance::getInstanceName) .forEach(instanceIds::add); } @@ -1132,13 +1133,17 @@ public class CodeDeployService { return instanceIds; } + private enum TagFilterMatchMode { ALL, ANY } + private boolean matchesTagFilters(List> instanceTags, - List> filters) { + List> filters, TagFilterMatchMode mode) { + boolean hasApplicableFilter = false; for (Map filter : filters) { String key = filter.get("Key"); String value = filter.get("Value"); String type = filter.get("Type"); if (key == null && type == null) { continue; } + hasApplicableFilter = true; boolean found = instanceTags.stream().anyMatch(tag -> { if ("VALUE_ONLY".equals(type)) { return value != null && value.equals(tag.get("Value")); @@ -1152,9 +1157,10 @@ public class CodeDeployService { return key != null && key.equals(tag.get("Key")) && (value == null || value.equals(tag.get("Value"))); }); - if (!found) { return false; } + if (mode == TagFilterMatchMode.ANY && found) { return true; } + if (mode == TagFilterMatchMode.ALL && !found) { return false; } } - return true; + return mode == TagFilterMatchMode.ALL || !hasApplicableFilter; } private AppSpecInfo parseAppSpec(Map revision) { @@ -1941,14 +1947,4 @@ public class CodeDeployService { setter.accept(group, (Map) m); } } - - private void applyTags(String arn, List> tagList) { - Map tagMap = tags.computeIfAbsent(arn, k -> new ConcurrentHashMap<>()); - for (Map t : tagList) { - String key = t.containsKey("Key") ? t.get("Key") : t.get("key"); - String value = t.containsKey("Value") ? t.get("Value") : t.get("value"); - if (key != null) { tagMap.put(key, value != null ? value : ""); } - } - tags.put(arn, tagMap); // write back the in-place inner-map mutation - } } diff --git a/src/test/java/io/floci/aws/services/codedeploy/CodeDeployServiceTest.java b/src/test/java/io/floci/aws/services/codedeploy/CodeDeployServiceTest.java index 64f05ac51f..d6a8cbe14f 100644 --- a/src/test/java/io/floci/aws/services/codedeploy/CodeDeployServiceTest.java +++ b/src/test/java/io/floci/aws/services/codedeploy/CodeDeployServiceTest.java @@ -112,6 +112,31 @@ class CodeDeployServiceTest { Set.copyOf(service.listDeploymentTargets(REGION, deploymentId))); } + @Test + void deploymentGroupMatchesAnyOnPremisesTagFilterWithoutChangingListFiltering() { + for (String name : List.of("team-only", "role-only", "both", "neither")) { + service.registerOnPremisesInstance(REGION, name, null, null); + } + service.addTagsToOnPremisesInstances(REGION, List.of("team-only", "both"), + List.of(Map.of("Key", "team", "Value", "red"))); + service.addTagsToOnPremisesInstances(REGION, List.of("role-only", "both"), + List.of(Map.of("Key", "role", "Value", "web"))); + List> filters = List.of( + Map.of("Key", "team", "Value", "red", "Type", "KEY_AND_VALUE"), + Map.of("Key", "role", "Value", "web", "Type", "KEY_AND_VALUE")); + + assertEquals(Set.of("both"), Set.copyOf(service.listOnPremisesInstances(REGION, null, filters))); + service.createApplication(REGION, "any-filter-app", "Server", null); + service.createDeploymentGroup(REGION, "any-filter-app", "group", null, ROLE_ARN, + Map.of("onPremisesInstanceTagFilters", filters), null); + + String deploymentId = service.createDeployment(REGION, "any-filter-app", "group", null, + Map.of("appSpecContent", Map.of("content", "os: linux")), null); + + assertEquals(Set.of("team-only", "role-only", "both"), + Set.copyOf(service.listDeploymentTargets(REGION, deploymentId))); + } + // ---- Server platform (SSM) hooks --------------------------------------------------- @Test