|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +// ---------------------------------------------------------------------------- |
| 4 | +// |
| 5 | +// *** AUTO GENERATED CODE *** Type: Handwritten *** |
| 6 | +// |
| 7 | +// ---------------------------------------------------------------------------- |
| 8 | +// |
| 9 | +// This code is generated by Magic Modules using the following: |
| 10 | +// |
| 11 | +// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/services/gkehub/data_source_google_gke_hub_membership_test.go |
| 12 | +// |
| 13 | +// DO NOT EDIT this file directly. Any changes made to this file will be |
| 14 | +// overwritten during the next generation cycle. |
| 15 | +// |
| 16 | +// ---------------------------------------------------------------------------- |
| 17 | +package gkehub_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 25 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 26 | + "github.com/hashicorp/terraform-provider-google/google/acctest" |
| 27 | + "github.com/hashicorp/terraform-provider-google/google/envvar" |
| 28 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 29 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 30 | +) |
| 31 | + |
| 32 | +func TestAccDataSourceGoogleGkeHubMembership_basic(t *testing.T) { |
| 33 | + t.Parallel() |
| 34 | + |
| 35 | + project := envvar.GetTestProjectFromEnv() |
| 36 | + gkeClusterRegion := "us-central1" |
| 37 | + gkeClusterZone := "us-central1-a" |
| 38 | + membershipLocation := "global" |
| 39 | + randomSuffix := acctest.RandString(t, 10) |
| 40 | + |
| 41 | + // Define unique names for network and subnetwork for this test run |
| 42 | + networkName := fmt.Sprintf("tf-test-mem-ds-net-%s", randomSuffix) |
| 43 | + subnetworkName := fmt.Sprintf("tf-test-mem-ds-sub-%s", randomSuffix) |
| 44 | + |
| 45 | + context := map[string]interface{}{ |
| 46 | + "project": project, |
| 47 | + "gke_cluster_region": gkeClusterRegion, |
| 48 | + "gke_cluster_zone": gkeClusterZone, |
| 49 | + "membership_location": membershipLocation, |
| 50 | + "random_suffix": randomSuffix, |
| 51 | + "network_name": networkName, |
| 52 | + "subnetwork_name": subnetworkName, |
| 53 | + } |
| 54 | + |
| 55 | + acctest.VcrTest(t, resource.TestCase{ |
| 56 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 57 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 58 | + CheckDestroy: testAccCheckGoogleGkeHubMembershipDestroyProducer(t), |
| 59 | + Steps: []resource.TestStep{ |
| 60 | + { |
| 61 | + Config: testAccDataSourceGoogleGkeHubMembership_basic_config(context), |
| 62 | + Check: resource.ComposeTestCheckFunc( |
| 63 | + acctest.CheckDataSourceStateMatchesResourceState("data.google_gke_hub_membership.example", "google_gke_hub_membership.example"), |
| 64 | + ), |
| 65 | + }, |
| 66 | + }, |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +func testAccDataSourceGoogleGkeHubMembership_basic_config(context map[string]interface{}) string { |
| 71 | + return acctest.Nprintf(` |
| 72 | +resource "google_compute_network" "default" { |
| 73 | + project = "%{project}" |
| 74 | + name = "%{network_name}" |
| 75 | + auto_create_subnetworks = false |
| 76 | +} |
| 77 | +
|
| 78 | +resource "google_compute_subnetwork" "default" { |
| 79 | + project = "%{project}" |
| 80 | + name = "%{subnetwork_name}" |
| 81 | + ip_cidr_range = "10.2.0.0/16" // Example CIDR |
| 82 | + region = "%{gke_cluster_region}" |
| 83 | + network = google_compute_network.default.id |
| 84 | +} |
| 85 | +
|
| 86 | +resource "google_container_cluster" "primary" { |
| 87 | + project = "%{project}" |
| 88 | + name = "tf-test-mem-ds-cl-%{random_suffix}" |
| 89 | + location = "%{gke_cluster_zone}" |
| 90 | + initial_node_count = 1 |
| 91 | + deletion_protection = false |
| 92 | + network = google_compute_network.default.id |
| 93 | + subnetwork = google_compute_subnetwork.default.id |
| 94 | +
|
| 95 | + master_auth { |
| 96 | + client_certificate_config { |
| 97 | + issue_client_certificate = false |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | +
|
| 102 | +resource "google_gke_hub_membership" "example" { |
| 103 | + project = "%{project}" |
| 104 | + membership_id = "tf-test-mem-%{random_suffix}" |
| 105 | + location = "%{membership_location}" |
| 106 | +
|
| 107 | + endpoint { |
| 108 | + gke_cluster { |
| 109 | + resource_link = "//container.googleapis.com/${google_container_cluster.primary.id}" |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + depends_on = [google_container_cluster.primary] |
| 114 | +} |
| 115 | +
|
| 116 | +data "google_gke_hub_membership" "example" { |
| 117 | + project = google_gke_hub_membership.example.project |
| 118 | + location = google_gke_hub_membership.example.location |
| 119 | + membership_id = google_gke_hub_membership.example.membership_id |
| 120 | +} |
| 121 | +`, context) |
| 122 | +} |
| 123 | + |
| 124 | +func testAccCheckGoogleGkeHubMembershipDestroyProducer(t *testing.T) func(s *terraform.State) error { |
| 125 | + return func(s *terraform.State) error { |
| 126 | + for name, rs := range s.RootModule().Resources { |
| 127 | + if rs.Type != "google_gke_hub_membership" { |
| 128 | + continue |
| 129 | + } |
| 130 | + if strings.HasPrefix(name, "data.") { |
| 131 | + continue |
| 132 | + } |
| 133 | + |
| 134 | + config := acctest.GoogleProviderConfig(t) |
| 135 | + url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{GKEHub2BasePath}}projects/{{project}}/locations/{{location}}/memberships/{{membership_id}}") |
| 136 | + if err != nil { |
| 137 | + return fmt.Errorf("Error constructing URL for GKE Hub Membership: %s", err) |
| 138 | + } |
| 139 | + |
| 140 | + billingProject := "" |
| 141 | + |
| 142 | + if config.BillingProject != "" { |
| 143 | + billingProject = config.BillingProject |
| 144 | + } |
| 145 | + |
| 146 | + _, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 147 | + Config: config, |
| 148 | + Method: "GET", |
| 149 | + RawURL: url, |
| 150 | + UserAgent: config.UserAgent, |
| 151 | + Project: billingProject, |
| 152 | + }) |
| 153 | + |
| 154 | + if err == nil { |
| 155 | + return fmt.Errorf("GKEHubMembership still exists at %s", url) |
| 156 | + } |
| 157 | + } |
| 158 | + return nil |
| 159 | + } |
| 160 | +} |
0 commit comments