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