Skip to content

Commit

Permalink
Cloud Function - fix docs and test, fix regexp for name (#1640)
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 53d6c58 commit 34f20b2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 34 deletions.
24 changes: 4 additions & 20 deletions google-beta/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
23 changes: 17 additions & 6 deletions google-beta/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 Expand Up @@ -323,6 +325,7 @@ func TestAccCloudFunctionsFunction_vpcConnector(t *testing.T) {

functionName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
bucketName := fmt.Sprintf("tf-test-bucket-%d", acctest.RandInt())
networkName := fmt.Sprintf("tf-test-net-%d", acctest.RandInt())
vpcConnectorName := fmt.Sprintf("tf-test-connector-%s", acctest.RandString(5))
zipFilePath := createZIPArchiveForCloudFunctionSource(t, testHTTPTriggerPath)
projectNumber := os.Getenv("GOOGLE_PROJECT_NUMBER")
Expand All @@ -334,7 +337,7 @@ func TestAccCloudFunctionsFunction_vpcConnector(t *testing.T) {
CheckDestroy: testAccCheckCloudFunctionsFunctionDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudFunctionsFunction_vpcConnector(projectNumber, functionName, bucketName, zipFilePath, vpcConnectorName),
Config: testAccCloudFunctionsFunction_vpcConnector(projectNumber, networkName, functionName, bucketName, zipFilePath, vpcConnectorName),
},
},
})
Expand Down Expand Up @@ -756,23 +759,29 @@ resource "google_cloudfunctions_function" "function" {
`, bucketName, zipFilePath, functionName)
}

func testAccCloudFunctionsFunction_vpcConnector(projectNumber, functionName, bucketName, zipFilePath, vpcConnectorName string) string {
func testAccCloudFunctionsFunction_vpcConnector(projectNumber, networkName, functionName, bucketName, zipFilePath, vpcConnectorName string) string {
return fmt.Sprintf(`
provider "google-beta" { }
resource "google_project_iam_member" "project" {
resource "google_project_iam_member" "gcfadmin" {
provider = "google-beta"
role = "roles/editor"
member = "serviceAccount:service-%s@gcf-admin-robot.iam.gserviceaccount.com"
}
resource "google_compute_network" "vpc" {
provider = "google-beta"
name = "%s"
auto_create_subnetworks = false
}
resource "google_vpc_access_connector" "connector" {
provider = "google-beta"
name = "%s"
region = "us-central1"
ip_cidr_range = "10.10.0.0/28"
network = "default"
network = google_compute_network.vpc.name
}
resource "google_storage_bucket" "bucket" {
Expand Down Expand Up @@ -807,6 +816,8 @@ resource "google_cloudfunctions_function" "function" {
}
max_instances = 10
vpc_connector = google_vpc_access_connector.connector.self_link
depends_on = [google_project_iam_member.gcfadmin]
}
`, projectNumber, vpcConnectorName, bucketName, zipFilePath, functionName)
`, projectNumber, networkName, vpcConnectorName, bucketName, zipFilePath, functionName)
}
1 change: 0 additions & 1 deletion google-beta/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"

sqladmin "google.golang.org/api/sqladmin/v1beta4"
)

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 34f20b2

Please sign in to comment.