|
| 1 | +package backupdr_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 5 | + "github.com/hashicorp/terraform-provider-google/google/acctest" |
| 6 | + "github.com/hashicorp/terraform-provider-google/google/envvar" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +func TestAccBackupDRBackupPlanAssociation_fullUpdate(t *testing.T) { |
| 12 | + // Uses time.Now |
| 13 | + acctest.SkipIfVcr(t) |
| 14 | + |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + timeNow := time.Now().UTC() |
| 18 | + referenceTime := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, time.UTC) |
| 19 | + |
| 20 | + context := map[string]interface{}{ |
| 21 | + "project": envvar.GetTestProjectFromEnv(), |
| 22 | + "effective_time": referenceTime.Add(24 * time.Hour).Format(time.RFC3339), |
| 23 | + "random_suffix": acctest.RandString(t, 10), |
| 24 | + } |
| 25 | + |
| 26 | + acctest.VcrTest(t, resource.TestCase{ |
| 27 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 28 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 29 | + Steps: []resource.TestStep{ |
| 30 | + { |
| 31 | + Config: testAccBackupDRBackupPlanAssociation_fullCreate(context), |
| 32 | + }, |
| 33 | + { |
| 34 | + ResourceName: "google_backup_dr_backup_plan_association.bpa", |
| 35 | + ImportState: true, |
| 36 | + ImportStateVerify: true, |
| 37 | + ImportStateVerifyIgnore: []string{"resource"}, |
| 38 | + }, |
| 39 | + { |
| 40 | + Config: testAccBackupDRBackupPlanAssociation_fullUpdate(context), |
| 41 | + }, |
| 42 | + { |
| 43 | + ResourceName: "google_backup_dr_backup_plan_association.bpa", |
| 44 | + ImportState: true, |
| 45 | + ImportStateVerify: true, |
| 46 | + ImportStateVerifyIgnore: []string{"resource"}, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +func testAccBackupDRBackupPlanAssociation_fullCreate(context map[string]interface{}) string { |
| 53 | + return acctest.Nprintf(` |
| 54 | +resource "google_service_account" "default" { |
| 55 | + account_id = "tf-test-my-custom-%{random_suffix}" |
| 56 | + display_name = "Custom SA for VM Instance" |
| 57 | +} |
| 58 | +
|
| 59 | +resource "google_compute_instance" "default" { |
| 60 | + name = "tf-test-compute-instance-%{random_suffix}" |
| 61 | + machine_type = "n2-standard-2" |
| 62 | + zone = "us-central1-a" |
| 63 | + tags = ["foo", "bar"] |
| 64 | + boot_disk { |
| 65 | + initialize_params { |
| 66 | + image = "debian-cloud/debian-11" |
| 67 | + labels = { |
| 68 | + my_label = "value" |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + // Local SSD disk |
| 73 | + scratch_disk { |
| 74 | + interface = "NVME" |
| 75 | + } |
| 76 | + network_interface { |
| 77 | + network = "default" |
| 78 | + access_config { |
| 79 | + // Ephemeral public IP |
| 80 | + } |
| 81 | + } |
| 82 | + service_account { |
| 83 | + # Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles. |
| 84 | + email = google_service_account.default.email |
| 85 | + scopes = ["cloud-platform"] |
| 86 | + } |
| 87 | +} |
| 88 | +resource "google_backup_dr_backup_vault" "my-backup-vault" { |
| 89 | + location ="us-central1" |
| 90 | + backup_vault_id = "tf-test-bv-%{random_suffix}" |
| 91 | + description = "This is a second backup vault built by Terraform." |
| 92 | + backup_minimum_enforced_retention_duration = "100000s" |
| 93 | + labels = { |
| 94 | + foo = "bar1" |
| 95 | + bar = "baz1" |
| 96 | + } |
| 97 | + annotations = { |
| 98 | + annotations1 = "bar1" |
| 99 | + annotations2 = "baz1" |
| 100 | + } |
| 101 | + force_update = "true" |
| 102 | + force_delete = "true" |
| 103 | + allow_missing = "true" |
| 104 | +} |
| 105 | +
|
| 106 | +resource "google_backup_dr_backup_plan" "foo" { |
| 107 | + location = "us-central1" |
| 108 | + backup_plan_id = "tf-test-bp-test-%{random_suffix}" |
| 109 | + resource_type = "compute.googleapis.com/Instance" |
| 110 | + backup_vault = google_backup_dr_backup_vault.my-backup-vault.name |
| 111 | +
|
| 112 | + backup_rules { |
| 113 | + rule_id = "rule-1" |
| 114 | + backup_retention_days = 2 |
| 115 | +
|
| 116 | + standard_schedule { |
| 117 | + recurrence_type = "HOURLY" |
| 118 | + hourly_frequency = 6 |
| 119 | + time_zone = "UTC" |
| 120 | +
|
| 121 | + backup_window { |
| 122 | + start_hour_of_day = 0 |
| 123 | + end_hour_of_day = 6 |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | +
|
| 129 | +resource "google_backup_dr_backup_plan_association" "bpa" { |
| 130 | + location = "us-central1" |
| 131 | + backup_plan_association_id = "tf-test-bpa-test-%{random_suffix}" |
| 132 | + resource = google_compute_instance.default.id |
| 133 | + resource_type= "compute.googleapis.com/Instance" |
| 134 | + backup_plan = google_backup_dr_backup_plan.foo.name |
| 135 | +} |
| 136 | +`, context) |
| 137 | +} |
| 138 | + |
| 139 | +func testAccBackupDRBackupPlanAssociation_fullUpdate(context map[string]interface{}) string { |
| 140 | + return acctest.Nprintf(` |
| 141 | +resource "google_service_account" "default" { |
| 142 | + account_id = "tf-test-my-custom-%{random_suffix}" |
| 143 | + display_name = "Custom SA for VM Instance" |
| 144 | +} |
| 145 | +
|
| 146 | +resource "google_compute_instance" "default" { |
| 147 | + name = "tf-test-compute-instance-%{random_suffix}" |
| 148 | + machine_type = "n2-standard-2" |
| 149 | + zone = "us-central1-a" |
| 150 | + tags = ["foo", "bar"] |
| 151 | + boot_disk { |
| 152 | + initialize_params { |
| 153 | + image = "debian-cloud/debian-11" |
| 154 | + labels = { |
| 155 | + my_label = "value" |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + // Local SSD disk |
| 160 | + scratch_disk { |
| 161 | + interface = "NVME" |
| 162 | + } |
| 163 | + network_interface { |
| 164 | + network = "default" |
| 165 | + access_config { |
| 166 | + // Ephemeral public IP |
| 167 | + } |
| 168 | + } |
| 169 | + service_account { |
| 170 | + # Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles. |
| 171 | + email = google_service_account.default.email |
| 172 | + scopes = ["cloud-platform"] |
| 173 | + } |
| 174 | +} |
| 175 | +
|
| 176 | +resource "google_backup_dr_backup_vault" "my-backup-vault" { |
| 177 | + location ="us-central1" |
| 178 | + backup_vault_id = "tf-test-bv-%{random_suffix}" |
| 179 | + description = "This is a second backup vault built by Terraform." |
| 180 | + backup_minimum_enforced_retention_duration = "100000s" |
| 181 | + labels = { |
| 182 | + foo = "bar1" |
| 183 | + bar = "baz1" |
| 184 | + } |
| 185 | + annotations = { |
| 186 | + annotations1 = "bar1" |
| 187 | + annotations2 = "baz1" |
| 188 | + } |
| 189 | + force_update = "true" |
| 190 | + force_delete = "true" |
| 191 | + allow_missing = "true" |
| 192 | +} |
| 193 | +
|
| 194 | +resource "google_backup_dr_backup_plan" "updated-bp" { |
| 195 | + location = "us-central1" |
| 196 | + backup_plan_id = "tf-test-bp-test-1-%{random_suffix}" |
| 197 | + resource_type = "compute.googleapis.com/Instance" |
| 198 | + backup_vault = google_backup_dr_backup_vault.my-backup-vault.name |
| 199 | +
|
| 200 | + backup_rules { |
| 201 | + rule_id = "rule-1" |
| 202 | + backup_retention_days = 4 |
| 203 | +
|
| 204 | + standard_schedule { |
| 205 | + recurrence_type = "HOURLY" |
| 206 | + hourly_frequency = 10 |
| 207 | + time_zone = "UTC" |
| 208 | +
|
| 209 | + backup_window { |
| 210 | + start_hour_of_day = 0 |
| 211 | + end_hour_of_day = 6 |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | +} |
| 216 | +
|
| 217 | +resource "google_backup_dr_backup_plan" "foo" { |
| 218 | + location = "us-central1" |
| 219 | + backup_plan_id = "tf-test-bp-test-%{random_suffix}" |
| 220 | + resource_type = "compute.googleapis.com/Instance" |
| 221 | + backup_vault = google_backup_dr_backup_vault.my-backup-vault.name |
| 222 | +
|
| 223 | + backup_rules { |
| 224 | + rule_id = "rule-1" |
| 225 | + backup_retention_days = 2 |
| 226 | +
|
| 227 | + standard_schedule { |
| 228 | + recurrence_type = "HOURLY" |
| 229 | + hourly_frequency = 6 |
| 230 | + time_zone = "UTC" |
| 231 | +
|
| 232 | + backup_window { |
| 233 | + start_hour_of_day = 0 |
| 234 | + end_hour_of_day = 6 |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | +} |
| 239 | +
|
| 240 | +resource "google_backup_dr_backup_plan_association" "bpa" { |
| 241 | + location = "us-central1" |
| 242 | + backup_plan_association_id = "tf-test-bpa-test-%{random_suffix}" |
| 243 | + resource = google_compute_instance.default.id |
| 244 | + resource_type= "compute.googleapis.com/Instance" |
| 245 | + backup_plan = google_backup_dr_backup_plan.updated-bp.name |
| 246 | +} |
| 247 | +`, context) |
| 248 | +} |
0 commit comments