Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper/schema: Additional validation for schema attribute references [v1] #416

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,28 +770,28 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro
}

if len(v.ConflictsWith) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.ConflictsWith, topSchemaMap)
err := checkKeysAgainstSchemaFlags(k, v.ConflictsWith, topSchemaMap, v)
if err != nil {
return fmt.Errorf("ConflictsWith: %+v", err)
}
}

if len(v.RequiredWith) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.RequiredWith, topSchemaMap)
err := checkKeysAgainstSchemaFlags(k, v.RequiredWith, topSchemaMap, v)
if err != nil {
return fmt.Errorf("RequiredWith: %+v", err)
}
}

if len(v.ExactlyOneOf) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.ExactlyOneOf, topSchemaMap)
err := checkKeysAgainstSchemaFlags(k, v.ExactlyOneOf, topSchemaMap, v)
if err != nil {
return fmt.Errorf("ExactlyOneOf: %+v", err)
}
}

if len(v.AtLeastOneOf) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.AtLeastOneOf, topSchemaMap)
err := checkKeysAgainstSchemaFlags(k, v.AtLeastOneOf, topSchemaMap, v)
if err != nil {
return fmt.Errorf("AtLeastOneOf: %+v", err)
}
Expand Down Expand Up @@ -860,14 +860,20 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro
return nil
}

func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap) error {
func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap, self *Schema) error {
for _, key := range keys {
parts := strings.Split(key, ".")
sm := topSchemaMap
var target *Schema
for _, part := range parts {
// Skip index fields
if _, err := strconv.Atoi(part); err == nil {
// Skip index fields if 0
partInt, err := strconv.Atoi(part)

if err == nil {
if partInt != 0 {
return fmt.Errorf("%s configuration block reference (%s) can only use the .0. index for TypeList and MaxItems: 1 configuration blocks", k, key)
}

continue
}

Expand All @@ -876,13 +882,27 @@ func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap
return fmt.Errorf("%s references unknown attribute (%s) at part (%s)", k, key, part)
}

if subResource, ok := target.Elem.(*Resource); ok {
sm = schemaMap(subResource.Schema)
subResource, ok := target.Elem.(*Resource)

if !ok {
continue
}

if target.Type == TypeSet || target.MaxItems != 1 {
return fmt.Errorf("%s configuration block reference (%s) can only be used with TypeList and MaxItems: 1 configuration blocks", k, key)
}

sm = schemaMap(subResource.Schema)
}

if target == nil {
return fmt.Errorf("%s cannot find target attribute (%s), sm: %#v", k, key, sm)
}

if target == self {
return fmt.Errorf("%s cannot reference self (%s)", k, key)
}

if target.Required {
return fmt.Errorf("%s cannot contain Required attribute (%s)", k, key)
}
Expand Down
96 changes: 60 additions & 36 deletions helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3597,12 +3597,12 @@ func TestSchemaMap_InternalValidate(t *testing.T) {

"ConflictsWith list index syntax with self reference": {
map[string]*Schema{
"config_block_attr": &Schema{
"config_block_attr": {
Type: TypeList,
Optional: true,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": &Schema{
"nested_attr": {
Type: TypeString,
Optional: true,
ConflictsWith: []string{"config_block_attr.0.nested_attr"},
Expand All @@ -3611,24 +3611,25 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
},
},
},
false, // TODO: This should return an error as it will always error during runtime
true,
},

"ConflictsWith list index syntax with list configuration block existing attribute": {
map[string]*Schema{
"config_block_attr": &Schema{
"config_block_attr": {
Type: TypeList,
Optional: true,
MaxItems: 1,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": &Schema{
"nested_attr": {
Type: TypeString,
Optional: true,
},
},
},
},
"test": &Schema{
"test": {
Type: TypeBool,
Optional: true,
ConflictsWith: []string{"config_block_attr.0.nested_attr"},
Expand All @@ -3639,19 +3640,42 @@ func TestSchemaMap_InternalValidate(t *testing.T) {

"ConflictsWith list index syntax with list configuration block missing attribute": {
map[string]*Schema{
"config_block_attr": &Schema{
"config_block_attr": {
Type: TypeList,
Optional: true,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": &Schema{
"nested_attr": {
Type: TypeString,
Optional: true,
},
},
},
},
"test": &Schema{
"test": {
Type: TypeBool,
Optional: true,
ConflictsWith: []string{"config_block_attr.0.missing_attr"},
},
},
true,
},

"ConflictsWith list index syntax with list configuration block missing MaxItems": {
map[string]*Schema{
"config_block_attr": {
Type: TypeList,
Optional: true,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": {
Type: TypeString,
Optional: true,
},
},
},
},
"test": {
Type: TypeBool,
Optional: true,
ConflictsWith: []string{"config_block_attr.0.missing_attr"},
Expand All @@ -3662,42 +3686,42 @@ func TestSchemaMap_InternalValidate(t *testing.T) {

"ConflictsWith list index syntax with set configuration block existing attribute": {
map[string]*Schema{
"config_block_attr": &Schema{
"config_block_attr": {
Type: TypeSet,
Optional: true,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": &Schema{
"nested_attr": {
Type: TypeString,
Optional: true,
},
},
},
},
"test": &Schema{
"test": {
Type: TypeBool,
Optional: true,
ConflictsWith: []string{"config_block_attr.0.nested_attr"},
},
},
false, // TODO: This should return an error as sets cannot be indexed
true,
},

"ConflictsWith list index syntax with set configuration block missing attribute": {
map[string]*Schema{
"config_block_attr": &Schema{
"config_block_attr": {
Type: TypeSet,
Optional: true,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": &Schema{
"nested_attr": {
Type: TypeString,
Optional: true,
},
},
},
},
"test": &Schema{
"test": {
Type: TypeBool,
Optional: true,
ConflictsWith: []string{"config_block_attr.0.missing_attr"},
Expand All @@ -3708,35 +3732,35 @@ func TestSchemaMap_InternalValidate(t *testing.T) {

"ConflictsWith map key syntax with list configuration block existing attribute": {
map[string]*Schema{
"config_block_attr": &Schema{
"config_block_attr": {
Type: TypeList,
Optional: true,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": &Schema{
"nested_attr": {
Type: TypeString,
Optional: true,
},
},
},
},
"test": &Schema{
"test": {
Type: TypeBool,
Optional: true,
ConflictsWith: []string{"config_block_attr.nested_attr"},
},
},
false, // TODO: This should return an error as validateConflictingAttributes will never error
true,
},

"ConflictsWith map key syntax with list configuration block self reference": {
map[string]*Schema{
"config_block_attr": &Schema{
"config_block_attr": {
Type: TypeList,
Optional: true,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": &Schema{
"nested_attr": {
Type: TypeString,
Optional: true,
ConflictsWith: []string{"config_block_attr.nested_attr"},
Expand All @@ -3745,40 +3769,40 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
},
},
},
false, // TODO: This should return an error as validateConflictingAttributes will never error
true,
},

"ConflictsWith map key syntax with set configuration block existing attribute": {
map[string]*Schema{
"config_block_attr": &Schema{
"config_block_attr": {
Type: TypeSet,
Optional: true,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": &Schema{
"nested_attr": {
Type: TypeString,
Optional: true,
},
},
},
},
"test": &Schema{
"test": {
Type: TypeBool,
Optional: true,
ConflictsWith: []string{"config_block_attr.nested_attr"},
},
},
false, // TODO: This should return an error as validateConflictingAttributes will never error and sets cannot be indexed
true,
},

"ConflictsWith map key syntax with set configuration block self reference": {
map[string]*Schema{
"config_block_attr": &Schema{
"config_block_attr": {
Type: TypeSet,
Optional: true,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": &Schema{
"nested_attr": {
Type: TypeString,
Optional: true,
ConflictsWith: []string{"config_block_attr.nested_attr"},
Expand All @@ -3787,17 +3811,17 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
},
},
},
false, // TODO: This should return an error as validateConflictingAttributes will never error and sets cannot be indexed
true,
},

"ConflictsWith map key syntax with map attribute": {
map[string]*Schema{
"map_attr": &Schema{
"map_attr": {
Type: TypeMap,
Optional: true,
Elem: &Schema{Type: TypeString},
},
"test": &Schema{
"test": {
Type: TypeBool,
Optional: true,
ConflictsWith: []string{"map_attr.some_key"},
Expand All @@ -3808,12 +3832,12 @@ func TestSchemaMap_InternalValidate(t *testing.T) {

"ConflictsWith string syntax with map attribute": {
map[string]*Schema{
"map_attr": &Schema{
"map_attr": {
Type: TypeMap,
Optional: true,
Elem: &Schema{Type: TypeString},
},
"test": &Schema{
"test": {
Type: TypeBool,
Optional: true,
ConflictsWith: []string{"map_attr"},
Expand All @@ -3824,13 +3848,13 @@ func TestSchemaMap_InternalValidate(t *testing.T) {

"ConflictsWith string syntax with self reference": {
map[string]*Schema{
"test": &Schema{
"test": {
Type: TypeBool,
Optional: true,
ConflictsWith: []string{"test"},
},
},
false, // TODO: This should return an error as it will always error during runtime
true,
},

"Sub-resource invalid": {
Expand Down