Skip to content

Commit 1661ce3

Browse files
Adding Terraform resources for Tenancy APIs in GKEHub (#8396) (#15670)
Signed-off-by: Modular Magician <[email protected]>
1 parent 665e344 commit 1661ce3

20 files changed

+3627
-3
lines changed

.changelog/8396.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
gkehub: added `Namespace`, `Scope`, `MembershipBinding`, `MembershipRBACRoleBinding`, `ScopeRBACRoleBinding` resources
3+
```

google/provider/provider.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,7 @@ func DatasourceMapWithErrors() (map[string]*schema.Resource, error) {
922922
"google_gke_backup_backup_plan_iam_policy": tpgiamresource.DataSourceIamPolicy(gkebackup.GKEBackupBackupPlanIamSchema, gkebackup.GKEBackupBackupPlanIamUpdaterProducer),
923923
"google_gke_hub_membership_iam_policy": tpgiamresource.DataSourceIamPolicy(gkehub.GKEHubMembershipIamSchema, gkehub.GKEHubMembershipIamUpdaterProducer),
924924
"google_gke_hub_feature_iam_policy": tpgiamresource.DataSourceIamPolicy(gkehub2.GKEHub2FeatureIamSchema, gkehub2.GKEHub2FeatureIamUpdaterProducer),
925+
"google_gke_hub_scope_iam_policy": tpgiamresource.DataSourceIamPolicy(gkehub2.GKEHub2ScopeIamSchema, gkehub2.GKEHub2ScopeIamUpdaterProducer),
925926
"google_healthcare_consent_store_iam_policy": tpgiamresource.DataSourceIamPolicy(healthcare.HealthcareConsentStoreIamSchema, healthcare.HealthcareConsentStoreIamUpdaterProducer),
926927
"google_iap_app_engine_service_iam_policy": tpgiamresource.DataSourceIamPolicy(iap.IapAppEngineServiceIamSchema, iap.IapAppEngineServiceIamUpdaterProducer),
927928
"google_iap_app_engine_version_iam_policy": tpgiamresource.DataSourceIamPolicy(iap.IapAppEngineVersionIamSchema, iap.IapAppEngineVersionIamUpdaterProducer),
@@ -972,9 +973,9 @@ func DatasourceMapWithErrors() (map[string]*schema.Resource, error) {
972973
})
973974
}
974975

975-
// Generated resources: 312
976-
// Generated IAM resources: 204
977-
// Total generated resources: 516
976+
// Generated resources: 316
977+
// Generated IAM resources: 207
978+
// Total generated resources: 523
978979
func ResourceMap() map[string]*schema.Resource {
979980
resourceMap, _ := ResourceMapWithErrors()
980981
return resourceMap
@@ -1316,6 +1317,13 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
13161317
"google_gke_hub_feature_iam_binding": tpgiamresource.ResourceIamBinding(gkehub2.GKEHub2FeatureIamSchema, gkehub2.GKEHub2FeatureIamUpdaterProducer, gkehub2.GKEHub2FeatureIdParseFunc),
13171318
"google_gke_hub_feature_iam_member": tpgiamresource.ResourceIamMember(gkehub2.GKEHub2FeatureIamSchema, gkehub2.GKEHub2FeatureIamUpdaterProducer, gkehub2.GKEHub2FeatureIdParseFunc),
13181319
"google_gke_hub_feature_iam_policy": tpgiamresource.ResourceIamPolicy(gkehub2.GKEHub2FeatureIamSchema, gkehub2.GKEHub2FeatureIamUpdaterProducer, gkehub2.GKEHub2FeatureIdParseFunc),
1320+
"google_gke_hub_membership_binding": gkehub2.ResourceGKEHub2MembershipBinding(),
1321+
"google_gke_hub_namespace": gkehub2.ResourceGKEHub2Namespace(),
1322+
"google_gke_hub_scope": gkehub2.ResourceGKEHub2Scope(),
1323+
"google_gke_hub_scope_iam_binding": tpgiamresource.ResourceIamBinding(gkehub2.GKEHub2ScopeIamSchema, gkehub2.GKEHub2ScopeIamUpdaterProducer, gkehub2.GKEHub2ScopeIdParseFunc),
1324+
"google_gke_hub_scope_iam_member": tpgiamresource.ResourceIamMember(gkehub2.GKEHub2ScopeIamSchema, gkehub2.GKEHub2ScopeIamUpdaterProducer, gkehub2.GKEHub2ScopeIdParseFunc),
1325+
"google_gke_hub_scope_iam_policy": tpgiamresource.ResourceIamPolicy(gkehub2.GKEHub2ScopeIamSchema, gkehub2.GKEHub2ScopeIamUpdaterProducer, gkehub2.GKEHub2ScopeIdParseFunc),
1326+
"google_gke_hub_scope_rbac_role_binding": gkehub2.ResourceGKEHub2ScopeRBACRoleBinding(),
13191327
"google_healthcare_consent_store": healthcare.ResourceHealthcareConsentStore(),
13201328
"google_healthcare_consent_store_iam_binding": tpgiamresource.ResourceIamBinding(healthcare.HealthcareConsentStoreIamSchema, healthcare.HealthcareConsentStoreIamUpdaterProducer, healthcare.HealthcareConsentStoreIdParseFunc),
13211329
"google_healthcare_consent_store_iam_member": tpgiamresource.ResourceIamMember(healthcare.HealthcareConsentStoreIamSchema, healthcare.HealthcareConsentStoreIamUpdaterProducer, healthcare.HealthcareConsentStoreIdParseFunc),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
// ----------------------------------------------------------------------------
5+
//
6+
// *** AUTO GENERATED CODE *** Type: MMv1 ***
7+
//
8+
// ----------------------------------------------------------------------------
9+
//
10+
// This file is automatically generated by Magic Modules and manual
11+
// changes will be clobbered when the file is regenerated.
12+
//
13+
// Please read more about how to change this file in
14+
// .github/CONTRIBUTING.md.
15+
//
16+
// ----------------------------------------------------------------------------
17+
18+
package gkehub2
19+
20+
import (
21+
"fmt"
22+
23+
"github.com/hashicorp/errwrap"
24+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
25+
"google.golang.org/api/cloudresourcemanager/v1"
26+
27+
"github.com/hashicorp/terraform-provider-google/google/tpgiamresource"
28+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
29+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
30+
)
31+
32+
var GKEHub2ScopeIamSchema = map[string]*schema.Schema{
33+
"project": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
Optional: true,
37+
ForceNew: true,
38+
},
39+
"scope_id": {
40+
Type: schema.TypeString,
41+
Required: true,
42+
ForceNew: true,
43+
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
44+
},
45+
}
46+
47+
type GKEHub2ScopeIamUpdater struct {
48+
project string
49+
scopeId string
50+
d tpgresource.TerraformResourceData
51+
Config *transport_tpg.Config
52+
}
53+
54+
func GKEHub2ScopeIamUpdaterProducer(d tpgresource.TerraformResourceData, config *transport_tpg.Config) (tpgiamresource.ResourceIamUpdater, error) {
55+
values := make(map[string]string)
56+
57+
project, _ := tpgresource.GetProject(d, config)
58+
if project != "" {
59+
if err := d.Set("project", project); err != nil {
60+
return nil, fmt.Errorf("Error setting project: %s", err)
61+
}
62+
}
63+
values["project"] = project
64+
if v, ok := d.GetOk("scope_id"); ok {
65+
values["scope_id"] = v.(string)
66+
}
67+
68+
// We may have gotten either a long or short name, so attempt to parse long name if possible
69+
m, err := tpgresource.GetImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/locations/global/scopes/(?P<scope_id>[^/]+)", "(?P<project>[^/]+)/(?P<scope_id>[^/]+)", "(?P<scope_id>[^/]+)"}, d, config, d.Get("scope_id").(string))
70+
if err != nil {
71+
return nil, err
72+
}
73+
74+
for k, v := range m {
75+
values[k] = v
76+
}
77+
78+
u := &GKEHub2ScopeIamUpdater{
79+
project: values["project"],
80+
scopeId: values["scope_id"],
81+
d: d,
82+
Config: config,
83+
}
84+
85+
if err := d.Set("project", u.project); err != nil {
86+
return nil, fmt.Errorf("Error setting project: %s", err)
87+
}
88+
if err := d.Set("scope_id", u.GetResourceId()); err != nil {
89+
return nil, fmt.Errorf("Error setting scope_id: %s", err)
90+
}
91+
92+
return u, nil
93+
}
94+
95+
func GKEHub2ScopeIdParseFunc(d *schema.ResourceData, config *transport_tpg.Config) error {
96+
values := make(map[string]string)
97+
98+
project, _ := tpgresource.GetProject(d, config)
99+
if project != "" {
100+
values["project"] = project
101+
}
102+
103+
m, err := tpgresource.GetImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/locations/global/scopes/(?P<scope_id>[^/]+)", "(?P<project>[^/]+)/(?P<scope_id>[^/]+)", "(?P<scope_id>[^/]+)"}, d, config, d.Id())
104+
if err != nil {
105+
return err
106+
}
107+
108+
for k, v := range m {
109+
values[k] = v
110+
}
111+
112+
u := &GKEHub2ScopeIamUpdater{
113+
project: values["project"],
114+
scopeId: values["scope_id"],
115+
d: d,
116+
Config: config,
117+
}
118+
if err := d.Set("scope_id", u.GetResourceId()); err != nil {
119+
return fmt.Errorf("Error setting scope_id: %s", err)
120+
}
121+
d.SetId(u.GetResourceId())
122+
return nil
123+
}
124+
125+
func (u *GKEHub2ScopeIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
126+
url, err := u.qualifyScopeUrl("getIamPolicy")
127+
if err != nil {
128+
return nil, err
129+
}
130+
131+
project, err := tpgresource.GetProject(u.d, u.Config)
132+
if err != nil {
133+
return nil, err
134+
}
135+
var obj map[string]interface{}
136+
137+
userAgent, err := tpgresource.GenerateUserAgentString(u.d, u.Config.UserAgent)
138+
if err != nil {
139+
return nil, err
140+
}
141+
142+
policy, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
143+
Config: u.Config,
144+
Method: "GET",
145+
Project: project,
146+
RawURL: url,
147+
UserAgent: userAgent,
148+
Body: obj,
149+
})
150+
if err != nil {
151+
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
152+
}
153+
154+
out := &cloudresourcemanager.Policy{}
155+
err = tpgresource.Convert(policy, out)
156+
if err != nil {
157+
return nil, errwrap.Wrapf("Cannot convert a policy to a resource manager policy: {{err}}", err)
158+
}
159+
160+
return out, nil
161+
}
162+
163+
func (u *GKEHub2ScopeIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
164+
json, err := tpgresource.ConvertToMap(policy)
165+
if err != nil {
166+
return err
167+
}
168+
169+
obj := make(map[string]interface{})
170+
obj["policy"] = json
171+
172+
url, err := u.qualifyScopeUrl("setIamPolicy")
173+
if err != nil {
174+
return err
175+
}
176+
project, err := tpgresource.GetProject(u.d, u.Config)
177+
if err != nil {
178+
return err
179+
}
180+
181+
userAgent, err := tpgresource.GenerateUserAgentString(u.d, u.Config.UserAgent)
182+
if err != nil {
183+
return err
184+
}
185+
186+
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
187+
Config: u.Config,
188+
Method: "POST",
189+
Project: project,
190+
RawURL: url,
191+
UserAgent: userAgent,
192+
Body: obj,
193+
Timeout: u.d.Timeout(schema.TimeoutCreate),
194+
})
195+
if err != nil {
196+
return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err)
197+
}
198+
199+
return nil
200+
}
201+
202+
func (u *GKEHub2ScopeIamUpdater) qualifyScopeUrl(methodIdentifier string) (string, error) {
203+
urlTemplate := fmt.Sprintf("{{GKEHub2BasePath}}%s:%s", fmt.Sprintf("projects/%s/locations/global/scopes/%s", u.project, u.scopeId), methodIdentifier)
204+
url, err := tpgresource.ReplaceVars(u.d, u.Config, urlTemplate)
205+
if err != nil {
206+
return "", err
207+
}
208+
return url, nil
209+
}
210+
211+
func (u *GKEHub2ScopeIamUpdater) GetResourceId() string {
212+
return fmt.Sprintf("projects/%s/locations/global/scopes/%s", u.project, u.scopeId)
213+
}
214+
215+
func (u *GKEHub2ScopeIamUpdater) GetMutexKey() string {
216+
return fmt.Sprintf("iam-gkehub2-scope-%s", u.GetResourceId())
217+
}
218+
219+
func (u *GKEHub2ScopeIamUpdater) DescribeResource() string {
220+
return fmt.Sprintf("gkehub2 scope %q", u.GetResourceId())
221+
}

0 commit comments

Comments
 (0)