Skip to content

Commit 78b920f

Browse files
slevenickmodular-magician
authored andcommitted
Generate runtimeconfig IAM resources
Signed-off-by: Modular Magician <[email protected]>
1 parent 7d22e78 commit 78b920f

6 files changed

+502
-15
lines changed

google/config.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ type Config struct {
9292
PubsubBasePath string
9393
RedisBasePath string
9494
ResourceManagerBasePath string
95+
RuntimeConfigBasePath string
9596
SecurityCenterBasePath string
9697
SourceRepoBasePath string
9798
SpannerBasePath string
@@ -147,8 +148,7 @@ type Config struct {
147148
ResourceManagerV2Beta1BasePath string
148149
clientResourceManagerV2Beta1 *resourceManagerV2Beta1.Service
149150

150-
RuntimeconfigBasePath string
151-
clientRuntimeconfig *runtimeconfig.Service
151+
clientRuntimeconfig *runtimeconfig.Service
152152

153153
clientSpanner *spanner.Service
154154

@@ -218,6 +218,7 @@ var MonitoringDefaultBasePath = "https://monitoring.googleapis.com/v3/"
218218
var PubsubDefaultBasePath = "https://pubsub.googleapis.com/v1/"
219219
var RedisDefaultBasePath = "https://redis.googleapis.com/v1/"
220220
var ResourceManagerDefaultBasePath = "https://cloudresourcemanager.googleapis.com/v1/"
221+
var RuntimeConfigDefaultBasePath = "https://runtimeconfig.googleapis.com/v1beta1/"
221222
var SecurityCenterDefaultBasePath = "https://securitycenter.googleapis.com/v1/"
222223
var SourceRepoDefaultBasePath = "https://sourcerepo.googleapis.com/v1/"
223224
var SpannerDefaultBasePath = "https://spanner.googleapis.com/v1/"
@@ -390,14 +391,14 @@ func (c *Config) LoadAndValidate() error {
390391
c.clientResourceManagerV2Beta1.UserAgent = userAgent
391392
c.clientResourceManagerV2Beta1.BasePath = resourceManagerV2Beta1BasePath
392393

393-
runtimeconfigClientBasePath := removeBasePathVersion(c.RuntimeconfigBasePath)
394-
log.Printf("[INFO] Instantiating Google Cloud Runtimeconfig client for path %s", runtimeconfigClientBasePath)
394+
runtimeConfigClientBasePath := removeBasePathVersion(c.RuntimeConfigBasePath)
395+
log.Printf("[INFO] Instantiating Google Cloud Runtimeconfig client for path %s", runtimeConfigClientBasePath)
395396
c.clientRuntimeconfig, err = runtimeconfig.NewService(context, option.WithHTTPClient(client))
396397
if err != nil {
397398
return err
398399
}
399400
c.clientRuntimeconfig.UserAgent = userAgent
400-
c.clientRuntimeconfig.BasePath = runtimeconfigClientBasePath
401+
c.clientRuntimeconfig.BasePath = runtimeConfigClientBasePath
401402

402403
iamClientBasePath := removeBasePathVersion(c.IAMBasePath)
403404
log.Printf("[INFO] Instantiating Google Cloud IAM client for path %s", iamClientBasePath)
@@ -681,6 +682,7 @@ func ConfigureBasePaths(c *Config) {
681682
c.PubsubBasePath = PubsubDefaultBasePath
682683
c.RedisBasePath = RedisDefaultBasePath
683684
c.ResourceManagerBasePath = ResourceManagerDefaultBasePath
685+
c.RuntimeConfigBasePath = RuntimeConfigDefaultBasePath
684686
c.SecurityCenterBasePath = SecurityCenterDefaultBasePath
685687
c.SourceRepoBasePath = SourceRepoDefaultBasePath
686688
c.SpannerBasePath = SpannerDefaultBasePath
@@ -699,7 +701,6 @@ func ConfigureBasePaths(c *Config) {
699701
c.DnsBetaBasePath = DnsBetaDefaultBasePath
700702
c.IamCredentialsBasePath = IamCredentialsDefaultBasePath
701703
c.ResourceManagerV2Beta1BasePath = ResourceManagerV2Beta1DefaultBasePath
702-
c.RuntimeconfigBasePath = RuntimeconfigDefaultBasePath
703704
c.IAMBasePath = IAMDefaultBasePath
704705
c.ServiceManagementBasePath = ServiceManagementDefaultBasePath
705706
c.ServiceNetworkingBasePath = ServiceNetworkingDefaultBasePath

google/iam_runtime_config_config.go

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// ----------------------------------------------------------------------------
2+
//
3+
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
4+
//
5+
// ----------------------------------------------------------------------------
6+
//
7+
// This file is automatically generated by Magic Modules and manual
8+
// changes will be clobbered when the file is regenerated.
9+
//
10+
// Please read more about how to change this file in
11+
// .github/CONTRIBUTING.md.
12+
//
13+
// ----------------------------------------------------------------------------
14+
package google
15+
16+
import (
17+
"fmt"
18+
19+
"github.com/hashicorp/errwrap"
20+
"github.com/hashicorp/terraform/helper/schema"
21+
"google.golang.org/api/cloudresourcemanager/v1"
22+
)
23+
24+
var RuntimeConfigConfigIamSchema = map[string]*schema.Schema{
25+
"project": {
26+
Type: schema.TypeString,
27+
Computed: true,
28+
Optional: true,
29+
ForceNew: true,
30+
},
31+
"config": {
32+
Type: schema.TypeString,
33+
Required: true,
34+
ForceNew: true,
35+
DiffSuppressFunc: compareSelfLinkOrResourceName,
36+
},
37+
}
38+
39+
type RuntimeConfigConfigIamUpdater struct {
40+
project string
41+
config string
42+
d *schema.ResourceData
43+
Config *Config
44+
}
45+
46+
func RuntimeConfigConfigIamUpdaterProducer(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) {
47+
values := make(map[string]string)
48+
49+
project, err := getProject(d, config)
50+
if err != nil {
51+
return nil, err
52+
}
53+
values["project"] = project
54+
55+
// We may have gotten either a long or short name, so attempt to parse long name if possible
56+
m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/configs/(?P<config>[^/]+)", "(?P<project>[^/]+)/(?P<config>[^/]+)", "(?P<config>[^/]+)"}, d, config, d.Get("config").(string))
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
for k, v := range m {
62+
values[k] = v
63+
}
64+
65+
u := &RuntimeConfigConfigIamUpdater{
66+
project: values["project"],
67+
config: values["config"],
68+
d: d,
69+
Config: config,
70+
}
71+
72+
d.Set("project", u.project)
73+
d.Set("config", u.GetResourceId())
74+
75+
d.SetId(u.GetResourceId())
76+
77+
return u, nil
78+
}
79+
80+
func RuntimeConfigConfigIdParseFunc(d *schema.ResourceData, config *Config) error {
81+
values := make(map[string]string)
82+
83+
project, err := getProject(d, config)
84+
if err != nil {
85+
return err
86+
}
87+
values["project"] = project
88+
89+
m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/configs/(?P<config>[^/]+)", "(?P<project>[^/]+)/(?P<config>[^/]+)", "(?P<config>[^/]+)"}, d, config, d.Id())
90+
if err != nil {
91+
return err
92+
}
93+
94+
for k, v := range m {
95+
values[k] = v
96+
}
97+
98+
u := &RuntimeConfigConfigIamUpdater{
99+
project: values["project"],
100+
config: values["config"],
101+
d: d,
102+
Config: config,
103+
}
104+
d.Set("config", u.GetResourceId())
105+
d.SetId(u.GetResourceId())
106+
return nil
107+
}
108+
109+
func (u *RuntimeConfigConfigIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
110+
url := u.qualifyConfigUrl("getIamPolicy")
111+
112+
project, err := getProject(u.d, u.Config)
113+
if err != nil {
114+
return nil, err
115+
}
116+
117+
policy, err := sendRequest(u.Config, "GET", project, url, nil)
118+
if err != nil {
119+
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
120+
}
121+
122+
out := &cloudresourcemanager.Policy{}
123+
err = Convert(policy, out)
124+
if err != nil {
125+
return nil, errwrap.Wrapf("Cannot convert a policy to a resource manager policy: {{err}}", err)
126+
}
127+
128+
return out, nil
129+
}
130+
131+
func (u *RuntimeConfigConfigIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
132+
json, err := ConvertToMap(policy)
133+
if err != nil {
134+
return err
135+
}
136+
137+
obj := make(map[string]interface{})
138+
obj["policy"] = json
139+
140+
url := u.qualifyConfigUrl("setIamPolicy")
141+
142+
project, err := getProject(u.d, u.Config)
143+
if err != nil {
144+
return err
145+
}
146+
147+
_, err = sendRequestWithTimeout(u.Config, "POST", project, url, obj, u.d.Timeout(schema.TimeoutCreate))
148+
if err != nil {
149+
return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err)
150+
}
151+
152+
return nil
153+
}
154+
155+
func (u *RuntimeConfigConfigIamUpdater) qualifyConfigUrl(methodIdentifier string) string {
156+
return fmt.Sprintf("https://runtimeconfig.googleapis.com/v1beta1/%s:%s", fmt.Sprintf("projects/%s/configs/%s", u.project, u.config), methodIdentifier)
157+
}
158+
159+
func (u *RuntimeConfigConfigIamUpdater) GetResourceId() string {
160+
return fmt.Sprintf("projects/%s/configs/%s", u.project, u.config)
161+
}
162+
163+
func (u *RuntimeConfigConfigIamUpdater) GetMutexKey() string {
164+
return fmt.Sprintf("iam-runtimeconfig-config-%s", u.GetResourceId())
165+
}
166+
167+
func (u *RuntimeConfigConfigIamUpdater) DescribeResource() string {
168+
return fmt.Sprintf("runtimeconfig config %q", u.GetResourceId())
169+
}

0 commit comments

Comments
 (0)