Merge pull request #9942 from hashicorp/paddy_6495_invalid_disk_crash

provider/google: throw an error for invalid disks
This commit is contained in:
Paddy 2016-11-08 09:03:18 -08:00 committed by GitHub
commit 9a1f050971
2 changed files with 56 additions and 0 deletions

View File

@ -361,6 +361,13 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
disk.Boot = i == 0 disk.Boot = i == 0
disk.AutoDelete = d.Get(prefix + ".auto_delete").(bool) disk.AutoDelete = d.Get(prefix + ".auto_delete").(bool)
if _, ok := d.GetOk(prefix + ".disk"); ok {
if _, ok := d.GetOk(prefix + ".type"); ok {
return fmt.Errorf(
"Error: cannot define both disk and type.")
}
}
// Load up the disk for this disk if specified // Load up the disk for this disk if specified
if v, ok := d.GetOk(prefix + ".disk"); ok { if v, ok := d.GetOk(prefix + ".disk"); ok {
diskName := v.(string) diskName := v.(string)

View File

@ -2,6 +2,7 @@ package google
import ( import (
"fmt" "fmt"
"regexp"
"strings" "strings"
"testing" "testing"
@ -482,6 +483,23 @@ func TestAccComputeInstance_private_image_family(t *testing.T) {
}) })
} }
func TestAccComputeInstance_invalid_disk(t *testing.T) {
var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
var diskName = fmt.Sprintf("instance-testd-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstance_invalid_disk(diskName, instanceName),
ExpectError: regexp.MustCompile("Error: cannot define both disk and type."),
},
},
})
}
func testAccCheckComputeInstanceDestroy(s *terraform.State) error { func testAccCheckComputeInstanceDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config) config := testAccProvider.Meta().(*Config)
@ -1152,3 +1170,34 @@ func testAccComputeInstance_private_image_family(disk, image, family, instance s
} }
}`, disk, image, family, instance) }`, disk, image, family, instance)
} }
func testAccComputeInstance_invalid_disk(disk, instance string) string {
return fmt.Sprintf(`
resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "f1-micro"
zone = "us-central1-a"
disk {
image = "ubuntu-os-cloud/ubuntu-1604-lts"
type = "pd-standard"
}
disk {
disk = "${google_compute_disk.foobar.name}"
type = "pd-standard"
device_name = "xvdb"
}
network_interface {
network = "default"
}
}
resource "google_compute_disk" "foobar" {
name = "%s"
zone = "us-central1-a"
type = "pd-standard"
size = "1"
}`, instance, disk)
}