Skip to content

Commit 2e0e73c

Browse files
gkehub: datasource for gke_hub_membership resource (#13915) (#22972)
[upstream:807a5d4d56849bc3c0820f144fb0d0a5e6739fe9] Signed-off-by: Modular Magician <[email protected]>
1 parent ae35ab6 commit 2e0e73c

File tree

6 files changed

+312
-0
lines changed

6 files changed

+312
-0
lines changed

.changelog/13915.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_gke_hub_membership`
3+
```

google/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
285285
"google_dns_managed_zone": dns.DataSourceDnsManagedZone(),
286286
"google_dns_managed_zones": dns.DataSourceDnsManagedZones(),
287287
"google_dns_record_set": dns.DataSourceDnsRecordSet(),
288+
"google_gke_hub_membership": gkehub.DataSourceGoogleGkeHubMembership(),
288289
"google_gke_hub_membership_binding": gkehub2.DataSourceGoogleGkeHubMembershipBinding(),
289290
"google_gke_hub_feature": gkehub2.DataSourceGoogleGkeHubFeature(),
290291
"google_filestore_instance": filestore.DataSourceGoogleFilestoreInstance(),
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
// ----------------------------------------------------------------------------
4+
//
5+
// *** AUTO GENERATED CODE *** Type: Handwritten ***
6+
//
7+
// ----------------------------------------------------------------------------
8+
//
9+
// This code is generated by Magic Modules using the following:
10+
//
11+
// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/services/gkehub/data_source_google_gke_hub_membership.go
12+
//
13+
// DO NOT EDIT this file directly. Any changes made to this file will be
14+
// overwritten during the next generation cycle.
15+
//
16+
// ----------------------------------------------------------------------------
17+
package gkehub
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
23+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
24+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
25+
)
26+
27+
func DataSourceGoogleGkeHubMembership() *schema.Resource {
28+
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceGKEHubMembership().Schema)
29+
tpgresource.AddRequiredFieldsToSchema(dsSchema, "membership_id")
30+
tpgresource.AddRequiredFieldsToSchema(dsSchema, "location")
31+
tpgresource.AddOptionalFieldsToSchema(dsSchema, "project")
32+
33+
return &schema.Resource{
34+
Read: dataSourceGoogleGkeHubMembershipRead,
35+
Schema: dsSchema,
36+
}
37+
}
38+
39+
func dataSourceGoogleGkeHubMembershipRead(d *schema.ResourceData, meta interface{}) error {
40+
config := meta.(*transport_tpg.Config)
41+
42+
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/memberships/{{membership_id}}")
43+
if err != nil {
44+
return fmt.Errorf("Error constructing id: %s", err)
45+
}
46+
d.SetId(id)
47+
48+
err = resourceGKEHubMembershipRead(d, meta)
49+
if err != nil {
50+
return err
51+
}
52+
53+
// No labels or annotations for Membership datasource
54+
return nil
55+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
// ----------------------------------------------------------------------------
4+
//
5+
// *** AUTO GENERATED CODE *** Type: Handwritten ***
6+
//
7+
// ----------------------------------------------------------------------------
8+
//
9+
// This code is generated by Magic Modules using the following:
10+
//
11+
// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/services/gkehub/data_source_google_gke_hub_membership_test.go
12+
//
13+
// DO NOT EDIT this file directly. Any changes made to this file will be
14+
// overwritten during the next generation cycle.
15+
//
16+
// ----------------------------------------------------------------------------
17+
package gkehub_test
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
"testing"
23+
24+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
25+
"github.com/hashicorp/terraform-plugin-testing/terraform"
26+
"github.com/hashicorp/terraform-provider-google/google/acctest"
27+
"github.com/hashicorp/terraform-provider-google/google/envvar"
28+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
29+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
30+
)
31+
32+
func TestAccDataSourceGoogleGkeHubMembership_basic(t *testing.T) {
33+
t.Parallel()
34+
35+
project := envvar.GetTestProjectFromEnv()
36+
gkeClusterRegion := "us-central1"
37+
gkeClusterZone := "us-central1-a"
38+
membershipLocation := "global"
39+
randomSuffix := acctest.RandString(t, 10)
40+
41+
// Define unique names for network and subnetwork for this test run
42+
networkName := fmt.Sprintf("tf-test-mem-ds-net-%s", randomSuffix)
43+
subnetworkName := fmt.Sprintf("tf-test-mem-ds-sub-%s", randomSuffix)
44+
45+
context := map[string]interface{}{
46+
"project": project,
47+
"gke_cluster_region": gkeClusterRegion,
48+
"gke_cluster_zone": gkeClusterZone,
49+
"membership_location": membershipLocation,
50+
"random_suffix": randomSuffix,
51+
"network_name": networkName,
52+
"subnetwork_name": subnetworkName,
53+
}
54+
55+
acctest.VcrTest(t, resource.TestCase{
56+
PreCheck: func() { acctest.AccTestPreCheck(t) },
57+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
58+
CheckDestroy: testAccCheckGoogleGkeHubMembershipDestroyProducer(t),
59+
Steps: []resource.TestStep{
60+
{
61+
Config: testAccDataSourceGoogleGkeHubMembership_basic_config(context),
62+
Check: resource.ComposeTestCheckFunc(
63+
acctest.CheckDataSourceStateMatchesResourceState("data.google_gke_hub_membership.example", "google_gke_hub_membership.example"),
64+
),
65+
},
66+
},
67+
})
68+
}
69+
70+
func testAccDataSourceGoogleGkeHubMembership_basic_config(context map[string]interface{}) string {
71+
return acctest.Nprintf(`
72+
resource "google_compute_network" "default" {
73+
project = "%{project}"
74+
name = "%{network_name}"
75+
auto_create_subnetworks = false
76+
}
77+
78+
resource "google_compute_subnetwork" "default" {
79+
project = "%{project}"
80+
name = "%{subnetwork_name}"
81+
ip_cidr_range = "10.2.0.0/16" // Example CIDR
82+
region = "%{gke_cluster_region}"
83+
network = google_compute_network.default.id
84+
}
85+
86+
resource "google_container_cluster" "primary" {
87+
project = "%{project}"
88+
name = "tf-test-mem-ds-cl-%{random_suffix}"
89+
location = "%{gke_cluster_zone}"
90+
initial_node_count = 1
91+
deletion_protection = false
92+
network = google_compute_network.default.id
93+
subnetwork = google_compute_subnetwork.default.id
94+
95+
master_auth {
96+
client_certificate_config {
97+
issue_client_certificate = false
98+
}
99+
}
100+
}
101+
102+
resource "google_gke_hub_membership" "example" {
103+
project = "%{project}"
104+
membership_id = "tf-test-mem-%{random_suffix}"
105+
location = "%{membership_location}"
106+
107+
endpoint {
108+
gke_cluster {
109+
resource_link = "//container.googleapis.com/${google_container_cluster.primary.id}"
110+
}
111+
}
112+
113+
depends_on = [google_container_cluster.primary]
114+
}
115+
116+
data "google_gke_hub_membership" "example" {
117+
project = google_gke_hub_membership.example.project
118+
location = google_gke_hub_membership.example.location
119+
membership_id = google_gke_hub_membership.example.membership_id
120+
}
121+
`, context)
122+
}
123+
124+
func testAccCheckGoogleGkeHubMembershipDestroyProducer(t *testing.T) func(s *terraform.State) error {
125+
return func(s *terraform.State) error {
126+
for name, rs := range s.RootModule().Resources {
127+
if rs.Type != "google_gke_hub_membership" {
128+
continue
129+
}
130+
if strings.HasPrefix(name, "data.") {
131+
continue
132+
}
133+
134+
config := acctest.GoogleProviderConfig(t)
135+
url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{GKEHub2BasePath}}projects/{{project}}/locations/{{location}}/memberships/{{membership_id}}")
136+
if err != nil {
137+
return fmt.Errorf("Error constructing URL for GKE Hub Membership: %s", err)
138+
}
139+
140+
billingProject := ""
141+
142+
if config.BillingProject != "" {
143+
billingProject = config.BillingProject
144+
}
145+
146+
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
147+
Config: config,
148+
Method: "GET",
149+
RawURL: url,
150+
UserAgent: config.UserAgent,
151+
Project: billingProject,
152+
})
153+
154+
if err == nil {
155+
return fmt.Errorf("GKEHubMembership still exists at %s", url)
156+
}
157+
}
158+
return nil
159+
}
160+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
# ----------------------------------------------------------------------------
3+
#
4+
# *** AUTO GENERATED CODE *** Type: Handwritten ***
5+
#
6+
# ----------------------------------------------------------------------------
7+
#
8+
# This code is generated by Magic Modules using the following:
9+
#
10+
# Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/website/docs/d/gke_hub_feature.html.markdown
11+
#
12+
# DO NOT EDIT this file directly. Any changes made to this file will be
13+
# overwritten during the next generation cycle.
14+
#
15+
# ----------------------------------------------------------------------------
16+
subcategory: "GKEHub"
17+
description: |-
18+
Retrieves the details of a GKE Hub Feature.
19+
---
20+
21+
# `google_gke_hub_feature`
22+
Retrieves the details of a specific GKE Hub Feature. Use this data source to retrieve the feature's configuration and state.
23+
24+
## Example Usage
25+
26+
```hcl
27+
data "google_gke_hub_feature" "example" {
28+
location = "global"
29+
name = "servicemesh"
30+
}
31+
```
32+
33+
## Argument Reference
34+
35+
The following arguments are supported:
36+
37+
* `name` - (Required) The name of the feature you want to know the status of.
38+
* `location` - (Required) The location for the GKE Hub Feature.
39+
* `project` - (Optional) The ID of the project in which the resource belongs.
40+
If it is not provided, the provider project is used.
41+
42+
## Attributes Reference
43+
44+
See [google_gke_hub_feature](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/gke_hub_feature) resource for details of the available attributes.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
# ----------------------------------------------------------------------------
3+
#
4+
# *** AUTO GENERATED CODE *** Type: Handwritten ***
5+
#
6+
# ----------------------------------------------------------------------------
7+
#
8+
# This code is generated by Magic Modules using the following:
9+
#
10+
# Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/website/docs/d/gke_hub_membership.html.markdown
11+
#
12+
# DO NOT EDIT this file directly. Any changes made to this file will be
13+
# overwritten during the next generation cycle.
14+
#
15+
# ----------------------------------------------------------------------------
16+
subcategory: "GKEHub"
17+
description: |-
18+
Retrieves the details of a GKE Hub Membership.
19+
---
20+
21+
# `google_gke_hub_membership`
22+
23+
Retrieves the details of a specific GKE Hub Membership. Use this data source to retrieve the membership's configuration and state.
24+
25+
## Example Usage
26+
27+
```hcl
28+
data "google_gke_hub_membership" "example" {
29+
project = "my-project-id"
30+
location = "global"
31+
membership_id = "my-membership-id" # GKE Cluster's name
32+
}
33+
```
34+
35+
## Argument Reference
36+
37+
The following arguments are supported:
38+
39+
* `membership_id` - (Required) The GKE Hub Membership id or GKE Cluster's name.
40+
41+
* `location` - (Required) The location for the GKE Hub Membership.
42+
Currently only `global` is supported.
43+
44+
* `project` - (Optional) The ID of the project in which the resource belongs.
45+
If it is not provided, the provider project is used.
46+
47+
## Attributes Reference
48+
49+
See [google_gke_hub_membership](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/gke_hub_membership) resource for details of the available attributes.

0 commit comments

Comments
 (0)