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: InternalValidate fix for configuration block type checking [v1] #426

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
5 changes: 3 additions & 2 deletions helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap
parts := strings.Split(key, ".")
sm := topSchemaMap
var target *Schema
for _, part := range parts {
for idx, part := range parts {
// Skip index fields if 0
partInt, err := strconv.Atoi(part)

Expand All @@ -891,7 +891,8 @@ func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap
continue
}

if target.Type == TypeSet || target.MaxItems != 1 {
// Skip Type/MaxItems check if not the last element
if (target.Type == TypeSet || target.MaxItems != 1) && idx+1 != len(parts) {
return fmt.Errorf("%s configuration block reference (%s) can only be used with TypeList and MaxItems: 1 configuration blocks", k, key)
}

Expand Down
312 changes: 312 additions & 0 deletions helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3831,6 +3831,45 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
true,
},

"AtLeastOneOf string syntax with list attribute": {
map[string]*Schema{
"list_attr": {
Type: TypeList,
Optional: true,
Elem: &Schema{Type: TypeString},
},
"test": {
Type: TypeBool,
Optional: true,
AtLeastOneOf: []string{"list_attr"},
},
},
false,
},

"AtLeastOneOf string syntax with list configuration block": {
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,
AtLeastOneOf: []string{"config_block_attr"},
},
},
false,
},

"AtLeastOneOf string syntax with map attribute": {
map[string]*Schema{
"map_attr": {
Expand All @@ -3847,6 +3886,45 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
false,
},

"AtLeastOneOf string syntax with set attribute": {
map[string]*Schema{
"set_attr": {
Type: TypeSet,
Optional: true,
Elem: &Schema{Type: TypeString},
},
"test": {
Type: TypeBool,
Optional: true,
AtLeastOneOf: []string{"set_attr"},
},
},
false,
},

"AtLeastOneOf string syntax with set configuration block": {
map[string]*Schema{
"config_block_attr": {
Type: TypeSet,
Optional: true,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": {
Type: TypeString,
Optional: true,
},
},
},
},
"test": {
Type: TypeBool,
Optional: true,
AtLeastOneOf: []string{"config_block_attr"},
},
},
false,
},

"AtLeastOneOf string syntax with self reference": {
map[string]*Schema{
"test": {
Expand Down Expand Up @@ -4093,6 +4171,45 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
true,
},

"ConflictsWith string syntax with list attribute": {
map[string]*Schema{
"list_attr": {
Type: TypeList,
Optional: true,
Elem: &Schema{Type: TypeString},
},
"test": {
Type: TypeBool,
Optional: true,
ConflictsWith: []string{"list_attr"},
},
},
false,
},

"ConflictsWith string syntax with list configuration block": {
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"},
},
},
false,
},

"ConflictsWith string syntax with map attribute": {
map[string]*Schema{
"map_attr": {
Expand All @@ -4109,6 +4226,45 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
false,
},

"ConflictsWith string syntax with set attribute": {
map[string]*Schema{
"set_attr": {
Type: TypeSet,
Optional: true,
Elem: &Schema{Type: TypeString},
},
"test": {
Type: TypeBool,
Optional: true,
ConflictsWith: []string{"set_attr"},
},
},
false,
},

"ConflictsWith string syntax with set configuration block": {
map[string]*Schema{
"config_block_attr": {
Type: TypeSet,
Optional: true,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": {
Type: TypeString,
Optional: true,
},
},
},
},
"test": {
Type: TypeBool,
Optional: true,
ConflictsWith: []string{"config_block_attr"},
},
},
false,
},

"ConflictsWith string syntax with self reference": {
map[string]*Schema{
"test": {
Expand Down Expand Up @@ -4356,6 +4512,45 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
true,
},

"ExactlyOneOf string syntax with list attribute": {
map[string]*Schema{
"list_attr": {
Type: TypeList,
Optional: true,
Elem: &Schema{Type: TypeString},
},
"test": {
Type: TypeBool,
Optional: true,
ExactlyOneOf: []string{"list_attr"},
},
},
false,
},

"ExactlyOneOf string syntax with list configuration block": {
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,
ExactlyOneOf: []string{"config_block_attr"},
},
},
false,
},

"ExactlyOneOf string syntax with map attribute": {
map[string]*Schema{
"map_attr": {
Expand All @@ -4372,6 +4567,45 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
false,
},

"ExactlyOneOf string syntax with set attribute": {
map[string]*Schema{
"set_attr": {
Type: TypeSet,
Optional: true,
Elem: &Schema{Type: TypeString},
},
"test": {
Type: TypeBool,
Optional: true,
ExactlyOneOf: []string{"set_attr"},
},
},
false,
},

"ExactlyOneOf string syntax with set configuration block": {
map[string]*Schema{
"config_block_attr": {
Type: TypeSet,
Optional: true,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": {
Type: TypeString,
Optional: true,
},
},
},
},
"test": {
Type: TypeBool,
Optional: true,
ExactlyOneOf: []string{"config_block_attr"},
},
},
false,
},

"ExactlyOneOf string syntax with self reference": {
map[string]*Schema{
"test": {
Expand Down Expand Up @@ -4619,6 +4853,45 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
true,
},

"RequiredWith string syntax with list attribute": {
map[string]*Schema{
"list_attr": {
Type: TypeList,
Optional: true,
Elem: &Schema{Type: TypeString},
},
"test": {
Type: TypeBool,
Optional: true,
RequiredWith: []string{"list_attr"},
},
},
false,
},

"RequiredWith string syntax with list configuration block": {
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,
RequiredWith: []string{"config_block_attr"},
},
},
false,
},

"RequiredWith string syntax with map attribute": {
map[string]*Schema{
"map_attr": {
Expand All @@ -4635,6 +4908,45 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
false,
},

"RequiredWith string syntax with set attribute": {
map[string]*Schema{
"set_attr": {
Type: TypeSet,
Optional: true,
Elem: &Schema{Type: TypeString},
},
"test": {
Type: TypeBool,
Optional: true,
RequiredWith: []string{"set_attr"},
},
},
false,
},

"RequiredWith string syntax with set configuration block": {
map[string]*Schema{
"config_block_attr": {
Type: TypeSet,
Optional: true,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": {
Type: TypeString,
Optional: true,
},
},
},
},
"test": {
Type: TypeBool,
Optional: true,
RequiredWith: []string{"config_block_attr"},
},
},
false,
},

"RequiredWith string syntax with self reference": {
map[string]*Schema{
"test": {
Expand Down