-
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.
generate compute instance and subnetwork iam
Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
b61e5ec
commit 804ba8a
Showing
23 changed files
with
1,042 additions
and
600 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,147 +1,191 @@ | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// *** 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" | ||
"strings" | ||
|
||
"github.com/hashicorp/errwrap" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"google.golang.org/api/cloudresourcemanager/v1" | ||
"google.golang.org/api/compute/v1" | ||
) | ||
|
||
var IamComputeInstanceSchema = map[string]*schema.Schema{ | ||
"instance_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
var ComputeInstanceIamSchema = map[string]*schema.Schema{ | ||
"project": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"zone": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"instance_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
DiffSuppressFunc: compareSelfLinkOrResourceName, | ||
}, | ||
} | ||
|
||
type ComputeInstanceIamUpdater struct { | ||
project string | ||
zone string | ||
resourceId string | ||
Config *Config | ||
project string | ||
zone string | ||
instanceName string | ||
d *schema.ResourceData | ||
Config *Config | ||
} | ||
|
||
func NewComputeInstanceIamUpdater(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) { | ||
func ComputeInstanceIamUpdaterProducer(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) { | ||
values := make(map[string]string) | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
values["project"] = project | ||
zone, err := getZone(d, config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
values["zone"] = zone | ||
if v, ok := d.GetOk("instance_name"); ok { | ||
values["instance_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>[^/]+)/instances/(?P<instance>[^/]+)", "(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<instance>[^/]+)", "(?P<zone>[^/]+)/(?P<instance>[^/]+)", "(?P<instance>[^/]+)"}, d, config, d.Get("instance_name").(string)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &ComputeInstanceIamUpdater{ | ||
project: project, | ||
zone: zone, | ||
resourceId: d.Get("instance_name").(string), | ||
Config: config, | ||
}, nil | ||
for k, v := range m { | ||
values[k] = v | ||
} | ||
|
||
u := &ComputeInstanceIamUpdater{ | ||
project: values["project"], | ||
zone: values["zone"], | ||
instanceName: values["instance_name"], | ||
d: d, | ||
Config: config, | ||
} | ||
|
||
d.Set("project", u.project) | ||
d.Set("zone", u.zone) | ||
d.Set("instance_name", u.GetResourceId()) | ||
|
||
return u, nil | ||
} | ||
|
||
func ComputeInstanceIdParseFunc(d *schema.ResourceData, config *Config) error { | ||
parts := strings.Split(d.Id(), "/") | ||
var fv *ZonalFieldValue | ||
if len(parts) == 3 { | ||
// {project}/{zone}/{name} syntax | ||
fv = &ZonalFieldValue{ | ||
Project: parts[0], | ||
Zone: parts[1], | ||
Name: parts[2], | ||
resourceType: "instances", | ||
} | ||
} else if len(parts) == 2 { | ||
// /{zone}/{name} syntax | ||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
fv = &ZonalFieldValue{ | ||
Project: project, | ||
Zone: parts[0], | ||
Name: parts[1], | ||
resourceType: "instances", | ||
} | ||
} else { | ||
// We either have a name or a full self link, so use the field helper | ||
var err error | ||
fv, err = ParseInstanceFieldValue(d.Id(), d, config) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
d.Set("project", fv.Project) | ||
d.Set("zone", fv.Zone) | ||
d.Set("instance_name", fv.Name) | ||
|
||
// Explicitly set the id so imported resources have the same ID format as non-imported ones. | ||
d.SetId(fv.RelativeLink()) | ||
values := make(map[string]string) | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
values["project"] = project | ||
zone, err := getZone(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
values["zone"] = zone | ||
|
||
m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/zones/(?P<zone>[^/]+)/instances/(?P<instance>[^/]+)", "(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<instance>[^/]+)", "(?P<zone>[^/]+)/(?P<instance>[^/]+)", "(?P<instance>[^/]+)"}, d, config, d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for k, v := range m { | ||
values[k] = v | ||
} | ||
|
||
u := &ComputeInstanceIamUpdater{ | ||
project: values["project"], | ||
zone: values["zone"], | ||
instanceName: values["instance_name"], | ||
d: d, | ||
Config: config, | ||
} | ||
d.Set("instance_name", u.GetResourceId()) | ||
d.SetId(u.GetResourceId()) | ||
return nil | ||
} | ||
|
||
func (u *ComputeInstanceIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) { | ||
p, err := u.Config.clientCompute.Instances.GetIamPolicy(u.project, u.zone, u.resourceId).Do() | ||
url := u.qualifyInstanceUrl("getIamPolicy") | ||
|
||
project, err := getProject(u.d, u.Config) | ||
if err != nil { | ||
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err) | ||
return nil, err | ||
} | ||
var obj map[string]interface{} | ||
|
||
cloudResourcePolicy, err := computeToResourceManagerPolicy(p) | ||
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(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 *ComputeInstanceIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error { | ||
computePolicy, err := resourceManagerToComputePolicy(policy) | ||
|
||
json, err := ConvertToMap(policy) | ||
if err != nil { | ||
return errwrap.Wrapf(fmt.Sprintf("Invalid IAM policy for %s: {{err}}", u.DescribeResource()), err) | ||
return err | ||
} | ||
|
||
req := &compute.ZoneSetPolicyRequest{ | ||
Policy: computePolicy, | ||
obj := make(map[string]interface{}) | ||
obj["policy"] = json | ||
|
||
url := u.qualifyInstanceUrl("setIamPolicy") | ||
|
||
project, err := getProject(u.d, u.Config) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = u.Config.clientCompute.Instances.SetIamPolicy(u.project, u.zone, u.resourceId, req).Do() | ||
|
||
_, 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 *ComputeInstanceIamUpdater) qualifyInstanceUrl(methodIdentifier string) string { | ||
return fmt.Sprintf("https://www.googleapis.com/compute/v1/%s/%s", fmt.Sprintf("projects/%s/zones/%s/instances/%s", u.project, u.zone, u.instanceName), methodIdentifier) | ||
} | ||
|
||
func (u *ComputeInstanceIamUpdater) GetResourceId() string { | ||
return fmt.Sprintf("projects/%s/zones/%s/instances/%s", u.project, u.zone, u.resourceId) | ||
return fmt.Sprintf("projects/%s/zones/%s/instances/%s", u.project, u.zone, u.instanceName) | ||
} | ||
|
||
func (u *ComputeInstanceIamUpdater) GetMutexKey() string { | ||
return fmt.Sprintf("iam-compute-Instance-%s-%s-%s", u.project, u.zone, u.resourceId) | ||
return fmt.Sprintf("iam-compute-instance-%s", u.GetResourceId()) | ||
} | ||
|
||
func (u *ComputeInstanceIamUpdater) DescribeResource() string { | ||
return fmt.Sprintf("Compute Instance %s/%s/%s", u.project, u.zone, u.resourceId) | ||
return fmt.Sprintf("compute instance %q", u.GetResourceId()) | ||
} |
Oops, something went wrong.