From 1a6fc051babdeec45b18bd118b9e82daaa9db586 Mon Sep 17 00:00:00 2001 From: Dainis Tillers Date: Tue, 2 Sep 2014 16:52:49 +0300 Subject: [PATCH] Added - flag to set whether disk needs to be delete or not when instance terminates --- resource_compute_instance.go | 8 ++++ resource_compute_instance_test.go | 64 +++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/resource_compute_instance.go b/resource_compute_instance.go index 8766bca7..7e332dc5 100644 --- a/resource_compute_instance.go +++ b/resource_compute_instance.go @@ -60,6 +60,10 @@ func resourceComputeInstance() *schema.Resource { Type: schema.TypeString, Optional: true, }, + "auto_delete": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + }, }, }, }, @@ -160,6 +164,10 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err disk.Boot = i == 0 disk.AutoDelete = true + if v, ok := d.GetOk(prefix + ".auto_delete"); ok { + disk.AutoDelete = v.(bool) + } + // Load up the disk for this disk if specified if v, ok := d.GetOk(prefix + ".disk"); ok { diskName := v.(string) diff --git a/resource_compute_instance_test.go b/resource_compute_instance_test.go index 24a75214..5538da8e 100644 --- a/resource_compute_instance_test.go +++ b/resource_compute_instance_test.go @@ -3,6 +3,7 @@ package google import ( "fmt" "testing" + "strings" "code.google.com/p/google-api-go-client/compute/v1" "github.com/hashicorp/terraform/helper/resource" @@ -24,6 +25,7 @@ func TestAccComputeInstance_basic(t *testing.T) { "google_compute_instance.foobar", &instance), testAccCheckComputeInstanceTag(&instance, "foo"), testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"), + testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true), ), }, }, @@ -50,6 +52,28 @@ func TestAccComputeInstance_IP(t *testing.T) { }) } +//!NB requires that disk with name terraform-test-disk is present in gce, +//if created as dependency then it tries to remove it while it is still attached +//to instance and that fails with an error +func TestAccComputeInstance_disks(t *testing.T) { + var instance compute.Instance + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeInstanceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeInstance_disks, + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true), + testAccCheckComputeInstanceDisk(&instance, "terraform-test-disk", false, false), + ), + }, + }, + }) +} + func TestAccComputeInstance_update(t *testing.T) { var instance compute.Instance @@ -164,6 +188,22 @@ func testAccCheckComputeInstanceNetwork(instance *compute.Instance) resource.Tes } } +func testAccCheckComputeInstanceDisk(instance *compute.Instance, source string, delete bool, boot bool) resource.TestCheckFunc { + return func(s *terraform.State) error { + if instance.Disks == nil { + return fmt.Errorf("no disks") + } + + for _, disk := range instance.Disks { + if strings.LastIndex(disk.Source, "/"+source) == (len(disk.Source) - len(source) - 1) && disk.AutoDelete == delete && disk.Boot == boot{ + return nil + } + } + + return fmt.Errorf("Disk not found: %s", source) + } +} + func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resource.TestCheckFunc { return func(s *terraform.State) error { if instance.Tags == nil { @@ -244,3 +284,27 @@ resource "google_compute_instance" "foobar" { foo = "bar" } }` + +const testAccComputeInstance_disks = ` +resource "google_compute_instance" "foobar" { + name = "terraform-test" + machine_type = "n1-standard-1" + zone = "us-central1-a" + + disk { + image = "debian-7-wheezy-v20140814" + } + + disk { + disk = "terraform-test-disk" + auto_delete = false + } + + network { + source = "default" + } + + metadata { + foo = "bar" + } +}`