mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): generate the create-only table from the registry and compare nested pointers (#8093)
This commit is contained in:
1 parent
b2fa9b3f6c
commit
410e09bf62
11 files changed
+854
-39
No files matched your search
@@ -1,11 +1,14 @@
|
||||
package io.floci.aws.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -64,4 +67,99 @@ public final class CfnCreateOnlyProperties {
|
||||
public static Set<String> getCreateOnlyProperties(String resourceType) {
|
||||
return PROPERTIES.getOrDefault(resourceType, Collections.emptySet());
|
||||
}
|
||||
|
||||
/** True when the registry table has an entry for the type, so its pointers are authoritative. */
|
||||
public static boolean hasSchema(String resourceType) {
|
||||
return PROPERTIES.containsKey(resourceType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* element's subtree.
|
||||
*/
|
||||
public static boolean createOnlyValueChanged(String resourceType, JsonNode oldProps, JsonNode newProps) {
|
||||
for (String pointer : getCreateOnlyProperties(resourceType)) {
|
||||
String[] segments = pointer.split("/");
|
||||
if (pointerChanged(oldProps, newProps, segments, 0, segments.length == 1)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** The subtrees of the new properties that sit under a create-only pointer of the type. */
|
||||
public static List<JsonNode> createOnlySubtrees(String resourceType, JsonNode props) {
|
||||
List<JsonNode> out = new ArrayList<>();
|
||||
for (String pointer : getCreateOnlyProperties(resourceType)) {
|
||||
collect(props, pointer.split("/"), 0, out);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private static boolean pointerChanged(JsonNode oldNode, JsonNode newNode, String[] segments, int index,
|
||||
boolean topLevel) {
|
||||
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) {
|
||||
return false;
|
||||
}
|
||||
if (index > 0 && (isIntrinsic(oldNode) || isIntrinsic(newNode))) {
|
||||
// A Ref or Fn:: node stands for a value this walk cannot see into, so compare it whole.
|
||||
return oldNode == null ? newNode != null : !oldNode.equals(newNode);
|
||||
}
|
||||
if ("*".equals(segments[index])) {
|
||||
int size = Math.max(oldNode != null && oldNode.isArray() ? oldNode.size() : 0,
|
||||
newNode != null && newNode.isArray() ? newNode.size() : 0);
|
||||
if (size == 0 && (oldNode != null && !oldNode.isArray() || newNode != null && !newNode.isArray())) {
|
||||
return oldNode == null ? newNode != null : !oldNode.equals(newNode);
|
||||
}
|
||||
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)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
private static void collect(JsonNode node, String[] segments, int index, List<JsonNode> out) {
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
if (index == segments.length || (index > 0 && isIntrinsic(node))) {
|
||||
out.add(node);
|
||||
return;
|
||||
}
|
||||
if ("*".equals(segments[index])) {
|
||||
if (node.isArray()) {
|
||||
for (JsonNode element : node) {
|
||||
collect(element, segments, index + 1, out);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (node.isObject()) {
|
||||
collect(node.get(segments[index]), segments, index + 1, out);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isIntrinsic(JsonNode node) {
|
||||
if (node == null || !node.isObject() || node.size() != 1) {
|
||||
return false;
|
||||
}
|
||||
String key = node.fieldNames().next();
|
||||
return "Ref".equals(key) || key.startsWith("Fn::");
|
||||
}
|
||||
}
|
||||
@@ -1224,16 +1224,27 @@ public class CloudFormationService implements ResourceProvider {
|
||||
boolean createOnlyReferencesReplaced = false;
|
||||
JsonNode props = newDef.path("Properties");
|
||||
if (props != null && props.isObject()) {
|
||||
for (Iterator<String> it = props.fieldNames(); it.hasNext(); ) {
|
||||
String field = it.next();
|
||||
if (CfnCreateOnlyProperties.isCreateOnly(resourceType, field)) {
|
||||
if (CfnCreateOnlyProperties.hasSchema(resourceType)) {
|
||||
for (JsonNode subtree : CfnCreateOnlyProperties.createOnlySubtrees(resourceType, props)) {
|
||||
Set<String> fieldDeps = new LinkedHashSet<>();
|
||||
collectDependencies(props.get(field), allResourceIds, fieldDeps, newConditions);
|
||||
collectDependencies(subtree, allResourceIds, fieldDeps, newConditions);
|
||||
if (fieldDeps.stream().anyMatch(replacedResourceIds::contains)) {
|
||||
createOnlyReferencesReplaced = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (Iterator<String> it = props.fieldNames(); it.hasNext(); ) {
|
||||
String field = it.next();
|
||||
if (CfnCreateOnlyProperties.isCreateOnly(resourceType, field)) {
|
||||
Set<String> fieldDeps = new LinkedHashSet<>();
|
||||
collectDependencies(props.get(field), allResourceIds, fieldDeps, newConditions);
|
||||
if (fieldDeps.stream().anyMatch(replacedResourceIds::contains)) {
|
||||
createOnlyReferencesReplaced = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1303,6 +1314,18 @@ public class CloudFormationService implements ResourceProvider {
|
||||
if (oldProps == null || newProps == null || oldProps.isMissingNode() || newProps.isMissingNode()) {
|
||||
return false;
|
||||
}
|
||||
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)) {
|
||||
return true;
|
||||
}
|
||||
for (JsonNode subtree : CfnCreateOnlyProperties.createOnlySubtrees(resourceType, newProps)) {
|
||||
if (referencesAnyParameter(subtree, changedParams) || referencesAnyCondition(subtree, changedConditions)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
for (Iterator<String> it = newProps.fieldNames(); it.hasNext(); ) {
|
||||
String field = it.next();
|
||||
if (CfnCreateOnlyProperties.isCreateOnly(resourceType, field)) {
|
||||
|
||||
Reference in new issue
Block a user