|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +package compute_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" |
| 10 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccDataSourceGoogleComputeRegionInstanceGroupManager(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + regionName := "us-central1" |
| 17 | + igmName := "tf-test-igm" + acctest.RandString(t, 6) |
| 18 | + |
| 19 | + context := map[string]interface{}{ |
| 20 | + "regionName": regionName, |
| 21 | + "igmName": igmName, |
| 22 | + "baseName": "tf-test-igm-base" + acctest.RandString(t, 6), |
| 23 | + "poolName": "tf-test-pool" + acctest.RandString(t, 6), |
| 24 | + "templateName": "tf-test-templt" + acctest.RandString(t, 6), |
| 25 | + "autoHealName": "tf-test-ah-name" + acctest.RandString(t, 6), |
| 26 | + } |
| 27 | + |
| 28 | + acctest.VcrTest(t, resource.TestCase{ |
| 29 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 30 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 31 | + Steps: []resource.TestStep{ |
| 32 | + { |
| 33 | + Config: testAccDataSourceGoogleComputeRegionInstanceGroupManager_usingSelfLink(context), |
| 34 | + Check: resource.ComposeTestCheckFunc( |
| 35 | + resource.TestCheckResourceAttr("data.google_compute_region_instance_group_manager.data_source", "project", envvar.GetTestProjectFromEnv()), |
| 36 | + resource.TestCheckResourceAttr("data.google_compute_region_instance_group_manager.data_source", "region", regionName), |
| 37 | + resource.TestCheckResourceAttr("data.google_compute_region_instance_group_manager.data_source", "name", igmName)), |
| 38 | + }, |
| 39 | + { |
| 40 | + Config: testAccDataSourceGoogleComputeRegionInstanceGroupManager_usingNameAndRegion(context), |
| 41 | + Check: resource.ComposeTestCheckFunc( |
| 42 | + resource.TestCheckResourceAttr("data.google_compute_region_instance_group_manager.data_source", "project", envvar.GetTestProjectFromEnv()), |
| 43 | + resource.TestCheckResourceAttr("data.google_compute_region_instance_group_manager.data_source", "region", regionName), |
| 44 | + resource.TestCheckResourceAttr("data.google_compute_region_instance_group_manager.data_source", "name", igmName)), |
| 45 | + }, |
| 46 | + }, |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +func testAccDataSourceGoogleComputeRegionInstanceGroupManager_usingSelfLink(context map[string]interface{}) string { |
| 51 | + return acctest.Nprintf(` |
| 52 | + resource "google_compute_health_check" "autohealing" { |
| 53 | + name = "%{autoHealName}" |
| 54 | + check_interval_sec = 5 |
| 55 | + timeout_sec = 5 |
| 56 | + healthy_threshold = 2 |
| 57 | + unhealthy_threshold = 10 # 50 seconds |
| 58 | +
|
| 59 | + http_health_check { |
| 60 | + request_path = "/healthz" |
| 61 | + port = "8080" |
| 62 | + } |
| 63 | + } |
| 64 | +
|
| 65 | + resource "google_compute_region_instance_group_manager" "appserver" { |
| 66 | + name = "%{igmName}" |
| 67 | + base_instance_name = "%{baseName}" |
| 68 | + region = "us-central1" |
| 69 | +
|
| 70 | + version { |
| 71 | + instance_template = google_compute_instance_template.igm-basic.id |
| 72 | + name = "primary" |
| 73 | + } |
| 74 | +
|
| 75 | + target_pools = [google_compute_target_pool.igm-basic.id] |
| 76 | + target_size = 2 |
| 77 | +
|
| 78 | + named_port { |
| 79 | + name = "customhttp" |
| 80 | + port = 8888 |
| 81 | + } |
| 82 | +
|
| 83 | + auto_healing_policies { |
| 84 | + health_check = google_compute_health_check.autohealing.id |
| 85 | + initial_delay_sec = 300 |
| 86 | + } |
| 87 | + } |
| 88 | +
|
| 89 | + data "google_compute_region_instance_group_manager" "data_source" { |
| 90 | + self_link = google_compute_region_instance_group_manager.appserver.instance_group |
| 91 | + } |
| 92 | +
|
| 93 | + resource "google_compute_target_pool" "igm-basic" { |
| 94 | + description = "Resource created for Terraform acceptance testing" |
| 95 | + name = "%{poolName}" |
| 96 | + session_affinity = "CLIENT_IP_PROTO" |
| 97 | + } |
| 98 | +
|
| 99 | + data "google_compute_image" "my_image" { |
| 100 | + family = "debian-11" |
| 101 | + project = "debian-cloud" |
| 102 | + } |
| 103 | +
|
| 104 | + resource "google_compute_instance_template" "igm-basic" { |
| 105 | + name = "%{templateName}" |
| 106 | + machine_type = "e2-medium" |
| 107 | + can_ip_forward = false |
| 108 | + tags = ["foo", "bar"] |
| 109 | +
|
| 110 | + disk { |
| 111 | + source_image = data.google_compute_image.my_image.self_link |
| 112 | + auto_delete = true |
| 113 | + boot = true |
| 114 | + } |
| 115 | +
|
| 116 | + network_interface { |
| 117 | + network = "default" |
| 118 | + } |
| 119 | +
|
| 120 | + service_account { |
| 121 | + scopes = ["userinfo-email", "compute-ro", "storage-ro"] |
| 122 | + } |
| 123 | + }`, context) |
| 124 | +} |
| 125 | + |
| 126 | +func testAccDataSourceGoogleComputeRegionInstanceGroupManager_usingNameAndRegion(context map[string]interface{}) string { |
| 127 | + return acctest.Nprintf(` |
| 128 | + resource "google_compute_health_check" "autohealing" { |
| 129 | + name = "%{autoHealName}" |
| 130 | + check_interval_sec = 5 |
| 131 | + timeout_sec = 5 |
| 132 | + healthy_threshold = 2 |
| 133 | + unhealthy_threshold = 10 # 50 seconds |
| 134 | +
|
| 135 | + http_health_check { |
| 136 | + request_path = "/healthz" |
| 137 | + port = "8080" |
| 138 | + } |
| 139 | + } |
| 140 | +
|
| 141 | + resource "google_compute_region_instance_group_manager" "appserver" { |
| 142 | + name = "%{igmName}" |
| 143 | + base_instance_name = "%{baseName}" |
| 144 | + region = "us-central1" |
| 145 | +
|
| 146 | + version { |
| 147 | + instance_template = google_compute_instance_template.igm-basic.id |
| 148 | + name = "primary" |
| 149 | + } |
| 150 | +
|
| 151 | + target_pools = [google_compute_target_pool.igm-basic.id] |
| 152 | + target_size = 2 |
| 153 | +
|
| 154 | + named_port { |
| 155 | + name = "customhttp" |
| 156 | + port = 8888 |
| 157 | + } |
| 158 | +
|
| 159 | + auto_healing_policies { |
| 160 | + health_check = google_compute_health_check.autohealing.id |
| 161 | + initial_delay_sec = 300 |
| 162 | + } |
| 163 | + } |
| 164 | +
|
| 165 | + data "google_compute_region_instance_group_manager" "data_source" { |
| 166 | + name = "%{igmName}" |
| 167 | + region = "us-central1" |
| 168 | + } |
| 169 | +
|
| 170 | + resource "google_compute_target_pool" "igm-basic" { |
| 171 | + description = "Resource created for Terraform acceptance testing" |
| 172 | + name = "%{poolName}" |
| 173 | + session_affinity = "CLIENT_IP_PROTO" |
| 174 | + } |
| 175 | +
|
| 176 | + data "google_compute_image" "my_image" { |
| 177 | + family = "debian-11" |
| 178 | + project = "debian-cloud" |
| 179 | + } |
| 180 | +
|
| 181 | + resource "google_compute_instance_template" "igm-basic" { |
| 182 | + name = "%{templateName}" |
| 183 | + machine_type = "e2-medium" |
| 184 | + can_ip_forward = false |
| 185 | + tags = ["foo", "bar"] |
| 186 | +
|
| 187 | + disk { |
| 188 | + source_image = data.google_compute_image.my_image.self_link |
| 189 | + auto_delete = true |
| 190 | + boot = true |
| 191 | + } |
| 192 | +
|
| 193 | + network_interface { |
| 194 | + network = "default" |
| 195 | + } |
| 196 | +
|
| 197 | + service_account { |
| 198 | + scopes = ["userinfo-email", "compute-ro", "storage-ro"] |
| 199 | + } |
| 200 | + }`, context) |
| 201 | +} |
0 commit comments