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: allow ConflictsWith and Computed Optional fields #9825

Merged
merged 1 commit into from
Nov 4, 2016
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
7 changes: 5 additions & 2 deletions helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ type Schema struct {
// NOTE: This currently does not work.
ComputedWhen []string

// ConflictsWith is a set of schema keys that conflict with this schema
// ConflictsWith is a set of schema keys that conflict with this schema.
// This will only check that they're set in the _config_. This will not
// raise an error for a malfunctioning resource that sets a conflicting
// key.
ConflictsWith []string

// When Deprecated is set, this attribute is deprecated.
Expand Down Expand Up @@ -571,7 +574,7 @@ func (m schemaMap) InternalValidate(topSchemaMap schemaMap) error {
return fmt.Errorf("%s: ConflictsWith cannot contain Required attribute (%s)", k, key)
}

if target.Computed || len(target.ComputedWhen) > 0 {
if len(target.ComputedWhen) > 0 {
return fmt.Errorf("%s: ConflictsWith cannot contain Computed(When) attribute (%s)", k, key)
}
}
Expand Down
89 changes: 73 additions & 16 deletions helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2885,21 +2885,6 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
true,
},

"ConflictsWith cannot be used w/ Computed": {
map[string]*Schema{
"blacklist": &Schema{
Type: TypeBool,
Computed: true,
},
"whitelist": &Schema{
Type: TypeBool,
Optional: true,
ConflictsWith: []string{"blacklist"},
},
},
true,
},

"ConflictsWith cannot be used w/ ComputedWhen": {
map[string]*Schema{
"blacklist": &Schema{
Expand Down Expand Up @@ -2963,7 +2948,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
}

for tn, tc := range cases {
err := schemaMap(tc.In).InternalValidate(schemaMap{})
err := schemaMap(tc.In).InternalValidate(nil)
if err != nil != tc.Err {
if tc.Err {
t.Fatalf("%q: Expected error did not occur:\n\n%#v", tn, tc.In)
Expand Down Expand Up @@ -3758,6 +3743,78 @@ func TestSchemaMap_Validate(t *testing.T) {
},
},

"Computed + Optional fields conflicting with each other": {
Schema: map[string]*Schema{
"foo_att": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"bar_att"},
},
"bar_att": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"foo_att"},
},
},

Config: map[string]interface{}{
"foo_att": "foo-val",
"bar_att": "bar-val",
},

Err: true,
Errors: []error{
fmt.Errorf(`"foo_att": conflicts with bar_att ("bar-val")`),
fmt.Errorf(`"bar_att": conflicts with foo_att ("foo-val")`),
},
},

"Computed + Optional fields NOT conflicting with each other": {
Schema: map[string]*Schema{
"foo_att": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"bar_att"},
},
"bar_att": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"foo_att"},
},
},

Config: map[string]interface{}{
"foo_att": "foo-val",
},

Err: false,
},

"Computed + Optional fields that conflict with none set": {
Schema: map[string]*Schema{
"foo_att": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"bar_att"},
},
"bar_att": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"foo_att"},
},
},

Config: map[string]interface{}{},

Err: false,
},

"Good with ValidateFunc": {
Schema: map[string]*Schema{
"validate_me": &Schema{
Expand Down