-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
60af650
commit 41dfd37
Showing
20 changed files
with
377 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
Oops, something went wrong.