From 729723a7ca99842e3ffb289b1fc140a4de6fec82 Mon Sep 17 00:00:00 2001 From: "Max W. Portocarrero" Date: Fri, 27 Oct 2023 11:21:27 -0500 Subject: [PATCH 1/6] add autoNetworkTier to Router NAT --- mmv1/products/compute/RouterNat.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/mmv1/products/compute/RouterNat.yaml b/mmv1/products/compute/RouterNat.yaml index 9b0d414391a1..aef8d03f9a5b 100644 --- a/mmv1/products/compute/RouterNat.yaml +++ b/mmv1/products/compute/RouterNat.yaml @@ -436,3 +436,13 @@ properties: - :PUBLIC - :PRIVATE default_value: :PUBLIC + - !ruby/object:Api::Type::Enum + name: 'autoNetworkTier' + required: false + description: | + The network tier to use when automatically reserving NAT IP addresses. + Must be one of: PREMIUM, STANDARD. If not specified, then the current + project-level default tier is used. + values: + - :PREMIUN + - :STANDAR From c981c0b51a7698a3719ef9038ed201cf878d02e8 Mon Sep 17 00:00:00 2001 From: "Max W. Portocarrero" Date: Mon, 13 Nov 2023 09:12:27 -0500 Subject: [PATCH 2/6] fixed tier type typo --- mmv1/products/compute/RouterNat.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmv1/products/compute/RouterNat.yaml b/mmv1/products/compute/RouterNat.yaml index aef8d03f9a5b..5c89a68b8836 100644 --- a/mmv1/products/compute/RouterNat.yaml +++ b/mmv1/products/compute/RouterNat.yaml @@ -445,4 +445,4 @@ properties: project-level default tier is used. values: - :PREMIUN - - :STANDAR + - :STANDARD From 3c024bd29ac75b760f10834cec3d12e6d6ad8368 Mon Sep 17 00:00:00 2001 From: "Max W. Portocarrero" Date: Tue, 23 Jan 2024 08:21:53 -0500 Subject: [PATCH 3/6] added encoder fix and integration test --- mmv1/products/compute/RouterNat.yaml | 2 +- .../terraform/encoders/router_nat.go.erb | 32 ++++++++++++ .../resource_compute_router_nat_test.go.erb | 51 ++++++++++++++++--- 3 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 mmv1/templates/terraform/encoders/router_nat.go.erb diff --git a/mmv1/products/compute/RouterNat.yaml b/mmv1/products/compute/RouterNat.yaml index 5c89a68b8836..53e9d747cca1 100644 --- a/mmv1/products/compute/RouterNat.yaml +++ b/mmv1/products/compute/RouterNat.yaml @@ -107,6 +107,7 @@ custom_code: !ruby/object:Provider::Terraform::CustomCode constants: 'templates/terraform/constants/router_nat.go.erb' pre_create: 'templates/terraform/constants/router_nat_validate_action_active_range.go.erb' pre_update: 'templates/terraform/constants/router_nat_validate_action_active_range.go.erb' + encoder: 'templates/terraform/encoders/router_nat.go.erb' custom_diff: [ 'resourceComputeRouterNatDrainNatIpsCustomDiff', ] @@ -438,7 +439,6 @@ properties: default_value: :PUBLIC - !ruby/object:Api::Type::Enum name: 'autoNetworkTier' - required: false description: | The network tier to use when automatically reserving NAT IP addresses. Must be one of: PREMIUM, STANDARD. If not specified, then the current diff --git a/mmv1/templates/terraform/encoders/router_nat.go.erb b/mmv1/templates/terraform/encoders/router_nat.go.erb new file mode 100644 index 000000000000..d2154454e102 --- /dev/null +++ b/mmv1/templates/terraform/encoders/router_nat.go.erb @@ -0,0 +1,32 @@ +<%# The license inside this block applies to this file. + # Copyright 2020 Google Inc. + # Licensed under the Apache License, Version 2.0 (the "License"); + # you may not use this file except in compliance with the License. + # You may obtain a copy of the License at + # + # http://www.apache.org/licenses/LICENSE-2.0 + # + # Unless required by applicable law or agreed to in writing, software + # distributed under the License is distributed on an "AS IS" BASIS, + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + # See the License for the specific language governing permissions and + # limitations under the License. +-%> +config := meta.(*transport_tpg.Config) +userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) +if err != nil { + return nil, err +} + +projectID, err := tpgresource.GetProject(d, meta.(*transport_tpg.Config)) +if err != nil { + return nil, err +} + +project, err := config.NewComputeClient(userAgent).Projects.Get(projectID).Do() +if err != nil { + return nil, transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("Project data for project %q", projectID)) +} + +obj["autoNetworkTier"] = project.DefaultNetworkTier +return obj, nil diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_router_nat_test.go.erb b/mmv1/third_party/terraform/services/compute/resource_compute_router_nat_test.go.erb index b1c17157506f..91be654ed24a 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_router_nat_test.go.erb +++ b/mmv1/third_party/terraform/services/compute/resource_compute_router_nat_test.go.erb @@ -574,6 +574,28 @@ func TestAccComputeRouterNat_withPrivateNatAndEmptyAction(t *testing.T) { }) } +func TestAccComputeRouterNat_AutoNetworkTier(t *testing.T) { + t.Parallel() + + testId := acctest.RandString(t, 10) + routerName := fmt.Sprintf("tf-test-router-private-nat-%s", testId) + hubName := fmt.Sprintf("%s-hub", routerName) + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckComputeRouterNatDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccComputeRouterNatWitAutoNetworkTier(routerName, hubName), + ResourceName: "google_compute_router_nat.foobar", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func TestAccComputeRouterNat_withPrivateNatAndEmptyActionActiveRanges(t *testing.T) { t.Parallel() @@ -1606,7 +1628,7 @@ resource "google_compute_router_nat" "foobar" { router = google_compute_router.foobar.name region = google_compute_router.foobar.region source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS" - type = "PRIVATE" + type = "PRIVATE" enable_dynamic_port_allocation = false enable_endpoint_independent_mapping = false min_ports_per_vm = 32 @@ -1692,7 +1714,7 @@ resource "google_compute_router_nat" "foobar" { router = google_compute_router.foobar.name region = google_compute_router.foobar.region source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS" - type = "PRIVATE" + type = "PRIVATE" enable_dynamic_port_allocation = false enable_endpoint_independent_mapping = false min_ports_per_vm = 32 @@ -1713,7 +1735,7 @@ resource "google_compute_router_nat" "foobar" { router = google_compute_router.foobar.name region = google_compute_router.foobar.region source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS" - type = "PRIVATE" + type = "PRIVATE" enable_dynamic_port_allocation = false enable_endpoint_independent_mapping = false min_ports_per_vm = 32 @@ -1744,7 +1766,7 @@ resource "google_compute_router_nat" "foobar" { router = google_compute_router.foobar.name region = google_compute_router.foobar.region source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS" - type = "PRIVATE" + type = "PRIVATE" enable_dynamic_port_allocation = false enable_endpoint_independent_mapping = false min_ports_per_vm = 32 @@ -1772,7 +1794,7 @@ resource "google_compute_router_nat" "foobar" { router = google_compute_router.foobar.name region = google_compute_router.foobar.region source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS" - type = "PRIVATE" + type = "PRIVATE" enable_dynamic_port_allocation = false enable_endpoint_independent_mapping = false min_ports_per_vm = 32 @@ -1793,4 +1815,21 @@ resource "google_compute_router_nat" "foobar" { `, testAccComputeRouterNatBaseResourcesWithPrivateNatSubnetworks(routerName, hubName), routerName, ruleNumber, ruleDescription, match) } -<% end -%> \ No newline at end of file +func testAccComputeRouterNatWitAutoNetworkTier(routerName, hubName string) string { + return fmt.Sprintf(` +%s + +resource "google_compute_router_nat" "foobar" { + name = "%s" + router = google_compute_router.foobar.name + region = google_compute_router.foobar.region + + source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES" + nat_ip_allocate_option = "AUTO_ONLY" + auto_network_tier = "PREMIUM" +} +`, testAccComputeRouterNatBaseResourcesWithPrivateNatSubnetworks(routerName, hubName), routerName) +} + + +<% end -%> From b7c356db7e87e617058179ac447d2ba24fae19f4 Mon Sep 17 00:00:00 2001 From: "Max W. Portocarrero" Date: Tue, 23 Apr 2024 09:03:23 -0500 Subject: [PATCH 4/6] fixed tests and changed to default_from_api --- mmv1/products/compute/RouterNat.yaml | 5 +++-- .../services/compute/resource_compute_router_nat_test.go.erb | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/mmv1/products/compute/RouterNat.yaml b/mmv1/products/compute/RouterNat.yaml index 2ea99e299cce..f3bf3eb3fdb8 100644 --- a/mmv1/products/compute/RouterNat.yaml +++ b/mmv1/products/compute/RouterNat.yaml @@ -107,7 +107,7 @@ custom_code: !ruby/object:Provider::Terraform::CustomCode constants: 'templates/terraform/constants/router_nat.go.erb' pre_create: 'templates/terraform/constants/router_nat_validate_action_active_range.go.erb' pre_update: 'templates/terraform/constants/router_nat_validate_action_active_range.go.erb' - encoder: 'templates/terraform/encoders/router_nat.go.erb' + # encoder: 'templates/terraform/encoders/router_nat.go.erb' custom_diff: [ 'resourceComputeRouterNatDrainNatIpsCustomDiff', ] @@ -456,5 +456,6 @@ properties: Must be one of: PREMIUM, STANDARD. If not specified, then the current project-level default tier is used. values: - - :PREMIUN + - :PREMIUM - :STANDARD + default_from_api: true diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_router_nat_test.go.erb b/mmv1/third_party/terraform/services/compute/resource_compute_router_nat_test.go.erb index 0177f781f7bd..de5f38b706d3 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_router_nat_test.go.erb +++ b/mmv1/third_party/terraform/services/compute/resource_compute_router_nat_test.go.erb @@ -484,6 +484,9 @@ func TestAccComputeRouterNat_AutoNetworkTier(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccComputeRouterNatWitAutoNetworkTier(routerName, hubName), + }, + { + // implicitly full ImportStateId ResourceName: "google_compute_router_nat.foobar", ImportState: true, ImportStateVerify: true, From f5e68de666cf30eeef9339387e896eb216b6e75e Mon Sep 17 00:00:00 2001 From: "Max W. Portocarrero" Date: Wed, 8 May 2024 10:29:45 -0500 Subject: [PATCH 5/6] moved private nat subnetwork test --- .../resource_compute_router_nat_test.go.erb | 78 +++++++++---------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_router_nat_test.go.erb b/mmv1/third_party/terraform/services/compute/resource_compute_router_nat_test.go.erb index de5f38b706d3..f638bab4861e 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_router_nat_test.go.erb +++ b/mmv1/third_party/terraform/services/compute/resource_compute_router_nat_test.go.erb @@ -1698,45 +1698,6 @@ resource "google_compute_router_nat" "foobar" { `, routerName, routerName, routerName, routerName) } -<% unless version == "ga" -%> -func testAccComputeRouterNatPrivateType(routerName string) string { - return fmt.Sprintf(` -resource "google_compute_network" "foobar" { - name = "%s-net" - auto_create_subnetworks = false -} - -resource "google_compute_subnetwork" "foobar" { - name = "%s-subnet" - network = google_compute_network.foobar.self_link - ip_cidr_range = "10.0.0.0/16" - region = "us-central1" - purpose = "PRIVATE_NAT" -} - -resource "google_compute_router" "foobar" { - name = "%s" - region = google_compute_subnetwork.foobar.region - network = google_compute_network.foobar.self_link -} - -resource "google_compute_router_nat" "foobar" { - name = "%s" - router = google_compute_router.foobar.name - region = google_compute_router.foobar.region - source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS" - type = "PRIVATE" - enable_dynamic_port_allocation = false - enable_endpoint_independent_mapping = false - min_ports_per_vm = 32 - subnetwork { - name = google_compute_subnetwork.foobar.id - source_ip_ranges_to_nat = ["ALL_IP_RANGES"] - } -} -`, routerName, routerName, routerName, routerName) -} - func testAccComputeRouterNatBaseResourcesWithPrivateNatSubnetworks(routerName, hubName string) string { return fmt.Sprintf(` resource "google_compute_network" "foobar" { @@ -1805,6 +1766,45 @@ resource "google_compute_router" "foobar" { `, routerName, routerName, routerName, routerName, routerName, hubName, routerName, routerName) } +<% unless version == "ga" -%> +func testAccComputeRouterNatPrivateType(routerName string) string { + return fmt.Sprintf(` +resource "google_compute_network" "foobar" { + name = "%s-net" + auto_create_subnetworks = false +} + +resource "google_compute_subnetwork" "foobar" { + name = "%s-subnet" + network = google_compute_network.foobar.self_link + ip_cidr_range = "10.0.0.0/16" + region = "us-central1" + purpose = "PRIVATE_NAT" +} + +resource "google_compute_router" "foobar" { + name = "%s" + region = google_compute_subnetwork.foobar.region + network = google_compute_network.foobar.self_link +} + +resource "google_compute_router_nat" "foobar" { + name = "%s" + router = google_compute_router.foobar.name + region = google_compute_router.foobar.region + source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS" + type = "PRIVATE" + enable_dynamic_port_allocation = false + enable_endpoint_independent_mapping = false + min_ports_per_vm = 32 + subnetwork { + name = google_compute_subnetwork.foobar.id + source_ip_ranges_to_nat = ["ALL_IP_RANGES"] + } +} +`, routerName, routerName, routerName, routerName) +} + func testAccComputeRouterNatRulesBasic_privateNatOmitRules(routerName, hubName string) string { return fmt.Sprintf(` %s From a50c5908e60f683d07edde3cf5744bf69248c118 Mon Sep 17 00:00:00 2001 From: "Max W. Portocarrero" Date: Wed, 8 May 2024 10:31:51 -0500 Subject: [PATCH 6/6] removed commented encoder logic --- mmv1/products/compute/RouterNat.yaml | 1 - .../terraform/encoders/router_nat.go.erb | 32 ------------------- 2 files changed, 33 deletions(-) delete mode 100644 mmv1/templates/terraform/encoders/router_nat.go.erb diff --git a/mmv1/products/compute/RouterNat.yaml b/mmv1/products/compute/RouterNat.yaml index f3bf3eb3fdb8..864939964063 100644 --- a/mmv1/products/compute/RouterNat.yaml +++ b/mmv1/products/compute/RouterNat.yaml @@ -107,7 +107,6 @@ custom_code: !ruby/object:Provider::Terraform::CustomCode constants: 'templates/terraform/constants/router_nat.go.erb' pre_create: 'templates/terraform/constants/router_nat_validate_action_active_range.go.erb' pre_update: 'templates/terraform/constants/router_nat_validate_action_active_range.go.erb' - # encoder: 'templates/terraform/encoders/router_nat.go.erb' custom_diff: [ 'resourceComputeRouterNatDrainNatIpsCustomDiff', ] diff --git a/mmv1/templates/terraform/encoders/router_nat.go.erb b/mmv1/templates/terraform/encoders/router_nat.go.erb deleted file mode 100644 index d2154454e102..000000000000 --- a/mmv1/templates/terraform/encoders/router_nat.go.erb +++ /dev/null @@ -1,32 +0,0 @@ -<%# The license inside this block applies to this file. - # Copyright 2020 Google Inc. - # Licensed under the Apache License, Version 2.0 (the "License"); - # you may not use this file except in compliance with the License. - # You may obtain a copy of the License at - # - # http://www.apache.org/licenses/LICENSE-2.0 - # - # Unless required by applicable law or agreed to in writing, software - # distributed under the License is distributed on an "AS IS" BASIS, - # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - # See the License for the specific language governing permissions and - # limitations under the License. --%> -config := meta.(*transport_tpg.Config) -userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) -if err != nil { - return nil, err -} - -projectID, err := tpgresource.GetProject(d, meta.(*transport_tpg.Config)) -if err != nil { - return nil, err -} - -project, err := config.NewComputeClient(userAgent).Projects.Get(projectID).Do() -if err != nil { - return nil, transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("Project data for project %q", projectID)) -} - -obj["autoNetworkTier"] = project.DefaultNetworkTier -return obj, nil