diff --git a/.changelog/11032.txt b/.changelog/11032.txt new file mode 100644 index 0000000000..d2e7dbc225 --- /dev/null +++ b/.changelog/11032.txt @@ -0,0 +1,3 @@ +```release-note:new-datasource +`google_gke_hub_membership_binding` +``` \ No newline at end of file diff --git a/google-beta/provider/provider_mmv1_resources.go b/google-beta/provider/provider_mmv1_resources.go index 2f4d1ea2e3..8178155315 100644 --- a/google-beta/provider/provider_mmv1_resources.go +++ b/google-beta/provider/provider_mmv1_resources.go @@ -247,6 +247,7 @@ var handwrittenDatasources = map[string]*schema.Resource{ "google_dns_managed_zone": dns.DataSourceDnsManagedZone(), "google_dns_managed_zones": dns.DataSourceDnsManagedZones(), "google_dns_record_set": dns.DataSourceDnsRecordSet(), + "google_gke_hub_membership_binding": gkehub2.DataSourceGoogleGkeHubMembershipBinding(), "google_filestore_instance": filestore.DataSourceGoogleFilestoreInstance(), "google_iam_policy": resourcemanager.DataSourceGoogleIamPolicy(), "google_iam_role": resourcemanager.DataSourceGoogleIamRole(), diff --git a/google-beta/services/gkehub2/data_source_google_gke_hub_membership_binding.go b/google-beta/services/gkehub2/data_source_google_gke_hub_membership_binding.go new file mode 100644 index 0000000000..e90fa0a950 --- /dev/null +++ b/google-beta/services/gkehub2/data_source_google_gke_hub_membership_binding.go @@ -0,0 +1,53 @@ +// 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-beta/google-beta/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" +) + +func DataSourceGoogleGkeHubMembershipBinding() *schema.Resource { + dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceGKEHub2MembershipBinding().Schema) + tpgresource.AddRequiredFieldsToSchema(dsSchema, "membership_binding_id") + tpgresource.AddRequiredFieldsToSchema(dsSchema, "membership_id") + tpgresource.AddRequiredFieldsToSchema(dsSchema, "location") + tpgresource.AddOptionalFieldsToSchema(dsSchema, "project") + + return &schema.Resource{ + Read: dataSourceGoogleGkeHubMembershipBindingRead, + Schema: dsSchema, + } +} + +func dataSourceGoogleGkeHubMembershipBindingRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*transport_tpg.Config) + + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/memberships/{{membership_id}}/bindings/{{membership_binding_id}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + + d.SetId(id) + + err = resourceGKEHub2MembershipBindingRead(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-beta/services/gkehub2/data_source_google_gke_hub_membership_binding_test.go b/google-beta/services/gkehub2/data_source_google_gke_hub_membership_binding_test.go new file mode 100644 index 0000000000..3ce88d3462 --- /dev/null +++ b/google-beta/services/gkehub2/data_source_google_gke_hub_membership_binding_test.go @@ -0,0 +1,72 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 +package gkehub2_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" + "github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar" +) + +func TestAccDataSourceGoogleGKEHub2MembershipBinding_basic(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "org_id": envvar.GetTestOrgFromEnv(t), + "billing_account": envvar.GetTestBillingAccountFromEnv(t), + "random_suffix": acctest.RandString(t, 10), + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckGKEHub2MembershipBindingDestroyProducer(t), + ExternalProviders: map[string]resource.ExternalProvider{ + "time": {}, + }, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleGKEHub2MembershipBinding_basic(context), + Check: resource.ComposeTestCheckFunc( + acctest.CheckDataSourceStateMatchesResourceState("data.google_gke_hub_membership_binding.example", "google_gke_hub_membership_binding.example"), + ), + }, + }, + }) +} + +func testAccDataSourceGoogleGKEHub2MembershipBinding_basic(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_gke_hub_membership" "example" { + membership_id = "tf-test-membership%{random_suffix}" +} + +resource "google_gke_hub_scope" "example" { + scope_id = "tf-test-scope%{random_suffix}" +} + +resource "google_gke_hub_membership_binding" "example" { + membership_binding_id = "tf-test-membership-binding%{random_suffix}" + scope = google_gke_hub_scope.example.name + membership_id = "tf-test-membership%{random_suffix}" + location = "global" + labels = { + keyb = "valueb" + keya = "valuea" + keyc = "valuec" + } + depends_on = [ + google_gke_hub_membership.example, + google_gke_hub_scope.example + ] +} + +data "google_gke_hub_membership_binding" "example" { + location = google_gke_hub_membership_binding.example.location + membership_id = google_gke_hub_membership_binding.example.membership_id + membership_binding_id = google_gke_hub_membership_binding.example.membership_binding_id +} +`, context) +}