blob: 357b17441a98a83bce792537c77de3960e7ab4d8 [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 Perrittd9a4bf72014-10-23 23:44:04 -050092 serverMap["block_device_mapping_v2"] = opts.BlockDevice
93
Jon Perritt4149d7c2014-10-23 21:23:46 -050094 return res, nil
Ash Wilsonae0ca652014-10-23 12:30:12 -040095}
Ash Wilsond7814a32014-10-23 12:49:25 -040096
97// RebuildOpts represents all of the configuration options used in a server rebuild operation that
98// are supported by Rackspace.
99type RebuildOpts struct {
100 // Required. The ID of the image you want your server to be provisioned on
101 ImageID string
102
103 // Name to set the server to
104 Name string
105
106 // Required. The server's admin password
107 AdminPass string
108
109 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
110 AccessIPv4 string
111
112 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
113 AccessIPv6 string
114
115 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
116 Metadata map[string]string
117
118 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
119 // The maximum size of the file is 255 bytes (decoded).
120 Personality []byte
121
122 // Rackspace-specific stuff begins here.
123
124 // DiskConfig [optional] controls how the created server's disk is partitioned. See the "diskconfig"
125 // extension in OpenStack compute v2.
126 DiskConfig diskconfig.DiskConfig
127}
128
129// ToServerRebuildMap constructs a request body using all of the OpenStack extensions that are
130// active on Rackspace.
131func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
132 base := os.RebuildOpts{
133 ImageID: opts.ImageID,
134 Name: opts.Name,
135 AdminPass: opts.AdminPass,
136 AccessIPv4: opts.AccessIPv4,
137 AccessIPv6: opts.AccessIPv6,
138 Metadata: opts.Metadata,
139 Personality: opts.Personality,
140 }
141
142 drive := diskconfig.RebuildOptsExt{
143 RebuildOptsBuilder: base,
144 DiskConfig: opts.DiskConfig,
145 }
146
147 return drive.ToServerRebuildMap()
148}