Skip to content

Commit

Permalink
Allow updating of resource policies in compute_disk
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
chrissng authored and modular-magician committed Sep 9, 2019
1 parent f160b35 commit 04002f5
Show file tree
Hide file tree
Showing 9 changed files with 1,754 additions and 48 deletions.
6 changes: 4 additions & 2 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,9 @@ func Provider() terraform.ResourceProvider {
return provider
}

// Generated resources: 78
// Generated resources: 80
// Generated IAM resources: 18
// Total generated resources: 96
// Total generated resources: 98
func ResourceMap() map[string]*schema.Resource {
resourceMap, _ := ResourceMapWithErrors()
return resourceMap
Expand Down Expand Up @@ -445,6 +445,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_compute_backend_service": resourceComputeBackendService(),
"google_compute_region_backend_service": resourceComputeRegionBackendService(),
"google_compute_backend_service_signed_url_key": resourceComputeBackendServiceSignedUrlKey(),
"google_compute_disk_resource_policy_attachment": resourceComputeDiskResourcePolicyAttachment(),
"google_compute_disk": resourceComputeDisk(),
"google_compute_firewall": resourceComputeFirewall(),
"google_compute_forwarding_rule": resourceComputeForwardingRule(),
Expand All @@ -462,6 +463,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_compute_node_template": resourceComputeNodeTemplate(),
"google_compute_region_autoscaler": resourceComputeRegionAutoscaler(),
"google_compute_region_disk": resourceComputeRegionDisk(),
"google_compute_resource_policy": resourceComputeResourcePolicy(),
"google_compute_route": resourceComputeRoute(),
"google_compute_router": resourceComputeRouter(),
"google_compute_snapshot": resourceComputeSnapshot(),
Expand Down
331 changes: 331 additions & 0 deletions google/resource_compute_disk_resource_policy_attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,331 @@
// ----------------------------------------------------------------------------
//
// *** 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"
"log"
"reflect"
"time"

"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
)

func resourceComputeDiskResourcePolicyAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceComputeDiskResourcePolicyAttachmentCreate,
Read: resourceComputeDiskResourcePolicyAttachmentRead,
Delete: resourceComputeDiskResourcePolicyAttachmentDelete,

Importer: &schema.ResourceImporter{
State: resourceComputeDiskResourcePolicyAttachmentImport,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(4 * time.Minute),
Delete: schema.DefaultTimeout(4 * time.Minute),
},

Schema: map[string]*schema.Schema{
"disk": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"zone": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}

func resourceComputeDiskResourcePolicyAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

obj := make(map[string]interface{})
nameProp, err := expandComputeDiskResourcePolicyAttachmentName(d.Get("name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
obj["name"] = nameProp
}

obj, err = resourceComputeDiskResourcePolicyAttachmentEncoder(d, meta, obj)
if err != nil {
return err
}

url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/disks/{{disk}}/addResourcePolicies")
if err != nil {
return err
}

log.Printf("[DEBUG] Creating new DiskResourcePolicyAttachment: %#v", obj)
project, err := getProject(d, config)
if err != nil {
return err
}
res, err := sendRequestWithTimeout(config, "POST", project, url, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating DiskResourcePolicyAttachment: %s", err)
}

// Store the ID now
id, err := replaceVars(d, config, "{{project}}/{{zone}}/{{disk}}/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

op := &compute.Operation{}
err = Convert(res, op)
if err != nil {
return err
}

waitErr := computeOperationWaitTime(
config.clientCompute, op, project, "Creating DiskResourcePolicyAttachment",
int(d.Timeout(schema.TimeoutCreate).Minutes()))

if waitErr != nil {
// The resource didn't actually create
d.SetId("")
return fmt.Errorf("Error waiting to create DiskResourcePolicyAttachment: %s", waitErr)
}

log.Printf("[DEBUG] Finished creating DiskResourcePolicyAttachment %q: %#v", d.Id(), res)

return resourceComputeDiskResourcePolicyAttachmentRead(d, meta)
}

func resourceComputeDiskResourcePolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/disks/{{disk}}")
if err != nil {
return err
}

project, err := getProject(d, config)
if err != nil {
return err
}
res, err := sendRequest(config, "GET", project, url, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("ComputeDiskResourcePolicyAttachment %q", d.Id()))
}

res, err = flattenNestedComputeDiskResourcePolicyAttachment(d, meta, res)
if err != nil {
return err
}

if res == nil {
// Object isn't there any more - remove it from the state.
log.Printf("[DEBUG] Removing ComputeDiskResourcePolicyAttachment because it couldn't be matched.")
d.SetId("")
return nil
}

res, err = resourceComputeDiskResourcePolicyAttachmentDecoder(d, meta, res)
if err != nil {
return err
}

