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

Feature: AtLeastOneOf and ExactlyOneOf validation flags added to schema #225

Merged
merged 15 commits into from
Nov 4, 2019

Conversation

mbfrahry
Copy link
Member

This PR is looking to add additional validation checks that occur at apply time versus the current method which is to write these checks manually and occur during an apply which has the potential to cause terraform to error out mid run.

ExactlyOneOf is a set of schema keys that, when set, only one of the keys in that list can be specified. It will error if none are specified as well.

AtLeastOneOf is a set of schema keys that, when set, at least one of the keys in that list must be specified.

@hashicorp-cla
Copy link

hashicorp-cla commented Oct 30, 2019

CLA assistant check
All committers have signed the CLA.

return nil
}

allKeys := removeDuplicates(append(schema.AtLeastOneOf, k))
Copy link
Contributor

@appilon appilon Oct 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my brain is tingling about this LOC in both functions, but if the tests pass I'm sure it all makes sense.

Copy link
Member Author

@mbfrahry mbfrahry Oct 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added to ensure that users coming from the ConflictsWith pattern where you specify every key but the attribute you're in are covered. For example:

"foo": {
  ConflictsWith: ["bar"]
}
"bar": {
  ConflictsWith:["foo"]
}

We wanted users to go a different route where you specify the whole list:

"foo": {
  AtLeastOneOf: ["foo", "bar"]
}
"bar": {
  AtLeastOneOf:["foo", "bar"]
}

but we don't have anyway to enforce that they use this pattern so we add the current attribute key and then remove duplicates to ensure our validation doesn't go sideways

}

allKeys := removeDuplicates(append(schema.ExactlyOneOf, k))
sort.Strings(allKeys)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to sort?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for nicer error messages. These have the potential to stack up on one another so if we sort them, they will line up nicely with one another

for _, exactlyOneOfKey := range allKeys {
if raw, ok := c.Get(exactlyOneOfKey); ok {
if raw == hcl2shim.UnknownVariableValue {
// An unknown value might become unset (null) once known, so
Copy link
Contributor

@appilon appilon Oct 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sorta makes me concerned for some weird edgecase bugs. These are larger Terraform questions like when does validate get called and how often?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, however this code has been around for awhile and was copy pasted to be in line with ConflictsWith

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terraform Core calls ValidateResourceChange again during apply so we can catch things like this. It does mean that there is the potential for an error not to show up until apply time, but that is generally true for all validation.

}

allKeys := removeDuplicates(append(schema.AtLeastOneOf, k))
sort.Strings(allKeys)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above do we need to sort?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing it makes for nicer printout in the error message having the keys being sorted?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly!

Copy link
Contributor

@appilon appilon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall approve!! I had some general comments as I load the context of all this into my brain. Seems like a very useful feature! Would be good to get another set of 👀 from @kmoe or @radeksimko

@paultyng paultyng added the enhancement New feature or request label Oct 31, 2019
@appilon appilon requested a review from a team October 31, 2019 22:22
Copy link
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Martin about adding at least one more test to cover case(s) with hcl2shim.UnknownVariableValue, otherwise LGTM.

return result
}

func validateExactlyOneAttributes(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Shouldn't this be called validateExactlyOneAttribute?

return nil
}

func validateAtLeastOneAttributes(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Shouldn't this be called validateAtLeastOneAttribute?

@mbfrahry
Copy link
Member Author

mbfrahry commented Nov 4, 2019

hcl2shim.UnknownVariableValue has been tested, additional checks were added,. and tests still pass

@appilon
Copy link
Contributor

appilon commented Nov 4, 2019

@mbfrahry Can you fixup/squash the commits a bit? to a few meaningful commits or just one? Once this is merged We need a changelog entry describing this new feature

@mbfrahry mbfrahry merged commit 97d4d4b into master Nov 4, 2019
@mbfrahry mbfrahry deleted the f-oneof-mm branch November 4, 2019 21:36
@ghost
Copy link

ghost commented Mar 31, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Mar 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants