providers/google: support disks coming from other disks

This commit is contained in:
Mitchell Hashimoto 2014-08-25 21:35:23 -07:00
parent 81c9b695a1
commit 99918bde2e
2 changed files with 41 additions and 12 deletions

View File

@ -47,9 +47,16 @@ func resourceComputeInstance() *schema.Resource {
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"source": &schema.Schema{
// TODO(mitchellh): one of image or disk is required
"disk": &schema.Schema{
Type: schema.TypeString,
Required: true,
Optional: true,
},
"image": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
@ -130,13 +137,35 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
disksCount := d.Get("disk.#").(int)
disks := make([]*compute.AttachedDisk, 0, disksCount)
for i := 0; i < disksCount; i++ {
// Load up the image for this disk
imageName := d.Get(fmt.Sprintf("disk.%d.source", i)).(string)
image, err := readImage(config, imageName)
if err != nil {
return fmt.Errorf(
"Error loading image '%s': %s",
imageName, err)
prefix := fmt.Sprintf("disk.%d", i)
var sourceLink string
// 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(
config.Project, zone.Name, diskName).Do()
if err != nil {
return fmt.Errorf(
"Error loading disk '%s': %s",
diskName, err)
}
sourceLink = disk.SelfLink
}
// Load up the image for this disk if specified
if v, ok := d.GetOk(prefix + ".image"); ok {
imageName := v.(string)
image, err := readImage(config, imageName)
if err != nil {
return fmt.Errorf(
"Error loading image '%s': %s",
imageName, err)
}
sourceLink = image.SelfLink
}
// Build the disk
@ -146,7 +175,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
disk.Boot = i == 0
disk.AutoDelete = true
disk.InitializeParams = &compute.AttachedDiskInitializeParams{
SourceImage: image.SelfLink,
SourceImage: sourceLink,
}
disks = append(disks, &disk)

View File

@ -159,7 +159,7 @@ resource "google_compute_instance" "foobar" {
tags = ["foo", "bar"]
disk {
source = "debian-7-wheezy-v20140814"
image = "debian-7-wheezy-v20140814"
}
network {
@ -183,7 +183,7 @@ resource "google_compute_instance" "foobar" {
tags = ["foo", "bar"]
disk {
source = "debian-7-wheezy-v20140814"
image = "debian-7-wheezy-v20140814"
}
network {