Skip to content

Commit

Permalink
fix regexp for cloud functions name, change vpc connector test to use…
Browse files Browse the repository at this point in the history
… non-default network
  • Loading branch information
emilymye committed Jan 13, 2020
1 parent ed80aef commit 0600c87
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
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-z0-9]{0,61}[a-zA-Z0-9])?)$`
return validateRegexp(re)(v, k)
}

// based on compareSelfLinkOrResourceName, but less reusable and allows multi-/
Expand Down Expand Up @@ -117,7 +101,7 @@ func resourceCloudFunctionsFunction() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateResourceCloudFunctionsFunctionName,
ValidateFunc: validateGCPName,
},

"source_archive_bucket": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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 @@ -59,7 +61,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 @@ -758,23 +761,29 @@ resource "google_cloudfunctions_function" "function" {
}

<% unless version == 'ga' -%>
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 @@ -809,7 +818,9 @@ 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)
}
<% end -%>

0 comments on commit 0600c87

Please sign in to comment.