Skip to content

Commit e7bc637

Browse files
tmeckelappilon
authored andcommitted
Renamed AllOf to RequiredWith and made RequiredWith usable with other property constraints; updated tests accordingly
1 parent d80ee95 commit e7bc637

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
@@ -193,12 +193,11 @@ type Schema struct {
193193
// AtLeastOneOf is a set of schema keys that, when set, at least one of
194194
// the keys in that list must be specified.
195195
//
196-
// AllOf is a set of schema keys that must be set simultaneously. This
197-
// setting conflicts with ExactlyOneOf or AtLeastOneOf
196+
// RequiredWith is a set of schema keys that must be set simultaneously.
198197
ConflictsWith []string
199198
ExactlyOneOf []string
200199
AtLeastOneOf []string
201-
AllOf []string
200+
RequiredWith []string
202201

203202
// When Deprecated is set, this attribute is deprecated.
204203
//
@@ -693,13 +692,10 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro
693692
}
694693
}
695694

696-
if len(v.AllOf) > 0 {
697-
if len(v.ExactlyOneOf) > 0 || len(v.AtLeastOneOf) > 0 {
698-
return fmt.Errorf("AllOf cannot be used simultaneously with ExactlyOneOf or AtLeastOneOf")
699-
}
700-
err := checkKeysAgainstSchemaFlags(k, v.AllOf, topSchemaMap)
695+
if len(v.RequiredWith) > 0 {
696+
err := checkKeysAgainstSchemaFlags(k, v.RequiredWith, topSchemaMap)
701697
if err != nil {
702-
return fmt.Errorf("AllOf: %+v", err)
698+
return fmt.Errorf("RequiredWith: %+v", err)
703699
}
704700
}
705701

@@ -1357,7 +1353,13 @@ func (m schemaMap) validate(
13571353
return nil, nil
13581354
}
13591355

1360-
err := validateAllOfAttribute(k, schema, c)
1356+
if !schema.Required && !schema.Optional {
1357+
// This is a computed-only field
1358+
return nil, []error{fmt.Errorf(
1359+
"%q: this field cannot be set", k)}
1360+
}
1361+
1362+
err := validateRequiredWithAttribute(k, schema, c)
13611363
if err != nil {
13621364
return nil, []error{err}
13631365
}
@@ -1372,12 +1374,6 @@ func (m schemaMap) validate(
13721374
return nil, []error{err}
13731375
}
13741376

1375-
if !schema.Required && !schema.Optional {
1376-
// This is a computed-only field
1377-
return nil, []error{fmt.Errorf(
1378-
"%q: this field cannot be set", k)}
1379-
}
1380-
13811377
// If the value is unknown then we can't validate it yet.
13821378
// In particular, this avoids spurious type errors where downstream
13831379
// validation code sees UnknownVariableValue as being just a string.
@@ -1458,20 +1454,20 @@ func removeDuplicates(elements []string) []string {
14581454
return result
14591455
}
14601456

1461-
func validateAllOfAttribute(
1457+
func validateRequiredWithAttribute(
14621458
k string,
14631459
schema *Schema,
14641460
c *terraform.ResourceConfig) error {
14651461

1466-
if len(schema.AllOf) == 0 {
1462+
if len(schema.RequiredWith) == 0 {
14671463
return nil
14681464
}
14691465

1470-
allKeys := removeDuplicates(append(schema.AllOf, k))
1466+
allKeys := removeDuplicates(append(schema.RequiredWith, k))
14711467
sort.Strings(allKeys)
14721468

1473-
for _, allOfKey := range allKeys {
1474-
if _, ok := c.Get(allOfKey); !ok {
1469+
for _, key := range allKeys {
1470+
if _, ok := c.Get(key); !ok {
14751471
return fmt.Errorf("%q: all of `%s` must be specified", k, strings.Join(allKeys, ","))
14761472
}
14771473
}

helper/schema/schema_test.go

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -6502,7 +6502,7 @@ func Test_panicOnErrUnparsableDefaultTrue(t *testing.T) {
65026502
os.Setenv(PanicOnErr, oldEnv)
65036503
}
65046504

6505-
func TestValidateAllOfAttributes(t *testing.T) {
6505+
func TestValidateRequiredWithAttributes(t *testing.T) {
65066506
cases := map[string]struct {
65076507
Key string
65086508
Schema map[string]*Schema
@@ -6514,14 +6514,14 @@ func TestValidateAllOfAttributes(t *testing.T) {
65146514
Key: "whitelist",
65156515
Schema: map[string]*Schema{
65166516
"whitelist": {
6517-
Type: TypeBool,
6518-
Optional: true,
6519-
AllOf: []string{"blacklist"},
6517+
Type: TypeBool,
6518+
Optional: true,
6519+
RequiredWith: []string{"blacklist"},
65206520
},
65216521
"blacklist": {
6522-
Type: TypeBool,
6523-
Optional: true,
6524-
AllOf: []string{"whitelist"},
6522+
Type: TypeBool,
6523+
Optional: true,
6524+
RequiredWith: []string{"whitelist"},
65256525
},
65266526
},
65276527

@@ -6536,14 +6536,14 @@ func TestValidateAllOfAttributes(t *testing.T) {
65366536
Key: "whitelist",
65376537
Schema: map[string]*Schema{
65386538
"whitelist": {
6539-
Type: TypeBool,
6540-
Optional: true,
6541-
AllOf: []string{"blacklist"},
6539+
Type: TypeBool,
6540+
Optional: true,
6541+
RequiredWith: []string{"blacklist"},
65426542
},
65436543
"blacklist": {
6544-
Type: TypeBool,
6545-
Optional: true,
6546-
AllOf: []string{"whitelist"},
6544+
Type: TypeBool,
6545+
Optional: true,
6546+
RequiredWith: []string{"whitelist"},
65476547
},
65486548
},
65496549

@@ -6557,14 +6557,14 @@ func TestValidateAllOfAttributes(t *testing.T) {
65576557
Key: "whitelist",
65586558
Schema: map[string]*Schema{
65596559
"whitelist": {
6560-
Type: TypeBool,
6561-
Optional: true,
6562-
AllOf: []string{"blacklist"},
6560+
Type: TypeBool,
6561+
Optional: true,
6562+
RequiredWith: []string{"blacklist"},
65636563
},
65646564
"blacklist": {
6565-
Type: TypeBool,
6566-
Optional: true,
6567-
AllOf: []string{"whitelist"},
6565+
Type: TypeBool,
6566+
Optional: true,
6567+
RequiredWith: []string{"whitelist"},
65686568
},
65696569
},
65706570

@@ -6576,19 +6576,19 @@ func TestValidateAllOfAttributes(t *testing.T) {
65766576
Key: "whitelist",
65776577
Schema: map[string]*Schema{
65786578
"whitelist": {
6579-
Type: TypeBool,
6580-
Optional: true,
6581-
AllOf: []string{"purplelist"},
6579+
Type: TypeBool,
6580+
Optional: true,
6581+
RequiredWith: []string{"purplelist"},
65826582
},
65836583
"blacklist": {
6584-
Type: TypeBool,
6585-
Optional: true,
6586-
AllOf: []string{"whitelist", "purplelist"},
6584+
Type: TypeBool,
6585+
Optional: true,
6586+
RequiredWith: []string{"whitelist", "purplelist"},
65876587
},
65886588
"purplelist": {
6589-
Type: TypeBool,
6590-
Optional: true,
6591-
AllOf: []string{"whitelist"},
6589+
Type: TypeBool,
6590+
Optional: true,
6591+
RequiredWith: []string{"whitelist"},
65926592
},
65936593
},
65946594

@@ -6603,19 +6603,19 @@ func TestValidateAllOfAttributes(t *testing.T) {
66036603
Key: "whitelist",
66046604
Schema: map[string]*Schema{
66056605
"whitelist": {
6606-
Type: TypeBool,
6607-
Optional: true,
6608-
AllOf: []string{"blacklist", "purplelist"},
6606+
Type: TypeBool,
6607+
Optional: true,
6608+
RequiredWith: []string{"blacklist", "purplelist"},
66096609
},
66106610
"blacklist": {
6611-
Type: TypeBool,
6612-
Optional: true,
6613-
AllOf: []string{"whitelist", "purplelist"},
6611+
Type: TypeBool,
6612+
Optional: true,
6613+
RequiredWith: []string{"whitelist", "purplelist"},
66146614
},
66156615
"purplelist": {
6616-
Type: TypeBool,
6617-
Optional: true,
6618-
AllOf: []string{"whitelist", "blacklist"},
6616+
Type: TypeBool,
6617+
Optional: true,
6618+
RequiredWith: []string{"whitelist", "blacklist"},
66196619
},
66206620
},
66216621

@@ -6631,19 +6631,19 @@ func TestValidateAllOfAttributes(t *testing.T) {
66316631
Key: "whitelist",
66326632
Schema: map[string]*Schema{
66336633
"whitelist": {
6634-
Type: TypeBool,
6635-
Optional: true,
6636-
AllOf: []string{"blacklist", "purplelist"},
6634+
Type: TypeBool,
6635+
Optional: true,
6636+
RequiredWith: []string{"blacklist", "purplelist"},
66376637
},
66386638
"blacklist": {
6639-
Type: TypeBool,
6640-
Optional: true,
6641-
AllOf: []string{"whitelist", "purplelist"},
6639+
Type: TypeBool,
6640+
Optional: true,
6641+
RequiredWith: []string{"whitelist", "purplelist"},
66426642
},
66436643
"purplelist": {
6644-
Type: TypeBool,
6645-
Optional: true,
6646-
AllOf: []string{"whitelist", "blacklist"},
6644+
Type: TypeBool,
6645+
Optional: true,
6646+
RequiredWith: []string{"whitelist", "blacklist"},
66476647
},
66486648
},
66496649

@@ -6657,19 +6657,19 @@ func TestValidateAllOfAttributes(t *testing.T) {
66576657
Key: "whitelist",
66586658
Schema: map[string]*Schema{
66596659
"whitelist": {
6660-
Type: TypeBool,
6661-
Optional: true,
6662-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6660+
Type: TypeBool,
6661+
Optional: true,
6662+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
66636663
},
66646664
"blacklist": {
6665-
Type: TypeBool,
6666-
Optional: true,
6667-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6665+
Type: TypeBool,
6666+
Optional: true,
6667+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
66686668
},
66696669
"purplelist": {
6670-
Type: TypeBool,
6671-
Optional: true,
6672-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6670+
Type: TypeBool,
6671+
Optional: true,
6672+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
66736673
},
66746674
},
66756675

@@ -6680,19 +6680,19 @@ func TestValidateAllOfAttributes(t *testing.T) {
66806680
"Only Unknown Variable Value": {
66816681
Schema: map[string]*Schema{
66826682
"whitelist": {
6683-
Type: TypeBool,
6684-
Optional: true,
6685-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6683+
Type: TypeBool,
6684+
Optional: true,
6685+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
66866686
},
66876687
"blacklist": {
6688-
Type: TypeBool,
6689-
Optional: true,
6690-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6688+
Type: TypeBool,
6689+
Optional: true,
6690+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
66916691
},
66926692
"purplelist": {
6693-
Type: TypeBool,
6694-
Optional: true,
6695-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6693+
Type: TypeBool,
6694+
Optional: true,
6695+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
66966696
},
66976697
},
66986698

@@ -6706,16 +6706,16 @@ func TestValidateAllOfAttributes(t *testing.T) {
67066706
"only unknown list value": {
67076707
Schema: map[string]*Schema{
67086708
"whitelist": {
6709-
Type: TypeList,
6710-
Optional: true,
6711-
Elem: &Schema{Type: TypeString},
6712-
AllOf: []string{"whitelist", "blacklist"},
6709+
Type: TypeList,
6710+
Optional: true,
6711+
Elem: &Schema{Type: TypeString},
6712+
RequiredWith: []string{"whitelist", "blacklist"},
67136713
},
67146714
"blacklist": {
6715-
Type: TypeList,
6716-
Optional: true,
6717-
Elem: &Schema{Type: TypeString},
6718-
AllOf: []string{"whitelist", "blacklist"},
6715+
Type: TypeList,
6716+
Optional: true,
6717+
Elem: &Schema{Type: TypeString},
6718+
RequiredWith: []string{"whitelist", "blacklist"},
67196719
},
67206720
},
67216721

@@ -6729,19 +6729,19 @@ func TestValidateAllOfAttributes(t *testing.T) {
67296729
"Unknown Variable Value and Known Value": {
67306730
Schema: map[string]*Schema{
67316731
"whitelist": {
6732-
Type: TypeBool,
6733-
Optional: true,
6734-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6732+
Type: TypeBool,
6733+
Optional: true,
6734+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
67356735
},
67366736
"blacklist": {
6737-
Type: TypeBool,
6738-
Optional: true,
6739-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6737+
Type: TypeBool,
6738+
Optional: true,
6739+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
67406740
},
67416741
"purplelist": {
6742-
Type: TypeBool,
6743-
Optional: true,
6744-
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6742+
Type: TypeBool,
6743+
Optional: true,
6744+
RequiredWith: []string{"whitelist", "blacklist", "purplelist"},
67456745
},
67466746
},
67476747

0 commit comments

Comments
 (0)