From 2332256af635fbfed736bf55e104d75db825d131 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Thu, 6 Apr 2017 19:12:20 +0100 Subject: [PATCH] Refactoring Bootable Storage Volumes --- .../providers/opc/resource_storage_volume.go | 113 ++++++------------ .../opc/resource_storage_volume_test.go | 76 ++++++------ 2 files changed, 75 insertions(+), 114 deletions(-) diff --git a/builtin/providers/opc/resource_storage_volume.go b/builtin/providers/opc/resource_storage_volume.go index 28f3e7e050..0d101f649f 100644 --- a/builtin/providers/opc/resource_storage_volume.go +++ b/builtin/providers/opc/resource_storage_volume.go @@ -42,47 +42,24 @@ func resourceOPCStorageVolume() *schema.Resource { }, true), }, - "snapshot": { - Type: schema.TypeString, + "bootable": { + Type: schema.TypeBool, Optional: true, + Default: false, ForceNew: true, - Computed: true, }, - "snapshot_id": { + "image_list": { Type: schema.TypeString, Optional: true, - Computed: true, ForceNew: true, }, - "snapshot_account": { - Type: schema.TypeString, + "image_list_entry": { + Type: schema.TypeInt, Optional: true, ForceNew: true, - }, - - "bootable": { - Type: schema.TypeList, - Optional: true, - ForceNew: true, - MaxItems: 1, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "image_list": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - - "image_list_entry": { - Type: schema.TypeInt, - Optional: true, - ForceNew: true, - Default: -1, - }, - }, - }, + Default: -1, }, "tags": tagsOptionalSchema(), @@ -139,27 +116,29 @@ func resourceOPCStorageVolumeCreate(d *schema.ResourceData, meta interface{}) er description := d.Get("description").(string) size := d.Get("size").(int) storageType := d.Get("storage_type").(string) + bootable := d.Get("bootable").(bool) + imageList := d.Get("image_list").(string) + imageListEntry := d.Get("image_list_entry").(int) - input := compute.CreateStorageVolumeInput{ - Name: name, - Description: description, - Size: strconv.Itoa(size), - Properties: []string{storageType}, - Tags: getStringList(d, "tags"), - } - - expandOPCStorageVolumeOptionalFields(d, &input) - - if v, ok := d.GetOk("snapshot"); ok { - input.Snapshot = v.(string) - } + if bootable == true { + if imageList == "" { + return fmt.Errorf("Error: A Bootable Volume must have an Image List!") + } - if v, ok := d.GetOk("snapshot_account"); ok { - input.SnapshotAccount = v.(string) + if imageListEntry == -1 { + return fmt.Errorf("Error: A Bootable Volume must have an Image List Entry!") + } } - if v, ok := d.GetOk("snapshot_id"); ok { - input.SnapshotID = v.(string) + input := compute.CreateStorageVolumeInput{ + Name: name, + Description: description, + Size: strconv.Itoa(size), + Properties: []string{storageType}, + Bootable: bootable, + ImageList: imageList, + ImageListEntry: imageListEntry, + Tags: getStringList(d, "tags"), } info, err := client.CreateStorageVolume(&input) @@ -178,13 +157,17 @@ func resourceOPCStorageVolumeUpdate(d *schema.ResourceData, meta interface{}) er description := d.Get("description").(string) size := d.Get("size").(int) storageType := d.Get("storage_type").(string) + imageList := d.Get("image_list").(string) + imageListEntry := d.Get("image_list_entry").(int) input := compute.UpdateStorageVolumeInput{ - Name: name, - Description: description, - Size: strconv.Itoa(size), - Properties: []string{storageType}, - Tags: getStringList(d, "tags"), + Name: name, + Description: description, + Size: strconv.Itoa(size), + Properties: []string{storageType}, + ImageList: imageList, + ImageListEntry: imageListEntry, + Tags: getStringList(d, "tags"), } _, err := client.UpdateStorageVolume(&input) if err != nil { @@ -220,21 +203,19 @@ func resourceOPCStorageVolumeRead(d *schema.ResourceData, meta interface{}) erro d.Set("name", result.Name) d.Set("description", result.Description) d.Set("storage", result.Properties[0]) - d.Set("snapshot", result.Snapshot) - d.Set("snapshot_id", result.SnapshotID) - d.Set("snapshot_account", result.SnapshotAccount) size, err := strconv.Atoi(result.Size) if err != nil { return err } d.Set("size", size) + d.Set("bootable", result.Bootable) + d.Set("image_list", result.ImageList) + d.Set("image_list_entry", result.ImageListEntry) if err := setStringList(d, "tags", result.Tags); err != nil { return err } - flattenOPCStorageVolumeOptionalFields(d, result) - flattenOPCStorageVolumeComputedFields(d, result) return nil @@ -255,24 +236,6 @@ func resourceOPCStorageVolumeDelete(d *schema.ResourceData, meta interface{}) er return nil } -func expandOPCStorageVolumeOptionalFields(d *schema.ResourceData, input *compute.CreateStorageVolumeInput) { - bootValue, bootExists := d.GetOk("bootable") - input.Bootable = bootExists - if bootExists { - configs := bootValue.([]interface{}) - config := configs[0].(map[string]interface{}) - - input.ImageList = config["image_list"].(string) - input.ImageListEntry = config["image_list_entry"].(int) - } -} - -func flattenOPCStorageVolumeOptionalFields(d *schema.ResourceData, result *compute.StorageVolumeInfo) { - d.Set("bootable", result.Bootable) - d.Set("image_list", result.ImageList) - d.Set("image_list_entry", result.ImageListEntry) -} - func flattenOPCStorageVolumeComputedFields(d *schema.ResourceData, result *compute.StorageVolumeInfo) { d.Set("hypervisor", result.Hypervisor) d.Set("machine_image", result.MachineImage) diff --git a/builtin/providers/opc/resource_storage_volume_test.go b/builtin/providers/opc/resource_storage_volume_test.go index fc157ef94b..122380f166 100644 --- a/builtin/providers/opc/resource_storage_volume_test.go +++ b/builtin/providers/opc/resource_storage_volume_test.go @@ -116,9 +116,10 @@ func TestAccOPCStorageVolume_Bootable(t *testing.T) { }) } -func TestAccOPCStorageVolume_FromSnapshot(t *testing.T) { +func TestAccOPCStorageVolume_ImageListEntry(t *testing.T) { volumeResourceName := "opc_compute_storage_volume.test" - rInt := acctest.RandInt() + ri := acctest.RandInt() + config := fmt.Sprintf(testAccStorageVolumeImageListEntry, ri, ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -126,13 +127,9 @@ func TestAccOPCStorageVolume_FromSnapshot(t *testing.T) { CheckDestroy: opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeDestroyed), Steps: []resource.TestStep{ { - Config: testAccStorageVolumeFromSnapshot(rInt), + Config: config, Check: resource.ComposeTestCheckFunc( opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeExists), - resource.TestCheckResourceAttr(volumeResourceName, "name", fmt.Sprintf("test-acc-stor-vol-final-%d", rInt)), - resource.TestCheckResourceAttrSet(volumeResourceName, "snapshot"), - resource.TestCheckResourceAttrSet(volumeResourceName, "snapshot_id"), - resource.TestCheckResourceAttr(volumeResourceName, "size", "5"), ), }, }, @@ -206,50 +203,51 @@ resource "opc_compute_storage_volume" "test" { const testAccStorageVolumeBootable = ` resource "opc_compute_image_list" "test" { name = "test-acc-stor-vol-bootable-image-list-%d" - description = "Provider Acceptance Tests Storage Volume" + description = "Provider Acceptance Tests Storage Volume Bootable" } -resource "opc_compute_storage_volume" "test" { - name = "test-acc-stor-vol-bootable-%d" - description = "Provider Acceptance Tests Storage Volume" - size = 2 - tags = ["bar", "foo"] - bootable { - image_list = "${opc_compute_image_list.test.name}" - } +resource "opc_compute_image_list_entry" "test" { + name = "${opc_compute_image_list.test.name}" + machine_images = [ "/oracle/public/oel_6.7_apaas_16.4.5_1610211300" ] + version = 1 } -` -const testAccStorageVolumeBasicMaxSize = ` resource "opc_compute_storage_volume" "test" { - name = "test-acc-stor-vol-%d" - description = "Provider Acceptance Tests Storage Volume Max Size" - size = 2048 + name = "test-acc-stor-vol-bootable-%d" + description = "Provider Acceptance Tests Storage Volume Bootable" + size = 20 + tags = ["bar", "foo"] + bootable = true + image_list = "${opc_compute_image_list.test.name}" + image_list_entry = "${opc_compute_image_list_entry.test.version}" } ` -func testAccStorageVolumeFromSnapshot(rInt int) string { - return fmt.Sprintf(` -// Initial Storage Volume to create snapshot with -resource "opc_compute_storage_volume" "foo" { - name = "test-acc-stor-vol-%d" - description = "Acc Test intermediary storage volume for snapshot" - size = 5 +const testAccStorageVolumeImageListEntry = ` +resource "opc_compute_image_list" "test" { + name = "test-acc-stor-vol-bootable-image-list-%d" + description = "Provider Acceptance Tests Storage Volume Image List Entry" } -resource "opc_compute_storage_volume_snapshot" "foo" { - description = "testing-acc" - name = "test-acc-stor-snapshot-%d" - collocated = true - volume_name = "${opc_compute_storage_volume.foo.name}" +resource "opc_compute_image_list_entry" "test" { + name = "${opc_compute_image_list.test.name}" + machine_images = [ "/oracle/public/oel_6.7_apaas_16.4.5_1610211300" ] + version = 1 } -// Create storage volume from snapshot resource "opc_compute_storage_volume" "test" { - name = "test-acc-stor-vol-final-%d" - description = "storage volume from snapshot" - size = 5 - snapshot_id = "${opc_compute_storage_volume_snapshot.foo.snapshot_id}" + name = "test-acc-stor-vol-bootable-%d" + description = "Provider Acceptance Tests Storage Volume Image List Entry" + size = 20 + tags = ["bar", "foo"] + image_list_entry = "${opc_compute_image_list_entry.test.version}" } -`, rInt, rInt, rInt) +` + +const testAccStorageVolumeBasicMaxSize = ` +resource "opc_compute_storage_volume" "test" { + name = "test-acc-stor-vol-%d" + description = "Provider Acceptance Tests Storage Volume Max Size" + size = 2048 } +`