-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New data source for google_gke_hub_membership_binding (#11032) (#7696)
[upstream:91abd111f4861ea0a260d6e338a65cc054f5637c] Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
9212c6b
commit 4bcc461
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-datasource | ||
`google_gke_hub_membership_binding` | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
google-beta/services/gkehub2/data_source_google_gke_hub_membership_binding.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
72 changes: 72 additions & 0 deletions
72
google-beta/services/gkehub2/data_source_google_gke_hub_membership_binding_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |