Skip to content

Commit

Permalink
[WIP] 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 Aug 22, 2019
1 parent c2b754e commit b79eebb
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
35 changes: 34 additions & 1 deletion google-beta/resource_compute_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ func resourceComputeDisk() *schema.Resource {
"resource_policies": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
DiffSuppressFunc: compareSelfLinkOrResourceName,
Expand Down Expand Up @@ -761,6 +760,40 @@ func resourceComputeDiskUpdate(d *schema.ResourceData, meta interface{}) error {

d.SetPartial("size")
}
if d.HasChange("resource_policies") {
obj := make(map[string]interface{})
resourcePoliciesProp, err := expandComputeDiskResourcePolicies(d.Get("resource_policies"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("resource_policies"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, resourcePoliciesProp)) {
obj["resourcePolicies"] = resourcePoliciesProp
}

url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/disks/{{name}}/addResourcePolicies")
if err != nil {
return err
}
res, err := sendRequestWithTimeout(config, "POST", project, url, obj, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("Error updating Disk %q: %s", d.Id(), err)
}

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

err = computeOperationWaitTime(
config.clientCompute, op, project, "Updating Disk",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}

d.SetPartial("resource_policies")
}

d.Partial(false)

Expand Down
88 changes: 87 additions & 1 deletion google-beta/resource_compute_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"google.golang.org/api/compute/v1"
computeBeta "google.golang.org/api/compute/v0.beta"
compute "google.golang.org/api/compute/v1"
)

func TestDiskImageDiffSuppress(t *testing.T) {
Expand Down Expand Up @@ -442,6 +443,8 @@ func TestAccComputeDisk_resourcePolicies(t *testing.T) {

diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
policyName := fmt.Sprintf("tf-test-policy-%s", acctest.RandString(10))
policyName2 := fmt.Sprintf("tf-test-policy-%s-bar", acctest.RandString(10))
var disk computeBeta.Disk

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -455,10 +458,65 @@ func TestAccComputeDisk_resourcePolicies(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeDisk_resourcePolicies_updated(diskName, policyName2),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeBetaDiskExists(
"google_compute_disk.foobar", getTestProjectFromEnv(), &disk),
testAccCheckComputeDiskHasResourcePolicies(
&disk, policyName2),
),
},
},
})
}

func testAccCheckComputeBetaDiskExists(n, p string, disk *computeBeta.Disk) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

config := testAccProvider.Meta().(*Config)

found, err := config.clientComputeBeta.Disks.Get(
p, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
if err != nil {
return err
}

if found.Name != rs.Primary.ID {
return fmt.Errorf("Disk not found")
}

*disk = *found

return nil
}
}

func testAccCheckComputeDiskHasResourcePolicies(disk *computeBeta.Disk, policyName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
found := false
for _, v := range disk.ResourcePolicies {
if v == policyName {
found = true
}
}

if !found {
return fmt.Errorf("Resource policy is wrong. Found: %s", disk.ResourcePolicies)
}

return nil
}
}

func testAccCheckComputeDiskExists(n, p string, disk *compute.Disk) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -820,3 +878,31 @@ resource "google_compute_disk" "foobar" {
resource_policies = [google_compute_resource_policy.foo.self_link]
}`, policyName, diskName)
}

func testAccComputeDisk_resourcePolicies_updated(diskName, policyName string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-9"
project = "debian-cloud"
}
resource "google_compute_resource_policy" "bar" {
name = "%s"
region = "us-central1"
snapshot_schedule_policy {
schedule {
daily_schedule {
days_in_cycle = 3
start_time = "06:00"
}
}
}
}
resource "google_compute_disk" "foobar" {
name = "%s"
image = "${data.google_compute_image.my_image.self_link}"
size = 50
type = "pd-ssd"
zone = "us-central1-a"
resource_policies = [google_compute_resource_policy.bar.self_link]
}`, policyName, diskName)
}

0 comments on commit b79eebb

Please sign in to comment.