if res == nil {
// Decoding the object has resulted in it being gone. It may be marked deleted
log.Printf("[DEBUG] Removing ComputeDiskResourcePolicyAttachment because it no longer exists.")
d.SetId("")
return nil
}

if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error reading DiskResourcePolicyAttachment: %s", err)
}

if err := d.Set("name", flattenComputeDiskResourcePolicyAttachmentName(res["name"], d)); err != nil {
return fmt.Errorf("Error reading DiskResourcePolicyAttachment: %s", err)
}

return nil
}

func resourceComputeDiskResourcePolicyAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/disks/{{disk}}/removeResourcePolicies")
if err != nil {
return err
}

var obj map[string]interface{}
obj = make(map[string]interface{})

// projects/{project}/regions/{region}/resourcePolicies/{resourceId}
region := getRegionFromZone(d.Get("zone").(string))

name, err := expandComputeDiskResourcePolicyAttachmentName(d.Get("name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(name)) && (ok || !reflect.DeepEqual(v, name)) {
obj["resourcePolicies"] = []interface{}{fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", project, region, name)}
}
log.Printf("[DEBUG] Deleting DiskResourcePolicyAttachment %q", d.Id())

res, err := sendRequestWithTimeout(config, "POST", project, url, obj, d.Timeout(schema.TimeoutDelete))
if err != nil {
return handleNotFoundError(err, d, "DiskResourcePolicyAttachment")
}

op := &compute.Operation{}
err = Convert(res, op)
if err != nil {
return err
}

err = computeOperationWaitTime(
config.clientCompute, op, project, "Deleting DiskResourcePolicyAttachment",
int(d.Timeout(schema.TimeoutDelete).Minutes()))

if err != nil {
return err
}

log.Printf("[DEBUG] Finished deleting DiskResourcePolicyAttachment %q: %#v", d.Id(), res)
return nil
}

func resourceComputeDiskResourcePolicyAttachmentImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
if err := parseImportId([]string{
"projects/(?P<project>[^/]+)/zones/(?P<zone>[^/]+)/disks/(?P<disk>[^/]+)/(?P<name>[^/]+)",
"(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<disk>[^/]+)/(?P<name>[^/]+)",
"(?P<zone>[^/]+)/(?P<disk>[^/]+)/(?P<name>[^/]+)",
"(?P<disk>[^/]+)/(?P<name>[^/]+)",
}, d, config); err != nil {
return nil, err
}

// Replace import id for the resource id
id, err := replaceVars(d, config, "{{project}}/{{zone}}/{{disk}}/{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

return []*schema.ResourceData{d}, nil
}

func flattenComputeDiskResourcePolicyAttachmentName(v interface{}, d *schema.ResourceData) interface{} {
return v
}

func expandComputeDiskResourcePolicyAttachmentName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func resourceComputeDiskResourcePolicyAttachmentEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return nil, err
}

region := getRegionFromZone(d.Get("zone").(string))

obj["resourcePolicies"] = []interface{}{fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", project, region, obj["name"])}
delete(obj, "name")
return obj, nil
}

func flattenNestedComputeDiskResourcePolicyAttachment(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) {
var v interface{}
var ok bool

v, ok = res["resourcePolicies"]
if !ok || v == nil {
return nil, nil
}

switch v.(type) {
case []interface{}:
break
case map[string]interface{}:
// Construct list out of single nested resource
v = []interface{}{v}
default:
return nil, fmt.Errorf("expected list or map for value resourcePolicies. Actual value: %v", v)
}

expectedName, err := expandComputeDiskResourcePolicyAttachmentName(d.Get("name"), d, meta.(*Config))
if err != nil {
return nil, err
}

// Search list for this resource.
items := v.([]interface{})
for _, itemRaw := range items {
if itemRaw == nil {
continue
}
// List response only contains the ID - construct a response object.
item := map[string]interface{}{
"name": itemRaw,
}

// Decode list item before comparing.
item, err := resourceComputeDiskResourcePolicyAttachmentDecoder(d, meta, item)
if err != nil {
return nil, err
}

itemName := flattenComputeDiskResourcePolicyAttachmentName(item["name"], d)
if !reflect.DeepEqual(itemName, expectedName) {
log.Printf("[DEBUG] Skipping item with name= %#v, looking for %#v)", itemName, expectedName)
continue
}
log.Printf("[DEBUG] Found item for resource %q: %#v)", d.Id(), item)
return item, nil
}

return nil, nil
}

func resourceComputeDiskResourcePolicyAttachmentDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) {
res["name"] = GetResourceNameFromSelfLink(res["name"].(string))
return res, nil
}
Loading

0 comments on commit 04002f5

Please sign in to comment.