Skip to content

Commit

Permalink
New data source for google_gke_hub_membership_binding (#11032) (#7696)
Browse files Browse the repository at this point in the history
[upstream:91abd111f4861ea0a260d6e338a65cc054f5637c]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jul 9, 2024
1 parent 9212c6b commit 4bcc461
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/11032.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
`google_gke_hub_membership_binding`
```
1 change: 1 addition & 0 deletions google-beta/provider/provider_mmv1_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 4bcc461

Please sign in to comment.