From 65b17ccd0651ff47880ac19b75d34f050afd950c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 2 Nov 2016 22:23:11 -0700 Subject: [PATCH] helper/schema: allow ConflictsWith and Computed Optional fields --- helper/schema/schema.go | 7 ++- helper/schema/schema_test.go | 89 +++++++++++++++++++++++++++++------- 2 files changed, 78 insertions(+), 18 deletions(-) diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 18e0f50e7..b6a16fdbd 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -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. @@ -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) } } diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 1e2451a69..f42c92c5b 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -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{ @@ -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) @@ -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{