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

Simplify Validation Functions in validators.go with Terraform helper/validation Functions #8424

Closed
bflad opened this issue Apr 24, 2019 · 5 comments
Labels
proposal Proposes new design or functionality. provider Pertains to the provider itself, rather than any interaction with AWS. stale Old or inactive issues managed by automation, if no further action taken these will get closed. technical-debt Addresses areas of the codebase that need refactoring or redesign.

Comments

@bflad
Copy link
Contributor

bflad commented Apr 24, 2019

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

The Terraform AWS Provider has a long history of adding custom validation functions used with Terraform resource schema ValidateFunc. Many times, the patterns discovered throughout these custom validation functions in the provider have influenced the creation of upstream Terraform Provider SDK functions (currently living in the Terraform core helper/validation package). With organic growth, the addition of these custom functions have created large and sometimes difficult to maintain aws/validators.go and aws/validators_test.go files. Much of the validation logic can be replaced with upstream functions to simplify the implementations.

Affected Resource(s)

Once design work is completed, this will be converted into a list. Each resource's validation should be individually updated to ease acceptance testing.

Code Examples

// Existing custom validation function
func validateDocDBParamGroupName(v interface{}, k string) (ws []string, errors []error) {
	value := v.(string)
	if !regexp.MustCompile(``).MatchString(value) {
		errors = append(errors, fmt.Errorf(
			" %q", k))
	}
	if !regexp.MustCompile(``).MatchString(value) {
		errors = append(errors, fmt.Errorf(
			"first character of parameter group %q must be a letter", k))
	}
	if regexp.MustCompile(``).MatchString(value) {
		errors = append(errors, fmt.Errorf(
			"parameter group %q cannot contain two consecutive hyphens", k))
	}
	if regexp.MustCompile(``).MatchString(value) {
		errors = append(errors, fmt.Errorf(
			"parameter group %q cannot end with a hyphen", k))
	}
	if len(value) > 255 {
		errors = append(errors, fmt.Errorf(
			"parameter group %q cannot be greater than 255 characters", k))
	}
	return
}

// Potential new implementation in resource schema or updated custom validation function
ValidateFunc: validation.All(
	validation.StringLenBetween(0, 255),
	validation.StringMatch(regexp.MustCompile(`^[a-z]`), "must begin with a lowercase letter"),
	validation.StringMatch(regexp.MustCompile(`--`), "must not contain two consecutive hyphens"),
	validation.StringMatch(regexp.MustCompile(`-$`), "must not end with a hyphen"),
	validation.StringMatch(regexp.MustCompile(`^[a-z0-9-]+$`), "must contain only lowercase letters, numbers, and hyphens"),
),
@bflad bflad added proposal Proposes new design or functionality. technical-debt Addresses areas of the codebase that need refactoring or redesign. provider Pertains to the provider itself, rather than any interaction with AWS. labels Apr 24, 2019
@ewbankkit
Copy link
Contributor

@jukie
Copy link
Contributor

jukie commented Apr 25, 2019

Could these also be used to simplify resource acceptance tests or is the proposal to remove things like below?

For example in a recent pr I spent a non-trivial amount of time hacking together a valid ExpectError string. I'll admit I'm still getting used to what's available but I imagine there must be a better way. I'm sure I can figure something out but if there's an agreed upon code style I'd rather use it.

func TestAccAWSTransferUser_UserName_Validation(t *testing.T) {
....
			{
				Config:      testAccAWSTransferUserName_validation(acctest.RandString(2)),
				ExpectError: regexp.MustCompile(`Invalid "user_name": must be between 3 and 32 alphanumeric or special characters hyphen and underscore. However, "user_name" cannot begin with a hyphen`),
			},
....
}

@bflad
Copy link
Contributor Author

bflad commented Apr 25, 2019

@jukie I think once we start seeing patterns of validation being used, we can likely start creating constants for the error messages that can be shared between the code and testing

bflad added a commit that referenced this issue Aug 30, 2019
…sterId into inline ValidateFunc

Reference: #8424

Output from acceptance testing:

```
--- PASS: TestAccAWSDAXCluster_encryption_disabled (739.98s)
--- PASS: TestAccAWSDAXCluster_basic (756.03s)
--- PASS: TestAccAWSDAXCluster_importBasic (796.86s)
--- PASS: TestAccAWSDAXCluster_encryption_enabled (865.80s)
--- PASS: TestAccAWSDAXCluster_resize (1414.26s)
```
@bflad bflad mentioned this issue Sep 8, 2019
13 tasks
@github-actions
Copy link

Marking this issue as stale due to inactivity. This helps our maintainers find and focus on the active issues. If this issue receives no comments in the next 30 days it will automatically be closed. Maintainers can also remove the stale label.

If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thank you!

@github-actions github-actions bot added the stale Old or inactive issues managed by automation, if no further action taken these will get closed. label Feb 27, 2022
@github-actions
Copy link

github-actions bot commented May 5, 2022

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
proposal Proposes new design or functionality. provider Pertains to the provider itself, rather than any interaction with AWS. stale Old or inactive issues managed by automation, if no further action taken these will get closed. technical-debt Addresses areas of the codebase that need refactoring or redesign.
Projects
None yet
Development

No branches or pull requests

3 participants