additionally return 'error' from 'ToCreateServerMap'
diff --git a/acceptance/openstack/compute/v2/servers_test.go b/acceptance/openstack/compute/v2/servers_test.go
index 6bdc9e9..ecadf5f 100644
--- a/acceptance/openstack/compute/v2/servers_test.go
+++ b/acceptance/openstack/compute/v2/servers_test.go
@@ -63,7 +63,7 @@
}
func createServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) {
- if testing.Short(){
+ if testing.Short() {
t.Skip("Skipping test that requires server creation in short mode.")
}
diff --git a/acceptance/rackspace/compute/v2/virtualinterfaces_test.go b/acceptance/rackspace/compute/v2/virtualinterfaces_test.go
index 446d3a3..da10d14 100644
--- a/acceptance/rackspace/compute/v2/virtualinterfaces_test.go
+++ b/acceptance/rackspace/compute/v2/virtualinterfaces_test.go
@@ -3,51 +3,51 @@
package v2
import (
- "testing"
+ "testing"
- "github.com/rackspace/gophercloud/pagination"
- "github.com/rackspace/gophercloud/rackspace/compute/v2/networks"
- "github.com/rackspace/gophercloud/rackspace/compute/v2/virtualinterfaces"
- th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/pagination"
+ "github.com/rackspace/gophercloud/rackspace/compute/v2/networks"
+ "github.com/rackspace/gophercloud/rackspace/compute/v2/virtualinterfaces"
+ th "github.com/rackspace/gophercloud/testhelper"
)
func TestVirtualInterfaces(t *testing.T) {
- client, err := newClient()
- th.AssertNoErr(t, err)
+ client, err := newClient()
+ th.AssertNoErr(t, err)
- // Create a server
- server := createServer(t, client)
- t.Logf("Created Server: %v\n", server)
- defer deleteServer(t, client, server)
- serverID := server.ID
+ // Create a server
+ server := createServer(t, client)
+ t.Logf("Created Server: %v\n", server)
+ defer deleteServer(t, client, server)
+ serverID := server.ID
- // Create a network
- n, err := networks.Create(client, networks.CreateOpts{Label: "sample_network", CIDR: "172.20.0.0/24"}).Extract()
- th.AssertNoErr(t, err)
- t.Logf("Created Network: %v\n", n)
- defer networks.Delete(client, n.ID)
- networkID := n.ID
+ // Create a network
+ n, err := networks.Create(client, networks.CreateOpts{Label: "sample_network", CIDR: "172.20.0.0/24"}).Extract()
+ th.AssertNoErr(t, err)
+ t.Logf("Created Network: %v\n", n)
+ defer networks.Delete(client, n.ID)
+ networkID := n.ID
- // Create a virtual interface
- vi, err := virtualinterfaces.Create(client, serverID, networkID).Extract()
- th.AssertNoErr(t, err)
- t.Logf("Created virtual interface: %+v\n", vi)
- defer virtualinterfaces.Delete(client, serverID, vi.ID)
+ // Create a virtual interface
+ vi, err := virtualinterfaces.Create(client, serverID, networkID).Extract()
+ th.AssertNoErr(t, err)
+ t.Logf("Created virtual interface: %+v\n", vi)
+ defer virtualinterfaces.Delete(client, serverID, vi.ID)
- // List virtual interfaces
- pager := virtualinterfaces.List(client, serverID)
- err = pager.EachPage(func(page pagination.Page) (bool, error) {
- t.Logf("--- Page ---")
+ // List virtual interfaces
+ pager := virtualinterfaces.List(client, serverID)
+ err = pager.EachPage(func(page pagination.Page) (bool, error) {
+ t.Logf("--- Page ---")
- virtualinterfacesList, err := virtualinterfaces.ExtractVirtualInterfaces(page)
- th.AssertNoErr(t, err)
+ virtualinterfacesList, err := virtualinterfaces.ExtractVirtualInterfaces(page)
+ th.AssertNoErr(t, err)
- for _, vi := range virtualinterfacesList {
- t.Logf("Virtual Interface: ID [%s] MAC Address [%s] IP Addresses [%v]",
- vi.ID, vi.MACAddress, vi.IPAddresses)
- }
+ for _, vi := range virtualinterfacesList {
+ t.Logf("Virtual Interface: ID [%s] MAC Address [%s] IP Addresses [%v]",
+ vi.ID, vi.MACAddress, vi.IPAddresses)
+ }
- return true, nil
- })
- th.CheckNoErr(t, err)
+ return true, nil
+ })
+ th.CheckNoErr(t, err)
}
diff --git a/openstack/compute/v2/extensions/bootfromvolume/requests.go b/openstack/compute/v2/extensions/bootfromvolume/requests.go
index b0cf61e..bbef496 100644
--- a/openstack/compute/v2/extensions/bootfromvolume/requests.go
+++ b/openstack/compute/v2/extensions/bootfromvolume/requests.go
@@ -1,31 +1,34 @@
package bootfromvolume
import (
- "errors"
+ "errors"
- "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
+ "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
)
type CreateOptsExt struct {
servers.CreateOptsBuilder
- BlockDeviceMapping BlockDeviceMapping
+ BDM BlockDeviceMapping
}
// ToServerCreateMap adds the block device mapping option to the base server
// creation options.
func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) {
- if opts.SourceType != "volume" && opts.SourceType != "image" && opts.SourceType != "snapshot" {
- return nil, errors.New("SourceType must be one of: volume, image, snapshot.")
- }
+ if opts.BDM.SourceType != "volume" && opts.BDM.SourceType != "image" && opts.BDM.SourceType != "snapshot" {
+ return nil, errors.New("SourceType must be one of: volume, image, snapshot.")
+ }
- if opts.UUID == "" {
- return nil, errors.New("Required field UUID not set.")
- }
+ if opts.BDM.UUID == "" {
+ return nil, errors.New("Required field UUID not set.")
+ }
- base := opts.CreateOptsBuilder.ToServerCreateMap()
+ base, err := opts.CreateOptsBuilder.ToServerCreateMap()
+ if err != nil {
+ return nil, err
+ }
- serverMap := base["server"].(map[string]interface{})
- serverMap["block_device_mapping_v2"] = opts.BlockDeviceMapping
+ serverMap := base["server"].(map[string]interface{})
+ serverMap["block_device_mapping_v2"] = opts.BDM
- return base
+ return base, nil
}
diff --git a/openstack/compute/v2/extensions/bootfromvolume/results.go b/openstack/compute/v2/extensions/bootfromvolume/results.go
index ef093e8..9d33728 100644
--- a/openstack/compute/v2/extensions/bootfromvolume/results.go
+++ b/openstack/compute/v2/extensions/bootfromvolume/results.go
@@ -1,10 +1,10 @@
package bootfromvolume
type BlockDeviceMapping struct {
- BootIndex int `json:"boot_index"`
- DeleteOnTermination bool `json:"delete_on_termination"`
- DestinationType string `json:"destination_type"`
- SourceType string `json:"source_type"`
- UUID string `json:"uuid"`
- VolumeSize int `json:"volume_size"`
+ BootIndex int `json:"boot_index"`
+ DeleteOnTermination bool `json:"delete_on_termination"`
+ DestinationType string `json:"destination_type"`
+ SourceType string `json:"source_type"`
+ UUID string `json:"uuid"`
+ VolumeSize int `json:"volume_size"`
}
diff --git a/openstack/compute/v2/extensions/diskconfig/requests.go b/openstack/compute/v2/extensions/diskconfig/requests.go
index 06a922a..cc390f7 100644
--- a/openstack/compute/v2/extensions/diskconfig/requests.go
+++ b/openstack/compute/v2/extensions/diskconfig/requests.go
@@ -45,13 +45,16 @@
}
// ToServerCreateMap adds the diskconfig option to the base server creation options.
-func (opts CreateOptsExt) ToServerCreateMap() map[string]interface{} {
- base := opts.CreateOptsBuilder.ToServerCreateMap()
+func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) {
+ base, err := opts.CreateOptsBuilder.ToServerCreateMap()
+ if err != nil {
+ return nil, err
+ }
serverMap := base["server"].(map[string]interface{})
serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
- return base
+ return base, nil
}
// RebuildOptsExt adds a DiskConfig option to the base RebuildOpts.
diff --git a/openstack/compute/v2/extensions/diskconfig/requests_test.go b/openstack/compute/v2/extensions/diskconfig/requests_test.go
index 1f4f626..e3c26d4 100644
--- a/openstack/compute/v2/extensions/diskconfig/requests_test.go
+++ b/openstack/compute/v2/extensions/diskconfig/requests_test.go
@@ -29,7 +29,9 @@
}
}
`
- th.CheckJSONEquals(t, expected, ext.ToServerCreateMap())
+ actual, err := ext.ToServerCreateMap()
+ th.AssertNoErr(t, err)
+ th.CheckJSONEquals(t, expected, actual)
}
func TestRebuildOpts(t *testing.T) {
diff --git a/openstack/compute/v2/servers/requests.go b/openstack/compute/v2/servers/requests.go
index c6eca11..544f816 100644
--- a/openstack/compute/v2/servers/requests.go
+++ b/openstack/compute/v2/servers/requests.go
@@ -79,7 +79,7 @@
// CreateOptsBuilder describes struct types that can be accepted by the Create call.
// The CreateOpts struct in this package does.
type CreateOptsBuilder interface {
- ToServerCreateMap() map[string]interface{}
+ ToServerCreateMap() (map[string]interface{}, error)
}
// Network is used within CreateOpts to control a new server's network attachments.
@@ -134,7 +134,7 @@
}
// ToServerCreateMap assembles a request body based on the contents of a CreateOpts.
-func (opts CreateOpts) ToServerCreateMap() map[string]interface{} {
+func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
server := make(map[string]interface{})
server["name"] = opts.Name
@@ -183,19 +183,26 @@
server["networks"] = networks
}
- return map[string]interface{}{"server": server}
+ return map[string]interface{}{"server": server}, nil
}
// Create requests a server to be provisioned to the user in the current tenant.
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
- var result CreateResult
- _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{
- Results: &result.Body,
- ReqBody: opts.ToServerCreateMap(),
+ var res CreateResult
+
+ reqBody, err := opts.ToServerCreateMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
+ _, res.Err = perigee.Request("POST", listURL(client), perigee.Options{
+ Results: &res.Body,
+ ReqBody: reqBody,
MoreHeaders: client.AuthenticatedHeaders(),
OkCodes: []int{202},
})
- return result
+ return res
}
// Delete requests that a server previously provisioned be removed from your account.
diff --git a/rackspace/compute/v2/servers/requests.go b/rackspace/compute/v2/servers/requests.go
index b83a893..4ba7619 100644
--- a/rackspace/compute/v2/servers/requests.go
+++ b/rackspace/compute/v2/servers/requests.go
@@ -55,7 +55,7 @@
// ToServerCreateMap constructs a request body using all of the OpenStack extensions that are
// active on Rackspace.
-func (opts CreateOpts) ToServerCreateMap() map[string]interface{} {
+func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
base := os.CreateOpts{
Name: opts.Name,
ImageRef: opts.ImageRef,
@@ -74,14 +74,17 @@
DiskConfig: opts.DiskConfig,
}
- result := drive.ToServerCreateMap()
+ res, err := drive.ToServerCreateMap()
+ if err != nil {
+ return nil, err
+ }
// key_name doesn't actually come from the extension (or at least isn't documented there) so
// we need to add it manually.
- serverMap := result["server"].(map[string]interface{})
+ serverMap := res["server"].(map[string]interface{})
serverMap["key_name"] = opts.KeyPair
- return result
+ return res, nil
}
// RebuildOpts represents all of the configuration options used in a server rebuild operation that
diff --git a/rackspace/compute/v2/servers/requests_test.go b/rackspace/compute/v2/servers/requests_test.go
index ac7058f..3c0f806 100644
--- a/rackspace/compute/v2/servers/requests_test.go
+++ b/rackspace/compute/v2/servers/requests_test.go
@@ -27,7 +27,9 @@
}
}
`
- th.CheckJSONEquals(t, expected, opts.ToServerCreateMap())
+ actual, err := opts.ToServerCreateMap()
+ th.AssertNoErr(t, err)
+ th.CheckJSONEquals(t, expected, actual)
}
func TestRebuildOpts(t *testing.T) {