blob: 96dc931dbf2779717885f085162aa0d9961a8794 [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
Kevin Pike7bf54c52015-04-09 11:27:29 -070039 // 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 Wilsonae0ca652014-10-23 12:30:12 -040043
44 // ConfigDrive [optional] enables metadata injection through a configuration drive.
45 ConfigDrive bool
46
Jon Perrittf3b2e142014-11-04 16:00:19 -060047 // 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 Wilsonae0ca652014-10-23 12:30:12 -040051 // 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 Perrittd9a4bf72014-10-23 23:44:04 -050060
61 // BlockDevice [optional] will create the server from a volume, which is created from an image,
62 // a snapshot, or an another volume.
Jon Perritt01686cd2014-10-24 14:10:16 -050063 BlockDevice []bootfromvolume.BlockDevice
Ash Wilsonae0ca652014-10-23 12:30:12 -040064}
65
66// ToServerCreateMap constructs a request body using all of the OpenStack extensions that are
67// active on Rackspace.
Jon Perritt4149d7c2014-10-23 21:23:46 -050068func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Ash Wilsonae0ca652014-10-23 12:30:12 -040069 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 Perrittf3b2e142014-11-04 16:00:19 -060080 AdminPass: opts.AdminPass,
Ash Wilsonae0ca652014-10-23 12:30:12 -040081 }
82
83 drive := diskconfig.CreateOptsExt{
84 CreateOptsBuilder: base,
85 DiskConfig: opts.DiskConfig,
86 }
87
Jon Perritt4149d7c2014-10-23 21:23:46 -050088 res, err := drive.ToServerCreateMap()
89 if err != nil {
90 return nil, err
91 }
Ash Wilsonae0ca652014-10-23 12:30:12 -040092
Jon Perritt01686cd2014-10-24 14:10:16 -050093 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 Perritt8dd49db2014-10-24 12:39:07 -0500103 }
Jon Perrittd9a4bf72014-10-23 23:44:04 -0500104
Jon Perrittaafafd52014-10-24 14:23:53 -0500105 // 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 Perritt4149d7c2014-10-23 21:23:46 -0500110 return res, nil
Ash Wilsonae0ca652014-10-23 12:30:12 -0400111}
Ash Wilsond7814a32014-10-23 12:49:25 -0400112
113// RebuildOpts represents all of the configuration options used in a server rebuild operation that
114// are supported by Rackspace.
115type 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 Pike7bf54c52015-04-09 11:27:29 -0700134 // 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 Wilsond7814a32014-10-23 12:49:25 -0400138
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.
148func (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}