blob: b83a893774d996936e634a5d2cbebcbb9fdc2f2d [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.
58func (opts CreateOpts) ToServerCreateMap() map[string]interface{} {
59 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
77 result := drive.ToServerCreateMap()
78
79 // key_name doesn't actually come from the extension (or at least isn't documented there) so
80 // we need to add it manually.
81 serverMap := result["server"].(map[string]interface{})
82 serverMap["key_name"] = opts.KeyPair
83
84 return result
85}
Ash Wilsond7814a32014-10-23 12:49:25 -040086
87// RebuildOpts represents all of the configuration options used in a server rebuild operation that
88// are supported by Rackspace.
89type RebuildOpts struct {
90 // Required. The ID of the image you want your server to be provisioned on
91 ImageID string
92
93 // Name to set the server to
94 Name string
95
96 // Required. The server's admin password
97 AdminPass string
98
99 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
100 AccessIPv4 string
101
102 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
103 AccessIPv6 string
104
105 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
106 Metadata map[string]string
107
108 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
109 // The maximum size of the file is 255 bytes (decoded).
110 Personality []byte
111
112 // Rackspace-specific stuff begins here.
113
114 // DiskConfig [optional] controls how the created server's disk is partitioned. See the "diskconfig"
115 // extension in OpenStack compute v2.
116 DiskConfig diskconfig.DiskConfig
117}
118
119// ToServerRebuildMap constructs a request body using all of the OpenStack extensions that are
120// active on Rackspace.
121func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
122 base := os.RebuildOpts{
123 ImageID: opts.ImageID,
124 Name: opts.Name,
125 AdminPass: opts.AdminPass,
126 AccessIPv4: opts.AccessIPv4,
127 AccessIPv6: opts.AccessIPv6,
128 Metadata: opts.Metadata,
129 Personality: opts.Personality,
130 }
131
132 drive := diskconfig.RebuildOptsExt{
133 RebuildOptsBuilder: base,
134 DiskConfig: opts.DiskConfig,
135 }
136
137 return drive.ToServerRebuildMap()
138}