mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(sam): append resource lists to Globals lists (#7118)
* fix(sam): append resource lists to Globals lists * fix(sam): replace intrinsic Globals values atomically * fix(sam): let function architectures replace globals
This commit is contained in:
1 parent
3cb727a85e
commit
227769385f
3 files changed
+130
-5
No files matched your search
+21
-5
@@ -1099,8 +1099,8 @@ class SamTransformProcessor {
|
||||
*
|
||||
* <p>Nested objects (e.g. {@code Environment.Variables}, {@code Tags}) are merged key-wise, so a
|
||||
* resource only overrides the individual keys it sets and global entries are preserved — matching
|
||||
* SAM's map-merge behavior. Scalar and array-valued properties (e.g. {@code Policies},
|
||||
* {@code Layers}) are overridden wholesale; SAM's additive list-append for those is not implemented.
|
||||
* SAM's map-merge behavior. Lists append global entries first, except
|
||||
* {@code Function.Architectures}, which the resource replaces; scalars are overridden.
|
||||
*/
|
||||
private JsonNode mergeGlobals(JsonNode globals, String section, JsonNode properties) {
|
||||
JsonNode sectionGlobals = globals.path(section);
|
||||
@@ -1110,12 +1110,17 @@ class SamTransformProcessor {
|
||||
if (!properties.isObject()) {
|
||||
return sectionGlobals.deepCopy();
|
||||
}
|
||||
return deepMerge((ObjectNode) sectionGlobals.deepCopy(), (ObjectNode) properties);
|
||||
ObjectNode merged = (ObjectNode) sectionGlobals.deepCopy();
|
||||
if ("Function".equals(section) && properties.has("Architectures")) {
|
||||
merged.remove("Architectures");
|
||||
}
|
||||
return deepMerge(merged, (ObjectNode) properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively merges {@code override} into {@code base}: when both sides hold an object for the
|
||||
* same key, the objects are merged key-wise; otherwise the override value replaces the base value.
|
||||
* same key, plain objects are merged key-wise; lists are appended with base entries first;
|
||||
* intrinsic function objects and scalar values are replaced by the override value.
|
||||
* {@code base} is mutated and returned.
|
||||
*/
|
||||
private ObjectNode deepMerge(ObjectNode base, ObjectNode override) {
|
||||
@@ -1123,8 +1128,11 @@ class SamTransformProcessor {
|
||||
String key = entry.getKey();
|
||||
JsonNode overrideValue = entry.getValue();
|
||||
JsonNode baseValue = base.get(key);
|
||||
if (baseValue != null && baseValue.isObject() && overrideValue.isObject()) {
|
||||
if (baseValue != null && baseValue.isObject() && overrideValue.isObject()
|
||||
&& !isIntrinsicObject(baseValue) && !isIntrinsicObject(overrideValue)) {
|
||||
base.set(key, deepMerge((ObjectNode) baseValue, (ObjectNode) overrideValue));
|
||||
} else if (baseValue instanceof ArrayNode baseArray && overrideValue.isArray()) {
|
||||
baseArray.addAll((ArrayNode) overrideValue.deepCopy());
|
||||
} else {
|
||||
base.set(key, overrideValue.deepCopy());
|
||||
}
|
||||
@@ -1132,6 +1140,14 @@ class SamTransformProcessor {
|
||||
return base;
|
||||
}
|
||||
|
||||
private static boolean isIntrinsicObject(JsonNode value) {
|
||||
if (!value.isObject() || value.size() != 1) {
|
||||
return false;
|
||||
}
|
||||
String key = value.fieldNames().next();
|
||||
return key.equals("Ref") || key.startsWith("Fn::");
|
||||
}
|
||||
|
||||
private String expandServerlessFunction(String logicalId, JsonNode properties, JsonNode source,
|
||||
ObjectNode template, ObjectNode resources,
|
||||
Map<String, String> parameterValues) {
|
||||
|
||||
+4
@@ -123,9 +123,13 @@ class SamLayerVersionTransformTest {
|
||||
function.put("Type", "AWS::Serverless::Function");
|
||||
function.putObject("Properties").put("Runtime", "nodejs22.x").put("Handler", "index.handler")
|
||||
.put("InlineCode", "exports.handler=async()=>({})");
|
||||
((ObjectNode) function.path("Properties")).putArray("Layers")
|
||||
.add("arn:aws:lambda:us-east-1:123456789012:layer:local:1");
|
||||
JsonNode result = processor.expandSamTemplate(input);
|
||||
String id = result.at("/Outputs/LayerArn/Value/Ref").asText();
|
||||
assertEquals(id, result.at("/Resources/Function/Properties/Layers/0/Ref").asText());
|
||||
assertEquals("arn:aws:lambda:us-east-1:123456789012:layer:local:1",
|
||||
result.at("/Resources/Function/Properties/Layers/1").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+105
@@ -2119,6 +2119,67 @@ class SamTransformProcessorTest {
|
||||
assertEquals(3, lambdaProps.path("Timeout").asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void expandSamTemplate_prependsGlobalFunctionLayers() throws Exception {
|
||||
JsonNode template = objectMapper.readTree("""
|
||||
{
|
||||
"Transform": "AWS::Serverless-2016-10-31",
|
||||
"Globals": {"Function": {"Layers": ["arn:aws:lambda:us-east-1:123456789012:layer:shared:1"]}},
|
||||
"Resources": {
|
||||
"MyFunc": {
|
||||
"Type": "AWS::Serverless::Function",
|
||||
"Properties": {
|
||||
"Runtime": "nodejs20.x",
|
||||
"Handler": "index.handler",
|
||||
"InlineCode": "exports.handler = async () => ({});",
|
||||
"Layers": ["arn:aws:lambda:us-east-1:123456789012:layer:local:1"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
JsonNode layers = processor.expandSamTemplate(template)
|
||||
.path("Resources").path("MyFunc").path("Properties").path("Layers");
|
||||
assertEquals(2, layers.size());
|
||||
assertEquals("arn:aws:lambda:us-east-1:123456789012:layer:shared:1", layers.get(0).asText());
|
||||
assertEquals("arn:aws:lambda:us-east-1:123456789012:layer:local:1", layers.get(1).asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void expandSamTemplate_functionArchitecturesReplaceGlobals() throws Exception {
|
||||
JsonNode template = objectMapper.readTree("""
|
||||
{
|
||||
"Transform": "AWS::Serverless-2016-10-31",
|
||||
"Globals": {"Function": {"Architectures": ["x86_64"]}},
|
||||
"Resources": {"MyFunc": {
|
||||
"Type": "AWS::Serverless::Function",
|
||||
"Properties": {
|
||||
"Runtime": "nodejs20.x",
|
||||
"Handler": "index.handler",
|
||||
"InlineCode": "exports.handler = async () => ({});",
|
||||
"Architectures": ["arm64"]
|
||||
}
|
||||
}, "InheritedFunc": {
|
||||
"Type": "AWS::Serverless::Function",
|
||||
"Properties": {
|
||||
"Runtime": "nodejs20.x",
|
||||
"Handler": "index.handler",
|
||||
"InlineCode": "exports.handler = async () => ({});"
|
||||
}
|
||||
}}
|
||||
}
|
||||
""");
|
||||
|
||||
JsonNode expanded = processor.expandSamTemplate(template);
|
||||
JsonNode architectures = expanded
|
||||
.path("Resources").path("MyFunc").path("Properties").path("Architectures");
|
||||
assertEquals(objectMapper.readTree("[\"arm64\"]"), architectures);
|
||||
JsonNode inheritedArchitectures = expanded
|
||||
.path("Resources").path("InheritedFunc").path("Properties").path("Architectures");
|
||||
assertEquals(objectMapper.readTree("[\"x86_64\"]"), inheritedArchitectures);
|
||||
}
|
||||
|
||||
@Test
|
||||
void expandSamTemplate_mergesNestedMapsFromGlobals() throws Exception {
|
||||
// Environment.Variables must merge key-wise: globals-only keys preserved, function keys win on clash
|
||||
@@ -2153,6 +2214,50 @@ class SamTransformProcessorTest {
|
||||
assertEquals("from-function", vars.path("SHARED").asText()); // clash resolved in favor of function
|
||||
}
|
||||
|
||||
@Test
|
||||
void expandSamTemplate_replacesIntrinsicValuesWithinGlobalsMaps() throws Exception {
|
||||
JsonNode template = objectMapper.readTree("""
|
||||
{
|
||||
"Transform": "AWS::Serverless-2016-10-31",
|
||||
"Globals": {"Function": {"Environment": {"Variables": {
|
||||
"GLOBAL": "global",
|
||||
"URL": {"Fn::Join": ["", ["https://global.example", "/old"]]},
|
||||
"SCALAR": {"Fn::Sub": "global-${AWS::Region}"},
|
||||
"REFERENCE": {"Ref": "GlobalParam"}
|
||||
}}}},
|
||||
"Resources": {"MyFunc": {
|
||||
"Type": "AWS::Serverless::Function",
|
||||
"Properties": {
|
||||
"Runtime": "nodejs20.x",
|
||||
"Handler": "index.handler",
|
||||
"InlineCode": "exports.handler = async () => ({});",
|
||||
"Environment": {"Variables": {
|
||||
"LOCAL": "local",
|
||||
"URL": {"Fn::Join": ["", ["https://local.example", "/v1"]]},
|
||||
"SCALAR": {"Fn::Base64": "local"},
|
||||
"REFERENCE": {"Fn::Sub": "local-${AWS::Region}"}
|
||||
}}
|
||||
}
|
||||
}}
|
||||
}
|
||||
""");
|
||||
|
||||
JsonNode variables = processor.expandSamTemplate(template)
|
||||
.path("Resources").path("MyFunc").path("Properties")
|
||||
.path("Environment").path("Variables");
|
||||
assertEquals("global", variables.path("GLOBAL").asText());
|
||||
assertEquals("local", variables.path("LOCAL").asText());
|
||||
assertEquals(objectMapper.readTree("""
|
||||
{"Fn::Join": ["", ["https://local.example", "/v1"]]}
|
||||
"""), variables.path("URL"));
|
||||
assertEquals(objectMapper.readTree("""
|
||||
{"Fn::Base64": "local"}
|
||||
"""), variables.path("SCALAR"));
|
||||
assertEquals(objectMapper.readTree("""
|
||||
{"Fn::Sub": "local-${AWS::Region}"}
|
||||
"""), variables.path("REFERENCE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expandSamTemplate_stripsGlobalsWhenResourcesAbsent() throws Exception {
|
||||
// SAM-only Globals must be stripped even on the early return (no/!object Resources)
|
||||
|
||||
Reference in new issue
Block a user