create server helper fields
diff --git a/openstack/compute/v2/servers/requests.go b/openstack/compute/v2/servers/requests.go
index af77546..48c7df2 100644
--- a/openstack/compute/v2/servers/requests.go
+++ b/openstack/compute/v2/servers/requests.go
@@ -7,6 +7,8 @@
"fmt"
"github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/openstack/compute/v2/flavors"
+ "github.com/rackspace/gophercloud/openstack/compute/v2/images"
"github.com/rackspace/gophercloud/pagination"
)
@@ -127,13 +129,24 @@
// Name [required] is the name to assign to the newly launched server.
Name string
- // ImageRef [required] is the ID or full URL to the image that contains the server's OS and initial state.
- // Optional if using the boot-from-volume extension.
+ // ImageRef [optional; required if ImageName is not provided] is the ID or full
+ // URL to the image that contains the server's OS and initial state.
+ // Also optional if using the boot-from-volume extension.
ImageRef string
- // FlavorRef [required] is the ID or full URL to the flavor that describes the server's specs.
+ // ImageName [optional; required if ImageRef is not provided] is the name of the
+ // image that contains the server's OS and initial state.
+ // Also optional if using the boot-from-volume extension.
+ ImageName string
+
+ // FlavorRef [optional; required if FlavorName is not provided] is the ID or
+ // full URL to the flavor that describes the server's specs.
FlavorRef string
+ // FlavorName [optional; required if FlavorRef is not provided] is the name of
+ // the flavor that describes the server's specs.
+ FlavorName string
+
// SecurityGroups [optional] lists the names of the security groups to which this server should belong.
SecurityGroups []string
@@ -175,7 +188,9 @@
server["name"] = opts.Name
server["imageRef"] = opts.ImageRef
+ server["imageName"] = opts.ImageName
server["flavorRef"] = opts.FlavorRef
+ server["flavorName"] = opts.FlavorName
if opts.UserData != nil {
encoded := base64.StdEncoding.EncodeToString(opts.UserData)
@@ -242,6 +257,38 @@
return res
}
+ // If ImageRef isn't provided, use ImageName to ascertain the image ID.
+ if reqBody["server"].(map[string]interface{})["imageRef"].(string) == "" {
+ imageName := reqBody["server"].(map[string]interface{})["imageName"].(string)
+ if imageName == "" {
+ res.Err = errors.New("One and only one of ImageRef and ImageName must be provided.")
+ return res
+ }
+ imageID, err := images.IDFromName(client, imageName)
+ if err != nil {
+ res.Err = err
+ return res
+ }
+ reqBody["server"].(map[string]interface{})["imageRef"] = imageID
+ }
+ delete(reqBody["server"].(map[string]interface{}), "imageName")
+
+ // If FlavorRef isn't provided, use FlavorName to ascertain the flavor ID.
+ if reqBody["server"].(map[string]interface{})["flavorRef"].(string) == "" {
+ flavorName := reqBody["server"].(map[string]interface{})["flavorName"].(string)
+ if flavorName == "" {
+ res.Err = errors.New("One and only one of FlavorRef and FlavorName must be provided.")
+ return res
+ }
+ flavorID, err := flavors.IDFromName(client, flavorName)
+ if err != nil {
+ res.Err = err
+ return res
+ }
+ reqBody["server"].(map[string]interface{})["flavorRef"] = flavorID
+ }
+ delete(reqBody["server"].(map[string]interface{}), "flavorName")
+
_, res.Err = client.Post(listURL(client), reqBody, &res.Body, nil)
return res
}