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

post-migration disk cleanup #600

Merged
merged 2 commits into from
Oct 23, 2017
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
73 changes: 19 additions & 54 deletions google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func resourceComputeInstance() *schema.Resource {
Schema: map[string]*schema.Schema{
"boot_disk": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Expand Down Expand Up @@ -604,14 +604,11 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
// Build up the list of disks

disks := []*computeBeta.AttachedDisk{}
var hasBootDisk bool
if _, hasBootDisk = d.GetOk("boot_disk"); hasBootDisk {
bootDisk, err := expandBootDisk(d, config, zone, project)
if err != nil {
return err
}
disks = append(disks, bootDisk)
bootDisk, err := expandBootDisk(d, config, zone, project)
if err != nil {
return err
}
disks = append(disks, bootDisk)

if _, hasScratchDisk := d.GetOk("scratch_disk"); hasScratchDisk {
scratchDisks, err := expandScratchDisks(d, config, zone, project)
Expand All @@ -623,19 +620,13 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err

attachedDisksCount := d.Get("attached_disk.#").(int)

if attachedDisksCount == 0 && !hasBootDisk {
return fmt.Errorf("At least one disk, attached_disk, or boot_disk must be set")
}

for i := 0; i < attachedDisksCount; i++ {
prefix := fmt.Sprintf("attached_disk.%d", i)
disk := computeBeta.AttachedDisk{
Source: d.Get(prefix + ".source").(string),
AutoDelete: false, // Don't allow autodelete; let terraform handle disk deletion
}

disk.Boot = i == 0 && !hasBootDisk

if v, ok := d.GetOk(prefix + ".device_name"); ok {
disk.DeviceName = v.(string)
}
Expand Down Expand Up @@ -920,68 +911,42 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
d.Set("label_fingerprint", instance.LabelFingerprint)
}

disksCount := d.Get("disk.#").(int)
attachedDisksCount := d.Get("attached_disk.#").(int)
scratchDisksCount := d.Get("scratch_disk.#").(int)

if _, ok := d.GetOk("boot_disk"); ok {
disksCount++
}
if expectedDisks := disksCount + attachedDisksCount + scratchDisksCount; len(instance.Disks) != expectedDisks {
return fmt.Errorf("Expected %d disks, API returned %d", expectedDisks, len(instance.Disks))
}

attachedDiskSources := make(map[string]int, attachedDisksCount)
for i := 0; i < attachedDisksCount; i++ {
attachedDiskSources[d.Get(fmt.Sprintf("attached_disk.%d.source", i)).(string)] = i
}

dIndex := 0
sIndex := 0
attachedDisks := make([]map[string]interface{}, attachedDisksCount)
scratchDisks := make([]map[string]interface{}, 0, scratchDisksCount)
scratchDisks := []map[string]interface{}{}
extraAttachedDisks := []map[string]interface{}{}
for _, disk := range instance.Disks {
if _, ok := d.GetOk("boot_disk"); ok && disk.Boot {
// This disk is a boot disk and there is a boot disk set in the config, therefore
// this is the boot disk set in the config.
if disk.Boot {
d.Set("boot_disk", flattenBootDisk(d, disk))
} else if _, ok := d.GetOk("scratch_disk"); ok && disk.Type == "SCRATCH" {
// This disk is a scratch disk and there are scratch disks set in the config, therefore
// this is a scratch disk set in the config.
} else if disk.Type == "SCRATCH" {
scratchDisks = append(scratchDisks, flattenScratchDisk(disk))
sIndex++
} else if _, ok := attachedDiskSources[disk.Source]; !ok {
di := map[string]interface{}{
"disk": d.Get(fmt.Sprintf("disk.%d.disk", dIndex)),
"image": d.Get(fmt.Sprintf("disk.%d.image", dIndex)),
"type": d.Get(fmt.Sprintf("disk.%d.type", dIndex)),
"scratch": d.Get(fmt.Sprintf("disk.%d.scratch", dIndex)),
"auto_delete": d.Get(fmt.Sprintf("disk.%d.auto_delete", dIndex)),
"size": d.Get(fmt.Sprintf("disk.%d.size", dIndex)),
"device_name": d.Get(fmt.Sprintf("disk.%d.device_name", dIndex)),
"disk_encryption_key_raw": d.Get(fmt.Sprintf("disk.%d.disk_encryption_key_raw", dIndex)),
}
if d.Get(fmt.Sprintf("disk.%d.disk_encryption_key_raw", dIndex)) != "" {
sha, err := hash256(d.Get(fmt.Sprintf("disk.%d.disk_encryption_key_raw", dIndex)).(string))
if err != nil {
return err
}
di["disk_encryption_key_sha256"] = sha
}
dIndex++
} else {
adIndex := attachedDiskSources[disk.Source]
adIndex, inConfig := attachedDiskSources[disk.Source]
di := map[string]interface{}{
"source": disk.Source,
"device_name": disk.DeviceName,
}
if key := disk.DiskEncryptionKey; key != nil {
di["disk_encryption_key_raw"] = d.Get(fmt.Sprintf("attached_disk.%d.disk_encryption_key_raw", adIndex))
if inConfig {
di["disk_encryption_key_raw"] = d.Get(fmt.Sprintf("attached_disk.%d.disk_encryption_key_raw", adIndex))
}
di["disk_encryption_key_sha256"] = key.Sha256
}
attachedDisks[adIndex] = di
if inConfig {
attachedDisks[adIndex] = di
} else {
extraAttachedDisks = append(extraAttachedDisks, di)
}
}
}
attachedDisks = append(attachedDisks, extraAttachedDisks...)

d.Set("attached_disk", attachedDisks)
d.Set("scratch_disk", scratchDisks)
Expand Down
48 changes: 7 additions & 41 deletions google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package google
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -245,7 +244,7 @@ func TestAccComputeInstance_attachedDisk(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
"google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceDisk(&instance, diskName, false, true),
testAccCheckComputeInstanceDisk(&instance, diskName, false, false),
),
},
},
Expand Down Expand Up @@ -300,24 +299,6 @@ func TestAccComputeInstance_bootDisk_type(t *testing.T) {
})
}

func TestAccComputeInstance_noDisk(t *testing.T) {
t.Parallel()

var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstance_noDisk(instanceName),
ExpectError: regexp.MustCompile("At least one disk, attached_disk, or boot_disk must be set"),
},
},
})
}

