Skip to content

Commit

Permalink
Add region disk support to attached disk (hashicorp#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
modular-magician authored and chrisst committed Dec 7, 2018
1 parent d4866ec commit a8eca9f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
15 changes: 13 additions & 2 deletions google-beta/resource_compute_attached_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,21 @@ func resourceAttachedDiskCreate(d *schema.ResourceData, meta interface{}) error
return err
}

diskName := GetResourceNameFromSelfLink(d.Get("disk").(string))
disk := d.Get("disk").(string)
diskName := GetResourceNameFromSelfLink(disk)
diskSrc := fmt.Sprintf("projects/%s/zones/%s/disks/%s", zv.Project, zv.Zone, diskName)

// Check if the disk is a regional disk
if strings.Contains(disk, "regions") {
rv, err := ParseRegionDiskFieldValue(disk, d, config)
if err != nil {
return err
}
diskSrc = rv.RelativeLink()
}

attachedDisk := compute.AttachedDisk{
Source: fmt.Sprintf("projects/%s/zones/%s/disks/%s", zv.Project, zv.Zone, diskName),
Source: diskSrc,
Mode: d.Get("mode").(string),
DeviceName: d.Get("device_name").(string),
}
Expand Down
64 changes: 64 additions & 0 deletions google-beta/resource_compute_attached_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,33 @@ func TestAccComputeAttachedDisk_full(t *testing.T) {

}

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

diskName := acctest.RandomWithPrefix("tf-test")
instanceName := acctest.RandomWithPrefix("tf-test")
importID := fmt.Sprintf("%s/us-central1-a/%s:%s", getTestProjectFromEnv(), instanceName, diskName)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
// Check destroy isn't a good test here, see comment on testCheckAttachedDiskIsNowDetached
CheckDestroy: nil,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAttachedDiskResource_region(diskName, instanceName),
},
resource.TestStep{
ResourceName: "google_compute_attached_disk.test",
ImportStateId: importID,
ImportState: true,
ImportStateVerify: true,
},
},
})

}

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

Expand Down Expand Up @@ -152,6 +179,43 @@ resource "google_compute_attached_disk" "test" {
}`)
}

func testAttachedDiskResource_region(diskName, instanceName string) string {
return fmt.Sprintf(`
resource "google_compute_attached_disk" "test" {
disk = "${google_compute_region_disk.region.self_link}"
instance = "${google_compute_instance.test.self_link}"
}
resource "google_compute_region_disk" "region" {
name = "%s"
region = "us-central1"
size = 10
replica_zones = ["us-central1-b", "us-central1-a"]
}
resource "google_compute_instance" "test" {
name = "%s"
machine_type = "f1-micro"
zone = "us-central1-a"
lifecycle {
ignore_changes = [
"attached_disk",
]
}
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
network = "default"
}
}`, diskName, instanceName)
}

func testAttachedDiskResource(diskName, instanceName string) string {
return fmt.Sprintf(`
resource "google_compute_disk" "test1" {
Expand Down

0 comments on commit a8eca9f

Please sign in to comment.