|
17 | 17 | package backupdr_test
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "fmt" |
| 21 | + "strconv" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
20 | 25 | "github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
| 26 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
21 | 27 | "github.com/hashicorp/terraform-provider-google/google/acctest"
|
22 |
| - "testing" |
23 | 28 | )
|
24 | 29 |
|
25 | 30 | func TestAccDataSourceGoogleBackupDRBackupPlanAssociation_basic(t *testing.T) {
|
@@ -137,3 +142,168 @@ data "google_backup_dr_backup_plan_association" "bpa-test" {
|
137 | 142 | }
|
138 | 143 | `, context)
|
139 | 144 | }
|
| 145 | + |
| 146 | +func TestAccDataSourceGoogleBackupDRBackupPlanAssociations(t *testing.T) { |
| 147 | + t.Parallel() |
| 148 | + context := map[string]interface{}{ |
| 149 | + "random_suffix": acctest.RandString(t, 10), |
| 150 | + "bpa_id": "tf-test-bpa-plural-" + acctest.RandString(t, 10), |
| 151 | + } |
| 152 | + |
| 153 | + acctest.VcrTest(t, resource.TestCase{ |
| 154 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 155 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 156 | + Steps: []resource.TestStep{ |
| 157 | + { |
| 158 | + Config: testAccDataSourceGoogleBackupDRBackupPlanAssociations_config(context), |
| 159 | + Check: testAccCheckBackupPlanAssociationInList( |
| 160 | + "data.google_backup_dr_backup_plan_associations.bpas", |
| 161 | + "google_compute_instance.default", |
| 162 | + "google_backup_dr_backup_plan.foo1", |
| 163 | + "data.google_project.project", |
| 164 | + ), |
| 165 | + }, |
| 166 | + }, |
| 167 | + }) |
| 168 | +} |
| 169 | + |
| 170 | +func testAccCheckBackupPlanAssociationInList(dataSourceName, instanceName, backupPlanName, projectDsName string) resource.TestCheckFunc { |
| 171 | + return func(s *terraform.State) error { |
| 172 | + ds, ok := s.RootModule().Resources[dataSourceName] |
| 173 | + if !ok { |
| 174 | + return fmt.Errorf("data source not found: %s", dataSourceName) |
| 175 | + } |
| 176 | + |
| 177 | + instance, ok := s.RootModule().Resources[instanceName] |
| 178 | + if !ok { |
| 179 | + return fmt.Errorf("instance resource not found: %s", instanceName) |
| 180 | + } |
| 181 | + |
| 182 | + backupPlan, ok := s.RootModule().Resources[backupPlanName] |
| 183 | + if !ok { |
| 184 | + return fmt.Errorf("backup plan resource not found: %s", backupPlanName) |
| 185 | + } |
| 186 | + backupPlanNameFromState := backupPlan.Primary.Attributes["name"] |
| 187 | + |
| 188 | + project, ok := s.RootModule().Resources[projectDsName] |
| 189 | + if !ok { |
| 190 | + return fmt.Errorf("project data source not found: %s", projectDsName) |
| 191 | + } |
| 192 | + projectID := project.Primary.Attributes["project_id"] |
| 193 | + projectNumber := project.Primary.Attributes["number"] |
| 194 | + |
| 195 | + fmt.Printf("\n--- Performing Direct Association Check ---\n") |
| 196 | + |
| 197 | + // 1. Reconstruct the 'resource' string using the project NUMBER and instance ID |
| 198 | + // to match the format returned by the BackupDR API. |
| 199 | + instanceID := instance.Primary.Attributes["instance_id"] |
| 200 | + zone := instance.Primary.Attributes["zone"] |
| 201 | + expectedResource := fmt.Sprintf("projects/%s/zones/%s/instances/%s", projectNumber, zone, instanceID) |
| 202 | + fmt.Printf("Expected Resource (constructed): %s\n", expectedResource) |
| 203 | + |
| 204 | + // 2. Normalize the backup plan name to also use the project NUMBER. |
| 205 | + expectedBackupPlan := strings.Replace(backupPlanNameFromState, "projects/"+projectID, "projects/"+projectNumber, 1) |
| 206 | + fmt.Printf("Expected Backup Plan (normalized): %s\n", expectedBackupPlan) |
| 207 | + |
| 208 | + associationsCount, _ := strconv.Atoi(ds.Primary.Attributes["associations.#"]) |
| 209 | + fmt.Printf("Total associations found by data source: %d\n", associationsCount) |
| 210 | + |
| 211 | + for i := 0; i < associationsCount; i++ { |
| 212 | + resourceAttr := ds.Primary.Attributes[fmt.Sprintf("associations.%d.resource", i)] |
| 213 | + backupPlanAttr := ds.Primary.Attributes[fmt.Sprintf("associations.%d.backup_plan", i)] |
| 214 | + |
| 215 | + fmt.Printf("Found Association #%d: Resource='%s', BackupPlan='%s'\n", i, resourceAttr, backupPlanAttr) |
| 216 | + |
| 217 | + if resourceAttr == expectedResource && backupPlanAttr == expectedBackupPlan { |
| 218 | + fmt.Println("--- Match found! Test successful. ---") |
| 219 | + return nil |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + fmt.Println("--- No match found after checking all associations. ---") |
| 224 | + return fmt.Errorf("no matching backup plan association found in data source '%s' for resource '%s'", dataSourceName, expectedResource) |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +func testAccDataSourceGoogleBackupDRBackupPlanAssociations_config(context map[string]interface{}) string { |
| 229 | + return acctest.Nprintf(` |
| 230 | + data "google_project" "project" {} |
| 231 | + |
| 232 | +resource "google_service_account" "default" { |
| 233 | + account_id = "tf-test-my-custom1-%{random_suffix}" |
| 234 | + display_name = "Custom SA for VM Instance" |
| 235 | +} |
| 236 | +
|
| 237 | +resource "google_compute_instance" "default" { |
| 238 | + name = "tf-test-compute-instance1-%{random_suffix}" |
| 239 | + machine_type = "n2-standard-2" |
| 240 | + zone = "us-central1-a" |
| 241 | + tags = ["foo", "bar"] |
| 242 | + boot_disk { |
| 243 | + initialize_params { |
| 244 | + image = "debian-cloud/debian-11" |
| 245 | + } |
| 246 | + } |
| 247 | + network_interface { |
| 248 | + network = "default" |
| 249 | + } |
| 250 | + service_account { |
| 251 | + email = google_service_account.default.email |
| 252 | + scopes = ["cloud-platform"] |
| 253 | + } |
| 254 | +} |
| 255 | +
|
| 256 | +resource "google_backup_dr_backup_vault" "my-backup-vault" { |
| 257 | + location = "us-central1" |
| 258 | + backup_vault_id = "tf-test-bv1-%{random_suffix}" |
| 259 | + description = "This is a backup vault for list datasource test." |
| 260 | + backup_minimum_enforced_retention_duration = "100000s" |
| 261 | + labels = { |
| 262 | + foo = "bar1" |
| 263 | + bar = "baz1" |
| 264 | + } |
| 265 | + annotations = { |
| 266 | + annotations1 = "bar1" |
| 267 | + annotations2 = "baz1" |
| 268 | + } |
| 269 | + force_update = "true" |
| 270 | + force_delete = "true" |
| 271 | + allow_missing = "true" |
| 272 | +} |
| 273 | +
|
| 274 | +resource "google_backup_dr_backup_plan" "foo1" { |
| 275 | + location = "us-central1" |
| 276 | + backup_plan_id = "tf-test-bp-test1-%{random_suffix}" |
| 277 | + resource_type = "compute.googleapis.com/Instance" |
| 278 | + backup_vault = google_backup_dr_backup_vault.my-backup-vault.name |
| 279 | +
|
| 280 | + backup_rules { |
| 281 | + rule_id = "rule-1" |
| 282 | + backup_retention_days = 2 |
| 283 | + standard_schedule { |
| 284 | + recurrence_type = "HOURLY" |
| 285 | + hourly_frequency = 6 |
| 286 | + time_zone = "UTC" |
| 287 | + backup_window { |
| 288 | + start_hour_of_day = 12 |
| 289 | + end_hour_of_day = 18 |
| 290 | + } |
| 291 | + } |
| 292 | + } |
| 293 | +} |
| 294 | +
|
| 295 | +resource "google_backup_dr_backup_plan_association" "bpa" { |
| 296 | + location = "us-central1" |
| 297 | + backup_plan_association_id = "%{bpa_id}" |
| 298 | + resource = google_compute_instance.default.id |
| 299 | + resource_type = "compute.googleapis.com/Instance" |
| 300 | + backup_plan = google_backup_dr_backup_plan.foo1.name |
| 301 | +} |
| 302 | +
|
| 303 | +data "google_backup_dr_backup_plan_associations" "bpas" { |
| 304 | + location = "us-central1" |
| 305 | + resource_type = "compute.googleapis.com/Instance" |
| 306 | + depends_on = [google_backup_dr_backup_plan_association.bpa] |
| 307 | +} |
| 308 | +`, context) |
| 309 | +} |
0 commit comments