Added - flag to set whether disk needs to be delete or not when instance terminates

This commit is contained in:
Dainis Tillers 2014-09-02 16:52:49 +03:00
parent 6aa3d2f0ec
commit 1a6fc051ba
2 changed files with 72 additions and 0 deletions

View File

@ -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)

View File

@ -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"
}
}`