From 436ef9e53ba74b975ae8d29106c0efc35f17a3d6 Mon Sep 17 00:00:00 2001 From: Jon Perritt Date: Sun, 1 Feb 2015 02:10:42 -0700 Subject: [PATCH] boot from volume ops and docs --- .../resource_openstack_compute_instance_v2.go | 41 +++++++++++++++++-- .../r/compute_instance_v2.html.markdown | 17 ++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/builtin/providers/openstack/resource_openstack_compute_instance_v2.go b/builtin/providers/openstack/resource_openstack_compute_instance_v2.go index 8059dec1b..766c5dea8 100644 --- a/builtin/providers/openstack/resource_openstack_compute_instance_v2.go +++ b/builtin/providers/openstack/resource_openstack_compute_instance_v2.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" "github.com/rackspace/gophercloud" + "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/bootfromvolume" "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs" "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups" "github.com/rackspace/gophercloud/openstack/compute/v2/servers" @@ -36,13 +37,13 @@ func resourceComputeInstanceV2() *schema.Resource { }, "image_ref": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, ForceNew: false, DefaultFunc: envDefaultFunc("OS_IMAGE_ID"), }, "flavor_ref": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, ForceNew: false, DefaultFunc: envDefaultFunc("OS_FLAVOR_ID"), }, @@ -113,6 +114,11 @@ func resourceComputeInstanceV2() *schema.Resource { Optional: true, ForceNew: true, }, + "block_device": &schema.Schema{ + Type: schema.TypeMap, + Optional: true, + ForceNew: true, + }, }, } } @@ -145,6 +151,14 @@ func resourceComputeInstanceV2Create(d *schema.ResourceData, meta interface{}) e } } + if blockDeviceRaw, ok := d.Get("block_device").(map[string]interface{}); ok && blockDeviceRaw != nil { + blockDevice := resourceInstanceBlockDeviceV2(d, blockDeviceRaw) + createOpts = &bootfromvolume.CreateOptsExt{ + createOpts, + blockDevice, + } + } + log.Printf("[INFO] Requesting instance creation") server, err := servers.Create(computeClient, createOpts).Extract() if err != nil { @@ -407,7 +421,7 @@ func resourceComputeInstanceV2Delete(d *schema.ResourceData, meta interface{}) e return nil } -// ServerStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch +// ServerV2StateRefreshFunc returns a resource.StateRefreshFunc that is used to watch // an OpenStack instance. func ServerV2StateRefreshFunc(client *gophercloud.ServiceClient, instanceID string) resource.StateRefreshFunc { return func() (interface{}, string, error) { @@ -450,3 +464,24 @@ func resourceInstanceMetadataV2(d *schema.ResourceData) map[string]string { } return m } + +func resourceInstanceBlockDeviceV2(d *schema.ResourceData, bd map[string]interface{}) []bootfromvolume.BlockDevice { + sourceType := bootfromvolume.SourceType(bd["source_type"].(string)) + bfvOpts := []bootfromvolume.BlockDevice{ + bootfromvolume.BlockDevice{ + UUID: bd["uuid"].(string), + SourceType: sourceType, + }, + } + if vs, ok := bd["volume_size"].(int); ok { + bfvOpts[0].VolumeSize = vs + } + if dt, ok := bd["destination_type"].(string); ok { + bfvOpts[0].DestinationType = dt + } + if bi, ok := bd["boot_index"].(int); ok { + bfvOpts[0].BootIndex = bi + } + + return bfvOpts +} diff --git a/website/source/docs/providers/openstack/r/compute_instance_v2.html.markdown b/website/source/docs/providers/openstack/r/compute_instance_v2.html.markdown index 6a9a5ac95..9095cb15a 100644 --- a/website/source/docs/providers/openstack/r/compute_instance_v2.html.markdown +++ b/website/source/docs/providers/openstack/r/compute_instance_v2.html.markdown @@ -62,6 +62,9 @@ The following arguments are supported: pair must already be created and associated with the tenant's account. Changing this creates a new server. +* `block_device` - (Optional) The object for booting by volume. The block_device + object structure is documented below. Changing this creates a new server. + The `network` block supports: * `uuid` - (Required unless `port` is provided) The network UUID to attach to @@ -73,6 +76,20 @@ The `network` block supports: * `fixed_ip` - (Optional) Specifies a fixed IP address to be used on this network. +The `block_device` block supports: + +* `uuid` - (Required) The UUID of the image, volume, or snapshot. + +* `source_type` - (Required) The source type of the device. Must be one of + "image", "volume", or "snapshot". + +* `volume_size` - (Optional) The size of the volume to create (in gigabytes). + +* `boot_index` - (Optional) The boot index of the volume. It defaults to 0. + +* `destination_type` - (Optional) The type that gets created. Possible values + are "volume" and "local". + ## Attributes Reference The following attributes are exported: