Skip to content

Commit

Permalink
Cloud Functions validation regexp
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
emilymye authored and modular-magician committed Mar 18, 2019
1 parent b309ccd commit 085c612
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 24 deletions.
50 changes: 26 additions & 24 deletions google/resource_cloudfunctions_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ func joinMapKeys(mapToJoin *map[int]bool) string {
return strings.Join(keys, ",")
}

func validateResourceCloudFunctionsFunctionName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

if len(value) > 48 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 48 characters", k))
}
if !regexp.MustCompile("^[a-zA-Z0-9-_]+$").MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q can only contain letters, numbers, underscores and hyphens", k))
}
if !regexp.MustCompile("^[a-zA-Z]").MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must start with a letter", k))
}
if !regexp.MustCompile("[a-zA-Z0-9]$").MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must end with a number or a letter", k))
}
return
}

func resourceCloudFunctionsFunction() *schema.Resource {
return &schema.Resource{
Create: resourceCloudFunctionsCreate,
Expand All @@ -91,30 +113,10 @@ func resourceCloudFunctionsFunction() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

if len(value) > 48 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 48 characters", k))
}
if !regexp.MustCompile("^[a-zA-Z0-9-]+$").MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q can only contain letters, numbers and hyphens", k))
}
if !regexp.MustCompile("^[a-zA-Z]").MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must start with a letter", k))
}
if !regexp.MustCompile("[a-zA-Z0-9]$").MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must end with a number or a letter", k))
}
return
},
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateResourceCloudFunctionsFunctionName,
},

"source_archive_bucket": {
Expand Down
35 changes: 35 additions & 0 deletions google/resource_cloudfunctions_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,41 @@ const testPubSubTriggerPath = "./test-fixtures/cloudfunctions/pubsub_trigger.js"
const testBucketTriggerPath = "./test-fixtures/cloudfunctions/bucket_trigger.js"
const testFirestoreTriggerPath = "./test-fixtures/cloudfunctions/firestore_trigger.js"

func TestCloudFunctionsFunction_nameValidator(t *testing.T) {
validNames := []string{
"a",
"aA",
"a0",
"has-hyphen",
"has_underscore",
"hasUpperCase",
"allChars_-A0",
}
for _, tc := range validNames {
wrns, errs := validateResourceCloudFunctionsFunctionName(tc, "function.name")
if len(wrns) > 0 {
t.Errorf("Expected no validation warnings for test case %q, got: %+v", tc, wrns)
}
if len(errs) > 0 {
t.Errorf("Expected no validation errors for test name %q, got: %+v", tc, errs)
}
}

invalidNames := []string{
"0startsWithNumber",
"endsWith_",
"endsWith-",
"bad*Character",
"aFunctionsNameThatIsLongerThanFortyEightCharacters",
}
for _, tc := range invalidNames {
_, errs := validateResourceCloudFunctionsFunctionName(tc, "function.name")
if len(errs) == 0 {
t.Errorf("Expected errors for invalid test name %q, got none", tc)
}
}
}

func TestAccCloudFunctionsFunction_basic(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 085c612

Please sign in to comment.