Skip to content

Commit 4e2395c

Browse files
slevenickmodular-magician
authored andcommitted
Generate runtimeconfig IAM resources
Signed-off-by: Modular Magician <[email protected]>
1 parent b072474 commit 4e2395c

7 files changed

+501
-9
lines changed

google/config.go

+4-3
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/"
@@ -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_runtimeconfig_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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
15+
package google
16+
17+
import (
18+
"fmt"
19+
"testing"
20+
21+
"github.com/hashicorp/terraform/helper/acctest"
22+
"github.com/hashicorp/terraform/helper/resource"
23+
)
24+
25+
func TestAccRuntimeconfigConfigIamBindingGenerated(t *testing.T) {
26+
t.Parallel()
27+
28+
context := map[string]interface{}{
29+
"random_suffix": acctest.RandString(10),
30+
"role": "roles/viewer",
31+
}
32+
33+
resource.Test(t, resource.TestCase{
34+
PreCheck: func() { testAccPreCheck(t) },
35+
Providers: testAccProviders,
36+
Steps: []resource.TestStep{
37+
{
38+
Config: testAccRuntimeconfigConfigIamBinding_basicGenerated(context),
39+
},
40+
{
41+
ResourceName: "google_runtimeconfig_config_iam_binding.foo",
42+
ImportStateId: fmt.Sprintf("projects/%s/configs/%s roles/viewer", getTestProjectFromEnv(), fmt.Sprintf("my-config%s", context["random_suffix"])),
43+
ImportState: true,
44+
ImportStateVerify: true,
45+
},
46+
{
47+
// Test Iam Binding update
48+
Config: testAccRuntimeconfigConfigIamBinding_updateGenerated(context),
49+
},
50+
{
51+
ResourceName: "google_runtimeconfig_config_iam_binding.foo",
52+
ImportStateId: fmt.Sprintf("projects/%s/configs/%s roles/viewer", getTestProjectFromEnv(), fmt.Sprintf("my-config%s", context["random_suffix"])),
53+
ImportState: true,
54+
ImportStateVerify: true,
55+
},
56+
},
57+
})
58+
}
59+
60+
func TestAccRuntimeconfigConfigIamMemberGenerated(t *testing.T) {
61+
t.Parallel()
62+
63+
context := map[string]interface{}{
64+
"random_suffix": acctest.RandString(10),
65+
"role": "roles/viewer",
66+
}
67+
68+
resource.Test(t, resource.TestCase{
69+
PreCheck: func() { testAccPreCheck(t) },
70+
Providers: testAccProviders,
71+
Steps: []resource.TestStep{
72+
{
73+
// Test Iam Member creation (no update for member, no need to test)
74+
Config: testAccRuntimeconfigConfigIamMember_basicGenerated(context),
75+
},
76+
{
77+
ResourceName: "google_runtimeconfig_config_iam_member.foo",
78+
ImportStateId: fmt.Sprintf("projects/%s/configs/%s roles/viewer user:[email protected]", getTestProjectFromEnv(), fmt.Sprintf("my-config%s", context["random_suffix"])),
79+
ImportState: true,
80+
ImportStateVerify: true,
81+
},
82+
},
83+
})
84+
}
85+
86+
func TestAccRuntimeconfigConfigIamPolicyGenerated(t *testing.T) {
87+
t.Parallel()
88+
89+
context := map[string]interface{}{
90+
"random_suffix": acctest.RandString(10),
91+
"role": "roles/viewer",
92+
}
93+
94+
resource.Test(t, resource.TestCase{
95+
PreCheck: func() { testAccPreCheck(t) },
96+
Providers: testAccProviders,
97+
Steps: []resource.TestStep{
98+
{
99+
Config: testAccRuntimeconfigConfigIamPolicy_basicGenerated(context),
100+
},
101+
{
102+
ResourceName: "google_runtimeconfig_config_iam_policy.foo",
103+
ImportStateId: fmt.Sprintf("projects/%s/configs/%s", getTestProjectFromEnv(), fmt.Sprintf("my-config%s", context["random_suffix"])),
104+
ImportState: true,
105+
ImportStateVerify: true,
106+
},
107+
},
108+
})
109+
}
110+
111+
func testAccRuntimeconfigConfigIamMember_basicGenerated(context map[string]interface{}) string {
112+
return Nprintf(`
113+
resource "google_runtimeconfig_config" "config" {
114+
name = "my-config%{random_suffix}"
115+
description = "Runtime configuration values for my service"
116+
}
117+
118+
resource "google_runtimeconfig_config_iam_member" "foo" {
119+
project = "${google_runtimeconfig_config.config.project}"
120+
config = "${google_runtimeconfig_config.config.name}"
121+
role = "%{role}"
122+
member = "user:[email protected]"
123+
}
124+
`, context)
125+
}
126+
127+
func testAccRuntimeconfigConfigIamPolicy_basicGenerated(context map[string]interface{}) string {
128+
return Nprintf(`
129+
resource "google_runtimeconfig_config" "config" {
130+
name = "my-config%{random_suffix}"
131+
description = "Runtime configuration values for my service"
132+
}
133+
134+
data "google_iam_policy" "foo" {
135+
binding {
136+
role = "%{role}"
137+
members = ["user:[email protected]"]
138+
}
139+
}
140+
141+
resource "google_runtimeconfig_config_iam_policy" "foo" {
142+
project = "${google_runtimeconfig_config.config.project}"
143+
config = "${google_runtimeconfig_config.config.name}"
144+
policy_data = "${data.google_iam_policy.foo.policy_data}"
145+
}
146+
`, context)
147+
}
148+
149+
func testAccRuntimeconfigConfigIamBinding_basicGenerated(context map[string]interface{}) string {
150+
return Nprintf(`
151+
resource "google_runtimeconfig_config" "config" {
152+
name = "my-config%{random_suffix}"
153+
description = "Runtime configuration values for my service"
154+
}
155+
156+
resource "google_runtimeconfig_config_iam_binding" "foo" {
157+
project = "${google_runtimeconfig_config.config.project}"
158+
config = "${google_runtimeconfig_config.config.name}"
159+
role = "%{role}"
160+
members = ["user:[email protected]"]
161+
}
162+
`, context)
163+
}
164+
165+
func testAccRuntimeconfigConfigIamBinding_updateGenerated(context map[string]interface{}) string {
166+
return Nprintf(`
167+
resource "google_runtimeconfig_config" "config" {
168+
name = "my-config%{random_suffix}"
169+
description = "Runtime configuration values for my service"
170+
}
171+
172+
resource "google_runtimeconfig_config_iam_binding" "foo" {
173+
project = "${google_runtimeconfig_config.config.project}"
174+
config = "${google_runtimeconfig_config.config.name}"
175+
role = "%{role}"
176+
members = ["user:[email protected]", "user:[email protected]"]
177+
}
178+
`, context)
179+
}

0 commit comments

Comments
 (0)