Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added IAM resources for disk & regionDisk #7110

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .changelog/3888.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
```release-note:new-resource
google_compute_region_disk_iam_binding
```
```release-note:new-resource
google_compute_region_disk_iam_policy
```
```release-note:new-resource
google_compute_region_disk_iam_member
```
```release-note:new-resource
google_compute_disk_iam_binding
```
```release-note:new-resource
google_compute_disk_iam_policy
```
```release-note:new-resource
google_compute_disk_iam_member
```
200 changes: 200 additions & 0 deletions google/iam_compute_disk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
//
// ----------------------------------------------------------------------------
//
// 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/helper/schema"
"google.golang.org/api/cloudresourcemanager/v1"
)

var ComputeDiskIamSchema = map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
"zone": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
}

type ComputeDiskIamUpdater struct {
project string
zone string
name string
d *schema.ResourceData
Config *Config
}

func ComputeDiskIamUpdaterProducer(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) {
values := make(map[string]string)

project, _ := getProject(d, config)
if project != "" {
d.Set("project", project)
}
values["project"] = project
zone, _ := getZone(d, config)
if zone != "" {
d.Set("zone", zone)
}
values["zone"] = zone
if v, ok := d.GetOk("name"); ok {
values["name"] = v.(string)
}

// We may have gotten either a long or short name, so attempt to parse long name if possible
m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/zones/(?P<zone>[^/]+)/disks/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<name>[^/]+)", "(?P<zone>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config, d.Get("name").(string))
if err != nil {
return nil, err
}

for k, v := range m {
values[k] = v
}

u := &ComputeDiskIamUpdater{
project: values["project"],
zone: values["zone"],
name: values["name"],
d: d,
Config: config,
}

d.Set("project", u.project)
d.Set("zone", u.zone)
d.Set("name", u.GetResourceId())

return u, nil
}

func ComputeDiskIdParseFunc(d *schema.ResourceData, config *Config) error {
values := make(map[string]string)

project, _ := getProject(d, config)
if project != "" {
values["project"] = project
}

zone, _ := getZone(d, config)
if zone != "" {
values["zone"] = zone
}

m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/zones/(?P<zone>[^/]+)/disks/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<name>[^/]+)", "(?P<zone>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config, d.Id())
if err != nil {
return err
}

for k, v := range m {
values[k] = v
}

u := &ComputeDiskIamUpdater{
project: values["project"],
zone: values["zone"],
name: values["name"],
d: d,
Config: config,
}
d.Set("name", u.GetResourceId())
d.SetId(u.GetResourceId())
return nil
}

func (u *ComputeDiskIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
url, err := u.qualifyDiskUrl("getIamPolicy")
if err != nil {
return nil, err
}

project, err := getProject(u.d, u.Config)
if err != nil {
return nil, err
}
var obj map[string]interface{}

policy, err := sendRequest(u.Config, "GET", project, url, 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 *ComputeDiskIamUpdater) 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.qualifyDiskUrl("setIamPolicy")
if err != nil {
return err
}
project, err := getProject(u.d, u.Config)
if err != nil {
return err
}

_, err = sendRequestWithTimeout(u.Config, "POST", project, url, 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 *ComputeDiskIamUpdater) qualifyDiskUrl(methodIdentifier string) (string, error) {
urlTemplate := fmt.Sprintf("{{ComputeBasePath}}%s/%s", fmt.Sprintf("projects/%s/zones/%s/disks/%s", u.project, u.zone, u.name), methodIdentifier)
url, err := replaceVars(u.d, u.Config, urlTemplate)
if err != nil {
return "", err
}
return url, nil
}

func (u *ComputeDiskIamUpdater) GetResourceId() string {
return fmt.Sprintf("projects/%s/zones/%s/disks/%s", u.project, u.zone, u.name)
}

func (u *ComputeDiskIamUpdater) GetMutexKey() string {
return fmt.Sprintf("iam-compute-disk-%s", u.GetResourceId())
}

func (u *ComputeDiskIamUpdater) DescribeResource() string {
return fmt.Sprintf("compute disk %q", u.GetResourceId())
}
Loading