|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +// ---------------------------------------------------------------------------- |
| 5 | +// |
| 6 | +// *** AUTO GENERATED CODE *** Type: MMv1 *** |
| 7 | +// |
| 8 | +// ---------------------------------------------------------------------------- |
| 9 | +// |
| 10 | +// This file is automatically generated by Magic Modules and manual |
| 11 | +// changes will be clobbered when the file is regenerated. |
| 12 | +// |
| 13 | +// Please read more about how to change this file in |
| 14 | +// .github/CONTRIBUTING.md. |
| 15 | +// |
| 16 | +// ---------------------------------------------------------------------------- |
| 17 | + |
| 18 | +package compute_test |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 26 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 27 | + |
| 28 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" |
| 29 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar" |
| 30 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" |
| 31 | + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" |
| 32 | +) |
| 33 | + |
| 34 | +func TestAccComputeFutureReservation_futureReservationBasicExample(t *testing.T) { |
| 35 | + t.Parallel() |
| 36 | + |
| 37 | + context := map[string]interface{}{ |
| 38 | + "billing_account": envvar.GetTestBillingAccountFromEnv(t), |
| 39 | + "org_id": envvar.GetTestOrgFromEnv(t), |
| 40 | + "project": envvar.GetTestProjectFromEnv(), |
| 41 | + "random_suffix": acctest.RandString(t, 10), |
| 42 | + } |
| 43 | + |
| 44 | + acctest.VcrTest(t, resource.TestCase{ |
| 45 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 46 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t), |
| 47 | + CheckDestroy: testAccCheckComputeFutureReservationDestroyProducer(t), |
| 48 | + Steps: []resource.TestStep{ |
| 49 | + { |
| 50 | + Config: testAccComputeFutureReservation_futureReservationBasicExample(context), |
| 51 | + }, |
| 52 | + { |
| 53 | + ResourceName: "google_compute_future_reservation.gce_future_reservation", |
| 54 | + ImportState: true, |
| 55 | + ImportStateVerify: true, |
| 56 | + ImportStateVerifyIgnore: []string{"auto_created_reservations_delete_time", "auto_delete_auto_created_reservations"}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +func testAccComputeFutureReservation_futureReservationBasicExample(context map[string]interface{}) string { |
| 63 | + return acctest.Nprintf(` |
| 64 | +resource "google_compute_future_reservation" "gce_future_reservation" { |
| 65 | + provider = google-beta |
| 66 | + name = "tf-test-gce-future-reservation%{random_suffix}" |
| 67 | + project = "%{project}" |
| 68 | + auto_delete_auto_created_reservations = true |
| 69 | + planning_status = "DRAFT" |
| 70 | + name_prefix = "fr-basic" |
| 71 | + time_window { |
| 72 | + start_time = "2025-11-01T00:00:00Z" |
| 73 | + end_time = "2025-11-02T00:00:00Z" |
| 74 | + } |
| 75 | + specific_sku_properties { |
| 76 | + total_count = "1" |
| 77 | +
|
| 78 | + instance_properties { |
| 79 | + machine_type = "e2-standard-2" |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | +`, context) |
| 84 | +} |
| 85 | + |
| 86 | +func TestAccComputeFutureReservation_sharedFutureReservationExample(t *testing.T) { |
| 87 | + acctest.SkipIfVcr(t) |
| 88 | + t.Parallel() |
| 89 | + |
| 90 | + context := map[string]interface{}{ |
| 91 | + "billing_account": envvar.GetTestBillingAccountFromEnv(t), |
| 92 | + "org_id": envvar.GetTestOrgFromEnv(t), |
| 93 | + "project": envvar.GetTestProjectFromEnv(), |
| 94 | + "random_suffix": acctest.RandString(t, 10), |
| 95 | + } |
| 96 | + |
| 97 | + acctest.VcrTest(t, resource.TestCase{ |
| 98 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 99 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t), |
| 100 | + CheckDestroy: testAccCheckComputeFutureReservationDestroyProducer(t), |
| 101 | + Steps: []resource.TestStep{ |
| 102 | + { |
| 103 | + Config: testAccComputeFutureReservation_sharedFutureReservationExample(context), |
| 104 | + }, |
| 105 | + { |
| 106 | + ResourceName: "google_compute_future_reservation.gce_future_reservation", |
| 107 | + ImportState: true, |
| 108 | + ImportStateVerify: true, |
| 109 | + ImportStateVerifyIgnore: []string{"auto_created_reservations_delete_time", "auto_delete_auto_created_reservations"}, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }) |
| 113 | +} |
| 114 | + |
| 115 | +func testAccComputeFutureReservation_sharedFutureReservationExample(context map[string]interface{}) string { |
| 116 | + return acctest.Nprintf(` |
| 117 | +resource "google_project" "owner_project" { |
| 118 | + project_id = "tf-test%{random_suffix}" |
| 119 | + name = "tf-test%{random_suffix}" |
| 120 | + org_id = "%{org_id}" |
| 121 | + billing_account = "%{billing_account}" |
| 122 | + deletion_policy = "DELETE" |
| 123 | +} |
| 124 | +
|
| 125 | +resource "google_project_service" "compute" { |
| 126 | + project = google_project.owner_project.project_id |
| 127 | + service = "compute.googleapis.com" |
| 128 | + disable_on_destroy = false |
| 129 | +} |
| 130 | +
|
| 131 | +resource "google_project" "guest_project" { |
| 132 | + project_id = "tf-test-2%{random_suffix}" |
| 133 | + name = "tf-test-2%{random_suffix}" |
| 134 | + org_id = "%{org_id}" |
| 135 | + deletion_policy = "DELETE" |
| 136 | +} |
| 137 | +
|
| 138 | +resource "google_organization_policy" "shared_future_reservation_org_policy" { |
| 139 | + org_id = "%{org_id}" |
| 140 | + constraint = "constraints/compute.sharedReservationsOwnerProjects" |
| 141 | + list_policy { |
| 142 | + allow { |
| 143 | + values = ["projects/${google_project.owner_project.number}"] |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | +
|
| 148 | +resource "google_compute_future_reservation" "gce_future_reservation" { |
| 149 | + project = google_project.owner_project.project_id |
| 150 | + name = "tf-test-gce-shared-future-reservation%{random_suffix}" |
| 151 | + time_window { |
| 152 | + start_time = "2025-08-01T00:00:00Z" |
| 153 | + end_time = "2025-08-02T00:00:00Z" |
| 154 | + } |
| 155 | +
|
| 156 | + share_settings { |
| 157 | + share_type = "SPECIFIC_PROJECTS" |
| 158 | + project_map { |
| 159 | + id = google_project.guest_project.project_id |
| 160 | + project_id = google_project.guest_project.project_id |
| 161 | + } |
| 162 | + } |
| 163 | +
|
| 164 | +
|
| 165 | + depends_on = [ |
| 166 | + google_organization_policy.shared_future_reservation_org_policy, |
| 167 | + google_project_service.compute |
| 168 | + ] |
| 169 | +} |
| 170 | +`, context) |
| 171 | +} |
| 172 | + |
| 173 | +func testAccCheckComputeFutureReservationDestroyProducer(t *testing.T) func(s *terraform.State) error { |
| 174 | + return func(s *terraform.State) error { |
| 175 | + for name, rs := range s.RootModule().Resources { |
| 176 | + if rs.Type != "google_compute_future_reservation" { |
| 177 | + continue |
| 178 | + } |
| 179 | + if strings.HasPrefix(name, "data.") { |
| 180 | + continue |
| 181 | + } |
| 182 | + |
| 183 | + config := acctest.GoogleProviderConfig(t) |
| 184 | + |
| 185 | + url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/futureReservations/{{name}}") |
| 186 | + if err != nil { |
| 187 | + return err |
| 188 | + } |
| 189 | + |
| 190 | + billingProject := "" |
| 191 | + |
| 192 | + if config.BillingProject != "" { |
| 193 | + billingProject = config.BillingProject |
| 194 | + } |
| 195 | + |
| 196 | + _, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 197 | + Config: config, |
| 198 | + Method: "GET", |
| 199 | + Project: billingProject, |
| 200 | + RawURL: url, |
| 201 | + UserAgent: config.UserAgent, |
| 202 | + }) |
| 203 | + if err == nil { |
| 204 | + return fmt.Errorf("ComputeFutureReservation still exists at %s", url) |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + return nil |
| 209 | + } |
| 210 | +} |
0 commit comments