Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gce persistent disk attaching #254

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions builtin/providers/google/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("GOOGLE_CLIENT_FILE"); v == "" {
t.Fatal("GOOGLE_CLIENT_FILE must be set for acceptance tests")
}

if v := os.Getenv("GOOGLE_PROJECT"); v == "" {
t.Fatal("GOOGLE_PROJECT must be set for acceptance tests")
}
}
35 changes: 21 additions & 14 deletions builtin/providers/google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func resourceComputeInstance() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"auto_delete": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
},
},
},
Expand Down Expand Up @@ -151,20 +155,31 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
for i := 0; i < disksCount; i++ {
prefix := fmt.Sprintf("disk.%d", i)

var sourceLink string
// var sourceLink string

// Build the disk
var disk compute.AttachedDisk
disk.Type = "PERSISTENT"
disk.Mode = "READ_WRITE"
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)
disk, err := config.clientCompute.Disks.Get(
diskData, err := config.clientCompute.Disks.Get(
config.Project, zone.Name, diskName).Do()
if err != nil {
return fmt.Errorf(
"Error loading disk '%s': %s",
diskName, err)
}

sourceLink = disk.SelfLink
disk.Source = diskData.SelfLink
}

// Load up the image for this disk if specified
Expand All @@ -177,17 +192,9 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
imageName, err)
}

sourceLink = image.SelfLink
}

// Build the disk
var disk compute.AttachedDisk
disk.Type = "PERSISTENT"
disk.Mode = "READ_WRITE"
disk.Boot = i == 0
disk.AutoDelete = true
disk.InitializeParams = &compute.AttachedDiskInitializeParams{
SourceImage: sourceLink,
disk.InitializeParams = &compute.AttachedDiskInitializeParams{
SourceImage: image.SelfLink,
}
}

disks = append(disks, &disk)
Expand Down
64 changes: 64 additions & 0 deletions builtin/providers/google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
),
},
},
Expand All @@ -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

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
}
}`