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

Adding PairedWith Validation Check #380

Closed
wants to merge 3 commits into from
Closed
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
56 changes: 56 additions & 0 deletions helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,13 @@ type Schema struct {
//
// AtLeastOneOf is a set of schema keys that, when set, at least one of
// the keys in that list must be specified.
//
// PairedWith is a set of schema keys that when one key is set, the rest
// of the keys in the set must also be specified.
ConflictsWith []string
ExactlyOneOf []string
AtLeastOneOf []string
PairedWith []string

// When Deprecated is set, this attribute is deprecated.
//
Expand Down Expand Up @@ -766,6 +770,10 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro
return fmt.Errorf("%s: AtLeastOneOf cannot be set with Required", k)
}

if len(v.PairedWith) > 0 && v.Required {
return fmt.Errorf("%s: PairedWith cannot be set with Required", k)
}

if len(v.ConflictsWith) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.ConflictsWith, topSchemaMap)
if err != nil {
Expand All @@ -787,6 +795,13 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro
}
}

if len(v.PairedWith) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.PairedWith, topSchemaMap)
if err != nil {
return fmt.Errorf("PairedWith: %+v", err)
}
}

if v.Type == TypeList || v.Type == TypeSet {
if v.Elem == nil {
return fmt.Errorf("%s: Elem must be set for lists", k)
Expand Down Expand Up @@ -1400,6 +1415,11 @@ func (m schemaMap) validate(
return nil, []error{err}
}

err = validatePairedWithAttribute(k, schema, c)
if err != nil {
return nil, []error{err}
}

if !ok {
if schema.Required {
return nil, []error{fmt.Errorf(
Expand Down Expand Up @@ -1553,6 +1573,42 @@ func validateAtLeastOneAttribute(
return fmt.Errorf("%q: one of `%s` must be specified", k, strings.Join(allKeys, ","))
}

func validatePairedWithAttribute(
k string,
schema *Schema,
c *terraform.ResourceConfig) error {

if len(schema.PairedWith) == 0 {
return nil
}

allKeys := removeDuplicates(append(schema.PairedWith, k))
sort.Strings(allKeys)
count := 0
unknownVariableValueCount := 0

for _, pairedWithKey := range allKeys {
if c.IsComputed(pairedWithKey) {
unknownVariableValueCount++
continue
}

if _, ok := c.Get(pairedWithKey); ok {
count++
}
}

if count == 0 && unknownVariableValueCount == 0 {
return nil
}

if count+unknownVariableValueCount != len(allKeys) {
return fmt.Errorf("all of `%s` must be specified when using %q", strings.Join(allKeys, ","), k)
}

return nil
}

func (m schemaMap) validateList(
k string,
raw interface{},
Expand Down
Loading