Skip to content

Commit

Permalink
Add ability to import Google Compute persistent disks (#14573)
Browse files Browse the repository at this point in the history
* Add ability to import Google Compute persistent disks

* Fix additional URL names
  • Loading branch information
Sam Bashton authored and radeksimko committed May 30, 2017
1 parent 893b2f7 commit 56fbe02
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
31 changes: 31 additions & 0 deletions builtin/providers/google/import_compute_disk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccComputeDisk_importBasic(t *testing.T) {
resourceName := "google_compute_disk.foobar"
diskName := fmt.Sprintf("disk-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeDiskDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeDisk_basic(diskName),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
49 changes: 45 additions & 4 deletions builtin/providers/google/resource_compute_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"regexp"
"strings"

"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
Expand All @@ -23,6 +24,9 @@ func resourceComputeDisk() *schema.Resource {
Create: resourceComputeDiskCreate,
Read: resourceComputeDiskRead,
Delete: resourceComputeDiskDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Expand Down Expand Up @@ -189,17 +193,54 @@ func resourceComputeDiskRead(d *schema.ResourceData, meta interface{}) error {
return err
}

disk, err := config.clientCompute.Disks.Get(
project, d.Get("zone").(string), d.Id()).Do()
region, err := getRegion(d, config)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Disk %q", d.Get("name").(string)))
return err
}

getDisk := func(zone string) (interface{}, error) {
return config.clientCompute.Disks.Get(project, zone, d.Id()).Do()
}

var disk *compute.Disk
if zone, ok := d.GetOk("zone"); ok {
disk, err = config.clientCompute.Disks.Get(
project, zone.(string), d.Id()).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Disk %q", d.Get("name").(string)))
}
} else {
// If the resource was imported, the only info we have is the ID. Try to find the resource
// by searching in the region of the project.
var resource interface{}
resource, err = getZonalResourceFromRegion(getDisk, region, config.clientCompute, project)

if err != nil {
return err
}

disk = resource.(*compute.Disk)
}

zoneUrlParts := strings.Split(disk.Zone, "/")
typeUrlParts := strings.Split(disk.Type, "/")
d.Set("name", disk.Name)
d.Set("self_link", disk.SelfLink)
d.Set("type", typeUrlParts[len(typeUrlParts)-1])
d.Set("zone", zoneUrlParts[len(zoneUrlParts)-1])
d.Set("size", disk.SizeGb)
d.Set("users", disk.Users)
if disk.DiskEncryptionKey != nil && disk.DiskEncryptionKey.Sha256 != "" {
d.Set("disk_encryption_key_sha256", disk.DiskEncryptionKey.Sha256)
}
d.Set("users", disk.Users)
if disk.SourceImage != "" {
imageUrlParts := strings.Split(disk.SourceImage, "/")
d.Set("image", imageUrlParts[len(imageUrlParts)-1])
}
if disk.SourceSnapshot != "" {
snapshotUrlParts := strings.Split(disk.SourceSnapshot, "/")
d.Set("snapshot", snapshotUrlParts[len(snapshotUrlParts)-1])
}

return nil
}
Expand Down

0 comments on commit 56fbe02

Please sign in to comment.