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

storage bucket iam generation #5099

Merged
merged 1 commit into from
Dec 13, 2019
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
1 change: 0 additions & 1 deletion google/iam_binary_authorization_attestor.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ func (u *BinaryAuthorizationAttestorIamUpdater) SetResourceIamPolicy(policy *clo
if err != nil {
return err
}

project, err := getProject(u.d, u.Config)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion google/iam_cloud_functions_cloud_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ func (u *CloudFunctionsCloudFunctionIamUpdater) SetResourceIamPolicy(policy *clo
if err != nil {
return err
}

project, err := getProject(u.d, u.Config)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion google/iam_cloud_run_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ func (u *CloudRunServiceIamUpdater) SetResourceIamPolicy(policy *cloudresourcema
if err != nil {
return err
}

project, err := getProject(u.d, u.Config)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion google/iam_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ func (u *ComputeInstanceIamUpdater) SetResourceIamPolicy(policy *cloudresourcema
if err != nil {
return err
}

project, err := getProject(u.d, u.Config)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion google/iam_compute_subnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ func (u *ComputeSubnetworkIamUpdater) SetResourceIamPolicy(policy *cloudresource
if err != nil {
return err
}

project, err := getProject(u.d, u.Config)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion google/iam_iap_app_engine_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ func (u *IapAppEngineServiceIamUpdater) SetResourceIamPolicy(policy *cloudresour
if err != nil {
return err
}

project, err := getProject(u.d, u.Config)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion google/iam_iap_app_engine_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ func (u *IapAppEngineVersionIamUpdater) SetResourceIamPolicy(policy *cloudresour
if err != nil {
return err
}

project, err := getProject(u.d, u.Config)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion google/iam_iap_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func (u *IapWebIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Pol
if err != nil {
return err
}

project, err := getProject(u.d, u.Config)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion google/iam_iap_web_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ func (u *IapWebBackendServiceIamUpdater) SetResourceIamPolicy(policy *cloudresou
if err != nil {
return err
}

project, err := getProject(u.d, u.Config)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion google/iam_iap_web_type_app_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ func (u *IapWebTypeAppEngineIamUpdater) SetResourceIamPolicy(policy *cloudresour
if err != nil {
return err
}

project, err := getProject(u.d, u.Config)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion google/iam_iap_web_type_compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func (u *IapWebTypeComputeIamUpdater) SetResourceIamPolicy(policy *cloudresource
if err != nil {
return err
}

project, err := getProject(u.d, u.Config)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion google/iam_pubsub_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ func (u *PubsubTopicIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanage
if err != nil {
return err
}

project, err := getProject(u.d, u.Config)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion google/iam_runtime_config_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ func (u *RuntimeConfigConfigIamUpdater) SetResourceIamPolicy(policy *cloudresour
if err != nil {
return err
}

project, err := getProject(u.d, u.Config)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion google/iam_source_repo_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ func (u *SourceRepoRepositoryIamUpdater) SetResourceIamPolicy(policy *cloudresou
if err != nil {
return err
}

project, err := getProject(u.d, u.Config)
if err != nil {
return err
Expand Down
144 changes: 97 additions & 47 deletions google/iam_storage_bucket.go
Original file line number Diff line number Diff line change
@@ -1,101 +1,151 @@
// ----------------------------------------------------------------------------
//
// *** 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"
"google.golang.org/api/storage/v1"
)

var IamStorageBucketSchema = map[string]*schema.Schema{
var StorageBucketIamSchema = map[string]*schema.Schema{
"bucket": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
}

func StorageBucketIdParseFunc(d *schema.ResourceData, _ *Config) error {
d.Set("bucket", d.Id())
return nil
}

type StorageBucketIamUpdater struct {
bucket string
d *schema.ResourceData
Config *Config
}

func NewStorageBucketIamUpdater(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) {
bucket := d.Get("bucket").(string)
func StorageBucketIamUpdaterProducer(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) {
values := make(map[string]string)

return &StorageBucketIamUpdater{
bucket: bucket,
if v, ok := d.GetOk("bucket"); ok {
values["bucket"] = v.(string)
}

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

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

u := &StorageBucketIamUpdater{
bucket: values["bucket"],
d: d,
Config: config,
}, nil
}

d.Set("bucket", u.GetResourceId())

return u, nil
}

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

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

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

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

func (u *StorageBucketIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
p, err := u.Config.clientStorage.Buckets.GetIamPolicy(u.bucket).Do()
url, err := u.qualifyBucketUrl("iam")
if err != nil {
return nil, err
}

var obj map[string]interface{}

policy, err := sendRequest(u.Config, "GET", "", url, obj)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

cloudResourcePolicy, err := storageToResourceManagerPolicy(p)
out := &cloudresourcemanager.Policy{}
err = Convert(policy, out)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Invalid IAM policy for %s: {{err}}", u.DescribeResource()), err)
return nil, errwrap.Wrapf("Cannot convert a policy to a resource manager policy: {{err}}", err)
}

return cloudResourcePolicy, nil
return out, nil
}

func (u *StorageBucketIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
storagePolicy, err := resourceManagerToStoragePolicy(policy)

json, err := ConvertToMap(policy)
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Invalid IAM policy for %s: {{err}}", u.DescribeResource()), err)
return err
}

ppolicy, err := u.Config.clientStorage.Buckets.GetIamPolicy(u.bucket).Do()
obj := json

url, err := u.qualifyBucketUrl("iam")
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err)
return err
}
storagePolicy.Etag = ppolicy.Etag
_, err = u.Config.clientStorage.Buckets.SetIamPolicy(u.bucket, storagePolicy).Do()

_, err = sendRequestWithTimeout(u.Config, "PUT", "", 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 *StorageBucketIamUpdater) qualifyBucketUrl(methodIdentifier string) (string, error) {
urlTemplate := fmt.Sprintf("{{StorageBasePath}}%s/%s", fmt.Sprintf("b/%s", u.bucket), methodIdentifier)
url, err := replaceVars(u.d, u.Config, urlTemplate)
if err != nil {
return "", err
}
return url, nil
}

func (u *StorageBucketIamUpdater) GetResourceId() string {
return u.bucket
return fmt.Sprintf("b/%s", u.bucket)
}

func (u *StorageBucketIamUpdater) GetMutexKey() string {
return fmt.Sprintf("iam-storage-bucket-%s", u.bucket)
return fmt.Sprintf("iam-storage-bucket-%s", u.GetResourceId())
}

func (u *StorageBucketIamUpdater) DescribeResource() string {
return fmt.Sprintf("Storage Bucket %q", u.bucket)
}

func resourceManagerToStoragePolicy(p *cloudresourcemanager.Policy) (*storage.Policy, error) {
out := &storage.Policy{}
err := Convert(p, out)
if err != nil {
return nil, errwrap.Wrapf("Cannot convert a v1 policy to a storage policy: {{err}}", err)
}
return out, nil
}

func storageToResourceManagerPolicy(p *storage.Policy) (*cloudresourcemanager.Policy, error) {
out := &cloudresourcemanager.Policy{}
err := Convert(p, out)
if err != nil {
return nil, errwrap.Wrapf("Cannot convert a storage policy to a v1 policy: {{err}}", err)
}
return out, nil
return fmt.Sprintf("storage bucket %q", u.GetResourceId())
}
Loading