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