blob: 4ba7619e75b7db34fbc98bc75b6a36d3a5eae4ee [file] [log] [blame]
Ash Wilsonae0ca652014-10-23 12:30:12 -04001package servers
2
3import (
4 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/diskconfig"
5 os "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
6)
7
8// CreateOpts specifies all of the options that Rackspace accepts in its Create request, including
9// the union of all extensions that Rackspace supports.
10type CreateOpts struct {
11 // Name [required] is the name to assign to the newly launched server.
12 Name string
13
14 // ImageRef [required] is the ID or full URL to the image that contains the server's OS and initial state.
15 // Optional if using the boot-from-volume extension.
16 ImageRef string
17
18 // FlavorRef [required] is the ID or full URL to the flavor that describes the server's specs.
19 FlavorRef string
20
21 // SecurityGroups [optional] lists the names of the security groups to which this server should belong.
22 SecurityGroups []string
23
24 // UserData [optional] contains configuration information or scripts to use upon launch.
25 // Create will base64-encode it for you.
26 UserData []byte
27
28 // AvailabilityZone [optional] in which to launch the server.
29 AvailabilityZone string
30
31 // Networks [optional] dictates how this server will be attached to available networks.
32 // By default, the server will be attached to all isolated networks for the tenant.
33 Networks []os.Network
34
35 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
36 Metadata map[string]string
37
38 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
39 // The maximum size of the file is 255 bytes (decoded).
40 Personality []byte
41
42 // ConfigDrive [optional] enables metadata injection through a configuration drive.
43 ConfigDrive bool
44
45 // Rackspace-specific extensions begin here.
46
47 // KeyPair [optional] specifies the name of the SSH KeyPair to be injected into the newly launched
48 // server. See the "keypairs" extension in OpenStack compute v2.
49 KeyPair string
50
51 // DiskConfig [optional] controls how the created server's disk is partitioned. See the "diskconfig"
52 // extension in OpenStack compute v2.
53 DiskConfig diskconfig.DiskConfig
54}
55
56// ToServerCreateMap constructs a request body using all of the OpenStack extensions that are
57// active on Rackspace.
Jon Perritt4149d7c2014-10-23 21:23:46 -050058func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Ash Wilsonae0ca652014-10-23 12:30:12 -040059 base := os.CreateOpts{
60 Name: opts.Name,
61 ImageRef: opts.ImageRef,
62 FlavorRef: opts.FlavorRef,
63 SecurityGroups: opts.SecurityGroups,
64 UserData: opts.UserData,
65 AvailabilityZone: opts.AvailabilityZone,
66 Networks: opts.Networks,
67 Metadata: opts.Metadata,
68 Personality: opts.Personality,
69 ConfigDrive: opts.ConfigDrive,
70 }
71
72 drive := diskconfig.CreateOptsExt{
73 CreateOptsBuilder: base,
74 DiskConfig: opts.DiskConfig,
75 }
76
Jon Perritt4149d7c2014-10-23 21:23:46 -050077 res, err := drive.ToServerCreateMap()
78 if err != nil {
79 return nil, err
80 }
Ash Wilsonae0ca652014-10-23 12:30:12 -040081
82 // key_name doesn't actually come from the extension (or at least isn't documented there) so
83 // we need to add it manually.
Jon Perritt4149d7c2014-10-23 21:23:46 -050084 serverMap := res["server"].(map[string]interface{})
Ash Wilsonae0ca652014-10-23 12:30:12 -040085 serverMap["key_name"] = opts.KeyPair
86
Jon Perritt4149d7c2014-10-23 21:23:46 -050087 return res, nil
Ash Wilsonae0ca652014-10-23 12:30:12 -040088}
Ash Wilsond7814a32014-10-23 12:49:25 -040089
90// RebuildOpts represents all of the configuration options used in a server rebuild operation that
91// are supported by Rackspace.
92type RebuildOpts struct {
93 // Required. The ID of the image you want your server to be provisioned on
94 ImageID string
95
96 // Name to set the server to
97 Name string
98
99 // Required. The server's admin password
100 AdminPass string
101
102 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
103 AccessIPv4 string
104
105 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
106 AccessIPv6 string
107
108 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
109 Metadata map[string]string
110
111 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
112 // The maximum size of the file is 255 bytes (decoded).
113 Personality []byte
114
115 // Rackspace-specific stuff begins here.
116
117 // DiskConfig [optional] controls how the created server's disk is partitioned. See the "diskconfig"
118 // extension in OpenStack compute v2.
119 DiskConfig diskconfig.DiskConfig
120}
121
122// ToServerRebuildMap constructs a request body using all of the OpenStack extensions that are
123// active on Rackspace.
124func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
125 base := os.RebuildOpts{
126 ImageID: opts.ImageID,
127 Name: opts.Name,
128 AdminPass: opts.AdminPass,
129 AccessIPv4: opts.AccessIPv4,
130 AccessIPv6: opts.AccessIPv6,
131 Metadata: opts.Metadata,
132 Personality: opts.Personality,
133 }
134
135 drive := diskconfig.RebuildOptsExt{
136 RebuildOptsBuilder: base,
137 DiskConfig: opts.DiskConfig,
138 }
139
140 return drive.ToServerRebuildMap()
141}