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,36 +42,16 @@ func resourceOPCStorageVolume() *schema.Resource {
}, true), }, 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": { "bootable": {
Type: schema.TypeList, Type: schema.TypeBool,
Optional: true, Optional: true,
Default: false,
ForceNew: true, ForceNew: true,
MaxItems: 1, },
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"image_list": { "image_list": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Optional: true,
ForceNew: true, ForceNew: true,
}, },
@ -81,9 +61,6 @@ func resourceOPCStorageVolume() *schema.Resource {
ForceNew: true, ForceNew: true,
Default: -1, Default: -1,
}, },
},
},
},
"tags": tagsOptionalSchema(), "tags": tagsOptionalSchema(),
@ -139,29 +116,31 @@ func resourceOPCStorageVolumeCreate(d *schema.ResourceData, meta interface{}) er
description := d.Get("description").(string) description := d.Get("description").(string)
size := d.Get("size").(int) size := d.Get("size").(int)
storageType := d.Get("storage_type").(string) 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{ input := compute.CreateStorageVolumeInput{
Name: name, Name: name,
Description: description, Description: description,
Size: strconv.Itoa(size), Size: strconv.Itoa(size),
Properties: []string{storageType}, Properties: []string{storageType},
Bootable: bootable,
ImageList: imageList,
ImageListEntry: imageListEntry,
Tags: getStringList(d, "tags"), 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)
}
info, err := client.CreateStorageVolume(&input) info, err := client.CreateStorageVolume(&input)
if err != nil { if err != nil {
return fmt.Errorf("Error creating storage volume %s: %s", name, err) return fmt.Errorf("Error creating storage volume %s: %s", name, err)
@ -178,12 +157,16 @@ func resourceOPCStorageVolumeUpdate(d *schema.ResourceData, meta interface{}) er
description := d.Get("description").(string) description := d.Get("description").(string)
size := d.Get("size").(int) size := d.Get("size").(int)
storageType := d.Get("storage_type").(string) storageType := d.Get("storage_type").(string)
imageList := d.Get("image_list").(string)
imageListEntry := d.Get("image_list_entry").(int)
input := compute.UpdateStorageVolumeInput{ input := compute.UpdateStorageVolumeInput{
Name: name, Name: name,
Description: description, Description: description,
Size: strconv.Itoa(size), Size: strconv.Itoa(size),
Properties: []string{storageType}, Properties: []string{storageType},
ImageList: imageList,
ImageListEntry: imageListEntry,
Tags: getStringList(d, "tags"), Tags: getStringList(d, "tags"),
} }
_, err := client.UpdateStorageVolume(&input) _, err := client.UpdateStorageVolume(&input)
@ -220,21 +203,19 @@ func resourceOPCStorageVolumeRead(d *schema.ResourceData, meta interface{}) erro
d.Set("name", result.Name) d.Set("name", result.Name)
d.Set("description", result.Description) d.Set("description", result.Description)
d.Set("storage", result.Properties[0]) 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) size, err := strconv.Atoi(result.Size)
if err != nil { if err != nil {
return err return err
} }
d.Set("size", size) 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 { if err := setStringList(d, "tags", result.Tags); err != nil {
return err return err
} }
flattenOPCStorageVolumeOptionalFields(d, result)
flattenOPCStorageVolumeComputedFields(d, result) flattenOPCStorageVolumeComputedFields(d, result)
return nil return nil
@ -255,24 +236,6 @@ func resourceOPCStorageVolumeDelete(d *schema.ResourceData, meta interface{}) er
return nil 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) { func flattenOPCStorageVolumeComputedFields(d *schema.ResourceData, result *compute.StorageVolumeInfo) {
d.Set("hypervisor", result.Hypervisor) d.Set("hypervisor", result.Hypervisor)
d.Set("machine_image", result.MachineImage) 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" volumeResourceName := "opc_compute_storage_volume.test"
rInt := acctest.RandInt() ri := acctest.RandInt()
config := fmt.Sprintf(testAccStorageVolumeImageListEntry, ri, ri)
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
@ -126,13 +127,9 @@ func TestAccOPCStorageVolume_FromSnapshot(t *testing.T) {
CheckDestroy: opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeDestroyed), CheckDestroy: opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeDestroyed),
Steps: []resource.TestStep{ Steps: []resource.TestStep{
{ {
Config: testAccStorageVolumeFromSnapshot(rInt), Config: config,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeExists), 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 = ` const testAccStorageVolumeBootable = `
resource "opc_compute_image_list" "test" { resource "opc_compute_image_list" "test" {
name = "test-acc-stor-vol-bootable-image-list-%d" 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" { resource "opc_compute_storage_volume" "test" {
name = "test-acc-stor-vol-bootable-%d" name = "test-acc-stor-vol-bootable-%d"
description = "Provider Acceptance Tests Storage Volume" description = "Provider Acceptance Tests Storage Volume Bootable"
size = 2 size = 20
tags = ["bar", "foo"] tags = ["bar", "foo"]
bootable { bootable = true
image_list = "${opc_compute_image_list.test.name}" 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 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)
}