Skip to content

Commit

Permalink
add support for iam resources for google_compute_image (#3887) (#7070)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Aug 20, 2020
1 parent d2c28d1 commit 09801c6
Show file tree
Hide file tree
Showing 6 changed files with 568 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .changelog/3887.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```release-note:new-resource
`google_compute_image_iam_binding`
```
```release-note:new-resource
`google_compute_image_iam_member`
```
```release-note:new-resource
`google_compute_image_iam_policy`
```
184 changes: 184 additions & 0 deletions google/iam_compute_image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// ----------------------------------------------------------------------------
//
// *** 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 ComputeImageIamSchema = map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
"image": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
}

type ComputeImageIamUpdater struct {
project string
image string
d *schema.ResourceData
Config *Config
}

func ComputeImageIamUpdaterProducer(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
if v, ok := d.GetOk("image"); ok {
values["image"] = 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>[^/]+)/global/images/(?P<image>[^/]+)", "(?P<project>[^/]+)/(?P<image>[^/]+)", "(?P<image>[^/]+)"}, d, config, d.Get("image").(string))
if err != nil {
return nil, err
}

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

u := &ComputeImageIamUpdater{
project: values["project"],
image: values["image"],
d: d,
Config: config,
}

d.Set("project", u.project)
d.Set("image", u.GetResourceId())

return u, nil
}

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

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

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

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

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

func (u *ComputeImageIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
url, err := u.qualifyImageUrl("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{}
url, err = addQueryParams(url, map[string]string{"optionsRequestedPolicyVersion": fmt.Sprintf("%d", iamPolicyVersion)})
if err != nil {
return nil, err
}

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 *ComputeImageIamUpdater) 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.qualifyImageUrl("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 *ComputeImageIamUpdater) qualifyImageUrl(methodIdentifier string) (string, error) {
urlTemplate := fmt.Sprintf("{{ComputeBasePath}}%s/%s", fmt.Sprintf("projects/%s/global/images/%s", u.project, u.image), methodIdentifier)
url, err := replaceVars(u.d, u.Config, urlTemplate)
if err != nil {
return "", err
}
return url, nil
}

func (u *ComputeImageIamUpdater) GetResourceId() string {
return fmt.Sprintf("projects/%s/global/images/%s", u.project, u.image)
}

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

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

0 comments on commit 09801c6

Please sign in to comment.