Skip to content

Commit

Permalink
Fix disk type’Malformed URL’ error (hashicorp#275)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
sl1pm4t authored and danawillow committed Aug 1, 2017
1 parent 1e0f032 commit 8c883a5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
60 changes: 60 additions & 0 deletions google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

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

0 comments on commit 8c883a5

Please sign in to comment.