Ash Wilson | ae0ca65 | 2014-10-23 12:30:12 -0400 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/bootfromvolume" |
Ash Wilson | ae0ca65 | 2014-10-23 12:30:12 -0400 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/diskconfig" |
| 6 | os "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 7 | ) |
| 8 | |
| 9 | // CreateOpts specifies all of the options that Rackspace accepts in its Create request, including |
| 10 | // the union of all extensions that Rackspace supports. |
| 11 | type CreateOpts struct { |
| 12 | // Name [required] is the name to assign to the newly launched server. |
| 13 | Name string |
| 14 | |
| 15 | // ImageRef [required] is the ID or full URL to the image that contains the server's OS and initial state. |
| 16 | // Optional if using the boot-from-volume extension. |
| 17 | ImageRef string |
| 18 | |
| 19 | // FlavorRef [required] is the ID or full URL to the flavor that describes the server's specs. |
| 20 | FlavorRef string |
| 21 | |
| 22 | // SecurityGroups [optional] lists the names of the security groups to which this server should belong. |
| 23 | SecurityGroups []string |
| 24 | |
| 25 | // UserData [optional] contains configuration information or scripts to use upon launch. |
| 26 | // Create will base64-encode it for you. |
| 27 | UserData []byte |
| 28 | |
| 29 | // AvailabilityZone [optional] in which to launch the server. |
| 30 | AvailabilityZone string |
| 31 | |
| 32 | // Networks [optional] dictates how this server will be attached to available networks. |
| 33 | // By default, the server will be attached to all isolated networks for the tenant. |
| 34 | Networks []os.Network |
| 35 | |
| 36 | // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server. |
| 37 | Metadata map[string]string |
| 38 | |
Kevin Pike | 7bf54c5 | 2015-04-09 11:27:29 -0700 | [diff] [blame] | 39 | // Personality [optional] includes a list of maps with the path and contents |
| 40 | // of a file to inject into the server at launch. Contents should be |
| 41 | // base64 encoded. The maximum size of the file is 255 bytes (decoded). |
| 42 | Personality []map[string]string |
Ash Wilson | ae0ca65 | 2014-10-23 12:30:12 -0400 | [diff] [blame] | 43 | |
| 44 | // ConfigDrive [optional] enables metadata injection through a configuration drive. |
| 45 | ConfigDrive bool |
| 46 | |
Jon Perritt | f3b2e14 | 2014-11-04 16:00:19 -0600 | [diff] [blame] | 47 | // AdminPass [optional] sets the root user password. If not set, a randomly-generated |
| 48 | // password will be created and returned in the response. |
| 49 | AdminPass string |
| 50 | |
Ash Wilson | ae0ca65 | 2014-10-23 12:30:12 -0400 | [diff] [blame] | 51 | // Rackspace-specific extensions begin here. |
| 52 | |
| 53 | // KeyPair [optional] specifies the name of the SSH KeyPair to be injected into the newly launched |
| 54 | // server. See the "keypairs" extension in OpenStack compute v2. |
| 55 | KeyPair string |
| 56 | |
| 57 | // DiskConfig [optional] controls how the created server's disk is partitioned. See the "diskconfig" |
| 58 | // extension in OpenStack compute v2. |
| 59 | DiskConfig diskconfig.DiskConfig |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 60 | |
| 61 | // BlockDevice [optional] will create the server from a volume, which is created from an image, |
| 62 | // a snapshot, or an another volume. |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 63 | BlockDevice []bootfromvolume.BlockDevice |
Ash Wilson | ae0ca65 | 2014-10-23 12:30:12 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // ToServerCreateMap constructs a request body using all of the OpenStack extensions that are |
| 67 | // active on Rackspace. |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 68 | func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) { |
Ash Wilson | ae0ca65 | 2014-10-23 12:30:12 -0400 | [diff] [blame] | 69 | base := os.CreateOpts{ |
| 70 | Name: opts.Name, |
| 71 | ImageRef: opts.ImageRef, |
| 72 | FlavorRef: opts.FlavorRef, |
| 73 | SecurityGroups: opts.SecurityGroups, |
| 74 | UserData: opts.UserData, |
| 75 | AvailabilityZone: opts.AvailabilityZone, |
| 76 | Networks: opts.Networks, |
| 77 | Metadata: opts.Metadata, |
| 78 | Personality: opts.Personality, |
| 79 | ConfigDrive: opts.ConfigDrive, |
Jon Perritt | f3b2e14 | 2014-11-04 16:00:19 -0600 | [diff] [blame] | 80 | AdminPass: opts.AdminPass, |
Ash Wilson | ae0ca65 | 2014-10-23 12:30:12 -0400 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | drive := diskconfig.CreateOptsExt{ |
| 84 | CreateOptsBuilder: base, |
| 85 | DiskConfig: opts.DiskConfig, |
| 86 | } |
| 87 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 88 | res, err := drive.ToServerCreateMap() |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
Ash Wilson | ae0ca65 | 2014-10-23 12:30:12 -0400 | [diff] [blame] | 92 | |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 93 | if len(opts.BlockDevice) != 0 { |
| 94 | bfv := bootfromvolume.CreateOptsExt{ |
| 95 | CreateOptsBuilder: drive, |
| 96 | BlockDevice: opts.BlockDevice, |
| 97 | } |
| 98 | |
| 99 | res, err = bfv.ToServerCreateMap() |
| 100 | if err != nil { |
| 101 | return nil, err |
| 102 | } |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 103 | } |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 104 | |
Jon Perritt | aafafd5 | 2014-10-24 14:23:53 -0500 | [diff] [blame] | 105 | // key_name doesn't actually come from the extension (or at least isn't documented there) so |
| 106 | // we need to add it manually. |
| 107 | serverMap := res["server"].(map[string]interface{}) |
| 108 | serverMap["key_name"] = opts.KeyPair |
| 109 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 110 | return res, nil |
Ash Wilson | ae0ca65 | 2014-10-23 12:30:12 -0400 | [diff] [blame] | 111 | } |
Ash Wilson | d7814a3 | 2014-10-23 12:49:25 -0400 | [diff] [blame] | 112 | |
| 113 | // RebuildOpts represents all of the configuration options used in a server rebuild operation that |
| 114 | // are supported by Rackspace. |
| 115 | type RebuildOpts struct { |
| 116 | // Required. The ID of the image you want your server to be provisioned on |
| 117 | ImageID string |
| 118 | |
| 119 | // Name to set the server to |
| 120 | Name string |
| 121 | |
| 122 | // Required. The server's admin password |
| 123 | AdminPass string |
| 124 | |
| 125 | // AccessIPv4 [optional] provides a new IPv4 address for the instance. |
| 126 | AccessIPv4 string |
| 127 | |
| 128 | // AccessIPv6 [optional] provides a new IPv6 address for the instance. |
| 129 | AccessIPv6 string |
| 130 | |
| 131 | // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server. |
| 132 | Metadata map[string]string |
| 133 | |
Kevin Pike | 7bf54c5 | 2015-04-09 11:27:29 -0700 | [diff] [blame] | 134 | // Personality [optional] includes a list of maps with the path and contents |
| 135 | // of a file to inject into the server at launch. Contents should be |
| 136 | // base64 encoded. The maximum size of the file is 255 bytes (decoded). |
| 137 | Personality []map[string]string |
Ash Wilson | d7814a3 | 2014-10-23 12:49:25 -0400 | [diff] [blame] | 138 | |
| 139 | // Rackspace-specific stuff begins here. |
| 140 | |
| 141 | // DiskConfig [optional] controls how the created server's disk is partitioned. See the "diskconfig" |
| 142 | // extension in OpenStack compute v2. |
| 143 | DiskConfig diskconfig.DiskConfig |
| 144 | } |
| 145 | |
| 146 | // ToServerRebuildMap constructs a request body using all of the OpenStack extensions that are |
| 147 | // active on Rackspace. |
| 148 | func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) { |
| 149 | base := os.RebuildOpts{ |
| 150 | ImageID: opts.ImageID, |
| 151 | Name: opts.Name, |
| 152 | AdminPass: opts.AdminPass, |
| 153 | AccessIPv4: opts.AccessIPv4, |
| 154 | AccessIPv6: opts.AccessIPv6, |
| 155 | Metadata: opts.Metadata, |
| 156 | Personality: opts.Personality, |
| 157 | } |
| 158 | |
| 159 | drive := diskconfig.RebuildOptsExt{ |
| 160 | RebuildOptsBuilder: base, |
| 161 | DiskConfig: opts.DiskConfig, |
| 162 | } |
| 163 | |
| 164 | return drive.ToServerRebuildMap() |
| 165 | } |