fix(cloudformation): an update that adds a create-only value replaces the resource (#8471)

This commit is contained in:
Oleksandr Nikolaichuk authored and GitHub committed 2026-11-16 15:00:11 -04:00
1 parent 95fa805590
commit b2def4f421
6 files changed
+124 -39

No files matched your search

@@ -75,9 +75,8 @@ public final class CfnCreateOnlyProperties {
/**
* True when the value at any create-only pointer of the type differs between the old and the new
* properties. A top-level name compares the whole value (a value added where none existed is not a
* change, as before). A nested pointer {@code A/B/C} compares the values at that path: present on
* one side only is a change, absent on both is not. A {@code *} segment compares every array
* properties. A pointer compares the values at that path, a top-level name included: a value added, removed or
* changed is a change, absent on both sides is not. A {@code *} segment compares every array
* element's subtree.
*/
public static boolean createOnlyValueChanged(String resourceType, JsonNode oldProps, JsonNode newProps) {
@@ -92,7 +91,7 @@ public final class CfnCreateOnlyProperties {
/** {@link #createOnlyValueChanged} for one pointer of the type's table. */
public static boolean createOnlyPointerChanged(String pointer, JsonNode oldProps, JsonNode newProps) {
String[] segments = pointer.split("/");
return pointerChanged(oldProps, newProps, segments, 0, segments.length == 1);
return pointerChanged(oldProps, newProps, segments, 0);
}
/** The subtrees of the properties that sit under one create-only pointer. */
@@ -111,12 +110,8 @@ public final class CfnCreateOnlyProperties {
return out;
}
private static boolean pointerChanged(JsonNode oldNode, JsonNode newNode, String[] segments, int index,
boolean topLevel) {
private static boolean pointerChanged(JsonNode oldNode, JsonNode newNode, String[] segments, int index) {
if (index == segments.length) {
if (topLevel) {
return oldNode != null && !oldNode.equals(newNode);
}
return oldNode == null ? newNode != null : !oldNode.equals(newNode);
}
if (oldNode == null && newNode == null) {
@@ -135,7 +130,7 @@ public final class CfnCreateOnlyProperties {
for (int i = 0; i < size; i++) {
JsonNode o = oldNode != null && oldNode.isArray() ? oldNode.get(i) : null;
JsonNode n = newNode != null && newNode.isArray() ? newNode.get(i) : null;
if (pointerChanged(o, n, segments, index + 1, false)) {
if (pointerChanged(o, n, segments, index + 1)) {
return true;
}
}
@@ -143,7 +138,7 @@ public final class CfnCreateOnlyProperties {
}
JsonNode o = oldNode != null && oldNode.isObject() ? oldNode.get(segments[index]) : null;
JsonNode n = newNode != null && newNode.isObject() ? newNode.get(segments[index]) : null;
return pointerChanged(o, n, segments, index + 1, segments.length == 1);
return pointerChanged(o, n, segments, index + 1);
}
private static void collect(JsonNode node, String[] segments, int index, List<JsonNode> out) {
@@ -1520,8 +1520,10 @@ public class CloudFormationService implements ResourceProvider {
}
boolean decide(String resourceType, String logicalId, JsonNode oldDef, JsonNode newDef) {
JsonNode oldProps = oldDef.path("Properties");
JsonNode newProps = newDef.path("Properties");
JsonNode oldBlock = withoutMissing(oldDef.path("Properties"), newDef.path("Properties"));
JsonNode newBlock = withoutMissing(newDef.path("Properties"), oldDef.path("Properties"));
JsonNode oldProps = withoutNoValue(logicalId, oldBlock, oldParams, oldConditions, oldTemplate);
JsonNode newProps = withoutNoValue(logicalId, newBlock, newParams, newConditions, newTemplate);
boolean asWritten = requiresReplacement(resourceType, oldProps, newProps, changedParams,
changedConditions);
if (!CfnCreateOnlyProperties.hasSchema(resourceType) || !oldProps.isObject() || !newProps.isObject()) {
@@ -1540,6 +1542,30 @@ public class CloudFormationService implements ResourceProvider {
return resolved;
}
/** The block as written with every AWS::NoValue member left out, by the side's own conditions. */
private JsonNode withoutNoValue(String logicalId, JsonNode block, Map<String, String> params,
Map<String, Boolean> conditions, JsonNode template) {
try {
JsonNode omitted = engineOver(ownerAccount(stack), params, Map.of(), Map.of(), conditions, template)
.omitNoValue(block);
return omitted.isMissingNode() ? block : omitted;
} catch (RuntimeException e) {
if (warned.add(logicalId)) {
LOG.warnv("AWS::NoValue members of {0} could not be left out for the diff; the preview "
+ "compares the block as written: {1}", logicalId, e.getMessage());
}
return block;
}
}
/**
* A missing Properties block counts as an empty one when the other side has a block, as adding or
* dropping every property in it does.
*/
private JsonNode withoutMissing(JsonNode block, JsonNode other) {
return block.isMissingNode() && other.isObject() ? objectMapper.createObjectNode() : block;
}
private static boolean holdsDynamicReference(JsonNode subtree) {
return CfnDynamicReferences.holdsDynamicReference(subtree.toString());
}
@@ -1557,6 +1583,9 @@ public class CloudFormationService implements ResourceProvider {
}
JsonNode before = compared[0].path(logicalId).path("Properties");
JsonNode after = compared[1].path(logicalId).path("Properties");
JsonNode comparedBefore = withoutMissing(before, after);
after = withoutMissing(after, before);
before = comparedBefore;
if (!before.isObject() || !after.isObject()) {
return false;
}
@@ -1633,9 +1662,9 @@ public class CloudFormationService implements ResourceProvider {
String accountId = ownerAccount(stack);
return CfnCreateOnlyProperties.createOnlyValueChanged(resourceType,
engineOver(accountId, oldParams, physicalIds, attributes, oldConditions, oldTemplate)
.resolveNode(oldProps),
.resolveNodeOmittingNoValue(oldProps),
engineOver(accountId, newParams, physicalIds, attributes, newConditions, newTemplate)
.resolveNode(newProps));
.resolveNodeOmittingNoValue(newProps));
} catch (RuntimeException e) {
if (warned.add(logicalId)) {
LOG.warnv("Create-only values of {0} ({1}) could not be resolved for the diff; "
@@ -1681,7 +1710,8 @@ public class CloudFormationService implements ResourceProvider {
}
try {
return CfnCreateOnlyProperties.createOnlyValueChanged(resourceType,
previous.resolveNode(priorProps), current.resolveNode(props));
previous.resolveNodeOmittingNoValue(priorProps),
current.resolveNodeOmittingNoValue(props));
} catch (RuntimeException e) {
LOG.warnv("Resolved create-only values of {0} ({1}) could not be compared; the update adds no "
+ "replacement beyond the engine's own decision: {2}", logicalId, resourceType, e.getMessage());
@@ -1691,16 +1721,11 @@ public class CloudFormationService implements ResourceProvider {
private boolean requiresReplacement(String resourceType, JsonNode oldProps, JsonNode newProps,
Set<String> changedParams, Set<String> changedConditions) {
if (oldProps == null || oldProps.isMissingNode()) {
// The caller reads a dropped or an added Properties block as an empty one, so only a resource
// without a block on either side reaches here with a missing node.
if (oldProps == null || oldProps.isMissingNode() || newProps == null || newProps.isMissingNode()) {
return false;
}
if (newProps == null || newProps.isMissingNode()) {
// A dropped Properties block removes every property in it, as removing them one by one does.
if (!oldProps.isObject()) {
return false;
}
newProps = objectMapper.createObjectNode();
}
if (CfnCreateOnlyProperties.hasSchema(resourceType)) {
// The registry table names the exact pointers; only a value at one of them replaces.
if (CfnCreateOnlyProperties.createOnlyValueChanged(resourceType, oldProps, newProps)) {
@@ -1718,10 +1743,10 @@ public class CloudFormationService implements ResourceProvider {
if (CfnCreateOnlyProperties.isCreateOnly(resourceType, field)) {
JsonNode oldVal = oldProps.get(field);
JsonNode newVal = newProps.get(field);
if (oldVal != null && !oldVal.equals(newVal)) {
if (!newVal.equals(oldVal)) {
return true;
}
if (newVal != null && (referencesAnyParameter(newVal, changedParams) || referencesAnyCondition(newVal, changedConditions))) {
if (referencesAnyParameter(newVal, changedParams) || referencesAnyCondition(newVal, changedConditions)) {
return true;
}
}