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

add relative key link for ConflictsWith field #259

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 10 additions & 2 deletions helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1466,20 +1466,28 @@ func validateConflictingAttributes(
}

for _, conflictingKey := range schema.ConflictsWith {
if raw, ok := c.Get(conflictingKey); ok {
resolvedKey := resolveKey(k, conflictingKey)
if raw, ok := c.Get(resolvedKey); ok {
if raw == hcl2shim.UnknownVariableValue {
// An unknown value might become unset (null) once known, so
// we must defer validation until it's known.
continue
}
return fmt.Errorf(
"%q: conflicts with %s", k, conflictingKey)
"%q: conflicts with %s", k, resolvedKey)
}
}

return nil
}

func resolveKey(rootKey string, key string) string {
if strings.HasPrefix(key, "..") && strings.Contains(rootKey, ".") {
return rootKey[:strings.LastIndex(rootKey, ".")+1] + key[2:]
}
return key
}

func removeDuplicates(elements []string) []string {
encountered := make(map[string]struct{}, 0)
result := []string{}
Expand Down
77 changes: 77 additions & 0 deletions helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4907,6 +4907,83 @@ func TestSchemaMap_Validate(t *testing.T) {
},
},

"Conflicting subelements": {
Schema: map[string]*Schema{
"items": &Schema{
Type: TypeList,
Required: true,
Elem: &Resource{
Schema: map[string]*Schema{
"blacklist": &Schema{
Type: TypeString,
Optional: true,
ConflictsWith: []string{"..greenlist"},
},
"greenlist": &Schema{
Type: TypeString,
Optional: true,
ConflictsWith: []string{"..blacklist"},
},
},
},
},
},

Config: map[string]interface{}{
"items": []interface{}{
map[string]interface{}{
"blacklist": "black-val",
"greenlist": "green-val",
},
map[string]interface{}{
"blacklist": "black-val",
},
},
},

Err: true,
Errors: []error{
fmt.Errorf("\"items.0.blacklist\": conflicts with items.0.greenlist"),
fmt.Errorf("\"items.0.greenlist\": conflicts with items.0.blacklist"),
},
},

"Non-conflicting subelements": {
Schema: map[string]*Schema{
"items": &Schema{
Type: TypeList,
Required: true,
Elem: &Resource{
Schema: map[string]*Schema{
"blacklist": &Schema{
Type: TypeString,
Optional: true,
ConflictsWith: []string{"..greenlist"},
},
"greenlist": &Schema{
Type: TypeString,
Optional: true,
ConflictsWith: []string{"..blacklist"},
},
},
},
},
},

Config: map[string]interface{}{
"items": []interface{}{
map[string]interface{}{
"greenlist": "green-val",
},
map[string]interface{}{
"blacklist": "black-val",
},
},
},

Err: false,
},

"Required attribute & undefined conflicting optional are good": {
Schema: map[string]*Schema{
"required_att": &Schema{
Expand Down