Fix disk type’Malformed URL’ error (#275)

* Fix disk type’Malformed URL’ error

The API expects the disk type to be a SelfLink URL, but the disk type 
name was being used (e.g. “pd-ssd”).

* Add ACC Tests for boot disk type

* Fix acceptance test & fmt test config

The Instance data does not contain the actual disk type, just "PERSISTENT". This commit uses the computeClient to pull the disk data from the API, allowing checking of the disk type.

Also fmt'd the test configuration.
This commit is contained in:
Matt Morrison 2017-08-02 10:39:32 +12:00 committed by Dana Hoffman
parent 7faec27fba
commit 0595ef25a5
2 changed files with 61 additions and 1 deletions

View File

@ -1375,7 +1375,7 @@ func expandBootDisk(d *schema.ResourceData, config *Config, zone *compute.Zone,
if err != nil {
return nil, fmt.Errorf("Error loading disk type '%s': %s", diskTypeName, err)
}
disk.InitializeParams.DiskType = diskType.Name
disk.InitializeParams.DiskType = diskType.SelfLink
}
if v, ok := d.GetOk("boot_disk.0.initialize_params.0.image"); ok {

View File

@ -289,6 +289,28 @@ func TestAccComputeInstance_bootDisk_source(t *testing.T) {
})
}
func TestAccComputeInstance_bootDisk_type(t *testing.T) {
var instance compute.Instance
var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
var diskType = "pd-ssd"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstance_bootDisk_type(instanceName, diskType),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
"google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceBootDiskType(instanceName, diskType),
),
},
},
})
}
func TestAccComputeInstance_noDisk(t *testing.T) {
var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
@ -821,6 +843,23 @@ func testAccCheckComputeInstanceBootDisk(instance *compute.Instance, source stri
}
}
func testAccCheckComputeInstanceBootDiskType(instanceName string, diskType string) resource.TestCheckFunc {
return func(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
// boot disk is named the same as the Instance
disk, err := config.clientCompute.Disks.Get(config.Project, "us-central1-a", instanceName).Do()
if err != nil {
return err
}
if strings.Contains(disk.Type, diskType) {
return nil
}
return fmt.Errorf("Boot disk not found with type %q", diskType)
}
}
func testAccCheckComputeInstanceScratchDisk(instance *compute.Instance, interfaces []string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if instance.Disks == nil {
@ -1370,6 +1409,27 @@ resource "google_compute_instance" "foobar" {
`, disk, instance)
}
func testAccComputeInstance_bootDisk_type(instance string, diskType string) string {
return fmt.Sprintf(`
resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "n1-standard-1"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-8-jessie-v20160803"
type = "%s"
}
}
network_interface {
network = "default"
}
}
`, instance, diskType)
}
func testAccComputeInstance_noDisk(instance string) string {
return fmt.Sprintf(`
resource "google_compute_instance" "foobar" {