Skip to content

Commit e193bdb

Browse files
committed
Renamed AllOf to RequiredWith and made RequiredWith usable with other property constraints; updated tests accordingly
1 parent 61abb29 commit e193bdb

File tree

2 files changed

+98
-102
lines changed

2 files changed

+98
-102
lines changed

helper/schema/schema.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,11 @@ type Schema struct {
224224
// AtLeastOneOf is a set of schema keys that, when set, at least one of
225225
// the keys in that list must be specified.
226226
//
227-
// AllOf is a set of schema keys that must be set simultaneously. This
228-
// setting conflicts with ExactlyOneOf or AtLeastOneOf
227+
// RequiredWith is a set of schema keys that must be set simultaneously.
229228
ConflictsWith []string
230229
ExactlyOneOf []string
231230
AtLeastOneOf []string
232-
AllOf []string
231+
RequiredWith []string
233232

234233
// When Deprecated is set, this attribute is deprecated.
235234
//
@@ -777,13 +776,10 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro
777776
}
778777
}
779778

780-
if len(v.AllOf) > 0 {
781-
if len(v.ExactlyOneOf) > 0 || len(v.AtLeastOneOf) > 0 {
782-
return fmt.Errorf("AllOf cannot be used simultaneously with ExactlyOneOf or AtLeastOneOf")
783-
}
784-
err := checkKeysAgainstSchemaFlags(k, v.AllOf, topSchemaMap)
779+
if len(v.RequiredWith) > 0 {
780+
err := checkKeysAgainstSchemaFlags(k, v.RequiredWith, topSchemaMap)
785781
if err != nil {
786-
return fmt.Errorf("AllOf: %+v", err)
782+
return fmt.Errorf("RequiredWith: %+v", err)
787783
}
788784
}
789785

@@ -1412,7 +1408,13 @@ func (m schemaMap) validate(
14121408
return nil, nil
14131409
}
14141410

1415-
err := validateAllOfAttribute(k, schema, c)
1411+
if !schema.Required && !schema.Optional {
1412+
// This is a computed-only field
1413+
return nil, []error{fmt.Errorf(
1414+
"%q: this field cannot be set", k)}
1415+
}
1416+
1417+
err := validateRequiredWithAttribute(k, schema, c)
14161418
if err != nil {
14171419
return nil, []error{err}
14181420
}
@@ -1427,12 +1429,6 @@ func (m schemaMap) validate(
14271429
return nil, []error{err}
14281430
}
14291431

1430-
if !schema.Required && !schema.Optional {
1431-
// This is a computed-only field
1432-
return nil, []error{fmt.Errorf(
1433-
"%q: this field cannot be set", k)}
1434-
}
1435-
14361432
// If the value is unknown then we can't validate it yet.
14371433
// In particular, this avoids spurious type errors where downstream
14381434
// validation code sees UnknownVariableValue as being just a string.
@@ -1513,20 +1509,20 @@ func removeDuplicates(elements []string) []string {
15131509
return result
15141510
}
15151511

1516-
func validateAllOfAttribute(
1512+
func validateRequiredWithAttribute(
15171513
k string,
15181514
schema *Schema,
15191515
c *terraform.ResourceConfig) error {
15201516

1521-
if len(schema.AllOf) == 0 {
1517+
if len(schema.RequiredWith) == 0 {
15221518
return nil
15231519
}
15241520

1525-
allKeys := removeDuplicates(append(schema.AllOf, k))
1521+
allKeys := removeDuplicates(append(schema.RequiredWith, k))
15261522
sort.Strings(allKeys)
15271523

1528-
for _, allOfKey := range allKeys {
1529-
if _, ok := c.Get(allOfKey); !ok {
1524+
for _, key := range allKeys {
1525+
if _, ok := c.Get(key); !ok {
15301526
return fmt.Errorf("%q: all of `%s` must be specified", k, strings.Join(allKeys, ","))
15311527
}
15321528
}

helper/schema/schema_test.go

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -6636,7 +6636,7 @@ func TestValidateAtLeastOneOfAttributes(t *testing.T) {
66366636
}
66376637
}
66386638

6639-
func TestValidateAllOfAttributes(t *testing.T) {
6639+
func TestValidateRequiredWithAttributes(t *testing.T) {
66406640
cases := map[string]struct {
66416641
Key string
66426642
Schema map[string]*Schema
@@ -6648,14 +6648,14 @@ func TestValidateAllOfAttributes(t *testing.T) {
66486648
Key: "whitelist",
66496649
Schema: map[string]*Schema{
66506650
"whitelist": {
6651-
Type: TypeBool,
6652-
Optional: true,
6653-
AllOf: []string{"blacklist"},
6651+
Type: TypeBool,
6652+
Optional: true,
6653+
RequiredWith: []string{"blacklist"},
66546654
},
66556655
"blacklist": {
6656-
Type: TypeBool,
6657-
Optional: true,
6658-
AllOf: []string{"whitelist"},
6656+
Type: TypeBool,
6657+
Optional: true,
6658+
RequiredWith: []string{"whitelist"},
66596659
},
66606660
},
66616661

@@ -6670,14 +6670,14 @@ func TestValidateAllOfAttributes(t *testing.T) {
66706670
Key: "whitelist",
66716671
Schema: map[string]*Schema{
66726672
"whitelist": {
6673-
Type: TypeBool,
6674-
Optional: true,
6675-
AllOf: []string{"blacklist"},
6673+
Type: TypeBool,
6674+
Optional: true,
6675+
RequiredWith: []string{"blacklist"},
66766676
},
66776677
"blacklist": {
6678-
Type: TypeBool,
6679-
Optional: true,
6680-
AllOf: []string{"whitelist"},
6678+
Type: TypeBool,
6679+
Optional: true,
6680+
RequiredWith: []string{"whitelist"},
66816681
},
66826682
},
66836683

@@ -6691,14 +6691,14 @@ func TestValidateAllOfAttributes(t *testing.T) {
66916691
Key: "whitelist",
66926692
Schema: map[string]*Schema{
66936693
"whitelist": {
6694-
Type: TypeBool,
6695-
Optional: true,
6696-
AllOf: []string{"blacklist"},
6694+
Type: TypeBool,
6695+
Optional: true,
6696+
RequiredWith: []string{"blacklist"},
66976697
},
66986698
"blacklist": {
6699-
Type: TypeBool,
6700-
Optional: true,
6701-
AllOf: []string{"whitelist"},
6699+
Type: TypeBool,
6700+
Optional: true,
6701+
RequiredWith: []string{"whitelist"},
67026702
},
67036703
},
67046704

@@ -6710,19 +6710,19 @@ func TestValidateAllOfAttributes(t *testing.T) {
67106710
Key: "whitelist",
67116711
Schema: map[string]*Schema{
67126712
"whitelist": {
6713-
Type: TypeBool,
6714-
Optional: true,
6715-
AllOf: []string{"purplelist"},
6713+
Type: TypeBool,
6714+
Optional: true,
6715+
RequiredWith: []string{"purplelist"},
67166716
},
67176717
"blacklist": {
6718-
Type: TypeBool,
6719-
Optional: true,
6720-
AllOf: []string{"whitelist", "purplelist"},
6718+
Type: TypeBool,
6719+
Optional: true,
6720+
RequiredWith: []string{"whitelist", "purplelist"},
67216721
},
67226722
"purplelist": {
6723-
Type: TypeBool,
6724-
Optional: true,
6725-
AllOf: []string{"whitelist"},
6723+
Type: TypeBool,
6724+
Optional: true,
6725+
RequiredWith: []string{"whitelist"},
67266726
},
67276727
},
67286728

@@ -6737,19 +6737,19 @@ func TestValidateAllOfAttributes(t *testing.T) {
67376737
Key: "whitelist",
67386738
Schema: map[string]*Schema{
67396739
"whitelist": {
6740-
Type: TypeBool,
6741-
Optional: true,
6742-
AllOf: []string{"blacklist", "purplelist"},
6740+
Type: TypeBool,
6741+
Optional: true,
6742+
RequiredWith: []string{"blacklist", "purplelist"},
67436743
},
67446744
"blacklist": {
6745-
Type: TypeBool,
6746-
Optional: true,
6747-
AllOf: []string{"whitelist", "purplelist"},
6745+
Type: TypeBool,
6746+
Optional: true,
6747+
RequiredWith: []string{"whitelist", "purplelist"},
67486748
},
67496749
"purplelist": {
6750-
Type: TypeBool,
6751-
Optional: true,
6752-
AllOf: []string{"whitelist", "blacklist"},
6750+
Type: TypeBool,
6751+
Optional: true,
6752+
RequiredWith: []string{"whitelist", "blacklist"},
67536753
},
67546754
},
67556755

@@ -6765,19 +6765,19 @@ func TestValidateAllOfAttributes(t *testing.T) {
67656765
Key: "whitelist",
67666766
Schema: map[string]*Schema{
67676767
"whitelist": {
6768-
Type: TypeBool,
6769-
Optional: true,
6770-
AllOf: []string{"blacklist", "purplelist"},
6768+
Type: TypeBool,
6769+
Optional: true,
6770+
RequiredWith: []string{"blacklist", "purplelist"},
67716771
},
67726772
"blacklist": {
6773-
Type: TypeBool,
6774-
Optional: true,
6775-
AllOf: []string{"whitelist", "purplelist"},
6773+
Type: TypeBool,
6774+
Optional: true,
6775+
RequiredWith: []string{"whitelist", "purplelist"},
67766776
},
67776777
"purplelist": {
6778-
Type: TypeBool,
6779-
Optional: true,
6780-
AllOf: []string{"whitelist", "blacklist"},
6778+
Type: TypeBool,
6779+
Optional: true,
6780+
RequiredWith: []string{"whitelist", "blacklist"},
67816781
},
67826782
},
67836783

@@ -6791,19 +6791,19 @@ func TestValidateAllOfAttributes(t *testing.T) {
67916791
Key: "whitelist",
67926792
Schema: map[string]*Schema{
67936793
"whitelist": {
6794-
Type: TypeBool,
6795-
Optional: true,
6796-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6794+
Type: TypeBool,
6795+
Optional: true,
6796+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
67976797
},
67986798
"blacklist": {
6799-
Type: TypeBool,
6800-
Optional: true,
6801-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6799+
Type: TypeBool,
6800+
Optional: true,
6801+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
68026802
},
68036803
"purplelist": {
6804-
Type: TypeBool,
6805-
Optional: true,
6806-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6804+
Type: TypeBool,
6805+
Optional: true,
6806+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
68076807
},
68086808
},
68096809

@@ -6814,19 +6814,19 @@ func TestValidateAllOfAttributes(t *testing.T) {
68146814
"Only Unknown Variable Value": {
68156815
Schema: map[string]*Schema{
68166816
"whitelist": {
6817-
Type: TypeBool,
6818-
Optional: true,
6819-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6817+
Type: TypeBool,
6818+
Optional: true,
6819+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
68206820
},
68216821
"blacklist": {
6822-
Type: TypeBool,
6823-
Optional: true,
6824-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6822+
Type: TypeBool,
6823+
Optional: true,
6824+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
68256825
},
68266826
"purplelist": {
6827-
Type: TypeBool,
6828-
Optional: true,
6829-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6827+
Type: TypeBool,
6828+
Optional: true,
6829+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
68306830
},
68316831
},
68326832

@@ -6840,16 +6840,16 @@ func TestValidateAllOfAttributes(t *testing.T) {
68406840
"only unknown list value": {
68416841
Schema: map[string]*Schema{
68426842
"whitelist": {
6843-
Type: TypeList,
6844-
Optional: true,
6845-
Elem: &Schema{Type: TypeString},
6846-
AllOf: []string{"whitelist", "blacklist"},
6843+
Type: TypeList,
6844+
Optional: true,
6845+
Elem: &Schema{Type: TypeString},
6846+
RequiredWith: []string{"whitelist", "blacklist"},
68476847
},
68486848
"blacklist": {
6849-
Type: TypeList,
6850-
Optional: true,
6851-
Elem: &Schema{Type: TypeString},
6852-
AllOf: []string{"whitelist", "blacklist"},
6849+
Type: TypeList,
6850+
Optional: true,
6851+
Elem: &Schema{Type: TypeString},
6852+
RequiredWith: []string{"whitelist", "blacklist"},
68536853
},
68546854
},
68556855

@@ -6863,19 +6863,19 @@ func TestValidateAllOfAttributes(t *testing.T) {
68636863
"Unknown Variable Value and Known Value": {
68646864
Schema: map[string]*Schema{
68656865
"whitelist": {
6866-
Type: TypeBool,
6867-
Optional: true,
6868-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6866+
Type: TypeBool,
6867+
Optional: true,
6868+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
68696869
},
68706870
"blacklist": {
6871-
Type: TypeBool,
6872-
Optional: true,
6873-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6871+
Type: TypeBool,
6872+
Optional: true,
6873+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
68746874
},
68756875
"purplelist": {
6876-
Type: TypeBool,
6877-
Optional: true,
6878-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6876+
Type: TypeBool,
6877+
Optional: true,
6878+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
68796879
},
68806880
},
68816881

0 commit comments

Comments
 (0)