From 7ecb27bf1d2e6b5ec76cf12fd838a69ec006a2c5 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Tue, 17 Dec 2024 18:59:39 +0000 Subject: [PATCH] Supporting gke hub feature data source (#12459) [upstream:6f342c66088d7ab8e0167cdf5ed9bc841c75fc85] Signed-off-by: Modular Magician --- .changelog/12459.txt | 3 + google/provider/provider_mmv1_resources.go | 1 + .../data_source_google_gke_hub_feature.go | 52 +++++++++++ ...data_source_google_gke_hub_feature_test.go | 90 +++++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 .changelog/12459.txt create mode 100644 google/services/gkehub2/data_source_google_gke_hub_feature.go create mode 100644 google/services/gkehub2/data_source_google_gke_hub_feature_test.go diff --git a/.changelog/12459.txt b/.changelog/12459.txt new file mode 100644 index 00000000000..d959eded21b --- /dev/null +++ b/.changelog/12459.txt @@ -0,0 +1,3 @@ +```release-note:new-datasource +`google_gke_hub_feature` +``` \ No newline at end of file diff --git a/google/provider/provider_mmv1_resources.go b/google/provider/provider_mmv1_resources.go index 25166486baf..807ea7ed2f0 100644 --- a/google/provider/provider_mmv1_resources.go +++ b/google/provider/provider_mmv1_resources.go @@ -248,6 +248,7 @@ var handwrittenDatasources = map[string]*schema.Resource{ "google_dns_managed_zones": dns.DataSourceDnsManagedZones(), "google_dns_record_set": dns.DataSourceDnsRecordSet(), "google_gke_hub_membership_binding": gkehub2.DataSourceGoogleGkeHubMembershipBinding(), + "google_gke_hub_feature": gkehub2.DataSourceGoogleGkeHubFeature(), "google_filestore_instance": filestore.DataSourceGoogleFilestoreInstance(), "google_iam_policy": resourcemanager.DataSourceGoogleIamPolicy(), "google_iam_role": resourcemanager.DataSourceGoogleIamRole(), diff --git a/google/services/gkehub2/data_source_google_gke_hub_feature.go b/google/services/gkehub2/data_source_google_gke_hub_feature.go new file mode 100644 index 00000000000..4705a1a9722 --- /dev/null +++ b/google/services/gkehub2/data_source_google_gke_hub_feature.go @@ -0,0 +1,52 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 +package gkehub2 + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-provider-google/google/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" +) + +func DataSourceGoogleGkeHubFeature() *schema.Resource { + dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceGKEHub2Feature().Schema) + tpgresource.AddRequiredFieldsToSchema(dsSchema, "location") + tpgresource.AddOptionalFieldsToSchema(dsSchema, "project") + tpgresource.AddRequiredFieldsToSchema(dsSchema, "name") + + return &schema.Resource{ + Read: dataSourceGoogleGkeHubFeatureRead, + Schema: dsSchema, + } +} + +func dataSourceGoogleGkeHubFeatureRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*transport_tpg.Config) + + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/features/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + + d.SetId(id) + + err = resourceGKEHub2FeatureRead(d, meta) + if err != nil { + return err + } + + if err := tpgresource.SetDataSourceLabels(d); err != nil { + return err + } + + if err := tpgresource.SetDataSourceAnnotations(d); err != nil { + return err + } + + if d.Id() == "" { + return fmt.Errorf("%s not found", id) + } + return nil +} diff --git a/google/services/gkehub2/data_source_google_gke_hub_feature_test.go b/google/services/gkehub2/data_source_google_gke_hub_feature_test.go new file mode 100644 index 00000000000..c79a75ef5f2 --- /dev/null +++ b/google/services/gkehub2/data_source_google_gke_hub_feature_test.go @@ -0,0 +1,90 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 +package gkehub2_test + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/hashicorp/terraform-provider-google/google/acctest" + "github.com/hashicorp/terraform-provider-google/google/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" +) + +func TestAccDataSourceGoogleGkeHubFeature_basic(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(t, 10), + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + Providers: acctest.TestAccProviders, + CheckDestroy: testAccCheckGoogleGkeHubFeatureDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleGkeHubFeature_basic(context), + Check: resource.ComposeTestCheckFunc( + acctest.CheckDataSourceStateMatchesResourceState("data.google_gke_hub_feature.example", "google_gke_hub_feature.example"), + ), + }, + }, + }) +} + +func testAccDataSourceGoogleGkeHubFeature_basic(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_gke_hub_feature" "example" { + location = "global" + name = "servicemesh" +} + +data "google_gke_hub_feature" "example" { + location = google_gke_hub_feature.example.location + name = google_gke_hub_feature.example.name +} +`, context) +} + +func testAccCheckGoogleGkeHubFeatureDestroyProducer(t *testing.T) func(s *terraform.State) error { + return func(s *terraform.State) error { + for name, rs := range s.RootModule().Resources { + if rs.Type != "google_gke_hub_feature" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := acctest.GoogleProviderConfig(t) + + url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{GKEHub2BasePath}}projects/{{project}}/locations/{{location}}/features/{{name}}") + if err != nil { + return err + } + + billingProject := "" + + if config.BillingProject != "" { + billingProject = config.BillingProject + } + + _, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: billingProject, + RawURL: url, + UserAgent: config.UserAgent, + }) + if err == nil { + return fmt.Errorf("GKEHub2Feature still exists at %s", url) + } + } + + return nil + } +}