|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +package backupdr_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 7 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestAccDataSourceGoogleBackupDRBackupPlanAssociation_basic(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + context := map[string]interface{}{ |
| 14 | + "random_suffix": acctest.RandString(t, 10), |
| 15 | + } |
| 16 | + acctest.VcrTest(t, resource.TestCase{ |
| 17 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 18 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t), |
| 19 | + Steps: []resource.TestStep{ |
| 20 | + { |
| 21 | + Config: testAccDataSourceGoogleBackupDRBackupPlanAssociation_basic(context), |
| 22 | + Check: resource.ComposeTestCheckFunc( |
| 23 | + acctest.CheckDataSourceStateMatchesResourceStateWithIgnores("data.google_backup_dr_backup_plan_association.bpa-test", "google_backup_dr_backup_plan_association.bpa", map[string]struct{}{ |
| 24 | + "resource": {}, |
| 25 | + }, |
| 26 | + ), |
| 27 | + ), |
| 28 | + }, |
| 29 | + }, |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +func testAccDataSourceGoogleBackupDRBackupPlanAssociation_basic(context map[string]interface{}) string { |
| 34 | + return acctest.Nprintf(` |
| 35 | +
|
| 36 | +resource "google_service_account" "default" { |
| 37 | + provider = google-beta |
| 38 | + account_id = "tf-test-my-custom-%{random_suffix}" |
| 39 | + display_name = "Custom SA for VM Instance" |
| 40 | +} |
| 41 | +
|
| 42 | +resource "google_compute_instance" "default" { |
| 43 | + provider = google-beta |
| 44 | + name = "tf-test-compute-instance-%{random_suffix}" |
| 45 | + machine_type = "n2-standard-2" |
| 46 | + zone = "us-central1-a" |
| 47 | + tags = ["foo", "bar"] |
| 48 | + boot_disk { |
| 49 | + initialize_params { |
| 50 | + image = "debian-cloud/debian-11" |
| 51 | + labels = { |
| 52 | + my_label = "value" |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + // Local SSD disk |
| 57 | + scratch_disk { |
| 58 | + interface = "NVME" |
| 59 | + } |
| 60 | + network_interface { |
| 61 | + network = "default" |
| 62 | + access_config { |
| 63 | + // Ephemeral public IP |
| 64 | + } |
| 65 | + } |
| 66 | + service_account { |
| 67 | + # Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles. |
| 68 | + email = google_service_account.default.email |
| 69 | + scopes = ["cloud-platform"] |
| 70 | + } |
| 71 | +} |
| 72 | +resource "google_backup_dr_backup_vault" "my-backup-vault" { |
| 73 | + provider = google-beta |
| 74 | + location ="us-central1" |
| 75 | + backup_vault_id = "tf-test-bv-%{random_suffix}" |
| 76 | + description = "This is a second backup vault built by Terraform." |
| 77 | + backup_minimum_enforced_retention_duration = "100000s" |
| 78 | + labels = { |
| 79 | + foo = "bar1" |
| 80 | + bar = "baz1" |
| 81 | + } |
| 82 | + annotations = { |
| 83 | + annotations1 = "bar1" |
| 84 | + annotations2 = "baz1" |
| 85 | + } |
| 86 | + force_update = "true" |
| 87 | + force_delete = "true" |
| 88 | + allow_missing = "true" |
| 89 | +} |
| 90 | +
|
| 91 | +resource "google_backup_dr_backup_plan" "foo" { |
| 92 | + provider = google-beta |
| 93 | + location = "us-central1" |
| 94 | + backup_plan_id = "tf-test-bp-test-%{random_suffix}" |
| 95 | + resource_type = "compute.googleapis.com/Instance" |
| 96 | + backup_vault = google_backup_dr_backup_vault.my-backup-vault.name |
| 97 | +
|
| 98 | + backup_rules { |
| 99 | + rule_id = "rule-1" |
| 100 | + backup_retention_days = 2 |
| 101 | +
|
| 102 | + standard_schedule { |
| 103 | + recurrence_type = "HOURLY" |
| 104 | + hourly_frequency = 6 |
| 105 | + time_zone = "UTC" |
| 106 | +
|
| 107 | + backup_window { |
| 108 | + start_hour_of_day = 12 |
| 109 | + end_hour_of_day = 18 |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +
|
| 115 | +resource "google_backup_dr_backup_plan_association" "bpa" { |
| 116 | + provider = google-beta |
| 117 | + location = "us-central1" |
| 118 | + backup_plan_association_id = "tf-test-bpa-test-%{random_suffix}" |
| 119 | + resource = google_compute_instance.default.id |
| 120 | + resource_type= "compute.googleapis.com/Instance" |
| 121 | + backup_plan = google_backup_dr_backup_plan.foo.name |
| 122 | +} |
| 123 | +
|
| 124 | +data "google_backup_dr_backup_plan_association" "bpa-test" { |
| 125 | + provider = google-beta |
| 126 | + location = "us-central1" |
| 127 | + backup_plan_association_id="tf-test-bpa-test-%{random_suffix}" |
| 128 | + depends_on= [ google_backup_dr_backup_plan_association.bpa ] |
| 129 | + } |
| 130 | +`, context) |
| 131 | +} |
0 commit comments