Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for regional NEG stripping of maxUtilization based on url #3884

Merged
merged 3 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions templates/terraform/encoders/backend_service.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ for _, backendRaw := range backends {
if !ok {
continue
}
if strings.Contains(backendGroup.(string), "global/networkEndpointGroups") {
// Remove `max_utilization` from any backend that belongs to a global NEG. This field
// has a default value and causes API validation errors
backend["maxUtilization"] = nil
serverlessNegMatchers := [2]*regexp.Regexp{regexp.MustCompile("projects/(?P<project>[^/]+)/regions/(?P<region>[^/]+)/networkEndpointGroups/(?P<name>[^/]+)"), regexp.MustCompile("projects/(?P<project>[^/]+)/global/networkEndpointGroups/(?P<name>[^/]+)")}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regexes only need to match part of the string, so I think just using (?:global|region/[^/]+)/networkEndpointGroups as the regex would work too and be a bit less text. any reason to do this way instead?

for _, regex := range serverlessNegMatchers {
if regex.MatchString(backendGroup.(string)) {
// Remove `max_utilization` from any backend that belongs to a serverless NEG. This field
// has a default value and causes API validation errors
backend["maxUtilization"] = nil
break
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,30 @@ func TestAccComputeBackendService_trafficDirectorUpdateFull(t *testing.T) {
}
<% end -%>

<% unless version == 'ga' -%>
func TestAccComputeBackendService_regionNegBackend(t *testing.T) {
t.Parallel()

suffix := randString(t, 10)

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeBackendServiceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeBackendService_regionNegBackend(suffix),
},
{
ResourceName: "google_compute_backend_service.backend",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
<% end -%>

func testAccComputeBackendService_trafficDirectorBasic(serviceName, checkName string) string {
return fmt.Sprintf(`
resource "google_compute_backend_service" "foobar" {
Expand Down Expand Up @@ -1508,3 +1532,47 @@ resource "google_compute_http_health_check" "zero" {
}
`, serviceName, sampleRate, checkName)
}

<% unless version == 'ga' -%>
func testAccComputeBackendService_regionNegBackend(suffix string) string {
return fmt.Sprintf(`
resource "google_compute_backend_service" "backend" {
name = "tf-test-backend%s"
enable_cdn = true
connection_draining_timeout_sec = 10

backend {
group = google_compute_region_network_endpoint_group.cloudrun_neg.id
}
}

resource "google_compute_region_network_endpoint_group" "cloudrun_neg" {
name = "tf-test-neg%s"
network_endpoint_type = "SERVERLESS"
region = "us-central1"
cloud_run {
service = google_cloud_run_service.cloudrun_neg.name
}
}

resource "google_cloud_run_service" "cloudrun_neg" {
name = "tf-test-cr%s"
location = "us-central1"

template {
spec {
containers {
image = "gcr.io/cloudrun/hello"
}
}
}

traffic {
percent = 100
latest_revision = true
}
}

`, suffix, suffix, suffix)
}
<% end -%>