From f3a084296172ffe6f36d7d68fe6e102b2c96138e Mon Sep 17 00:00:00 2001 From: Michael Haro Date: Tue, 14 Aug 2018 22:43:42 -0700 Subject: [PATCH 1/2] Remove duplicate ProjectIDRegex variable --- google/validation.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/google/validation.go b/google/validation.go index 15ea6bad996..47dffef742a 100644 --- a/google/validation.go +++ b/google/validation.go @@ -49,7 +49,6 @@ var ( // Format of default App Engine service accounts created by Google AppEngineServiceAccountNameRegex = ProjectRegex + "@appspot.gserviceaccount.com" - ProjectIDRegex = "^[a-z][a-z0-9-]{4,28}[a-z0-9]$" ProjectNameRegex = "^[A-Za-z0-9-'\"\\s!]{4,30}$" ) @@ -166,7 +165,7 @@ func validateProjectID() schema.SchemaValidateFunc { return func(v interface{}, k string) (ws []string, errors []error) { value := v.(string) - if !regexp.MustCompile(ProjectIDRegex).MatchString(value) { + if !regexp.MustCompile(ProjectRegex).MatchString(value) { errors = append(errors, fmt.Errorf( "%q project_id must be 6 to 30 with lowercase letters, digits, hyphens and start with a letter. Trailing hyphens are prohibited.", value)) } From 8622e3dc74d996d5ad309d01922cf304d031dda2 Mon Sep 17 00:00:00 2001 From: Dana Hoffman Date: Tue, 11 Sep 2018 11:02:16 -0400 Subject: [PATCH 2/2] update validation and test for new regex --- google/validation.go | 2 +- google/validation_test.go | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/google/validation.go b/google/validation.go index 47dffef742a..15a3e6a466b 100644 --- a/google/validation.go +++ b/google/validation.go @@ -165,7 +165,7 @@ func validateProjectID() schema.SchemaValidateFunc { return func(v interface{}, k string) (ws []string, errors []error) { value := v.(string) - if !regexp.MustCompile(ProjectRegex).MatchString(value) { + if !regexp.MustCompile("^" + ProjectRegex + "$").MatchString(value) { errors = append(errors, fmt.Errorf( "%q project_id must be 6 to 30 with lowercase letters, digits, hyphens and start with a letter. Trailing hyphens are prohibited.", value)) } diff --git a/google/validation_test.go b/google/validation_test.go index 967ee5f7425..adeeb3210a8 100644 --- a/google/validation_test.go +++ b/google/validation_test.go @@ -2,11 +2,12 @@ package google import ( "fmt" - "github.com/hashicorp/terraform/helper/schema" - "github.com/hashicorp/terraform/helper/validation" "regexp" "strings" "testing" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" ) func TestValidateGCPName(t *testing.T) { @@ -247,15 +248,15 @@ func TestOrEmpty(t *testing.T) { ExpectValidationErrors bool }{ "accept empty value": { - Value: "", + Value: "", ExpectValidationErrors: false, }, "non empty value is accepted when valid": { - Value: "valid", + Value: "valid", ExpectValidationErrors: false, }, "non empty value is rejected if invalid": { - Value: "invalid", + Value: "invalid", ExpectValidationErrors: true, }, } @@ -282,11 +283,9 @@ func TestValidateProjectID(t *testing.T) { // With errors {TestName: "empty", Value: "", ExpectError: true}, - {TestName: "starts with a number", Value: "1foobar", ExpectError: true}, {TestName: "has an slash", Value: "foo/bar", ExpectError: true}, {TestName: "has an upercase letter", Value: "foo-Bar", ExpectError: true}, {TestName: "has a final hyphen", Value: "foo-bar-", ExpectError: true}, - {TestName: "too long", Value: strings.Repeat("a", 31), ExpectError: true}, } es := testStringValidationCases(x, validateProjectID())