Light, fluffy, and always free - The AWS Local Emulator alternative
feat: add support of Cloudformation mapping and Fn::FindInMap function (#101)
Ilya Bryzgaloveef66983d90e
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.javaindex 9093a628e9..4330cfc448 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java@@ -200,6 +200,10 @@ public class CloudFormationService {// Resolve conditions firstMap<String, Boolean> conditions = resolveConditions(template, resolvedParams, stack, region);+ // Mappings+ Map<String, JsonNode> mappings = new HashMap<>();+ template.path("Mappings").fields().forEachRemaining(e -> mappings.put(e.getKey(), e.getValue()));+// Process resources in orderJsonNode resources = template.path("Resources");Map<String, String> physicalIds = new LinkedHashMap<>();@@ -224,7 +228,7 @@ public class CloudFormationService {CloudFormationTemplateEngine engine = new CloudFormationTemplateEngine(config.defaultAccountId(), region, stack.getStackName(),- stack.getStackId(), resolvedParams, physicalIds, resourceAttrs, conditions, objectMapper);+ stack.getStackId(), resolvedParams, physicalIds, resourceAttrs, conditions, mappings, objectMapper);StackResource resource = stack.getResources().get(logicalId);if (resource == null) {@@ -251,7 +255,7 @@ public class CloudFormationService {stack.getOutputs().clear();CloudFormationTemplateEngine finalEngine = new CloudFormationTemplateEngine(config.defaultAccountId(), region, stack.getStackName(),- stack.getStackId(), resolvedParams, physicalIds, resourceAttrs, conditions, objectMapper);+ stack.getStackId(), resolvedParams, physicalIds, resourceAttrs, conditions, mappings, objectMapper);JsonNode outputs = template.path("Outputs");if (outputs.isObject()) {diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.javaindex b1828b21a0..1afaf447be 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java@@ -27,6 +27,7 @@ public class CloudFormationTemplateEngine {private final Map<String, String> physicalIds;private final Map<String, Map<String, String>> resourceAttributes;private final Map<String, Boolean> conditions;+ private final Map<String, JsonNode> mappings;private final ObjectMapper objectMapper;CloudFormationTemplateEngine(String accountId, String region, String stackName, String stackId,@@ -34,6 +35,7 @@ public class CloudFormationTemplateEngine {Map<String, String> physicalIds,Map<String, Map<String, String>> resourceAttributes,Map<String, Boolean> conditions,+ Map<String, JsonNode> mappings,ObjectMapper objectMapper) {this.accountId = accountId;this.region = region;@@ -43,6 +45,7 @@ public class CloudFormationTemplateEngine {this.physicalIds = physicalIds;this.resourceAttributes = resourceAttributes;this.conditions = conditions;+ this.mappings = mappings;this.objectMapper = objectMapper;}@@ -87,6 +90,9 @@ public class CloudFormationTemplateEngine {if (node.has("Fn::ImportValue")) {return resolve(node.get("Fn::ImportValue"));}+ if (node.has("Fn::FindInMap")) {+ return resolveFindInMap(node.get("Fn::FindInMap"));+ }}return node.asText();}@@ -245,4 +251,24 @@ public class CloudFormationTemplateEngine {LOG.debugv("Unresolved GetAtt: {0}.{1}", logicalId, attrName);return logicalId + "." + attrName;}++ private String resolveFindInMap(JsonNode node) {+ if (node.isArray()) {+ String mapName = resolve(node.get(0));+ String topLvlName = resolve(node.get(1));+ String secondLvlName = resolve(node.get(2));++ JsonNode map = mappings.get(mapName);+ if (map != null && map.isObject()) {+ JsonNode topLvl = map.get(topLvlName);+ if (topLvl != null && topLvl.isObject()) {+ JsonNode secondLvl = topLvl.get(secondLvlName);+ if (secondLvl != null) {+ return resolve(secondLvl);+ }+ }+ }+ }+ return "";+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.javaindex 3d4a3b021f..de0374469b 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java@@ -28,11 +28,24 @@ class CloudFormationIntegrationTest {void createStack_withS3AndSqs() {String template = """{+ "Mappings": {+ "Env": {+ "us-east-1": {+ "Name": "test"+ }+ }+ },"Resources": {"MyBucket": {"Type": "AWS::S3::Bucket","Properties": {- "BucketName": "cf-test-bucket"+ "BucketName": {+ "Fn::Sub": ["cf-${env}-bucket", {+ "env": {+ "Fn::FindInMap": ["Env", { "Ref" : "AWS::Region" }, "Name"]+ }+ }]+ }}},"MyQueue": {