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