Skip to content
This repository was archived by the owner on Nov 14, 2020. It is now read-only.

Replace unnecessary validation functions with validators from the plugin SDK #122

Merged

Conversation

tomelliff
Copy link
Contributor

The plugin SDK provides helper validation functions that are well tested and used in a lot of places.
In general we should prefer to use these where possible instead of rolling our own functions that do the same thing.

I noticed this after the recent release and looking at #105 but only after it had been merged.

Output from terraform validate after this change:

Error: expected connection_limit to be at least (-1), got -2

  on main.tf line 1, in resource "postgresql_role" "my_role":
   1: resource "postgresql_role" "my_role" {



Error: expected statement_timeout to be at least (0), got -1

  on main.tf line 1, in resource "postgresql_role" "my_role":
   1: resource "postgresql_role" "my_role" {

which is arguably slightly less nice than the existing output:

Error: connection_limit can not be less than -1

  on main.tf line 1, in resource "postgresql_role" "my_role":
   1: resource "postgresql_role" "my_role" {

but does so without duplicating code elsewhere so feel like this is a better approach.

validatePrivileges can probably also be refactored to use the StringInSlice validator but wanted to check if people were happy with replacing the validators here with the ones from the plugin SDK before going further.

…gin SDK

The plugin SDK provides helper validation functions that are well tested and used in a lot of places.
In general we should prefer to use these where possible instead of rolling our own functions that do the same thing.
@ghost ghost added the size/XS label Feb 25, 2020
Copy link
Contributor

@cyrilgdn cyrilgdn left a comment

Choose a reason for hiding this comment

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

@tomelliff Nice catch, I'm happy with that :) (and errors messages are ok for me)

For validatePrivileges, we didn't used the ValidateFunc attribute because allowed privileges depend on the object_type field, so I don't think we can use StringInSlice?

@tomelliff
Copy link
Contributor Author

tomelliff commented Feb 25, 2020

Oh right yeah that makes sense.

It would only really be possible if the grant and default privileges resources were split out into separate resources by object_type (eg postgresql_grant_table and postgresql_grant_sequence) and allow you to validate the privileges parameter directly but that seems unnecessary even if it would give plan/validate time validation that's currently missing.

I have seen cross field validation using the CustomizeDiff option on the parameters but it seemed like an ugly hack to me so haven't used it in other places where an API expects certain things to be set if others are set another way. Hopefully Terraform core will work out a way to do this kind of cross field validation later.

@cyrilgdn cyrilgdn merged commit 207a920 into hashicorp:master Feb 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants