Skip to content

Commit caaf99d

Browse files
Adding IAM resources for Dataplex Lake, Zone and Asset (#7436) (#14046)
Signed-off-by: Modular Magician <[email protected]>
1 parent b164571 commit caaf99d

14 files changed

+2320
-14
lines changed

.changelog/7436.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
```release-note:new-resource
2+
`google_dataplex_lake_iam_*`
3+
```
4+
```release-note:new-resource
5+
`google_dataplex_zone_iam_*`
6+
```
7+
```release-note:new-resource
8+
`google_dataplex_asset_iam_*`
9+
```

google/config.go

+4
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ type Config struct {
207207
DataCatalogBasePath string
208208
DataFusionBasePath string
209209
DataLossPreventionBasePath string
210+
DataplexBasePath string
210211
DataprocBasePath string
211212
DataprocMetastoreBasePath string
212213
DatastoreBasePath string
@@ -313,6 +314,7 @@ const ContainerAttachedBasePathKey = "ContainerAttached"
313314
const DataCatalogBasePathKey = "DataCatalog"
314315
const DataFusionBasePathKey = "DataFusion"
315316
const DataLossPreventionBasePathKey = "DataLossPrevention"
317+
const DataplexBasePathKey = "Dataplex"
316318
const DataprocBasePathKey = "Dataproc"
317319
const DataprocMetastoreBasePathKey = "DataprocMetastore"
318320
const DatastoreBasePathKey = "Datastore"
@@ -413,6 +415,7 @@ var DefaultBasePaths = map[string]string{
413415
DataCatalogBasePathKey: "https://datacatalog.googleapis.com/v1/",
414416
DataFusionBasePathKey: "https://datafusion.googleapis.com/v1/",
415417
DataLossPreventionBasePathKey: "https://dlp.googleapis.com/v2/",
418+
DataplexBasePathKey: "https://dataplex.googleapis.com/v1/",
416419
DataprocBasePathKey: "https://dataproc.googleapis.com/v1/",
417420
DataprocMetastoreBasePathKey: "https://metastore.googleapis.com/v1/",
418421
DatastoreBasePathKey: "https://datastore.googleapis.com/v1/",
@@ -1289,6 +1292,7 @@ func ConfigureBasePaths(c *Config) {
12891292
c.DataCatalogBasePath = DefaultBasePaths[DataCatalogBasePathKey]
12901293
c.DataFusionBasePath = DefaultBasePaths[DataFusionBasePathKey]
12911294
c.DataLossPreventionBasePath = DefaultBasePaths[DataLossPreventionBasePathKey]
1295+
c.DataplexBasePath = DefaultBasePaths[DataplexBasePathKey]
12921296
c.DataprocBasePath = DefaultBasePaths[DataprocBasePathKey]
12931297
c.DataprocMetastoreBasePath = DefaultBasePaths[DataprocMetastoreBasePathKey]
12941298
c.DatastoreBasePath = DefaultBasePaths[DatastoreBasePathKey]

google/config_test_utils.go

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func configureTestBasePaths(c *Config, url string) {
5353
c.DataCatalogBasePath = url
5454
c.DataFusionBasePath = url
5555
c.DataLossPreventionBasePath = url
56+
c.DataplexBasePath = url
5657
c.DataprocBasePath = url
5758
c.DataprocMetastoreBasePath = url
5859
c.DatastoreBasePath = url

google/iam_dataplex_asset.go

+253
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
// ----------------------------------------------------------------------------
2+
//
3+
// *** AUTO GENERATED CODE *** Type: MMv1 ***
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+
20+
"github.com/hashicorp/errwrap"
21+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
22+
"google.golang.org/api/cloudresourcemanager/v1"
23+
)
24+
25+
var DataplexAssetIamSchema = map[string]*schema.Schema{
26+
"project": {
27+
Type: schema.TypeString,
28+
Computed: true,
29+
Optional: true,
30+
ForceNew: true,
31+
},
32+
"location": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
Optional: true,
36+
ForceNew: true,
37+
},
38+
"lake": {
39+
Type: schema.TypeString,
40+
Required: true,
41+
ForceNew: true,
42+
},
43+
"dataplex_zone": {
44+
Type: schema.TypeString,
45+
Required: true,
46+
ForceNew: true,
47+
},
48+
"asset": {
49+
Type: schema.TypeString,
50+
Required: true,
51+
ForceNew: true,
52+
DiffSuppressFunc: compareSelfLinkOrResourceName,
53+
},
54+
}
55+
56+
type DataplexAssetIamUpdater struct {
57+
project string
58+
location string
59+
lake string
60+
dataplexZone string
61+
asset string
62+
d TerraformResourceData
63+
Config *Config
64+
}
65+
66+
func DataplexAssetIamUpdaterProducer(d TerraformResourceData, config *Config) (ResourceIamUpdater, error) {
67+
values := make(map[string]string)
68+
69+
project, _ := getProject(d, config)
70+
if project != "" {
71+
if err := d.Set("project", project); err != nil {
72+
return nil, fmt.Errorf("Error setting project: %s", err)
73+
}
74+
}
75+
values["project"] = project
76+
location, _ := getLocation(d, config)
77+
if location != "" {
78+
if err := d.Set("location", location); err != nil {
79+
return nil, fmt.Errorf("Error setting location: %s", err)
80+
}
81+
}
82+
values["location"] = location
83+
if v, ok := d.GetOk("lake"); ok {
84+
values["lake"] = v.(string)
85+
}
86+
87+
if v, ok := d.GetOk("dataplex_zone"); ok {
88+
values["dataplex_zone"] = v.(string)
89+
}
90+
91+
if v, ok := d.GetOk("asset"); ok {
92+
values["asset"] = v.(string)
93+
}
94+
95+
// We may have gotten either a long or short name, so attempt to parse long name if possible
96+
m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/lakes/(?P<lake>[^/]+)/zones/(?P<dataplex_zone>[^/]+)/assets/(?P<asset>[^/]+)", "(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<lake>[^/]+)/(?P<dataplex_zone>[^/]+)/(?P<asset>[^/]+)", "(?P<location>[^/]+)/(?P<lake>[^/]+)/(?P<dataplex_zone>[^/]+)/(?P<asset>[^/]+)", "(?P<asset>[^/]+)"}, d, config, d.Get("asset").(string))
97+
if err != nil {
98+
return nil, err
99+
}
100+
101+
for k, v := range m {
102+
values[k] = v
103+
}
104+
105+
u := &DataplexAssetIamUpdater{
106+
project: values["project"],
107+
location: values["location"],
108+
lake: values["lake"],
109+
dataplexZone: values["dataplex_zone"],
110+
asset: values["asset"],
111+
d: d,
112+
Config: config,
113+
}
114+
115+
if err := d.Set("project", u.project); err != nil {
116+
return nil, fmt.Errorf("Error setting project: %s", err)
117+
}
118+
if err := d.Set("location", u.location); err != nil {
119+
return nil, fmt.Errorf("Error setting location: %s", err)
120+
}
121+
if err := d.Set("lake", u.lake); err != nil {
122+
return nil, fmt.Errorf("Error setting lake: %s", err)
123+
}
124+
if err := d.Set("dataplex_zone", u.dataplexZone); err != nil {
125+
return nil, fmt.Errorf("Error setting dataplex_zone: %s", err)
126+
}
127+
if err := d.Set("asset", u.GetResourceId()); err != nil {
128+
return nil, fmt.Errorf("Error setting asset: %s", err)
129+
}
130+
131+
return u, nil
132+
}
133+
134+
func DataplexAssetIdParseFunc(d *schema.ResourceData, config *Config) error {
135+
values := make(map[string]string)
136+
137+
project, _ := getProject(d, config)
138+
if project != "" {
139+
values["project"] = project
140+
}
141+
142+
location, _ := getLocation(d, config)
143+
if location != "" {
144+
values["location"] = location
145+
}
146+
147+
m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/lakes/(?P<lake>[^/]+)/zones/(?P<dataplex_zone>[^/]+)/assets/(?P<asset>[^/]+)", "(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<lake>[^/]+)/(?P<dataplex_zone>[^/]+)/(?P<asset>[^/]+)", "(?P<location>[^/]+)/(?P<lake>[^/]+)/(?P<dataplex_zone>[^/]+)/(?P<asset>[^/]+)", "(?P<asset>[^/]+)"}, d, config, d.Id())
148+
if err != nil {
149+
return err
150+
}
151+
152+
for k, v := range m {
153+
values[k] = v
154+
}
155+
156+
u := &DataplexAssetIamUpdater{
157+
project: values["project"],
158+
location: values["location"],
159+
lake: values["lake"],
160+
dataplexZone: values["dataplex_zone"],
161+
asset: values["asset"],
162+
d: d,
163+
Config: config,
164+
}
165+
if err := d.Set("asset", u.GetResourceId()); err != nil {
166+
return fmt.Errorf("Error setting asset: %s", err)
167+
}
168+
d.SetId(u.GetResourceId())
169+
return nil
170+
}
171+
172+
func (u *DataplexAssetIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
173+
url, err := u.qualifyAssetUrl("getIamPolicy")
174+
if err != nil {
175+
return nil, err
176+
}
177+
178+
project, err := getProject(u.d, u.Config)
179+
if err != nil {
180+
return nil, err
181+
}
182+
var obj map[string]interface{}
183+
184+
userAgent, err := generateUserAgentString(u.d, u.Config.UserAgent)
185+
if err != nil {
186+
return nil, err
187+
}
188+
189+
policy, err := SendRequest(u.Config, "GET", project, url, userAgent, obj)
190+
if err != nil {
191+
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
192+
}
193+
194+
out := &cloudresourcemanager.Policy{}
195+
err = Convert(policy, out)
196+
if err != nil {
197+
return nil, errwrap.Wrapf("Cannot convert a policy to a resource manager policy: {{err}}", err)
198+
}
199+
200+
return out, nil
201+
}
202+
203+
func (u *DataplexAssetIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
204+
json, err := ConvertToMap(policy)
205+
if err != nil {
206+
return err
207+
}
208+
209+
obj := make(map[string]interface{})
210+
obj["policy"] = json
211+
212+
url, err := u.qualifyAssetUrl("setIamPolicy")
213+
if err != nil {
214+
return err
215+
}
216+
project, err := getProject(u.d, u.Config)
217+
if err != nil {
218+
return err
219+
}
220+
221+
userAgent, err := generateUserAgentString(u.d, u.Config.UserAgent)
222+
if err != nil {
223+
return err
224+
}
225+
226+
_, err = SendRequestWithTimeout(u.Config, "POST", project, url, userAgent, obj, u.d.Timeout(schema.TimeoutCreate))
227+
if err != nil {
228+
return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err)
229+
}
230+
231+
return nil
232+
}
233+
234+
func (u *DataplexAssetIamUpdater) qualifyAssetUrl(methodIdentifier string) (string, error) {
235+
urlTemplate := fmt.Sprintf("{{DataplexBasePath}}%s:%s", fmt.Sprintf("projects/%s/locations/%s/lakes/%s/zones/%s/assets/%s", u.project, u.location, u.lake, u.dataplexZone, u.asset), methodIdentifier)
236+
url, err := replaceVars(u.d, u.Config, urlTemplate)
237+
if err != nil {
238+
return "", err
239+
}
240+
return url, nil
241+
}
242+
243+
func (u *DataplexAssetIamUpdater) GetResourceId() string {
244+
return fmt.Sprintf("projects/%s/locations/%s/lakes/%s/zones/%s/assets/%s", u.project, u.location, u.lake, u.dataplexZone, u.asset)
245+
}
246+
247+
func (u *DataplexAssetIamUpdater) GetMutexKey() string {
248+
return fmt.Sprintf("iam-dataplex-asset-%s", u.GetResourceId())
249+
}
250+
251+
func (u *DataplexAssetIamUpdater) DescribeResource() string {
252+
return fmt.Sprintf("dataplex asset %q", u.GetResourceId())
253+
}

0 commit comments

Comments
 (0)