diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 138980c298..8e0ef4b7b3 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -770,28 +770,28 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro } if len(v.ConflictsWith) > 0 { - err := checkKeysAgainstSchemaFlags(k, v.ConflictsWith, topSchemaMap, v) + err := checkKeysAgainstSchemaFlags(k, v.ConflictsWith, topSchemaMap, v, false) if err != nil { return fmt.Errorf("ConflictsWith: %+v", err) } } if len(v.RequiredWith) > 0 { - err := checkKeysAgainstSchemaFlags(k, v.RequiredWith, topSchemaMap, v) + err := checkKeysAgainstSchemaFlags(k, v.RequiredWith, topSchemaMap, v, false) if err != nil { return fmt.Errorf("RequiredWith: %+v", err) } } if len(v.ExactlyOneOf) > 0 { - err := checkKeysAgainstSchemaFlags(k, v.ExactlyOneOf, topSchemaMap, v) + err := checkKeysAgainstSchemaFlags(k, v.ExactlyOneOf, topSchemaMap, v, true) if err != nil { return fmt.Errorf("ExactlyOneOf: %+v", err) } } if len(v.AtLeastOneOf) > 0 { - err := checkKeysAgainstSchemaFlags(k, v.AtLeastOneOf, topSchemaMap, v) + err := checkKeysAgainstSchemaFlags(k, v.AtLeastOneOf, topSchemaMap, v, true) if err != nil { return fmt.Errorf("AtLeastOneOf: %+v", err) } @@ -860,7 +860,9 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro return nil } -func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap, self *Schema) error { +func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap, self *Schema, requireSelfReference bool) error { + var foundSelfReference bool + for _, key := range keys { parts := strings.Split(key, ".") sm := topSchemaMap @@ -900,7 +902,11 @@ func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap } if target == self { - return fmt.Errorf("%s cannot reference self (%s)", k, key) + if !requireSelfReference { + return fmt.Errorf("%s cannot reference self (%s)", k, key) + } + + foundSelfReference = true } if target.Required { @@ -911,6 +917,11 @@ func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap return fmt.Errorf("%s cannot contain Computed(When) attribute (%s)", k, key) } } + + if requireSelfReference && !foundSelfReference { + return fmt.Errorf("%s must reference self for AtLeastOneOf/ExactlyOneOf", k) + } + return nil } diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index f1f467b556..81c0914313 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -3600,6 +3600,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) { "config_block_attr": { Type: TypeList, Optional: true, + MaxItems: 1, Elem: &Resource{ Schema: map[string]*Schema{ "nested_attr": { @@ -3611,7 +3612,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) { }, }, }, - true, // TODO: AtLeastOneOf self reference is necessary + false, }, "AtLeastOneOf list index syntax with list configuration block existing attribute": { @@ -3635,7 +3636,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) { AtLeastOneOf: []string{"config_block_attr.0.nested_attr"}, }, }, - false, // TODO: AtLeastOneOf self reference is necessary + true, }, "AtLeastOneOf list index syntax with list configuration block missing attribute": { @@ -3843,7 +3844,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) { AtLeastOneOf: []string{"map_attr"}, }, }, - false, // TODO: AtLeastOneOf self reference is necessary + true, }, "AtLeastOneOf string syntax with self reference": { @@ -3854,7 +3855,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) { AtLeastOneOf: []string{"test"}, }, }, - true, // TODO: AtLeastOneOf self reference is necessary + false, }, "ConflictsWith list index syntax with self reference": { @@ -4124,6 +4125,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) { "config_block_attr": { Type: TypeList, Optional: true, + MaxItems: 1, Elem: &Resource{ Schema: map[string]*Schema{ "nested_attr": { @@ -4135,7 +4137,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) { }, }, }, - true, // TODO: ExactlyOneOf self reference is necessary + false, }, "ExactlyOneOf list index syntax with list configuration block existing attribute": { @@ -4159,7 +4161,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) { ExactlyOneOf: []string{"config_block_attr.0.nested_attr"}, }, }, - false, // TODO: ExactlyOneOf self reference is necessary + true, }, "ExactlyOneOf list index syntax with list configuration block missing attribute": { @@ -4367,7 +4369,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) { ExactlyOneOf: []string{"map_attr"}, }, }, - false, // TODO: ExactlyOneOf self reference is necessary + true, }, "ExactlyOneOf string syntax with self reference": { @@ -4378,7 +4380,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) { AtLeastOneOf: []string{"test"}, }, }, - true, // TODO: ExactlyOneOf self reference is necessary + false, }, "Sub-resource invalid": {