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 example and test to showcase count increase/decrease for backend service #13531

Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 11 additions & 0 deletions mmv1/products/compute/RegionBackendService.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ kind: 'compute#backendService'
description: |
A Region Backend Service defines a regionally-scoped group of virtual
machines that will serve traffic for load balancing.

~> **Note:** Recreating a `google_compute_region_backend_service` that references other dependant resources like `google_compute_instance_group` will give a `resourceInUseByAnotherResource` error.
Use `lifecycle.create_before_destroy` on this other resources to avoid this type of error as shown in the Dynamic Backend Count example.
references:
guides:
'Internal TCP/UDP Load Balancing': 'https://cloud.google.com/compute/docs/load-balancing/internal/'
Expand Down Expand Up @@ -128,6 +131,14 @@ examples:
default_neg_name: 'network-endpoint'
health_check_name: 'rbs-health-check'
network_name: 'network'
- name: 'region_backend_service_dynamic_backend_count'
primary_resource_id: 'default'
vars:
region_backend_service_name: 'region-service'
health_check_name: 'health-check'
instance_group_name: 'instance_group'
network_name: 'network'
exclude_test: true
parameters:
- name: 'region'
type: ResourceRef
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
locals {
zones = ["europe-west1-b", "europe-west1-c", "europe-west1-d"]
s1_count = 3
}

resource "google_compute_network" "network" {
name = "{{index $.Vars "network_name"}}"
}

resource "google_compute_region_backend_service" "default" {
name = "{{index $.Vars "region_backend_service_name"}}"
region = "europe-west1"

dynamic "backend" {
for_each = google_compute_instance_group.s1
content {
balancing_mode = "CONNECTION"
group = backend.value.self_link
}
}

health_checks = [
google_compute_health_check.default.self_link,
]
}

resource "google_compute_health_check" "default" {
name = "{{index $.Vars "health_check_name"}}"
tcp_health_check {
port = "80"
}
}

resource "google_compute_instance_group" "s1" {
count = local.s1_count
name = "{{index $.Vars "instance_group_name"}}-${count.index}"
zone = element(local.zones, count.index)
network = google_compute_network.network.self_link

lifecycle {
create_before_destroy = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,145 @@ func TestAccComputeRegionBackendService_withLogConfig(t *testing.T) {
})
}

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

randString := acctest.RandString(t, 10)

serviceName := fmt.Sprintf("tf-test-%s", randString)
checkName := fmt.Sprintf("tf-test-%s", randString)
netName := fmt.Sprintf("tf-test-%s", randString)
igName := fmt.Sprintf("tf-test-%s", randString)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeRegionBackendServiceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeRegionBackendService_withDynamicBackendCount(serviceName, netName, checkName, igName),
},
{
ResourceName: "google_compute_region_backend_service.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeRegionBackendService_withDynamicBackendCountUpdate(serviceName, netName, checkName, igName),
},
{
ResourceName: "google_compute_region_backend_service.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeRegionBackendService_withDynamicBackendCount(serviceName, netName, checkName, igName),
},
{
ResourceName: "google_compute_region_backend_service.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccComputeRegionBackendService_withDynamicBackendCount(serviceName, netName, hcName, igName string) string {
return fmt.Sprintf(`
locals {
zones = ["europe-west1-b", "europe-west1-c", "europe-west1-d"]
s1_count = 3
}

resource "google_compute_network" "network" {
name = "%s"
}

resource "google_compute_region_backend_service" "foobar" {
name = "%s"
region = "europe-west1"

dynamic "backend" {
for_each = google_compute_instance_group.s1
content {
balancing_mode = "CONNECTION"
group = backend.value.self_link
}
}

health_checks = [
google_compute_health_check.default.self_link,
]
}

resource "google_compute_health_check" "default" {
name = "%s"
tcp_health_check {
port = "80"
}
}

resource "google_compute_instance_group" "s1" {
count = local.s1_count
name = "%s-${count.index}"
zone = element(local.zones, count.index)
network = google_compute_network.network.self_link

lifecycle {
create_before_destroy = true
}
}
`, netName, serviceName, hcName, igName)
}

func testAccComputeRegionBackendService_withDynamicBackendCountUpdate(serviceName, netName, hcName, igName string) string {
return fmt.Sprintf(`
locals {
zones = ["europe-west1-b", "europe-west1-c", "europe-west1-d"]
s1_count = 1
}

resource "google_compute_network" "network" {
name = "%s"
}

resource "google_compute_region_backend_service" "foobar" {
name = "%s"
region = "europe-west1"

dynamic "backend" {
for_each = google_compute_instance_group.s1
content {
balancing_mode = "CONNECTION"
group = backend.value.self_link
}
}

health_checks = [
google_compute_health_check.default.self_link,
]
}

resource "google_compute_health_check" "default" {
name = "%s"
tcp_health_check {
port = "80"
}
}

resource "google_compute_instance_group" "s1" {
count = local.s1_count
name = "%s-${count.index}"
zone = element(local.zones, count.index)
network = google_compute_network.network.self_link

lifecycle {
create_before_destroy = true
}
}
`, netName, serviceName, hcName, igName)
}

func testAccComputeRegionBackendService_ilbBasic_withUnspecifiedProtocol(serviceName, checkName string) string {
return fmt.Sprintf(`
resource "google_compute_region_backend_service" "foobar" {
Expand Down
Loading