Skip to content

Commit

Permalink
Cloud Function - fix docs and test, fix regexp for name (#5400)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>

Co-authored-by: emily <emilyye@google.com>
  • Loading branch information
modular-magician and emilymye committed Jan 14, 2020
1 parent 0c32cb4 commit e3b195c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 28 deletions.
24 changes: 4 additions & 20 deletions google/resource_cloudfunctions_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"log"
"net/url"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -66,26 +65,11 @@ func joinMapKeys(mapToJoin *map[int]bool) string {
return strings.Join(keys, ",")
}

// Differs from validateGcpName because Cloud Functions allow capital letters
// at start/end
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
re := `^(?:[a-zA-Z](?:[-_a-zA-Z0-9]{0,61}[a-zA-Z0-9])?)$`
return validateRegexp(re)(v, k)
}

// based on compareSelfLinkOrResourceName, but less reusable and allows multi-/
Expand Down
4 changes: 3 additions & 1 deletion google/resource_cloudfunctions_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func TestCloudFunctionsFunction_nameValidator(t *testing.T) {
"has_underscore",
"hasUpperCase",
"allChars_-A0",
"StartsUpperCase",
"endsUpperCasE",
}
for _, tc := range validNames {
wrns, errs := validateResourceCloudFunctionsFunctionName(tc, "function.name")
Expand All @@ -60,7 +62,7 @@ func TestCloudFunctionsFunction_nameValidator(t *testing.T) {
"endsWith_",
"endsWith-",
"bad*Character",
"aFunctionsNameThatIsLongerThanFortyEightCharacters",
"aCloudFunctionsFunctionNameThatIsSeventyFiveCharactersLongWhichIsMoreThan63",
}
for _, tc := range invalidNames {
_, errs := validateResourceCloudFunctionsFunctionName(tc, "function.name")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ exported:

The `event_trigger` block contains:

* `event_type` - The type of event being observed. For example: `"providers/cloud.storage/eventTypes/object.change"`
and `"providers/cloud.pubsub/eventTypes/topic.publish"`. See the documentation on [calling Cloud Functions](https://cloud.google.com/functions/docs/calling/)
for a full reference.
* `event_type` - The type of event to observe. For example: `"google.storage.object.finalize"`.
See the documentation on [calling Cloud Functions](https://cloud.google.com/functions/docs/calling/)
for a full reference of accepted triggers.

* `resource` - The name of the resource whose events are being observed, for example, `"myBucket"`

Expand Down
6 changes: 2 additions & 4 deletions website/docs/r/cloudfunctions_function.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,8 @@ Eg. `"nodejs8"`, `"nodejs10"`, `"python37"`, `"go111"`.
The `event_trigger` block supports:

* `event_type` - (Required) The type of event to observe. For example: `"google.storage.object.finalize"`.
See the documentation on [calling Cloud Functions](https://cloud.google.com/functions/docs/calling/) for a full reference.
Cloud Storage, Cloud Pub/Sub and Cloud Firestore triggers are supported at this time.
Legacy triggers are supported, such as `"providers/cloud.storage/eventTypes/object.change"`,
`"providers/cloud.pubsub/eventTypes/topic.publish"` and `"providers/cloud.firestore/eventTypes/document.create"`.
See the documentation on [calling Cloud Functions](https://cloud.google.com/functions/docs/calling/) for a
full reference of accepted triggers.

* `resource` - (Required) Required. The name or partial URI of the resource from
which to observe events. For example, `"myBucket"` or `"projects/my-project/topics/my-topic"`
Expand Down

0 comments on commit e3b195c

Please sign in to comment.