mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(codedeploy): preserve deployment group creation tags (#8025)
This commit is contained in:
1 parent
195a036b63
commit
d0d0e0eef8
12 files changed
+645
-102
No files matched your search
@@ -41,12 +41,6 @@ final class TagOnCreate {
|
||||
/** CloudWatch's handler reads an alarm's tags from {@code tags} when the body has no {@code Tags}. */
|
||||
private static final Set<String> LOWER_CASE_TAGS = Set.of("cloudwatch:PutMetricAlarm", "cloudwatch:PutCompositeAlarm");
|
||||
|
||||
/**
|
||||
* CodeDeploy's handler reads a tag as {@code Key} and {@code Value}, or as {@code key} and {@code value} when it
|
||||
* has no {@code Key}.
|
||||
*/
|
||||
private static final Set<String> EITHER_CASE_TAGS = Set.of("codedeploy");
|
||||
|
||||
/**
|
||||
* The tags one resource gets, and the type of that resource where a tag specification names it, else null.
|
||||
*/
|
||||
@@ -105,8 +99,7 @@ final class TagOnCreate {
|
||||
otherSpellings.addAll(anySpellingInForm(form, member));
|
||||
} else if (body != null && body.isObject()) {
|
||||
Map<String, Map<String, String>> byType = new LinkedHashMap<>();
|
||||
collect(topLevel(body, operation, member.steps().getFirst().name()), member,
|
||||
EITHER_CASE_TAGS.contains(operation.substring(0, operation.indexOf(':'))), 0, null, byType);
|
||||
collect(topLevel(body, operation, member.steps().getFirst().name()), member, 0, null, byType);
|
||||
byType.forEach((type, tags) -> add(sets, type, tags));
|
||||
otherSpellings.addAll(anySpellingInBody(body, member));
|
||||
}
|
||||
@@ -430,23 +423,14 @@ final class TagOnCreate {
|
||||
* The tags a body carries at the member, by the resource type of the tag specification they are in, null
|
||||
* outside one.
|
||||
*/
|
||||
private static void collect(JsonNode value, TagOnCreateMembers.Member member, boolean eitherCase, int index,
|
||||
private static void collect(JsonNode value, TagOnCreateMembers.Member member, int index,
|
||||
String type, Map<String, Map<String, String>> byType) {
|
||||
List<PassedRoleMembers.Step> steps = member.steps();
|
||||
PassedRoleMembers.Step step = steps.get(index);
|
||||
if (index == steps.size() - 1) {
|
||||
Map<String, String> tags = byType.computeIfAbsent(type, ignored -> new LinkedHashMap<>());
|
||||
// CodeDeployJsonHandler iterates the member whatever it holds, which for an object is its values.
|
||||
if (step.into() == PassedRoleMembers.Into.LIST && value != null
|
||||
&& (value.isArray() || eitherCase && value.isObject())) {
|
||||
if (step.into() == PassedRoleMembers.Into.LIST && value != null && value.isArray()) {
|
||||
for (JsonNode tag : value) {
|
||||
if (eitherCase) {
|
||||
// As CodeDeployJsonHandler reads it: as text whatever it holds, a missing key an empty one.
|
||||
boolean lower = !tag.has(member.key());
|
||||
tags.put(tag.path(lower ? member.key().toLowerCase(Locale.ROOT) : member.key()).asText(),
|
||||
tag.path(lower ? member.value().toLowerCase(Locale.ROOT) : member.value()).asText());
|
||||
continue;
|
||||
}
|
||||
JsonNode key = tag.get(member.key());
|
||||
if (key != null && key.isValueNode() && !key.isNull()) {
|
||||
tags.put(key.asText(), text(tag.get(member.value())));
|
||||
@@ -463,13 +447,13 @@ final class TagOnCreate {
|
||||
}
|
||||
String next = steps.get(index + 1).name();
|
||||
switch (step.into()) {
|
||||
case NONE -> collect(value.get(next), member, eitherCase, index + 1, type, byType);
|
||||
case KEY -> collect(value.path(step.key()).get(next), member, eitherCase, index + 1, type, byType);
|
||||
case NONE -> collect(value.get(next), member, index + 1, type, byType);
|
||||
case KEY -> collect(value.path(step.key()).get(next), member, index + 1, type, byType);
|
||||
case LIST, MAP -> {
|
||||
if (step.into() == PassedRoleMembers.Into.LIST ? value.isArray() : value.isObject()) {
|
||||
for (JsonNode element : value) {
|
||||
JsonNode kind = member.resourceType() == null ? null : element.get(member.resourceType());
|
||||
collect(element.get(next), member, eitherCase, index + 1,
|
||||
collect(element.get(next), member, index + 1,
|
||||
kind != null && kind.isTextual() ? kind.asText() : type, byType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,10 +18,13 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ApplicationScoped
|
||||
public class CodeDeployJsonHandler {
|
||||
|
||||
private static final Set<String> TAG_FILTER_TYPES = Set.of("KEY_ONLY", "VALUE_ONLY", "KEY_AND_VALUE");
|
||||
|
||||
private final CodeDeployService service;
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
@@ -75,7 +78,7 @@ public class CodeDeployJsonHandler {
|
||||
private Response createApplication(JsonNode req, String region) {
|
||||
String name = req.path("applicationName").asText(null);
|
||||
String computePlatform = req.has("computePlatform") ? req.path("computePlatform").asText() : null;
|
||||
List<Map<String, String>> tags = parseTags(req, "tags");
|
||||
List<Map<String, String>> tags = parseOptionalTags(req, "tags");
|
||||
Application app = service.createApplication(region, name, computePlatform, tags);
|
||||
return Response.ok(Map.of("applicationId", app.getApplicationId())).build();
|
||||
}
|
||||
@@ -123,8 +126,9 @@ public class CodeDeployJsonHandler {
|
||||
String deploymentConfigName = req.has("deploymentConfigName") ? req.path("deploymentConfigName").asText() : null;
|
||||
String serviceRoleArn = req.has("serviceRoleArn") ? req.path("serviceRoleArn").asText() : null;
|
||||
Map<String, Object> fields = extractGroupFields(req);
|
||||
List<Map<String, String>> tags = parseOptionalTags(req, "tags");
|
||||
DeploymentGroup group = service.createDeploymentGroup(region, appName, groupName,
|
||||
deploymentConfigName, serviceRoleArn, fields);
|
||||
deploymentConfigName, serviceRoleArn, fields, tags);
|
||||
return Response.ok(Map.of("deploymentGroupId", group.getDeploymentGroupId())).build();
|
||||
}
|
||||
|
||||
@@ -313,13 +317,13 @@ public class CodeDeployJsonHandler {
|
||||
|
||||
private Response listOnPremisesInstances(JsonNode req, String region) {
|
||||
String registrationStatus = req.has("registrationStatus") ? req.path("registrationStatus").asText() : null;
|
||||
List<Map<String, String>> tagFilters = parseTags(req, "tagFilters");
|
||||
List<Map<String, String>> tagFilters = parseTagFilters(req);
|
||||
List<String> names = service.listOnPremisesInstances(region, registrationStatus, tagFilters);
|
||||
return Response.ok(Map.of("instanceNames", names)).build();
|
||||
}
|
||||
|
||||
private Response addTagsToOnPremisesInstances(JsonNode req, String region) {
|
||||
List<Map<String, String>> tags = parseTags(req, "tags");
|
||||
List<Map<String, String>> tags = parseTags(req, "tags", "InvalidTagException");
|
||||
List<String> names = new ArrayList<>();
|
||||
req.path("instanceNames").forEach(n -> names.add(n.asText()));
|
||||
service.addTagsToOnPremisesInstances(region, names, tags);
|
||||
@@ -327,7 +331,7 @@ public class CodeDeployJsonHandler {
|
||||
}
|
||||
|
||||
private Response removeTagsFromOnPremisesInstances(JsonNode req, String region) {
|
||||
List<Map<String, String>> tags = parseTags(req, "tags");
|
||||
List<Map<String, String>> tags = parseTags(req, "tags", "InvalidTagException");
|
||||
List<String> names = new ArrayList<>();
|
||||
req.path("instanceNames").forEach(n -> names.add(n.asText()));
|
||||
service.removeTagsFromOnPremisesInstances(region, names, tags);
|
||||
@@ -348,22 +352,58 @@ public class CodeDeployJsonHandler {
|
||||
return fields;
|
||||
}
|
||||
|
||||
private List<Map<String, String>> parseTags(JsonNode req, String fieldName) {
|
||||
if (!req.has(fieldName) || req.get(fieldName).isNull()) {
|
||||
private List<Map<String, String>> parseTagFilters(JsonNode req) {
|
||||
if (!req.has("tagFilters") || req.get("tagFilters").isNull()) {
|
||||
return List.of();
|
||||
}
|
||||
JsonNode filterList = req.get("tagFilters");
|
||||
if (!filterList.isArray()) {
|
||||
throw new AwsException("InvalidTagFilterException", "The specified tag filters are not valid.", 400);
|
||||
}
|
||||
List<Map<String, String>> result = new ArrayList<>();
|
||||
for (JsonNode tag : req.get(fieldName)) {
|
||||
Map<String, String> t = new HashMap<>();
|
||||
// Support both capitalizations (Tags uses Key/Value, tags uses key/value)
|
||||
if (tag.has("Key")) {
|
||||
t.put("Key", tag.path("Key").asText());
|
||||
t.put("Value", tag.path("Value").asText());
|
||||
} else {
|
||||
t.put("Key", tag.path("key").asText());
|
||||
t.put("Value", tag.path("value").asText());
|
||||
for (JsonNode filter : filterList) {
|
||||
if (!filter.isObject()) {
|
||||
throw new AwsException("InvalidTagFilterException", "The specified tag filters are not valid.", 400);
|
||||
}
|
||||
result.add(t);
|
||||
Map<String, String> values = new HashMap<>();
|
||||
for (String member : List.of("Key", "Value", "Type")) {
|
||||
if (filter.hasNonNull(member)) {
|
||||
JsonNode value = filter.get(member);
|
||||
if (!value.isTextual()
|
||||
|| (member.equals("Type") && !TAG_FILTER_TYPES.contains(value.asText()))) {
|
||||
throw new AwsException("InvalidTagFilterException", "The specified tag filters are not valid.", 400);
|
||||
}
|
||||
values.put(member, value.asText());
|
||||
}
|
||||
}
|
||||
result.add(values);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<Map<String, String>> parseTags(JsonNode req, String fieldName) {
|
||||
return parseTags(req, fieldName, "InvalidTagsToAddException");
|
||||
}
|
||||
|
||||
private List<Map<String, String>> parseOptionalTags(JsonNode req, String fieldName) {
|
||||
return req.path(fieldName).isNull() ? List.of() : parseTags(req, fieldName);
|
||||
}
|
||||
|
||||
private List<Map<String, String>> parseTags(JsonNode req, String fieldName, String invalidType) {
|
||||
if (!req.has(fieldName)) {
|
||||
return List.of();
|
||||
}
|
||||
JsonNode tagList = req.get(fieldName);
|
||||
if (!tagList.isArray()) {
|
||||
throw new AwsException(invalidType, "The specified tags are not valid.", 400);
|
||||
}
|
||||
List<Map<String, String>> result = new ArrayList<>();
|
||||
for (JsonNode tag : tagList) {
|
||||
if (!tag.isObject() || !tag.path("Key").isTextual()
|
||||
|| tag.has("Value") && !tag.get("Value").isTextual()) {
|
||||
throw new AwsException(invalidType, "The specified tags are not valid.", 400);
|
||||
}
|
||||
result.add(Map.of("Key", tag.get("Key").asText(), "Value", tag.path("Value").asText("")));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -424,7 +424,7 @@ public class CodeDeployService {
|
||||
|
||||
public DeploymentGroup createDeploymentGroup(String region, String appName, String groupName,
|
||||
String deploymentConfigName, String serviceRoleArn,
|
||||
Map<String, Object> fields) {
|
||||
Map<String, Object> fields, List<Map<String, String>> tags) {
|
||||
getApplication(region, appName);
|
||||
Map<String, Map<String, DeploymentGroup>> appGroups = deploymentGroupsFor(region);
|
||||
Map<String, DeploymentGroup> groupStore = appGroups.computeIfAbsent(appName, a -> new ConcurrentHashMap<>());
|
||||
@@ -442,6 +442,9 @@ public class CodeDeployService {
|
||||
applyGroupFields(group, fields);
|
||||
groupStore.put(groupName, group);
|
||||
persistRegion(deploymentGroups, region);
|
||||
if (tags != null && !tags.isEmpty()) {
|
||||
applyTags(deploymentGroupArn(region, appName, groupName), tags);
|
||||
}
|
||||
return group;
|
||||
}
|
||||
|
||||
@@ -465,14 +468,25 @@ public class CodeDeployService {
|
||||
Map<String, DeploymentGroup> groupStore = deploymentGroupsFor(region)
|
||||
.computeIfAbsent(appName, a -> new ConcurrentHashMap<>());
|
||||
|
||||
if (newGroupName != null && !newGroupName.equals(currentGroupName)
|
||||
&& groupStore.containsKey(newGroupName)) {
|
||||
throw new AwsException("DeploymentGroupAlreadyExistsException",
|
||||
"Deployment group already exists: " + newGroupName, 400);
|
||||
}
|
||||
|
||||
if (deploymentConfigName != null) { group.setDeploymentConfigName(deploymentConfigName); }
|
||||
if (serviceRoleArn != null) { group.setServiceRoleArn(serviceRoleArn); }
|
||||
applyGroupFields(group, fields);
|
||||
|
||||
if (newGroupName != null && !newGroupName.equals(currentGroupName)) {
|
||||
String oldArn = deploymentGroupArn(region, appName, currentGroupName);
|
||||
groupStore.remove(currentGroupName);
|
||||
group.setDeploymentGroupName(newGroupName);
|
||||
groupStore.put(newGroupName, group);
|
||||
Map<String, String> groupTags = tags.remove(oldArn);
|
||||
if (groupTags != null) {
|
||||
tags.put(deploymentGroupArn(region, appName, newGroupName), groupTags);
|
||||
}
|
||||
}
|
||||
persistRegion(deploymentGroups, region);
|
||||
return group;
|
||||
@@ -486,6 +500,7 @@ public class CodeDeployService {
|
||||
throw new AwsException("DeploymentGroupDoesNotExistException",
|
||||
"Deployment group does not exist: " + groupName, 400);
|
||||
}
|
||||
tags.remove(deploymentGroupArn(region, appName, groupName));
|
||||
persistRegion(deploymentGroups, region);
|
||||
}
|
||||
|
||||
@@ -1122,10 +1137,21 @@ public class CodeDeployService {
|
||||
for (Map<String, String> filter : filters) {
|
||||
String key = filter.get("Key");
|
||||
String value = filter.get("Value");
|
||||
if (key == null) { continue; }
|
||||
boolean found = instanceTags.stream()
|
||||
.anyMatch(t -> key.equals(t.get("Key"))
|
||||
&& (value == null || value.equals(t.get("Value"))));
|
||||
String type = filter.get("Type");
|
||||
if (key == null && type == null) { continue; }
|
||||
boolean found = instanceTags.stream().anyMatch(tag -> {
|
||||
if ("VALUE_ONLY".equals(type)) {
|
||||
return value != null && value.equals(tag.get("Value"));
|
||||
}
|
||||
if ("KEY_ONLY".equals(type)) {
|
||||
return key != null && key.equals(tag.get("Key"));
|
||||
}
|
||||
if ("KEY_AND_VALUE".equals(type) && value == null) {
|
||||
return false;
|
||||
}
|
||||
return key != null && key.equals(tag.get("Key"))
|
||||
&& (value == null || value.equals(tag.get("Value")));
|
||||
});
|
||||
if (!found) { return false; }
|
||||
}
|
||||
return true;
|
||||
|
||||
Reference in new issue
Block a user