Skip to content

Commit

Permalink
add support and coverate for immutable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
dikhan committed Oct 5, 2019
1 parent 388269e commit 5fb697b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
15 changes: 12 additions & 3 deletions openapi/resource_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,16 +382,16 @@ func (r resourceFactory) validateImmutableProperties(updatedResourceLocalData *s
if p.ReadOnly || p.IsParentProperty {
continue
}
err := r.validateImmutableProperty(p, remoteData[p.Name], localData[p.Name])
err := r.validateImmutableProperty(p, remoteData[p.Name], localData[p.Name], false)
if err != nil {
return err
}
}
return nil
}

func (r resourceFactory) validateImmutableProperty(property *specSchemaDefinitionProperty, remoteData interface{}, localData interface{}) error {
if property.Immutable {
func (r resourceFactory) validateImmutableProperty(property *specSchemaDefinitionProperty, remoteData interface{}, localData interface{}, isImmutableObjectProperty bool) error {
if property.Immutable || isImmutableObjectProperty { // isImmutableObjectProperty covers the recursive call from objects that are immutable which also make all its properties immutable
switch property.Type {
case typeList:
if isListOfPrimitives, _ := property.isTerraformListOfSimpleValues(); isListOfPrimitives {
Expand All @@ -403,6 +403,15 @@ func (r resourceFactory) validateImmutableProperty(property *specSchemaDefinitio
}
}
}
case typeObject:
localObject := localData.(map[string]interface{})
remoteObject := remoteData.(map[string]interface{})
for _, objProp := range property.SpecSchemaDefinition.Properties {
err := r.validateImmutableProperty(objProp, remoteObject[objProp.Name], localObject[objProp.Name], true)
if err != nil {
return fmt.Errorf("immutable object property '%s' value updated: [input: %s; remote: %s]", property.Name, localData, remoteData)
}
}
default:
if localData != remoteData {
return fmt.Errorf("immutable property '%s' value updated: [input: %s; remote: %s]", property.Name, localData, remoteData)
Expand Down
29 changes: 28 additions & 1 deletion openapi/resource_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,33 @@ func TestCheckImmutableFields(t *testing.T) {
},
expectedError: errors.New("validation for immutable properties failed: immutable list property 'immutable_prop' elements updated: [input: [value1Updated value2Updated]; remote: [value1 value2]]. Update operation was aborted; no updates were performed"),
},
{
name: "immutable object property is updated",
inputProps: []*specSchemaDefinitionProperty{
{
Name: "immutable_prop",
Type: typeObject,
Immutable: true,
SpecSchemaDefinition: &specSchemaDefinition{
Properties: specSchemaDefinitionProperties{
newIntSchemaDefinitionPropertyWithDefaults("origin_port", "", true, false, 80),
newStringSchemaDefinitionPropertyWithDefaults("protocol", "", true, false, "http"),
},
},
Default: map[string]interface{}{
"origin_port": 80,
"protocol": "http",
},
},
},
client: clientOpenAPIStub{
responsePayload: getMapFromJson(t, `{"immutable_prop": {"origin_port":443,"protocol":"https"}}`),
},
assertions: func(resourceData *schema.ResourceData) {
assert.Equal(t, map[string]interface{}{"origin_port": "443", "protocol": "https"}, resourceData.Get("immutable_prop"))
},
expectedError: errors.New("validation for immutable properties failed: immutable object property 'immutable_prop' value updated: [input: map[origin_port:%!s(int64=80) protocol:http]; remote: map[origin_port:%!s(float64=443) protocol:https]]. Update operation was aborted; no updates were performed"),
},
}

for _, tc := range testCases {
Expand All @@ -1139,7 +1166,7 @@ func TestCheckImmutableFields(t *testing.T) {
func getMapFromJson(t *testing.T, input string) map[string]interface{} {
var m map[string]interface{}
err := json.Unmarshal([]byte(input), &m)
assert.NoError(t, err)
assert.Nil(t, err)
return m
}

Expand Down

0 comments on commit 5fb697b

Please sign in to comment.