From 64b6bce14b45e85f76ffe0a282e7020477708bd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Niesiob=C4=99dzki?= Date: Sat, 6 Jan 2024 15:39:51 +0000 Subject: [PATCH] Use fixtures in net-lb-ext --- modules/net-lb-ext/README.md | 88 +++---------------- tests/fixtures/compute-mig.tf | 42 +++++++++ .../net_lb_ext/examples/dual_stack.yaml | 2 +- .../modules/net_lb_ext/examples/ext_migs.yaml | 4 +- .../net_lb_ext/examples/fwd_rules.yaml | 4 +- tests/modules/net_lb_ext/examples/migs.yaml | 3 +- 6 files changed, 62 insertions(+), 81 deletions(-) create mode 100644 tests/fixtures/compute-mig.tf diff --git a/modules/net-lb-ext/README.md b/modules/net-lb-ext/README.md index 674848cccc..67e5c7bd1e 100644 --- a/modules/net-lb-ext/README.md +++ b/modules/net-lb-ext/README.md @@ -13,42 +13,13 @@ This module allows managing a GCE Network Load Balancer and integrates the forwa This example shows how to reference existing Managed Infrastructure Groups (MIGs). ```hcl -module "instance_template" { - source = "./fabric/modules/compute-vm" - project_id = var.project_id - zone = "${var.region}-b" - name = "vm-test" - create_template = true - service_account = { - auto_create = true - } - network_interfaces = [ - { - network = var.vpc.self_link - subnetwork = var.subnet.self_link - } - ] - tags = [ - "http-server" - ] -} - -module "mig" { - source = "./fabric/modules/compute-mig" - project_id = var.project_id - location = var.region - name = "mig-test" - target_size = 1 - instance_template = module.instance_template.template.self_link -} - module "nlb" { source = "./fabric/modules/net-lb-ext" project_id = var.project_id region = var.region name = "nlb-test" backends = [{ - group = module.mig.group_manager.instance_group + group = module.compute-mig.group_manager.instance_group }] health_check_config = { http = { @@ -56,7 +27,7 @@ module "nlb" { } } } -# tftest modules=3 resources=6 inventory=migs.yaml e2e +# tftest modules=3 resources=5 fixtures=fixtures/compute-mig.tf inventory=migs.yaml e2e ``` ### Externally managed instances @@ -64,18 +35,6 @@ module "nlb" { This examples shows how to create an NLB by combining externally managed instances (in a custom module or even outside of the current root module) in an unmanaged group. When using internally managed groups, remember to run `terraform apply` each time group instances change. ```hcl -module "instance" { - source = "./fabric/modules/compute-vm" - for_each = toset(["b", "c"]) - name = "instance-${each.key}" - project_id = var.project_id - zone = "${var.region}-b" - network_interfaces = [{ - network = var.vpc.self_link - subnetwork = var.subnet.self_link - }] -} - module "nlb" { source = "./fabric/modules/net-lb-ext" project_id = var.project_id @@ -85,7 +44,7 @@ module "nlb" { my-group = { zone = "${var.region}-b" instances = [ - for z in ["b", "c"] : module.instance[z].id + module.compute-vm-group-b.id, ] } } @@ -98,10 +57,10 @@ module "nlb" { } } } -# tftest modules=3 resources=6 inventory=ext_migs.yaml e2e +# tftest modules=3 resources=8 fixtures=fixtures/compute-vm-group-bc.tf inventory=ext_migs.yaml e2e ``` -### Mutiple forwarding rules +### Multiple forwarding rules You can add more forwarding rules to your load balancer and override some forwarding rules defaults, including the global access policy, the IP protocol, the IP version and ports. @@ -111,18 +70,6 @@ The example adds two forwarding rules: - the second one, called `nlb-test-vip-two` exposes an IPv4 address, it listens on port 80 and allows connections from the same region only. ```hcl -module "instance" { - source = "./fabric/modules/compute-vm" - for_each = toset(["b", "c"]) - name = "instance-${each.key}" - project_id = var.project_id - zone = "${var.region}-b" - network_interfaces = [{ - network = var.vpc.self_link - subnetwork = var.subnet.self_link - }] -} - module "nlb" { source = "./fabric/modules/net-lb-ext" project_id = var.project_id @@ -141,12 +88,12 @@ module "nlb" { my-group = { zone = "${var.region}-b" instances = [ - for z in ["b", "c"] : module.instance[z].id + module.compute-vm-group-b.id, ] } } } -# tftest modules=3 resources=7 inventory=fwd_rules.yaml e2e +# tftest modules=3 resources=9 fixtures=fixtures/compute-vm-group-bc.tf inventory=fwd_rules.yaml e2e ``` ### Dual stack (IPv4 and IPv6) @@ -155,18 +102,6 @@ Your load balancer can use a combination of either or both IPv4 and IPv6 forward In this example we set the load balancer to work as dual stack, meaning it exposes both an IPv4 and an IPv6 address. ```hcl -module "instance" { - source = "./fabric/modules/compute-vm" - for_each = toset(["b", "c"]) - name = "instance-${each.key}" - project_id = var.project_id - zone = "${var.region}-b" - network_interfaces = [{ - network = var.vpc.self_link - subnetwork = var.subnet.self_link - }] -} - module "nlb" { source = "./fabric/modules/net-lb-ext" project_id = var.project_id @@ -187,12 +122,12 @@ module "nlb" { my-group = { zone = "${var.region}-b" instances = [ - for z in ["b", "c"] : module.instance[z].id + module.compute-vm-group-b.id, ] } } } -# tftest modules=3 resources=7 inventory=dual_stack.yaml e2e +# tftest modules=3 resources=9 fixtures=fixtures/compute-vm-group-bc.tf inventory=dual_stack.yaml e2e ``` ### End to end example @@ -293,4 +228,9 @@ module "nlb" { | [health_check_id](outputs.tf#L70) | Auto-created health-check id. | | | [health_check_self_link](outputs.tf#L75) | Auto-created health-check self link. | | | [id](outputs.tf#L80) | Fully qualified forwarding rule ids. | | + +## Fixtures + +- [compute-mig.tf](../../tests/fixtures/compute-mig.tf) +- [compute-vm-group-bc.tf](../../tests/fixtures/compute-vm-group-bc.tf) diff --git a/tests/fixtures/compute-mig.tf b/tests/fixtures/compute-mig.tf new file mode 100644 index 0000000000..bf7d6a0a69 --- /dev/null +++ b/tests/fixtures/compute-mig.tf @@ -0,0 +1,42 @@ +# Copyright 2024 Google LLC +# +# 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. + +module "_instance-template" { + source = "./fabric/modules/compute-vm" + project_id = var.project_id + name = "mig-e2e-template" + zone = "${var.region}-b" + tags = ["http-server", "ssh"] + network_interfaces = [{ + network = var.vpc.self_link + subnetwork = var.subnet.self_link + nat = false + addresses = null + }] + boot_disk = { + initialize_params = { + image = "projects/cos-cloud/global/images/family/cos-stable" + } + } + create_template = true +} + +module "compute-mig" { + source = "./fabric/modules/compute-mig" + project_id = var.project_id + location = "${var.region}-b" + name = "mig-e2e-tests" + # target_size = 0 + instance_template = module._instance-template.template.self_link +} diff --git a/tests/modules/net_lb_ext/examples/dual_stack.yaml b/tests/modules/net_lb_ext/examples/dual_stack.yaml index d1f4f9d19e..370b215a31 100644 --- a/tests/modules/net_lb_ext/examples/dual_stack.yaml +++ b/tests/modules/net_lb_ext/examples/dual_stack.yaml @@ -100,4 +100,4 @@ counts: google_compute_region_backend_service: 1 google_compute_region_health_check: 1 modules: 3 - resources: 7 + resources: 9 diff --git a/tests/modules/net_lb_ext/examples/ext_migs.yaml b/tests/modules/net_lb_ext/examples/ext_migs.yaml index 6fc686d2b8..c4f5b182fc 100644 --- a/tests/modules/net_lb_ext/examples/ext_migs.yaml +++ b/tests/modules/net_lb_ext/examples/ext_migs.yaml @@ -86,8 +86,8 @@ values: counts: google_compute_forwarding_rule: 1 - google_compute_instance_group: 1 + google_compute_instance_group: 3 google_compute_region_backend_service: 1 google_compute_region_health_check: 1 modules: 3 - resources: 6 + resources: 8 diff --git a/tests/modules/net_lb_ext/examples/fwd_rules.yaml b/tests/modules/net_lb_ext/examples/fwd_rules.yaml index 73321ec3c9..3c1fafdc5a 100644 --- a/tests/modules/net_lb_ext/examples/fwd_rules.yaml +++ b/tests/modules/net_lb_ext/examples/fwd_rules.yaml @@ -105,8 +105,8 @@ values: counts: google_compute_forwarding_rule: 2 - google_compute_instance_group: 1 + google_compute_instance_group: 3 google_compute_region_backend_service: 1 google_compute_region_health_check: 1 modules: 3 - resources: 7 + resources: 9 diff --git a/tests/modules/net_lb_ext/examples/migs.yaml b/tests/modules/net_lb_ext/examples/migs.yaml index 9c6fde6aba..f156774796 100644 --- a/tests/modules/net_lb_ext/examples/migs.yaml +++ b/tests/modules/net_lb_ext/examples/migs.yaml @@ -81,6 +81,5 @@ counts: google_compute_forwarding_rule: 1 google_compute_region_backend_service: 1 google_compute_region_health_check: 1 - google_service_account: 1 modules: 3 - resources: 6 + resources: 5