-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add name validation function, standardise validation functions used (#…
…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
- Loading branch information
1 parent
5882f31
commit 4cc6e99
Showing
10 changed files
with
108 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"regexp" | ||
) | ||
|
||
func validateGCPName(v interface{}, k string) (ws []string, errors []error) { | ||
re := `^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$` | ||
return validateRegexp(re)(v, k) | ||
} | ||
|
||
func validateRegexp(re string) schema.SchemaValidateFunc { | ||
return func(v interface{}, k string) (ws []string, errors []error) { | ||
value := v.(string) | ||
if !regexp.MustCompile(re).MatchString(value) { | ||
errors = append(errors, fmt.Errorf( | ||
"%q (%q) doesn't match regexp %q", k, value, re)) | ||
} | ||
|
||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestValidateGCPName(t *testing.T) { | ||
x := []GCPNameTestCase{ | ||
// No errors | ||
{TestName: "basic", Value: "foobar"}, | ||
{TestName: "with numbers", Value: "foobar123"}, | ||
{TestName: "short", Value: "f"}, | ||
{TestName: "long", Value: "foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoo"}, | ||
{TestName: "has a hyphen", Value: "foo-bar"}, | ||
|
||
// With errors | ||
{TestName: "empty", Value: "", ExpectError: true}, | ||
{TestName: "starts with a capital", Value: "Foobar", ExpectError: true}, | ||
{TestName: "starts with a number", Value: "1foobar", ExpectError: true}, | ||
{TestName: "has an underscore", Value: "foo_bar", ExpectError: true}, | ||
{TestName: "too long", Value: "foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoob", ExpectError: true}, | ||
} | ||
|
||
es := testGCPNames(x) | ||
if len(es) > 0 { | ||
t.Errorf("Failed to validate GCP names: %v", es) | ||
} | ||
} | ||
|
||
type GCPNameTestCase struct { | ||
TestName string | ||
Value string | ||
ExpectError bool | ||
} | ||
|
||
func testGCPNames(cases []GCPNameTestCase) []error { | ||
es := make([]error, 0) | ||
for _, c := range cases { | ||
es = append(es, testGCPName(c)...) | ||
} | ||
|
||
return es | ||
} | ||
|
||
func testGCPName(testCase GCPNameTestCase) []error { | ||
_, es := validateGCPName(testCase.Value, testCase.TestName) | ||
if testCase.ExpectError { | ||
if len(es) > 0 { | ||
return nil | ||
} else { | ||
return []error{fmt.Errorf("Didn't see expected error in case \"%s\" with string \"%s\"", testCase.TestName, testCase.Value)} | ||
} | ||
} | ||
|
||
return es | ||
} |