func TestAccComputeInstance_scratchDisk(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1006,9 +987,6 @@ func testAccCheckComputeInstanceDiskEncryptionKey(n string, instance *compute.In
for i, disk := range instance.Disks {
if disk.Boot {
attr := rs.Primary.Attributes["boot_disk.0.disk_encryption_key_sha256"]
if attr == "" {
attr = rs.Primary.Attributes[fmt.Sprintf("disk.%d.disk_encryption_key_sha256", i)]
}
if attr != bootDiskEncryptionKey {
return fmt.Errorf("Boot disk has wrong encryption key in state.\nExpected: %s\nActual: %s", bootDiskEncryptionKey, attr)
}
Expand Down Expand Up @@ -1566,6 +1544,12 @@ resource "google_compute_instance" "foobar" {
machine_type = "n1-standard-1"
zone = "us-central1-a"

boot_disk {
initialize_params {
image = "debian-8-jessie-v20160803"
}
}

attached_disk {
source = "${google_compute_disk.foobar.self_link}"
}
Expand Down Expand Up @@ -1626,24 +1610,6 @@ resource "google_compute_instance" "foobar" {
`, instance, diskType)
}

func testAccComputeInstance_noDisk(instance string) string {
return fmt.Sprintf(`
resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "n1-standard-1"
zone = "us-central1-a"

network_interface {
network = "default"
}

metadata {
foo = "bar"
}
}
`, instance)
}

func testAccComputeInstance_scratchDisk(instance string) string {
return fmt.Sprintf(`
resource "google_compute_instance" "scratch" {
Expand Down