Ash Wilson | ae0ca65 | 2014-10-23 12:30:12 -0400 | [diff] [blame^] | 1 | package servers |
| 2 | |
| 3 | import ( |
| 4 | "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/diskconfig" |
| 5 | os "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 6 | ) |
| 7 | |
| 8 | // CreateOpts specifies all of the options that Rackspace accepts in its Create request, including |
| 9 | // the union of all extensions that Rackspace supports. |
| 10 | type CreateOpts struct { |
| 11 | // Name [required] is the name to assign to the newly launched server. |
| 12 | Name string |
| 13 | |
| 14 | // ImageRef [required] is the ID or full URL to the image that contains the server's OS and initial state. |
| 15 | // Optional if using the boot-from-volume extension. |
| 16 | ImageRef string |
| 17 | |
| 18 | // FlavorRef [required] is the ID or full URL to the flavor that describes the server's specs. |
| 19 | FlavorRef string |
| 20 | |
| 21 | // SecurityGroups [optional] lists the names of the security groups to which this server should belong. |
| 22 | SecurityGroups []string |
| 23 | |
| 24 | // UserData [optional] contains configuration information or scripts to use upon launch. |
| 25 | // Create will base64-encode it for you. |
| 26 | UserData []byte |
| 27 | |
| 28 | // AvailabilityZone [optional] in which to launch the server. |
| 29 | AvailabilityZone string |
| 30 | |
| 31 | // Networks [optional] dictates how this server will be attached to available networks. |
| 32 | // By default, the server will be attached to all isolated networks for the tenant. |
| 33 | Networks []os.Network |
| 34 | |
| 35 | // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server. |
| 36 | Metadata map[string]string |
| 37 | |
| 38 | // Personality [optional] includes the path and contents of a file to inject into the server at launch. |
| 39 | // The maximum size of the file is 255 bytes (decoded). |
| 40 | Personality []byte |
| 41 | |
| 42 | // ConfigDrive [optional] enables metadata injection through a configuration drive. |
| 43 | ConfigDrive bool |
| 44 | |
| 45 | // Rackspace-specific extensions begin here. |
| 46 | |
| 47 | // KeyPair [optional] specifies the name of the SSH KeyPair to be injected into the newly launched |
| 48 | // server. See the "keypairs" extension in OpenStack compute v2. |
| 49 | KeyPair string |
| 50 | |
| 51 | // DiskConfig [optional] controls how the created server's disk is partitioned. See the "diskconfig" |
| 52 | // extension in OpenStack compute v2. |
| 53 | DiskConfig diskconfig.DiskConfig |
| 54 | } |
| 55 | |
| 56 | // ToServerCreateMap constructs a request body using all of the OpenStack extensions that are |
| 57 | // active on Rackspace. |
| 58 | func (opts CreateOpts) ToServerCreateMap() map[string]interface{} { |
| 59 | base := os.CreateOpts{ |
| 60 | Name: opts.Name, |
| 61 | ImageRef: opts.ImageRef, |
| 62 | FlavorRef: opts.FlavorRef, |
| 63 | SecurityGroups: opts.SecurityGroups, |
| 64 | UserData: opts.UserData, |
| 65 | AvailabilityZone: opts.AvailabilityZone, |
| 66 | Networks: opts.Networks, |
| 67 | Metadata: opts.Metadata, |
| 68 | Personality: opts.Personality, |
| 69 | ConfigDrive: opts.ConfigDrive, |
| 70 | } |
| 71 | |
| 72 | drive := diskconfig.CreateOptsExt{ |
| 73 | CreateOptsBuilder: base, |
| 74 | DiskConfig: opts.DiskConfig, |
| 75 | } |
| 76 | |
| 77 | result := drive.ToServerCreateMap() |
| 78 | |
| 79 | // key_name doesn't actually come from the extension (or at least isn't documented there) so |
| 80 | // we need to add it manually. |
| 81 | serverMap := result["server"].(map[string]interface{}) |
| 82 | serverMap["key_name"] = opts.KeyPair |
| 83 | |
| 84 | return result |
| 85 | } |