Skip to content

Commit

Permalink
Fix global_forwarding_rule labels (#12737)
Browse files Browse the repository at this point in the history
  • Loading branch information
wiktorn authored Jan 21, 2025
1 parent e055e09 commit 3a7ecb8
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 6 deletions.
1 change: 1 addition & 0 deletions mmv1/products/compute/GlobalForwardingRule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async:
resource_inside_response: false
collection_url_key: 'items'
custom_code:
pre_create: 'templates/terraform/pre_create/compute_global_forwarding_rule.go.tmpl'
post_create: 'templates/terraform/post_create/labels.tmpl'
legacy_long_form_project: true
examples:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Labels cannot be set in a create for PSC forwarding rules, so remove it from the CREATE request.
if strings.Contains(targetProp.(string), "/serviceAttachments/") ||
targetProp.(string) == "all-apis" ||
targetProp.(string) == "vpc-sc" {
if _, ok := obj["labels"]; ok {
delete(obj, "labels")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ func TestAccComputeGlobalForwardingRule_ipv6(t *testing.T) {
})
}

{{ if ne $.TargetVersionName `ga` -}}
func TestAccComputeGlobalForwardingRule_labels(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -123,7 +122,72 @@ func TestAccComputeGlobalForwardingRule_labels(t *testing.T) {
},
})
}
{{- end }}

func TestAccComputeGlobalForwardingRule_allApisLabels(t *testing.T) {
t.Parallel()

fr := fmt.Sprintf("frtest%s", acctest.RandString(t, 10))
address := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeGlobalForwardingRuleDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeGlobalForwardingRule_allApisLabels(fr, address),
},
{
ResourceName: "google_compute_global_forwarding_rule.forwarding_rule",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"port_range", "target", "labels", "terraform_labels"},
},
{
Config: testAccComputeGlobalForwardingRule_allApisLabelsUpdated(fr, address),
},
{
ResourceName: "google_compute_global_forwarding_rule.forwarding_rule",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"port_range", "target", "labels", "terraform_labels"},
},
},
})
}

func TestAccComputeGlobalForwardingRule_vpcscLabels(t *testing.T) {
t.Parallel()

fr := fmt.Sprintf("frtest%s", acctest.RandString(t, 10))
address := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeGlobalForwardingRuleDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeGlobalForwardingRule_vpcscLabels(fr, address),
},
{
ResourceName: "google_compute_global_forwarding_rule.forwarding_rule",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"port_range", "target", "labels", "terraform_labels"},
},
{
Config: testAccComputeGlobalForwardingRule_vpcscLabelsUpdated(fr, address),
},
{
ResourceName: "google_compute_global_forwarding_rule.forwarding_rule",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"port_range", "labels", "terraform_labels"},
},
},
})
}

{{ if ne $.TargetVersionName `ga` -}}
func TestAccComputeGlobalForwardingRule_internalLoadBalancing(t *testing.T) {
Expand Down Expand Up @@ -361,7 +425,6 @@ resource "google_compute_url_map" "url_map" {
`, fr, targetProxy, proxy, proxy2, backend, hc, urlmap)
}

{{ if ne $.TargetVersionName `ga` -}}
func testAccComputeGlobalForwardingRule_labels(fr, proxy, backend, hc, urlmap string) string {
return fmt.Sprintf(`
resource "google_compute_global_forwarding_rule" "forwarding_rule" {
Expand Down Expand Up @@ -416,9 +479,7 @@ resource "google_compute_url_map" "urlmap" {
}
`, fr, proxy, backend, hc, urlmap)
}
{{- end }}

{{ if ne $.TargetVersionName `ga` -}}
func testAccComputeGlobalForwardingRule_labelsUpdated(fr, proxy, backend, hc, urlmap string) string {
return fmt.Sprintf(`
resource "google_compute_global_forwarding_rule" "forwarding_rule" {
Expand Down Expand Up @@ -473,7 +534,106 @@ resource "google_compute_url_map" "urlmap" {
}
`, fr, proxy, backend, hc, urlmap)
}
{{- end }}

func testAccComputeGlobalForwardingRule_allApisLabels(fr, address string) string {
return fmt.Sprintf(`
resource "google_compute_global_forwarding_rule" "forwarding_rule" {
name = "%s"
network = "default"
target = "all-apis"
ip_address = google_compute_global_address.default.id
load_balancing_scheme = ""
labels = {
my-label = "a-value"
a-different-label = "my-second-label-value"
}
}

resource "google_compute_global_address" "default" {
name = "%s"
address_type = "INTERNAL"
purpose = "PRIVATE_SERVICE_CONNECT"
network = "default"
address = "100.100.100.105"
}

`, fr, address)
}

func testAccComputeGlobalForwardingRule_allApisLabelsUpdated(fr, address string) string {
return fmt.Sprintf(`
resource "google_compute_global_forwarding_rule" "forwarding_rule" {
name = "%s"
network = "default"
target = "all-apis"
ip_address = google_compute_global_address.default.id
load_balancing_scheme = ""
labels = {
my-label = "a-value"
a-different-label = "my-third-label-value"
}
}

resource "google_compute_global_address" "default" {
name = "%s"
address_type = "INTERNAL"
purpose = "PRIVATE_SERVICE_CONNECT"
network = "default"
address = "100.100.100.105"
}

`, fr, address)
}

func testAccComputeGlobalForwardingRule_vpcscLabels(fr, address string) string {
return fmt.Sprintf(`
resource "google_compute_global_forwarding_rule" "forwarding_rule" {
name = "%s"
network = "default"
target = "vpc-sc"
ip_address = google_compute_global_address.default.id
load_balancing_scheme = ""
labels = {
my-label = "a-value"
a-different-label = "my-second-label-value"
}
}

resource "google_compute_global_address" "default" {
name = "%s"
address_type = "INTERNAL"
purpose = "PRIVATE_SERVICE_CONNECT"
network = "default"
address = "100.100.100.106"
}

`, fr, address)
}

func testAccComputeGlobalForwardingRule_vpcscLabelsUpdated(fr, address string) string {
return fmt.Sprintf(`
resource "google_compute_global_forwarding_rule" "forwarding_rule" {
name = "%s"
network = "default"
target = "vpc-sc"
ip_address = google_compute_global_address.default.id
load_balancing_scheme = ""
labels = {
my-label = "a-value"
a-different-label = "my-third-label-value"
}
}

resource "google_compute_global_address" "default" {
name = "%s"
address_type = "INTERNAL"
purpose = "PRIVATE_SERVICE_CONNECT"
network = "default"
address = "100.100.100.106"
}

`, fr, address)
}

func testAccComputeGlobalForwardingRule_ipv6(fr, proxy, backend, hc, urlmap string) string {
return fmt.Sprintf(`
Expand Down

0 comments on commit 3a7ecb8

Please # to comment.