Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#764 from skarimo/sherz/nested-…
Browse files Browse the repository at this point in the history
…computed-fields

Properly ignore nested attributes
  • Loading branch information
sergeylanzman committed Jan 30, 2021
2 parents 78c3e21 + 74c0568 commit 3c5c607
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
10 changes: 7 additions & 3 deletions terraformutils/providerwrapper/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ func (p *ProviderWrapper) GetReadOnlyAttributes(resourceTypes []string) (map[str
func (p *ProviderWrapper) readObjBlocks(block map[string]*configschema.NestedBlock, readOnlyAttributes []string, parent string) []string {
for k, v := range block {
if len(v.BlockTypes) > 0 {
readOnlyAttributes = p.readObjBlocks(v.BlockTypes, readOnlyAttributes, k)
if parent == "-1" {
readOnlyAttributes = p.readObjBlocks(v.BlockTypes, readOnlyAttributes, k)
} else {
readOnlyAttributes = p.readObjBlocks(v.BlockTypes, readOnlyAttributes, parent+".[0-9]+."+k)
}
}
fieldCount := 0
for key, l := range v.Attributes {
Expand All @@ -116,13 +120,13 @@ func (p *ProviderWrapper) readObjBlocks(block map[string]*configschema.NestedBlo
switch v.Nesting {
case configschema.NestingList:
if parent == "-1" {
readOnlyAttributes = append(readOnlyAttributes, "^"+k+".[0-9]."+key+"($|\\.[0-9]|\\.#)")
readOnlyAttributes = append(readOnlyAttributes, "^"+k+".[0-9]+."+key+"($|\\.[0-9]+|\\.#)")
} else {
readOnlyAttributes = append(readOnlyAttributes, "^"+parent+".(.*)."+key+"$")
}
case configschema.NestingSet:
if parent == "-1" {
readOnlyAttributes = append(readOnlyAttributes, "^"+k+".[0-9]."+key+"$")
readOnlyAttributes = append(readOnlyAttributes, "^"+k+".[0-9]+."+key+"$")
} else {
readOnlyAttributes = append(readOnlyAttributes, "^"+parent+".(.*)."+key+"($|\\.(.*))")
}
Expand Down
84 changes: 84 additions & 0 deletions terraformutils/providerwrapper/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package providerwrapper //nolint

import (
"regexp"
"testing"

"github.com/hashicorp/terraform/configs/configschema"
"github.com/zclconf/go-cty/cty"
)

func TestIgnoredAttributes(t *testing.T) {
attributes := map[string]*configschema.Attribute{
"computed_attribute": {
Type: cty.Number,
Computed: true,
},
"required_attribute": {
Type: cty.String,
Required: true,
},
}

testCases := map[string]struct {
block map[string]*configschema.NestedBlock
ignoredAttributes []string
notIgnoredAttributes []string
}{
"nesting_set": {map[string]*configschema.NestedBlock{
"attribute_one": {
Block: configschema.Block{
Attributes: attributes,
},
Nesting: configschema.NestingSet,
},
}, []string{"nesting_set.attribute_one.computed_attribute"},
[]string{"nesting_set.attribute_one.required_attribute"}},
"nesting_list": {map[string]*configschema.NestedBlock{
"attribute_one": {
Block: configschema.Block{
Attributes: map[string]*configschema.Attribute{},
BlockTypes: map[string]*configschema.NestedBlock{
"attribute_two_nested": {
Nesting: configschema.NestingList,
Block: configschema.Block{
Attributes: attributes,
},
},
},
},
Nesting: configschema.NestingList,
},
}, []string{"nesting_list.0.attribute_one.0.attribute_two_nested.computed_attribute"},
[]string{"nesting_list.0.attribute_one.0.attribute_two_nested.required_attribute"}},
}

for key, tc := range testCases {
t.Run(key, func(t *testing.T) {
provider := ProviderWrapper{}
readOnlyAttributes := provider.readObjBlocks(tc.block, []string{}, key)
for _, attr := range tc.ignoredAttributes {
if ignored := isAttributeIgnored(attr, readOnlyAttributes); !ignored {
t.Errorf("attribute \"%s\" was not ignored. Pattern list: %s", attr, readOnlyAttributes)
}
}

for _, attr := range tc.notIgnoredAttributes {
if ignored := isAttributeIgnored(attr, readOnlyAttributes); ignored {
t.Errorf("attribute \"%s\" was ignored. Pattern list: %s", attr, readOnlyAttributes)
}
}
})
}
}

func isAttributeIgnored(name string, patterns []string) bool {
ignored := false
for _, pattern := range patterns {
if match, _ := regexp.MatchString(pattern, name); match {
ignored = true
break
}
}
return ignored
}

0 comments on commit 3c5c607

Please sign in to comment.