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

Add name validation function, standardise validation functions used #164

Merged
merged 3 commits into from
Jul 7, 2017

Conversation

rileykarson
Copy link
Collaborator

We have several validation functions that are just reimplementations of ones that live in core's helper/validation package; let's use those instead.

We define a separate validation function each time we want to validate the name field of a resource. Let's use a single one instead.

These two resources had validation functions for name that validated only length and not format, but both follow the same rules as most names, a 1-63 character RFC1035 string. Errors will now surface at plan time instead of apply time for them.

  • resource_compute_instance_template.go
  • resource_compute_ssl_certificate.go

"regexp"
)

func validateGCPName(v interface{}, k string) (ws []string, errors []error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Good candidate for unit testing here

Also what do you think about splitting it out into two functions - validateGCPName and validateRegexp (where validateGCPName is implemented in terms of validateRegexp)? I can see us needing regexs for other things later (for example, metadata keys)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@@ -68,7 +69,7 @@ func dataSourceGoogleSignedUrl() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Default: "GET",
ValidateFunc: validateHttpMethod,
ValidateFunc: validation.StringInSlice([]string{"GET", "HEAD", "PUT", "DELETE"}, true),
Copy link
Contributor

Choose a reason for hiding this comment

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

Could add our own validation function that is 'isHTTPVerb' (although given not all the http verbs are here, maybe that would be more confusing?) What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Given that not all of the http verbs are here, I don't think we should add a custom validator yet; if there are several other validators with identical parameters in the future, that is something we should consider, though.

Copy link
Contributor

Choose a reason for hiding this comment

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

SGTM

"regexp"
)

func validateGCPName(v interface{}, k string) (ws []string, errors []error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this also return schema.SchemaValidationFunc?

Copy link
Contributor

Choose a reason for hiding this comment

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

Or, better suggestion - make the other function return (ws []string, errors []error) (not a func)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

validateGCPName is implicitly a schema.SchemaValidationFunc because of its signature; we want validateRegexp to validate a against a variable regex and not a fixed one like validateGCPName, so we pass that regex into a function that makes a schema.SchemaValidationFunc for us.

There are similar functions to validateRegexp living in the core Terraform repo's helper/validation package.

Copy link
Contributor

Choose a reason for hiding this comment

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

Spoke in person; I understand the differing signatures here. I'm fine with the code as is.

@rileykarson rileykarson merged commit 4cc6e99 into hashicorp:master Jul 7, 2017
negz pushed a commit to negz/terraform-provider-google that referenced this pull request Oct 17, 2017
…ashicorp#164)

* Used standard validation functions where possible, added a GCP name validation function.

* Add tests for GCP name, factor out a ValidateRegexp function.

* make fmt
luis-silva pushed a commit to luis-silva/terraform-provider-google that referenced this pull request May 21, 2019
…_check (hashicorp#164)

<!-- This change is generated by MagicModules. -->
/cc @rileykarson
luis-silva pushed a commit to luis-silva/terraform-provider-google that referenced this pull request May 21, 2019
…_check (hashicorp#164)

<!-- This change is generated by MagicModules. -->
/cc @rileykarson
@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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks!

@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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants