From 8b80d05103b2a19d70de31165347a985db0b8456 Mon Sep 17 00:00:00 2001 From: Joe Topjian Date: Tue, 12 Jul 2016 02:13:52 -0600 Subject: [PATCH] provider/openstack: Support Import of OpenStack Block Storage Volumes (#7347) --- ...t_openstack_blockstorage_volume_v1_test.go | 29 ++++++++++++++ ...t_openstack_blockstorage_volume_v2_test.go | 29 ++++++++++++++ ...source_openstack_blockstorage_volume_v1.go | 21 +++++----- ...e_openstack_blockstorage_volume_v1_test.go | 8 +--- ...source_openstack_blockstorage_volume_v2.go | 21 +++++----- ...e_openstack_blockstorage_volume_v2_test.go | 40 +++++++++---------- 6 files changed, 102 insertions(+), 46 deletions(-) create mode 100644 builtin/providers/openstack/import_openstack_blockstorage_volume_v1_test.go create mode 100644 builtin/providers/openstack/import_openstack_blockstorage_volume_v2_test.go diff --git a/builtin/providers/openstack/import_openstack_blockstorage_volume_v1_test.go b/builtin/providers/openstack/import_openstack_blockstorage_volume_v1_test.go new file mode 100644 index 000000000..b5539f2cb --- /dev/null +++ b/builtin/providers/openstack/import_openstack_blockstorage_volume_v1_test.go @@ -0,0 +1,29 @@ +package openstack + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccOpenStackBlockStorageV1Volume_importBasic(t *testing.T) { + resourceName := "openstack_blockstorage_volume_v1.volume_1" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBlockStorageV1VolumeDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccBlockStorageV1Volume_basic, + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"region"}, + }, + }, + }) +} diff --git a/builtin/providers/openstack/import_openstack_blockstorage_volume_v2_test.go b/builtin/providers/openstack/import_openstack_blockstorage_volume_v2_test.go new file mode 100644 index 000000000..37489a820 --- /dev/null +++ b/builtin/providers/openstack/import_openstack_blockstorage_volume_v2_test.go @@ -0,0 +1,29 @@ +package openstack + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccOpenStackBlockStorageV2Volume_importBasic(t *testing.T) { + resourceName := "openstack_blockstorage_volume_v2.volume_1" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBlockStorageV2VolumeDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccBlockStorageV2Volume_basic, + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"region"}, + }, + }, + }) +} diff --git a/builtin/providers/openstack/resource_openstack_blockstorage_volume_v1.go b/builtin/providers/openstack/resource_openstack_blockstorage_volume_v1.go index 05328fa18..fca73f216 100644 --- a/builtin/providers/openstack/resource_openstack_blockstorage_volume_v1.go +++ b/builtin/providers/openstack/resource_openstack_blockstorage_volume_v1.go @@ -20,6 +20,9 @@ func resourceBlockStorageVolumeV1() *schema.Resource { Read: resourceBlockStorageVolumeV1Read, Update: resourceBlockStorageVolumeV1Update, Delete: resourceBlockStorageVolumeV1Delete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "region": &schema.Schema{ @@ -177,17 +180,15 @@ func resourceBlockStorageVolumeV1Read(d *schema.ResourceData, meta interface{}) d.Set("volume_type", v.VolumeType) d.Set("metadata", v.Metadata) - if len(v.Attachments) > 0 { - attachments := make([]map[string]interface{}, len(v.Attachments)) - for i, attachment := range v.Attachments { - attachments[i] = make(map[string]interface{}) - attachments[i]["id"] = attachment["id"] - attachments[i]["instance_id"] = attachment["server_id"] - attachments[i]["device"] = attachment["device"] - log.Printf("[DEBUG] attachment: %v", attachment) - } - d.Set("attachment", attachments) + attachments := make([]map[string]interface{}, len(v.Attachments)) + for i, attachment := range v.Attachments { + attachments[i] = make(map[string]interface{}) + attachments[i]["id"] = attachment["id"] + attachments[i]["instance_id"] = attachment["server_id"] + attachments[i]["device"] = attachment["device"] + log.Printf("[DEBUG] attachment: %v", attachment) } + d.Set("attachment", attachments) return nil } diff --git a/builtin/providers/openstack/resource_openstack_blockstorage_volume_v1_test.go b/builtin/providers/openstack/resource_openstack_blockstorage_volume_v1_test.go index 3900e2331..cae663c10 100644 --- a/builtin/providers/openstack/resource_openstack_blockstorage_volume_v1_test.go +++ b/builtin/providers/openstack/resource_openstack_blockstorage_volume_v1_test.go @@ -156,24 +156,20 @@ func testAccCheckBlockStorageV1VolumeMetadata( var testAccBlockStorageV1Volume_basic = fmt.Sprintf(` resource "openstack_blockstorage_volume_v1" "volume_1" { - region = "%s" name = "tf-test-volume" description = "first test volume" metadata{ foo = "bar" } size = 1 - }`, - OS_REGION_NAME) + }`) var testAccBlockStorageV1Volume_update = fmt.Sprintf(` resource "openstack_blockstorage_volume_v1" "volume_1" { - region = "%s" name = "tf-test-volume-updated" description = "first test volume" metadata{ foo = "bar" } size = 1 - }`, - OS_REGION_NAME) + }`) diff --git a/builtin/providers/openstack/resource_openstack_blockstorage_volume_v2.go b/builtin/providers/openstack/resource_openstack_blockstorage_volume_v2.go index 75dcc0a4d..d2d2a559d 100644 --- a/builtin/providers/openstack/resource_openstack_blockstorage_volume_v2.go +++ b/builtin/providers/openstack/resource_openstack_blockstorage_volume_v2.go @@ -20,6 +20,9 @@ func resourceBlockStorageVolumeV2() *schema.Resource { Read: resourceBlockStorageVolumeV2Read, Update: resourceBlockStorageVolumeV2Update, Delete: resourceBlockStorageVolumeV2Delete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "region": &schema.Schema{ @@ -189,17 +192,15 @@ func resourceBlockStorageVolumeV2Read(d *schema.ResourceData, meta interface{}) d.Set("volume_type", v.VolumeType) d.Set("metadata", v.Metadata) - if len(v.Attachments) > 0 { - attachments := make([]map[string]interface{}, len(v.Attachments)) - for i, attachment := range v.Attachments { - attachments[i] = make(map[string]interface{}) - attachments[i]["id"] = attachment["id"] - attachments[i]["instance_id"] = attachment["server_id"] - attachments[i]["device"] = attachment["device"] - log.Printf("[DEBUG] attachment: %v", attachment) - } - d.Set("attachment", attachments) + attachments := make([]map[string]interface{}, len(v.Attachments)) + for i, attachment := range v.Attachments { + attachments[i] = make(map[string]interface{}) + attachments[i]["id"] = attachment["id"] + attachments[i]["instance_id"] = attachment["server_id"] + attachments[i]["device"] = attachment["device"] + log.Printf("[DEBUG] attachment: %v", attachment) } + d.Set("attachment", attachments) return nil } diff --git a/builtin/providers/openstack/resource_openstack_blockstorage_volume_v2_test.go b/builtin/providers/openstack/resource_openstack_blockstorage_volume_v2_test.go index 7600a6527..914ff4688 100644 --- a/builtin/providers/openstack/resource_openstack_blockstorage_volume_v2_test.go +++ b/builtin/providers/openstack/resource_openstack_blockstorage_volume_v2_test.go @@ -15,26 +15,6 @@ import ( func TestAccBlockStorageV2Volume_basic(t *testing.T) { var volume volumes.Volume - var testAccBlockStorageV2Volume_basic = fmt.Sprintf(` - resource "openstack_blockstorage_volume_v2" "volume_1" { - name = "volume_1" - description = "first test volume" - metadata { - foo = "bar" - } - size = 1 - }`) - - var testAccBlockStorageV2Volume_update = fmt.Sprintf(` - resource "openstack_blockstorage_volume_v2" "volume_1" { - name = "volume_1-updated" - description = "first test volume" - metadata { - foo = "bar" - } - size = 1 - }`) - resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -186,3 +166,23 @@ func testAccCheckBlockStorageV2VolumeMetadata( return fmt.Errorf("Metadata not found: %s", k) } } + +var testAccBlockStorageV2Volume_basic = fmt.Sprintf(` + resource "openstack_blockstorage_volume_v2" "volume_1" { + name = "volume_1" + description = "first test volume" + metadata { + foo = "bar" + } + size = 1 + }`) + +var testAccBlockStorageV2Volume_update = fmt.Sprintf(` + resource "openstack_blockstorage_volume_v2" "volume_1" { + name = "volume_1-updated" + description = "first test volume" + metadata { + foo = "bar" + } + size = 1 + }`)