|
| 1 | +package gkehub2_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 10 | + "github.com/hashicorp/terraform-provider-google/google/acctest" |
| 11 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 12 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 13 | +) |
| 14 | + |
| 15 | +func TestAccDataSourceGoogleGkeHubFeature_basic(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + context := map[string]interface{}{ |
| 19 | + "random_suffix": acctest.RandString(t, 10), |
| 20 | + } |
| 21 | + |
| 22 | + acctest.VcrTest(t, resource.TestCase{ |
| 23 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 24 | + Providers: acctest.TestAccProviders, |
| 25 | + CheckDestroy: testAccCheckGoogleGkeHubFeatureDestroyProducer(t), |
| 26 | + Steps: []resource.TestStep{ |
| 27 | + { |
| 28 | + Config: testAccDataSourceGoogleGkeHubFeature_basic(context), |
| 29 | + Check: resource.ComposeTestCheckFunc( |
| 30 | + acctest.CheckDataSourceStateMatchesResourceState("data.google_gke_hub_feature.example", "google_gke_hub_feature.example"), |
| 31 | + ), |
| 32 | + }, |
| 33 | + }, |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +func testAccDataSourceGoogleGkeHubFeature_basic(context map[string]interface{}) string { |
| 38 | + return acctest.Nprintf(` |
| 39 | +resource "google_gke_hub_feature" "example" { |
| 40 | + location = "global" |
| 41 | + name = "servicemesh" |
| 42 | +} |
| 43 | +
|
| 44 | +data "google_gke_hub_feature" "example" { |
| 45 | + location = google_gke_hub_feature.example.location |
| 46 | + name = google_gke_hub_feature.example.name |
| 47 | +} |
| 48 | +`, context) |
| 49 | +} |
| 50 | + |
| 51 | +func testAccCheckGoogleGkeHubFeatureDestroyProducer(t *testing.T) func(s *terraform.State) error { |
| 52 | + return func(s *terraform.State) error { |
| 53 | + for name, rs := range s.RootModule().Resources { |
| 54 | + if rs.Type != "google_gke_hub_feature" { |
| 55 | + continue |
| 56 | + } |
| 57 | + if strings.HasPrefix(name, "data.") { |
| 58 | + continue |
| 59 | + } |
| 60 | + |
| 61 | + config := acctest.GoogleProviderConfig(t) |
| 62 | + |
| 63 | + url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{GKEHub2BasePath}}projects/{{project}}/locations/{{location}}/features/{{name}}") |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + billingProject := "" |
| 69 | + |
| 70 | + if config.BillingProject != "" { |
| 71 | + billingProject = config.BillingProject |
| 72 | + } |
| 73 | + |
| 74 | + _, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 75 | + Config: config, |
| 76 | + Method: "GET", |
| 77 | + Project: billingProject, |
| 78 | + RawURL: url, |
| 79 | + UserAgent: config.UserAgent, |
| 80 | + }) |
| 81 | + if err == nil { |
| 82 | + return fmt.Errorf("GKEHub2Feature still exists at %s", url) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return nil |
| 87 | + } |
| 88 | +} |
0 commit comments