From 34f20b22625d7d05e607877102498fbf28990e4a Mon Sep 17 00:00:00 2001 From: The Magician Date: Tue, 14 Jan 2020 15:16:42 -0800 Subject: [PATCH] Cloud Function - fix docs and test, fix regexp for name (#1640) Signed-off-by: Modular Magician Co-authored-by: emily --- .../resource_cloudfunctions_function.go | 24 ++++--------------- .../resource_cloudfunctions_function_test.go | 23 +++++++++++++----- .../resource_sql_database_instance_test.go | 1 - ...urce_cloudfunctions_function.html.markdown | 6 ++--- .../r/cloudfunctions_function.html.markdown | 6 ++--- 5 files changed, 26 insertions(+), 34 deletions(-) diff --git a/google-beta/resource_cloudfunctions_function.go b/google-beta/resource_cloudfunctions_function.go index 44a3cf6d5e..38ed3dd78a 100644 --- a/google-beta/resource_cloudfunctions_function.go +++ b/google-beta/resource_cloudfunctions_function.go @@ -8,7 +8,6 @@ import ( "fmt" "log" "net/url" - "regexp" "strconv" "strings" "time" @@ -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-/ diff --git a/google-beta/resource_cloudfunctions_function_test.go b/google-beta/resource_cloudfunctions_function_test.go index 03941f40e3..f5575f0c4d 100644 --- a/google-beta/resource_cloudfunctions_function_test.go +++ b/google-beta/resource_cloudfunctions_function_test.go @@ -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") @@ -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") @@ -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") @@ -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), }, }, }) @@ -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" { @@ -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) } diff --git a/google-beta/resource_sql_database_instance_test.go b/google-beta/resource_sql_database_instance_test.go index b04988ac0a..5b99680669 100644 --- a/google-beta/resource_sql_database_instance_test.go +++ b/google-beta/resource_sql_database_instance_test.go @@ -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" ) diff --git a/website/docs/d/datasource_cloudfunctions_function.html.markdown b/website/docs/d/datasource_cloudfunctions_function.html.markdown index 2de510d61b..2801165b17 100644 --- a/website/docs/d/datasource_cloudfunctions_function.html.markdown +++ b/website/docs/d/datasource_cloudfunctions_function.html.markdown @@ -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"` diff --git a/website/docs/r/cloudfunctions_function.html.markdown b/website/docs/r/cloudfunctions_function.html.markdown index 98611aa045..89e9f75868 100644 --- a/website/docs/r/cloudfunctions_function.html.markdown +++ b/website/docs/r/cloudfunctions_function.html.markdown @@ -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"`