From adb66a7ff779dce5eb8c061375726d0826f1add6 Mon Sep 17 00:00:00 2001 From: willdavsmith Date: Wed, 3 May 2023 11:06:52 -0700 Subject: [PATCH 1/2] Adding ConditionalCreateOnlyProperties to aws resource type schema --- pkg/aws/operations/operations.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pkg/aws/operations/operations.go b/pkg/aws/operations/operations.go index cf0a662727..f1edc03177 100644 --- a/pkg/aws/operations/operations.go +++ b/pkg/aws/operations/operations.go @@ -16,10 +16,11 @@ import ( ) type ResourceTypeSchema struct { - Properties map[string]any `json:"properties,omitempty"` - ReadOnlyProperties []string `json:"readOnlyProperties,omitempty"` - CreateOnlyProperties []string `json:"createOnlyProperties,omitempty"` - WriteOnlyProperties []string `json:"writeOnlyProperties,omitempty"` + Properties map[string]any `json:"properties,omitempty"` + ReadOnlyProperties []string `json:"readOnlyProperties,omitempty"` + CreateOnlyProperties []string `json:"createOnlyProperties,omitempty"` + ConditionalCreateOnlyProperties []string `json:"conditionalCreateOnlyProperties,omitempty"` + WriteOnlyProperties []string `json:"writeOnlyProperties,omitempty"` } // FlattenProperties flattens a state object. @@ -143,11 +144,12 @@ func GeneratePatch(currentState []byte, desiredState []byte, schema []byte) (jso if isWriteOnlyProperty && isCreateOnlyProperty { flattenedDesiredStateObject[k] = v } else if _, exists := flattenedDesiredStateObject[k]; !exists { - // Add the property (if not exists already) to the desired state if it is a read-only or create-only - // property. This ensures that these types of properties result in a no-op in the patch if they aren't - // updated in the desired state + // Add the property (if not exists already) to the desired state if it is a read-only, create-only, + // or conditional-create-only property. This ensures that these types of properties result in a + // no-op in the patch if they aren't updated in the desired state isReadOnlyProperty := slices.Contains(resourceTypeSchema.ReadOnlyProperties, property) - if isReadOnlyProperty || isCreateOnlyProperty { + isConditionalCreateOnlyProperty := slices.Contains(resourceTypeSchema.ConditionalCreateOnlyProperties, property) + if isReadOnlyProperty || isCreateOnlyProperty || isConditionalCreateOnlyProperty { flattenedDesiredStateObject[k] = v } } From a7c9eed2a84dd2d1385dcd61be7ee0dbdc6e93ec Mon Sep 17 00:00:00 2001 From: willdavsmith Date: Thu, 4 May 2023 09:52:41 -0700 Subject: [PATCH 2/2] Adding unit tests --- pkg/aws/operations/operations_test.go | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/pkg/aws/operations/operations_test.go b/pkg/aws/operations/operations_test.go index 5b56c789c6..389d07f657 100644 --- a/pkg/aws/operations/operations_test.go +++ b/pkg/aws/operations/operations_test.go @@ -265,6 +265,47 @@ func Test_GeneratePatch(t *testing.T) { }, nil, }, + { + "conditional-create-only-property noops if not updated", + map[string]any{ + "A": "B", + }, + map[string]any{}, + map[string]any{ + "properties": map[string]any{ + "A": map[string]any{}, + }, + "conditionalCreateOnlyProperties": []any{ + "/properties/A", + }, + }, + nil, + }, + { + "can update conditional-create-only-property", + map[string]any{ + "A": "B", + }, + map[string]any{ + "A": "C", + }, + map[string]any{ + "properties": map[string]any{ + "A": map[string]any{}, + }, + "conditionalCreateOnlyProperties": []any{ + "/properties/A", + }, + }, + jsondiff.Patch{ + { + Type: "replace", + Path: "/A", + OldValue: "B", + Value: "C", + }, + }, + }, } for _, testCase := range testCases {