mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
refactor(cloudformation): share API shape conversion across provisioners (#8670)
* refactor(cloudformation): share API shape conversion across provisioners
This commit is contained in:
1 parent
39a8c341e4
commit
f96cd91c8b
10 files changed
+216
-210
No files matched your search
+2
-25
@@ -3,7 +3,6 @@ package io.floci.aws.services.cloudformation.provisioners;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
@@ -562,30 +561,8 @@ public class BedrockAgentCoreCfnProvisioner implements CfnResourceProvisioner {
|
||||
return null;
|
||||
}
|
||||
JsonNode resolved = ctx.engine().resolveNodeOmittingNoValue(props.get(name));
|
||||
return resolved == null || resolved.isNull() || !resolved.isObject() ? null : toApiShape(resolved);
|
||||
}
|
||||
|
||||
/** Recursively renames the members of a resolved node to the API's camelCase, dropping nulls. */
|
||||
private static JsonNode toApiShape(JsonNode node) {
|
||||
if (node.isObject()) {
|
||||
ObjectNode out = JsonNodeFactory.instance.objectNode();
|
||||
node.fields().forEachRemaining(field -> {
|
||||
if (!field.getValue().isNull()) {
|
||||
out.set(decapitalize(field.getKey()), toApiShape(field.getValue()));
|
||||
}
|
||||
});
|
||||
return out;
|
||||
}
|
||||
if (node.isArray()) {
|
||||
ArrayNode out = JsonNodeFactory.instance.arrayNode();
|
||||
node.forEach(item -> out.add(toApiShape(item)));
|
||||
return out;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private static String decapitalize(String key) {
|
||||
return key.isEmpty() ? key : Character.toLowerCase(key.charAt(0)) + key.substring(1);
|
||||
return resolved == null || resolved.isNull() || !resolved.isObject() ? null
|
||||
: CfnApiShapeConverter.convert(resolved, CfnApiShapeConverter.OMIT_NULL_PROPERTIES);
|
||||
}
|
||||
|
||||
/** A {@code {key: value}} property with each value resolved, or an empty map when absent. */
|
||||
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
package io.floci.aws.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/** Converts resolved CloudFormation property names to their API request names. */
|
||||
final class CfnApiShapeConverter {
|
||||
|
||||
static final Rules OMIT_NULL_PROPERTIES = new Rules(Map.of(), Set.of(), Set.of(), true);
|
||||
|
||||
private CfnApiShapeConverter() {
|
||||
}
|
||||
|
||||
static JsonNode convert(JsonNode node, Rules rules) {
|
||||
if (node.isObject()) {
|
||||
ObjectNode result = JsonNodeFactory.instance.objectNode();
|
||||
node.fields().forEachRemaining(field -> {
|
||||
String name = field.getKey();
|
||||
JsonNode value = field.getValue();
|
||||
if (rules.omitNullProperties() && value.isNull()) {
|
||||
return;
|
||||
}
|
||||
String apiName = rules.names().getOrDefault(name, lowerFirst(name));
|
||||
if (rules.integerTextFields().contains(name) && value.isTextual()) {
|
||||
result.put(apiName, value.asInt());
|
||||
} else if (rules.opaqueFields().contains(name)) {
|
||||
result.set(apiName, value.deepCopy());
|
||||
} else {
|
||||
result.set(apiName, convert(value, rules));
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
if (node.isArray()) {
|
||||
ArrayNode result = JsonNodeFactory.instance.arrayNode();
|
||||
node.forEach(item -> result.add(convert(item, rules)));
|
||||
return result;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/** Capacity provider state uses Java maps and omits empty scalar values as well as nulls. */
|
||||
static Object convertToJava(JsonNode node) {
|
||||
if (node == null || node.isNull() || node.isMissingNode()) {
|
||||
return null;
|
||||
}
|
||||
if (node.isObject()) {
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
node.fields().forEachRemaining(field -> {
|
||||
Object value = convertToJava(field.getValue());
|
||||
if (value != null) {
|
||||
result.put(lowerFirst(field.getKey()), value);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
if (node.isArray()) {
|
||||
List<Object> result = new ArrayList<>();
|
||||
for (JsonNode item : node) {
|
||||
Object value = convertToJava(item);
|
||||
if (value != null) {
|
||||
result.add(value);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
if (node.isBoolean()) {
|
||||
return node.booleanValue();
|
||||
}
|
||||
if (node.isIntegralNumber()) {
|
||||
return node.asInt();
|
||||
}
|
||||
if (node.isNumber()) {
|
||||
return node.asDouble();
|
||||
}
|
||||
String text = node.asText();
|
||||
return text.isEmpty() ? null : text;
|
||||
}
|
||||
|
||||
static String lowerFirst(String name) {
|
||||
return name == null || name.isEmpty() ? name : Character.toLowerCase(name.charAt(0)) + name.substring(1);
|
||||
}
|
||||
|
||||
record Rules(Map<String, String> names, Set<String> opaqueFields, Set<String> integerTextFields,
|
||||
boolean omitNullProperties) {
|
||||
}
|
||||
}
|
||||
+4
-22
@@ -2,7 +2,6 @@ package io.floci.aws.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
@@ -14,6 +13,7 @@ import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -27,6 +27,8 @@ import java.util.Set;
|
||||
public class CodeBuildCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(CodeBuildCfnProvisioner.class);
|
||||
private static final CfnApiShapeConverter.Rules API_SHAPE =
|
||||
new CfnApiShapeConverter.Rules(Map.of("BuildSpec", "buildspec"), Set.of(), Set.of(), false);
|
||||
|
||||
private final CodeBuildJsonHandler codeBuildJsonHandler;
|
||||
private final RegionResolver regionResolver;
|
||||
@@ -47,7 +49,7 @@ public class CodeBuildCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
@Override
|
||||
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
ObjectNode request = (ObjectNode) lowerKeys(ctx.engine().resolveNode(props));
|
||||
ObjectNode request = (ObjectNode) CfnApiShapeConverter.convert(ctx.engine().resolveNode(props), API_SHAPE);
|
||||
boolean update = r.getPhysicalId() != null && !r.getPhysicalId().isBlank();
|
||||
String name = request.path("name").asText(null);
|
||||
if (name == null || name.isBlank()) {
|
||||
@@ -93,24 +95,4 @@ public class CodeBuildCfnProvisioner implements CfnResourceProvisioner {
|
||||
"CodeBuild " + action + " failed: " + e.getMessage(), 500);
|
||||
}
|
||||
}
|
||||
|
||||
private JsonNode lowerKeys(JsonNode node) {
|
||||
if (node instanceof ObjectNode objectNode) {
|
||||
ObjectNode result = mapper.createObjectNode();
|
||||
objectNode.fields().forEachRemaining(entry -> {
|
||||
String key = entry.getKey();
|
||||
String lowered = "BuildSpec".equals(key)
|
||||
? "buildspec"
|
||||
: key.isEmpty() ? key : Character.toLowerCase(key.charAt(0)) + key.substring(1);
|
||||
result.set(lowered, lowerKeys(entry.getValue()));
|
||||
});
|
||||
return result;
|
||||
}
|
||||
if (node instanceof ArrayNode arrayNode) {
|
||||
ArrayNode result = mapper.createArrayNode();
|
||||
arrayNode.forEach(entry -> result.add(lowerKeys(entry)));
|
||||
return result;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
+7
-33
@@ -2,7 +2,6 @@ package io.floci.aws.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
@@ -17,6 +16,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -31,6 +31,9 @@ import java.util.Set;
|
||||
public class CodePipelineCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(CodePipelineCfnProvisioner.class);
|
||||
// Configuration holds provider-defined keys that action executors match verbatim.
|
||||
private static final CfnApiShapeConverter.Rules API_SHAPE =
|
||||
new CfnApiShapeConverter.Rules(Map.of(), Set.of("Configuration"), Set.of(), false);
|
||||
|
||||
private final CodePipelineService codePipelineService;
|
||||
private final RegionResolver regionResolver;
|
||||
@@ -96,7 +99,7 @@ public class CodePipelineCfnProvisioner implements CfnResourceProvisioner {
|
||||
private static final String DISABLED_STAGES_ATTR = "__FlociCodePipelineDisabledStages";
|
||||
|
||||
private void provisionPipeline(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
ObjectNode declaration = (ObjectNode) lowerKeys(ctx.engine().resolveNode(props));
|
||||
ObjectNode declaration = (ObjectNode) CfnApiShapeConverter.convert(ctx.engine().resolveNode(props), API_SHAPE);
|
||||
JsonNode transitions = declaration.remove("disableInboundStageTransitions");
|
||||
JsonNode tags = declaration.remove("tags");
|
||||
declaration.remove("restartExecutionOnUpdate");
|
||||
@@ -212,7 +215,7 @@ public class CodePipelineCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
|
||||
private void provisionCustomActionType(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
ObjectNode request = (ObjectNode) lowerKeys(ctx.engine().resolveNode(props));
|
||||
ObjectNode request = (ObjectNode) CfnApiShapeConverter.convert(ctx.engine().resolveNode(props), API_SHAPE);
|
||||
request.remove("tags");
|
||||
String physicalId = request.path("category").asText() + "|Custom|"
|
||||
+ request.path("provider").asText() + "|" + request.path("version").asText("1");
|
||||
@@ -231,7 +234,7 @@ public class CodePipelineCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
|
||||
private void provisionWebhook(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
ObjectNode webhook = (ObjectNode) lowerKeys(ctx.engine().resolveNode(props));
|
||||
ObjectNode webhook = (ObjectNode) CfnApiShapeConverter.convert(ctx.engine().resolveNode(props), API_SHAPE);
|
||||
JsonNode registerNode = webhook.remove("registerWithThirdParty");
|
||||
boolean register = registerNode != null && registerNode.asBoolean(false);
|
||||
String previousPhysicalId = r.getPhysicalId();
|
||||
@@ -256,33 +259,4 @@ public class CodePipelineCfnProvisioner implements CfnResourceProvisioner {
|
||||
r.setPhysicalId(name);
|
||||
r.getAttributes().put("Url", response.path("webhook").path("url").asText(""));
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively lowercases the first letter of every object key — except inside
|
||||
* {@code Configuration} maps, whose keys are opaque provider-defined strings
|
||||
* ({@code Owner}, {@code Repo}, {@code ProjectName}, ...) that the action executors
|
||||
* match verbatim.
|
||||
*/
|
||||
private JsonNode lowerKeys(JsonNode node) {
|
||||
if (node instanceof ObjectNode objectNode) {
|
||||
ObjectNode result = mapper.createObjectNode();
|
||||
objectNode.fields().forEachRemaining(entry -> {
|
||||
String key = entry.getKey();
|
||||
if ("Configuration".equals(key)) {
|
||||
result.set("configuration", entry.getValue().deepCopy());
|
||||
return;
|
||||
}
|
||||
String lowered = key.isEmpty()
|
||||
? key : Character.toLowerCase(key.charAt(0)) + key.substring(1);
|
||||
result.set(lowered, lowerKeys(entry.getValue()));
|
||||
});
|
||||
return result;
|
||||
}
|
||||
if (node instanceof ArrayNode arrayNode) {
|
||||
ArrayNode result = mapper.createArrayNode();
|
||||
arrayNode.forEach(entry -> result.add(lowerKeys(entry)));
|
||||
return result;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
+1
-46
@@ -186,7 +186,7 @@ public class EcsCapacityCfnProvisioner implements CfnResourceProvisioner {
|
||||
if (asg == null || !asg.isObject()) {
|
||||
return new LinkedHashMap<>();
|
||||
}
|
||||
Object converted = toApiShape(ctx.engine().resolveNode(asg));
|
||||
Object converted = CfnApiShapeConverter.convertToJava(ctx.engine().resolveNode(asg));
|
||||
if (converted instanceof Map<?, ?> map) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> typed = (Map<String, Object>) map;
|
||||
@@ -211,51 +211,6 @@ public class EcsCapacityCfnProvisioner implements CfnResourceProvisioner {
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Recursively converts a resolved CloudFormation node to the ECS API's camelCase Java shape. */
|
||||
private Object toApiShape(JsonNode node) {
|
||||
if (node == null || node.isNull() || node.isMissingNode()) {
|
||||
return null;
|
||||
}
|
||||
if (node.isObject()) {
|
||||
Map<String, Object> out = new LinkedHashMap<>();
|
||||
node.fields().forEachRemaining(field -> {
|
||||
Object value = toApiShape(field.getValue());
|
||||
if (value != null) {
|
||||
out.put(decapitalize(field.getKey()), value);
|
||||
}
|
||||
});
|
||||
return out;
|
||||
}
|
||||
if (node.isArray()) {
|
||||
List<Object> out = new ArrayList<>();
|
||||
for (JsonNode item : node) {
|
||||
Object value = toApiShape(item);
|
||||
if (value != null) {
|
||||
out.add(value);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
if (node.isBoolean()) {
|
||||
return node.booleanValue();
|
||||
}
|
||||
if (node.isIntegralNumber()) {
|
||||
return node.asInt();
|
||||
}
|
||||
if (node.isNumber()) {
|
||||
return node.asDouble();
|
||||
}
|
||||
String text = node.asText();
|
||||
return text.isEmpty() ? null : text;
|
||||
}
|
||||
|
||||
private String decapitalize(String key) {
|
||||
if (key == null || key.isEmpty()) {
|
||||
return key;
|
||||
}
|
||||
return Character.toLowerCase(key.charAt(0)) + key.substring(1);
|
||||
}
|
||||
|
||||
private void deleteCapacityProvider(String physicalId) {
|
||||
// Idempotent: a provider that never got created (rollback) or was already removed is
|
||||
// delete-complete. describeCapacityProviders resolves both names and ARNs.
|
||||
|
||||
+7
-32
@@ -59,6 +59,12 @@ public class EcsCfnProvisioner implements CfnResourceProvisioner {
|
||||
private static final String SERVICE = "AWS::ECS::Service";
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final TypeReference<List<ClusterSetting>> SETTINGS = new TypeReference<>() { };
|
||||
private static final CfnApiShapeConverter.Rules API_SHAPE = new CfnApiShapeConverter.Rules(
|
||||
Map.of("EFSVolumeConfiguration", "efsVolumeConfiguration",
|
||||
"FSxWindowsFileServerVolumeConfiguration", "fsxWindowsFileServerVolumeConfiguration",
|
||||
"S3FilesVolumeConfiguration", "s3filesVolumeConfiguration",
|
||||
"FilesystemId", "fileSystemId", "IAM", "iam"),
|
||||
Set.of("DriverOpts", "Labels"), Set.of("TransitEncryptionPort"), false);
|
||||
|
||||
private final EcsService ecsService;
|
||||
private final EcsJsonHandler ecsJsonHandler;
|
||||
@@ -586,44 +592,13 @@ public class EcsCfnProvisioner implements CfnResourceProvisioner {
|
||||
if (node != null && node.isArray()) {
|
||||
node.forEach(item -> {
|
||||
if (item.isObject()) {
|
||||
result.add(normalizeCfnEcsNode(item));
|
||||
result.add(CfnApiShapeConverter.convert(item, API_SHAPE));
|
||||
}
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static JsonNode normalizeCfnEcsNode(JsonNode node) {
|
||||
if (node.isObject()) {
|
||||
ObjectNode result = MAPPER.createObjectNode();
|
||||
node.fields().forEachRemaining(field -> {
|
||||
String key = field.getKey();
|
||||
JsonNode value = field.getValue();
|
||||
String apiKey = switch (key) {
|
||||
case "EFSVolumeConfiguration" -> "efsVolumeConfiguration";
|
||||
case "FSxWindowsFileServerVolumeConfiguration" -> "fsxWindowsFileServerVolumeConfiguration";
|
||||
case "S3FilesVolumeConfiguration" -> "s3filesVolumeConfiguration";
|
||||
case "FilesystemId" -> "fileSystemId";
|
||||
case "IAM" -> "iam";
|
||||
default -> key.isEmpty() ? key : Character.toLowerCase(key.charAt(0)) + key.substring(1);
|
||||
};
|
||||
if ("TransitEncryptionPort".equals(key) && value.isTextual()) {
|
||||
result.put(apiKey, value.asInt());
|
||||
} else {
|
||||
result.set(apiKey, "DriverOpts".equals(key) || "Labels".equals(key)
|
||||
? value.deepCopy() : normalizeCfnEcsNode(value));
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
if (node.isArray()) {
|
||||
ArrayNode result = MAPPER.createArrayNode();
|
||||
node.forEach(item -> result.add(normalizeCfnEcsNode(item)));
|
||||
return result;
|
||||
}
|
||||
return node.deepCopy();
|
||||
}
|
||||
|
||||
private static Integer optionalInt(JsonNode node, String member) {
|
||||
return node.hasNonNull(member) ? node.path(member).asInt() : null;
|
||||
}
|
||||
|
||||
+2
-27
@@ -1,9 +1,6 @@
|
||||
package io.floci.aws.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.services.cloudformation.model.StackResource;
|
||||
import io.floci.aws.services.iot.IotService;
|
||||
@@ -154,7 +151,8 @@ public class IotCfnProvisioner implements CfnResourceProvisioner {
|
||||
if (props == null || !props.hasNonNull("TopicRulePayload")) {
|
||||
throw new IllegalArgumentException("AWS::IoT::TopicRule requires TopicRulePayload");
|
||||
}
|
||||
JsonNode payload = toApiShape(ctx.engine().resolveNode(props.get("TopicRulePayload")));
|
||||
JsonNode payload = CfnApiShapeConverter.convert(ctx.engine().resolveNode(props.get("TopicRulePayload")),
|
||||
CfnApiShapeConverter.OMIT_NULL_PROPERTIES);
|
||||
Map<String, String> tags = ctx.resolveTags(props, "Tags");
|
||||
String region = ctx.region();
|
||||
if (ctx.reusesPriorEntity(name)) {
|
||||
@@ -207,29 +205,6 @@ public class IotCfnProvisioner implements CfnResourceProvisioner {
|
||||
return attributes;
|
||||
}
|
||||
|
||||
/** Recursively renames the members of a resolved node to the API's camelCase, dropping nulls. */
|
||||
private static JsonNode toApiShape(JsonNode node) {
|
||||
if (node.isObject()) {
|
||||
ObjectNode out = JsonNodeFactory.instance.objectNode();
|
||||
node.fields().forEachRemaining(field -> {
|
||||
if (!field.getValue().isNull()) {
|
||||
out.set(decapitalize(field.getKey()), toApiShape(field.getValue()));
|
||||
}
|
||||
});
|
||||
return out;
|
||||
}
|
||||
if (node.isArray()) {
|
||||
ArrayNode out = JsonNodeFactory.instance.arrayNode();
|
||||
node.forEach(item -> out.add(toApiShape(item)));
|
||||
return out;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private static String decapitalize(String key) {
|
||||
return key.isEmpty() ? key : Character.toLowerCase(key.charAt(0)) + key.substring(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the calls that follow a create with the resource marked as owned by this stack, so a
|
||||
* failure among them still lets the create rollback delete the entity that now exists. When the
|
||||
|
||||
+2
-23
@@ -183,35 +183,14 @@ public class IotDomainConfigurationCfnProvisioner implements CfnResourceProvisio
|
||||
if (resolved != null && resolved.isObject()) {
|
||||
resolved.fields().forEachRemaining(field -> {
|
||||
if (!OUTSIDE_THE_REQUEST.contains(field.getKey()) && !field.getValue().isNull()) {
|
||||
request.set(decapitalize(field.getKey()), toApiShape(field.getValue()));
|
||||
request.set(CfnApiShapeConverter.lowerFirst(field.getKey()),
|
||||
CfnApiShapeConverter.convert(field.getValue(), CfnApiShapeConverter.OMIT_NULL_PROPERTIES));
|
||||
}
|
||||
});
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
private static JsonNode toApiShape(JsonNode node) {
|
||||
if (node.isObject()) {
|
||||
ObjectNode out = JsonNodeFactory.instance.objectNode();
|
||||
node.fields().forEachRemaining(field -> {
|
||||
if (!field.getValue().isNull()) {
|
||||
out.set(decapitalize(field.getKey()), toApiShape(field.getValue()));
|
||||
}
|
||||
});
|
||||
return out;
|
||||
}
|
||||
if (node.isArray()) {
|
||||
ArrayNode out = JsonNodeFactory.instance.arrayNode();
|
||||
node.forEach(item -> out.add(toApiShape(item)));
|
||||
return out;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private static String decapitalize(String key) {
|
||||
return key.isEmpty() ? key : Character.toLowerCase(key.charAt(0)) + key.substring(1);
|
||||
}
|
||||
|
||||
private static boolean sameCreateOnlyProperties(IotDomainConfiguration existing, JsonNode request) {
|
||||
List<String> desiredCertificates = new ArrayList<>();
|
||||
request.path("serverCertificateArns").forEach(arn -> desiredCertificates.add(arn.asText()));
|
||||
|
||||
@@ -133,14 +133,14 @@ class CfnSchemaCoverageTest {
|
||||
/**
|
||||
* Calls that hand the whole resolved property object to the service request, so every writable
|
||||
* property of the type counts as read without a name appearing in the source. Explicit, one entry
|
||||
* per helper: {@code camelisedProperties} (AppSync), {@code lowerKeys} over the resolved node
|
||||
* per helper: {@code camelisedProperties} (AppSync), {@code CfnApiShapeConverter} over the resolved node
|
||||
* (CodeBuild, CodePipeline), {@code jsonObjectToMap} over it (Cognito user pool), {@code
|
||||
* toCamelCase} over it (Lambda web), {@code requestBody} (IoT domain configuration), {@code
|
||||
* request} (EventBridge V2).
|
||||
*/
|
||||
private static final Set<String> WHOLESALE_CALLS = Set.of(
|
||||
"camelisedProperties(props, ctx)",
|
||||
"lowerKeys(ctx.engine().resolveNode(props))",
|
||||
"CfnApiShapeConverter.convert(ctx.engine().resolveNode(props)",
|
||||
"jsonObjectToMap(ctx.engine().resolveNode(props))",
|
||||
"toCamelCase(ctx.engine().resolveNodeOmittingNoValue(props)",
|
||||
"toCamelCase(resolved,",
|
||||
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
package io.floci.aws.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
|
||||
class CfnApiShapeConverterTest {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void ecsAliasesAndOpaqueVolumeOptionsKeepTheirApiShape() throws Exception {
|
||||
JsonNode properties = mapper.readTree("""
|
||||
{"EFSVolumeConfiguration":{"FilesystemId":"fs-1","TransitEncryptionPort":"2049",
|
||||
"AuthorizationConfig":{"IAM":"ENABLED"}},
|
||||
"DockerVolumeConfiguration":{"DriverOpts":{"MountType":"bind"},
|
||||
"Labels":{"TeamName":"infra"}},
|
||||
"FSxWindowsFileServerVolumeConfiguration":{"FileSystemId":"fsx-1"},
|
||||
"S3FilesVolumeConfiguration":{"BucketName":"bucket"}}
|
||||
""");
|
||||
CfnApiShapeConverter.Rules rules = new CfnApiShapeConverter.Rules(
|
||||
Map.of("EFSVolumeConfiguration", "efsVolumeConfiguration",
|
||||
"FSxWindowsFileServerVolumeConfiguration", "fsxWindowsFileServerVolumeConfiguration",
|
||||
"S3FilesVolumeConfiguration", "s3filesVolumeConfiguration",
|
||||
"FilesystemId", "fileSystemId", "IAM", "iam"),
|
||||
Set.of("DriverOpts", "Labels"), Set.of("TransitEncryptionPort"), false);
|
||||
|
||||
assertEquals(mapper.readTree("""
|
||||
{"efsVolumeConfiguration":{"fileSystemId":"fs-1","transitEncryptionPort":2049,
|
||||
"authorizationConfig":{"iam":"ENABLED"}},
|
||||
"dockerVolumeConfiguration":{"driverOpts":{"MountType":"bind"},
|
||||
"labels":{"TeamName":"infra"}},
|
||||
"fsxWindowsFileServerVolumeConfiguration":{"fileSystemId":"fsx-1"},
|
||||
"s3filesVolumeConfiguration":{"bucketName":"bucket"}}
|
||||
"""), CfnApiShapeConverter.convert(properties, rules));
|
||||
assertEquals("2049", properties.path("EFSVolumeConfiguration")
|
||||
.path("TransitEncryptionPort").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildSpecAndPipelineConfigurationUseDifferentExceptions() throws Exception {
|
||||
JsonNode properties = mapper.readTree("""
|
||||
{"Source":{"BuildSpec":"version: 0.2"},
|
||||
"Stages":[{"Actions":[{"Configuration":{"Owner":"awslabs",
|
||||
"Nested":{"ProjectName":"build"}}}]}]}
|
||||
""");
|
||||
CfnApiShapeConverter.Rules rules = new CfnApiShapeConverter.Rules(
|
||||
Map.of("BuildSpec", "buildspec"), Set.of("Configuration"), Set.of(), false);
|
||||
|
||||
assertEquals(mapper.readTree("""
|
||||
{"source":{"buildspec":"version: 0.2"},
|
||||
"stages":[{"actions":[{"configuration":{"Owner":"awslabs",
|
||||
"Nested":{"ProjectName":"build"}}}]}]}
|
||||
"""), CfnApiShapeConverter.convert(properties, rules));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullPropertiesAreOmittedWithoutRemovingNullArrayEntries() throws Exception {
|
||||
JsonNode properties = mapper.readTree("""
|
||||
{"NetworkConfiguration":{"NetworkMode":"PUBLIC","Unused":null,
|
||||
"Subnets":[null,{"SubnetId":"subnet-1","Other":null}]}}
|
||||
""");
|
||||
|
||||
assertEquals(mapper.readTree("""
|
||||
{"networkConfiguration":{"networkMode":"PUBLIC",
|
||||
"subnets":[null,{"subnetId":"subnet-1"}]}}
|
||||
"""), CfnApiShapeConverter.convert(properties, CfnApiShapeConverter.OMIT_NULL_PROPERTIES));
|
||||
}
|
||||
|
||||
@Test
|
||||
void capacityProviderUsesJavaShapeAndDropsNullAndEmptyValues() throws Exception {
|
||||
JsonNode properties = mapper.readTree("""
|
||||
{"ManagedScaling":{"Status":"ENABLED","TargetCapacity":80,
|
||||
"ScalingStep":1.5,"Unused":null,"Empty":"",
|
||||
"Settings":[null,"",true,2,{"Name":"x","Absent":null}]}}
|
||||
""");
|
||||
|
||||
Object converted = CfnApiShapeConverter.convertToJava(properties);
|
||||
|
||||
assertInstanceOf(LinkedHashMap.class, converted);
|
||||
assertEquals(Map.of("managedScaling", Map.of(
|
||||
"status", "ENABLED", "targetCapacity", 80, "scalingStep", 1.5,
|
||||
"settings", List.of(true, 2, Map.of("name", "x")))), converted);
|
||||
assertEquals("", properties.path("ManagedScaling").path("Empty").asText());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user