blob: 4f40e83e3f625aeb0adddc4ecb1f104a0c4aa22b [file] [log] [blame]
Ash Wilsonae0ca652014-10-23 12:30:12 -04001package servers
2
3import (
Jon Perrittd9a4bf72014-10-23 23:44:04 -05004 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/bootfromvolume"
Ash Wilsonae0ca652014-10-23 12:30:12 -04005 "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.
11type 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
39 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
40 // The maximum size of the file is 255 bytes (decoded).
41 Personality []byte
42
43 // ConfigDrive [optional] enables metadata injection through a configuration drive.
44 ConfigDrive bool
45
46 // Rackspace-specific extensions begin here.
47
48 // KeyPair [optional] specifies the name of the SSH KeyPair to be injected into the newly launched
49 // server. See the "keypairs" extension in OpenStack compute v2.
50 KeyPair string
51
52 // DiskConfig [optional] controls how the created server's disk is partitioned. See the "diskconfig"
53 // extension in OpenStack compute v2.
54 DiskConfig diskconfig.DiskConfig
Jon Perrittd9a4bf72014-10-23 23:44:04 -050055
56 // BlockDevice [optional] will create the server from a volume, which is created from an image,
57 // a snapshot, or an another volume.
58 BlockDevice bootfromvolume.BlockDevice
Ash Wilsonae0ca652014-10-23 12:30:12 -040059}
60
61// ToServerCreateMap constructs a request body using all of the OpenStack extensions that are
62// active on Rackspace.
Jon Perritt4149d7c2014-10-23 21:23:46 -050063func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Ash Wilsonae0ca652014-10-23 12:30:12 -040064 base := os.CreateOpts{
65 Name: opts.Name,
66 ImageRef: opts.ImageRef,
67 FlavorRef: opts.FlavorRef,
68 SecurityGroups: opts.SecurityGroups,
69 UserData: opts.UserData,
70 AvailabilityZone: opts.AvailabilityZone,
71 Networks: opts.Networks,
72 Metadata: opts.Metadata,
73 Personality: opts.Personality,
74 ConfigDrive: opts.ConfigDrive,
75 }
76
77 drive := diskconfig.CreateOptsExt{
78 CreateOptsBuilder: base,
79 DiskConfig: opts.DiskConfig,
80 }
81
Jon Perritt4149d7c2014-10-23 21:23:46 -050082 res, err := drive.ToServerCreateMap()
83 if err != nil {
84 return nil, err
85 }
Ash Wilsonae0ca652014-10-23 12:30:12 -040086
87 // key_name doesn't actually come from the extension (or at least isn't documented there) so
88 // we need to add it manually.
Jon Perritt4149d7c2014-10-23 21:23:46 -050089 serverMap := res["server"].(map[string]interface{})
Ash Wilsonae0ca652014-10-23 12:30:12 -040090 serverMap["key_name"] = opts.KeyPair
91
Jon Perritt8dd49db2014-10-24 12:39:07 -050092 var bd bootfromvolume.BlockDevice
93 if opts.BlockDevice != bd {
94 serverMap["block_device_mapping_v2"] = []bootfromvolume.BlockDevice{opts.BlockDevice}
95 }
Jon Perrittd9a4bf72014-10-23 23:44:04 -050096
Jon Perritt4149d7c2014-10-23 21:23:46 -050097 return res, nil
Ash Wilsonae0ca652014-10-23 12:30:12 -040098}
Ash Wilsond7814a32014-10-23 12:49:25 -040099
100// RebuildOpts represents all of the configuration options used in a server rebuild operation that
101// are supported by Rackspace.
102type RebuildOpts struct {
103 // Required. The ID of the image you want your server to be provisioned on
104 ImageID string
105
106 // Name to set the server to
107 Name string
108
109 // Required. The server's admin password
110 AdminPass string
111
112 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
113 AccessIPv4 string
114
115 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
116 AccessIPv6 string
117
118 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
119 Metadata map[string]string
120
121 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
122 // The maximum size of the file is 255 bytes (decoded).
123 Personality []byte
124
125 // Rackspace-specific stuff begins here.
126
127 // DiskConfig [optional] controls how the created server's disk is partitioned. See the "diskconfig"
128 // extension in OpenStack compute v2.
129 DiskConfig diskconfig.DiskConfig
130}
131
132// ToServerRebuildMap constructs a request body using all of the OpenStack extensions that are
133// active on Rackspace.
134func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
135 base := os.RebuildOpts{
136 ImageID: opts.ImageID,
137 Name: opts.Name,
138 AdminPass: opts.AdminPass,
139 AccessIPv4: opts.AccessIPv4,
140 AccessIPv6: opts.AccessIPv6,
141 Metadata: opts.Metadata,
142 Personality: opts.Personality,
143 }
144
145 drive := diskconfig.RebuildOptsExt{
146 RebuildOptsBuilder: base,
147 DiskConfig: opts.DiskConfig,
148 }
149
150 return drive.ToServerRebuildMap()
151}