boot from volume ops and docs

This commit is contained in:
Jon Perritt 2015-02-01 02:10:42 -07:00
parent 9c128b7c99
commit 436ef9e53b
2 changed files with 55 additions and 3 deletions

View File

@ -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
}

View File

@ -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: