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

Support container analysis note IAM #14706

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
3 changes: 3 additions & 0 deletions .changelog/8006.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
`google_container_analysis_note_iam_*`
```
218 changes: 218 additions & 0 deletions google/iam_container_analysis_note.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
// ----------------------------------------------------------------------------
//
// *** 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"

"github.com/hashicorp/terraform-provider-google/google/tpgiamresource"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

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

type ContainerAnalysisNoteIamUpdater struct {
project string
note string
d tpgresource.TerraformResourceData
Config *transport_tpg.Config
}

func ContainerAnalysisNoteIamUpdaterProducer(d tpgresource.TerraformResourceData, config *transport_tpg.Config) (tpgiamresource.ResourceIamUpdater, error) {
values := make(map[string]string)

project, _ := getProject(d, config)
if project != "" {
if err := d.Set("project", project); err != nil {
return nil, fmt.Errorf("Error setting project: %s", err)
}
}
values["project"] = project
if v, ok := d.GetOk("note"); ok {
values["note"] = v.(string)
}

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

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

u := &ContainerAnalysisNoteIamUpdater{
project: values["project"],
note: values["note"],
d: d,
Config: config,
}

if err := d.Set("project", u.project); err != nil {
return nil, fmt.Errorf("Error setting project: %s", err)
}
if err := d.Set("note", u.GetResourceId()); err != nil {
return nil, fmt.Errorf("Error setting note: %s", err)
}

return u, nil
}

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

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

m, err := tpgresource.GetImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/notes/(?P<note>[^/]+)", "(?P<project>[^/]+)/(?P<note>[^/]+)", "(?P<note>[^/]+)"}, d, config, d.Id())
if err != nil {
return err
}

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

u := &ContainerAnalysisNoteIamUpdater{
project: values["project"],
note: values["note"],
d: d,
Config: config,
}
if err := d.Set("note", u.GetResourceId()); err != nil {
return fmt.Errorf("Error setting note: %s", err)
}
d.SetId(u.GetResourceId())
return nil
}

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

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

userAgent, err := tpgresource.GenerateUserAgentString(u.d, u.Config.UserAgent)
if err != nil {
return nil, err
}

policy, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: u.Config,
Method: "POST",
Project: project,
RawURL: url,
UserAgent: userAgent,
Body: obj,
})
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

out := &cloudresourcemanager.Policy{}
err = tpgresource.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 *ContainerAnalysisNoteIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
json, err := tpgresource.ConvertToMap(policy)
if err != nil {
return err
}

obj := make(map[string]interface{})
obj["policy"] = json

url, err := u.qualifyNoteUrl("setIamPolicy")
if err != nil {
return err
}
project, err := tpgresource.GetProject(u.d, u.Config)
if err != nil {
return err
}

userAgent, err := tpgresource.GenerateUserAgentString(u.d, u.Config.UserAgent)
if err != nil {
return err
}

_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: u.Config,
Method: "POST",
Project: project,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: 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 *ContainerAnalysisNoteIamUpdater) qualifyNoteUrl(methodIdentifier string) (string, error) {
urlTemplate := fmt.Sprintf("{{ContainerAnalysisBasePath}}%s:%s", fmt.Sprintf("projects/%s/notes/%s", u.project, u.note), methodIdentifier)
url, err := tpgresource.ReplaceVars(u.d, u.Config, urlTemplate)
if err != nil {
return "", err
}
return url, nil
}

func (u *ContainerAnalysisNoteIamUpdater) GetResourceId() string {
return fmt.Sprintf("projects/%s/notes/%s", u.project, u.note)
}

func (u *ContainerAnalysisNoteIamUpdater) GetMutexKey() string {
return fmt.Sprintf("iam-containeranalysis-note-%s", u.GetResourceId())
}

func (u *ContainerAnalysisNoteIamUpdater) DescribeResource() string {
return fmt.Sprintf("containeranalysis note %q", u.GetResourceId())
}
Loading