BlockDevice -> []BlockDevice in CreateOpts
diff --git a/acceptance/openstack/compute/v2/bootfromvolume_test.go b/acceptance/openstack/compute/v2/bootfromvolume_test.go
index 9f18379..d08abe6 100644
--- a/acceptance/openstack/compute/v2/bootfromvolume_test.go
+++ b/acceptance/openstack/compute/v2/bootfromvolume_test.go
@@ -27,10 +27,12 @@
name := tools.RandomString("Gophercloud-", 8)
t.Logf("Creating server [%s].", name)
- bd := bootfromvolume.BlockDevice{
- UUID: choices.ImageID,
- SourceType: "image",
- VolumeSize: 10,
+ bd := []bootfromvolume.BlockDevice{
+ bootfromvolume.BlockDevice{
+ UUID: choices.ImageID,
+ SourceType: bootfromvolume.Image,
+ VolumeSize: 10,
+ },
}
serverCreateOpts := servers.CreateOpts{
@@ -43,6 +45,6 @@
}).Extract()
th.AssertNoErr(t, err)
t.Logf("Created server: %+v\n", server)
- defer deleteServer(t, client, server)
+ //defer deleteServer(t, client, server)
t.Logf("Deleting server [%s]...", name)
}
diff --git a/acceptance/rackspace/compute/v2/bootfromvolume_test.go b/acceptance/rackspace/compute/v2/bootfromvolume_test.go
index 591a137..010bf42 100644
--- a/acceptance/rackspace/compute/v2/bootfromvolume_test.go
+++ b/acceptance/rackspace/compute/v2/bootfromvolume_test.go
@@ -26,10 +26,12 @@
name := tools.RandomString("Gophercloud-", 8)
t.Logf("Creating server [%s].", name)
- bd := osBFV.BlockDevice{
- UUID: options.imageID,
- SourceType: "image",
- VolumeSize: 10,
+ bd := []osBFV.BlockDevice{
+ osBFV.BlockDevice{
+ UUID: options.imageID,
+ SourceType: osBFV.Image,
+ VolumeSize: 10,
+ },
}
server, err := bootfromvolume.Create(client, servers.CreateOpts{
@@ -39,6 +41,6 @@
}).Extract()
th.AssertNoErr(t, err)
t.Logf("Created server: %+v\n", server)
- defer deleteServer(t, client, server)
+ //defer deleteServer(t, client, server)
t.Logf("Deleting server [%s]...", name)
}
diff --git a/openstack/compute/v2/extensions/bootfromvolume/requests.go b/openstack/compute/v2/extensions/bootfromvolume/requests.go
index 8322b40..5a976d1 100644
--- a/openstack/compute/v2/extensions/bootfromvolume/requests.go
+++ b/openstack/compute/v2/extensions/bootfromvolume/requests.go
@@ -10,6 +10,15 @@
"github.com/racker/perigee"
)
+// SourceType represents the type of medium being used to create the volume.
+type SourceType string
+
+const (
+ Volume SourceType = "volume"
+ Snapshot SourceType = "snapshot"
+ Image SourceType = "image"
+)
+
// BlockDevice is a structure with options for booting a server instance
// from a volume. The volume may be created from an image, snapshot, or another
// volume.
@@ -26,7 +35,7 @@
DestinationType string `json:"destination_type"`
// SourceType [required] must be one of: "volume", "snapshot", "image".
- SourceType string `json:"source_type"`
+ SourceType SourceType `json:"source_type"`
// UUID [required] is the unique identifier for the volume, snapshot, or image (see above)
UUID string `json:"uuid"`
@@ -39,7 +48,7 @@
// by allowing for a block device mapping.
type CreateOptsExt struct {
servers.CreateOptsBuilder
- BlockDevice BlockDevice `json:"block_device_mapping_v2,omitempty"`
+ BlockDevice []BlockDevice `json:"block_device_mapping_v2,omitempty"`
}
// ToServerCreateMap adds the block device mapping option to the base server
@@ -50,40 +59,41 @@
return nil, err
}
- var blockDevice BlockDevice
- if opts.BlockDevice == blockDevice {
- return base, nil
- }
-
- if opts.BlockDevice.SourceType != "volume" &&
- opts.BlockDevice.SourceType != "image" &&
- opts.BlockDevice.SourceType != "snapshot" &&
- opts.BlockDevice.SourceType != "" {
- return nil, errors.New("SourceType must be one of: volume, image, snapshot, [blank].")
+ if len(opts.BlockDevice) == 0 {
+ return nil, errors.New("Required fields UUID and SourceType not set.")
}
serverMap := base["server"].(map[string]interface{})
- bd := make(map[string]interface{})
- bd["source_type"] = opts.BlockDevice.SourceType
- bd["boot_index"] = strconv.Itoa(opts.BlockDevice.BootIndex)
- bd["delete_on_termination"] = strconv.FormatBool(opts.BlockDevice.DeleteOnTermination)
- bd["volume_size"] = strconv.Itoa(opts.BlockDevice.VolumeSize)
- if opts.BlockDevice.UUID != "" {
- bd["uuid"] = opts.BlockDevice.UUID
- }
- if opts.BlockDevice.DestinationType != "" {
- bd["destination_type"] = opts.BlockDevice.DestinationType
- }
+ blockDevice := make([]map[string]interface{}, len(opts.BlockDevice))
- serverMap["block_device_mapping_v2"] = []map[string]interface{}{bd}
+ for i, bd := range opts.BlockDevice {
+ if string(bd.SourceType) == "" {
+ return nil, errors.New("SourceType must be one of: volume, image, snapshot.")
+ }
+
+ blockDevice[i] = make(map[string]interface{})
+
+ blockDevice[i]["source_type"] = bd.SourceType
+ blockDevice[i]["boot_index"] = strconv.Itoa(bd.BootIndex)
+ blockDevice[i]["delete_on_termination"] = strconv.FormatBool(bd.DeleteOnTermination)
+ blockDevice[i]["volume_size"] = strconv.Itoa(bd.VolumeSize)
+ if bd.UUID != "" {
+ blockDevice[i]["uuid"] = bd.UUID
+ }
+ if bd.DestinationType != "" {
+ blockDevice[i]["destination_type"] = bd.DestinationType
+ }
+
+ }
+ serverMap["block_device_mapping_v2"] = blockDevice
return base, nil
}
// Create requests the creation of a server from the given block device mapping.
-func Create(client *gophercloud.ServiceClient, opts servers.CreateOptsBuilder) CreateResult {
- var res CreateResult
+func Create(client *gophercloud.ServiceClient, opts servers.CreateOptsBuilder) servers.CreateResult {
+ var res servers.CreateResult
reqBody, err := opts.ToServerCreateMap()
if err != nil {
diff --git a/openstack/compute/v2/extensions/bootfromvolume/requests_test.go b/openstack/compute/v2/extensions/bootfromvolume/requests_test.go
index bad6c03..5bf9137 100644
--- a/openstack/compute/v2/extensions/bootfromvolume/requests_test.go
+++ b/openstack/compute/v2/extensions/bootfromvolume/requests_test.go
@@ -16,11 +16,13 @@
ext := CreateOptsExt{
CreateOptsBuilder: base,
- BlockDevice: BlockDevice{
- UUID: "123456",
- SourceType: "image",
- DestinationType: "volume",
- VolumeSize: 10,
+ BlockDevice: []BlockDevice{
+ BlockDevice{
+ UUID: "123456",
+ SourceType: Image,
+ DestinationType: "volume",
+ VolumeSize: 10,
+ },
},
}
diff --git a/rackspace/compute/v2/bootfromvolume/delegate.go b/rackspace/compute/v2/bootfromvolume/delegate.go
index 636ced6..2580459 100644
--- a/rackspace/compute/v2/bootfromvolume/delegate.go
+++ b/rackspace/compute/v2/bootfromvolume/delegate.go
@@ -1,12 +1,12 @@
package bootfromvolume
import (
- "github.com/rackspace/gophercloud"
- osBFV "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/bootfromvolume"
- osServers "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
+ "github.com/rackspace/gophercloud"
+ osBFV "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/bootfromvolume"
+ osServers "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
)
// Create requests the creation of a server from the given block device mapping.
-func Create(client *gophercloud.ServiceClient, opts osServers.CreateOptsBuilder) osBFV.CreateResult {
- return osBFV.Create(client, opts)
+func Create(client *gophercloud.ServiceClient, opts osServers.CreateOptsBuilder) osServers.CreateResult {
+ return osBFV.Create(client, opts)
}
diff --git a/rackspace/compute/v2/bootfromvolume/delegate_test.go b/rackspace/compute/v2/bootfromvolume/delegate_test.go
index 794f0c8..0b53527 100644
--- a/rackspace/compute/v2/bootfromvolume/delegate_test.go
+++ b/rackspace/compute/v2/bootfromvolume/delegate_test.go
@@ -17,11 +17,13 @@
ext := osBFV.CreateOptsExt{
CreateOptsBuilder: base,
- BlockDevice: osBFV.BlockDevice{
- UUID: "123456",
- SourceType: "image",
- DestinationType: "volume",
- VolumeSize: 10,
+ BlockDevice: []osBFV.BlockDevice{
+ osBFV.BlockDevice{
+ UUID: "123456",
+ SourceType: osBFV.Image,
+ DestinationType: "volume",
+ VolumeSize: 10,
+ },
},
}
diff --git a/rackspace/compute/v2/servers/requests.go b/rackspace/compute/v2/servers/requests.go
index 4f40e83..144e837 100644
--- a/rackspace/compute/v2/servers/requests.go
+++ b/rackspace/compute/v2/servers/requests.go
@@ -55,7 +55,7 @@
// BlockDevice [optional] will create the server from a volume, which is created from an image,
// a snapshot, or an another volume.
- BlockDevice bootfromvolume.BlockDevice
+ BlockDevice []bootfromvolume.BlockDevice
}
// ToServerCreateMap constructs a request body using all of the OpenStack extensions that are
@@ -89,9 +89,16 @@
serverMap := res["server"].(map[string]interface{})
serverMap["key_name"] = opts.KeyPair
- var bd bootfromvolume.BlockDevice
- if opts.BlockDevice != bd {
- serverMap["block_device_mapping_v2"] = []bootfromvolume.BlockDevice{opts.BlockDevice}
+ if len(opts.BlockDevice) != 0 {
+ bfv := bootfromvolume.CreateOptsExt{
+ CreateOptsBuilder: drive,
+ BlockDevice: opts.BlockDevice,
+ }
+
+ res, err = bfv.ToServerCreateMap()
+ if err != nil {
+ return nil, err
+ }
}
return res, nil