Refactoring Bootable Storage Volumes

This commit is contained in:
tombuildsstuff 2017-04-06 19:12:20 +01:00
parent 76b224ea0a
commit 2332256af6
2 changed files with 86 additions and 125 deletions

View File

@ -42,47 +42,24 @@ func resourceOPCStorageVolume() *schema.Resource {
}, true),
},
"snapshot": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"snapshot_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"snapshot_account": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"bootable": {
Type: schema.TypeList,
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
},
"image_list": {
Type: schema.TypeString,
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,
},
},
},
"image_list_entry": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
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)
if bootable == true {
if imageList == "" {
return fmt.Errorf("Error: A Bootable Volume must have an Image List!")
}
if imageListEntry == -1 {
return fmt.Errorf("Error: A Bootable Volume must have an Image List Entry!")
}
}
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 v, ok := d.GetOk("snapshot_account"); ok {
input.SnapshotAccount = v.(string)
}
if v, ok := d.GetOk("snapshot_id"); ok {
input.SnapshotID = v.(string)
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)

View File

@ -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,17 +203,44 @@ 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_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
}
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}"
}
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}"
}
`
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_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
}
resource "opc_compute_storage_volume" "test" {
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}"
}
`
@ -227,29 +251,3 @@ resource "opc_compute_storage_volume" "test" {
size = 2048
}
`
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
}
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}"
}
// 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}"
}
`, rInt, rInt, rInt)
}