blob: 884b9cb131e79f7c114781c56c158dcfaa217167 [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.
Jon Perritt01686cd2014-10-24 14:10:16 -050058 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
Jon Perritt01686cd2014-10-24 14:10:16 -050087 if len(opts.BlockDevice) != 0 {
88 bfv := bootfromvolume.CreateOptsExt{
89 CreateOptsBuilder: drive,
90 BlockDevice: opts.BlockDevice,
91 }
92
93 res, err = bfv.ToServerCreateMap()
94 if err != nil {
95 return nil, err
96 }
Jon Perritt8dd49db2014-10-24 12:39:07 -050097 }
Jon Perrittd9a4bf72014-10-23 23:44:04 -050098
Jon Perrittaafafd52014-10-24 14:23:53 -050099 // key_name doesn't actually come from the extension (or at least isn't documented there) so
100 // we need to add it manually.
101 serverMap := res["server"].(map[string]interface{})
102 serverMap["key_name"] = opts.KeyPair
103
Jon Perritt4149d7c2014-10-23 21:23:46 -0500104 return res, nil
Ash Wilsonae0ca652014-10-23 12:30:12 -0400105}
Ash Wilsond7814a32014-10-23 12:49:25 -0400106
107// RebuildOpts represents all of the configuration options used in a server rebuild operation that
108// are supported by Rackspace.
109type RebuildOpts struct {
110 // Required. The ID of the image you want your server to be provisioned on
111 ImageID string
112
113 // Name to set the server to
114 Name string
115
116 // Required. The server's admin password
117 AdminPass string
118
119 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
120 AccessIPv4 string
121
122 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
123 AccessIPv6 string
124
125 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
126 Metadata map[string]string
127
128 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
129 // The maximum size of the file is 255 bytes (decoded).
130 Personality []byte
131
132 // Rackspace-specific stuff begins here.
133
134 // DiskConfig [optional] controls how the created server's disk is partitioned. See the "diskconfig"
135 // extension in OpenStack compute v2.
136 DiskConfig diskconfig.DiskConfig
137}
138
139// ToServerRebuildMap constructs a request body using all of the OpenStack extensions that are
140// active on Rackspace.
141func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
142 base := os.RebuildOpts{
143 ImageID: opts.ImageID,
144 Name: opts.Name,
145 AdminPass: opts.AdminPass,
146 AccessIPv4: opts.AccessIPv4,
147 AccessIPv6: opts.AccessIPv6,
148 Metadata: opts.Metadata,
149 Personality: opts.Personality,
150 }
151
152 drive := diskconfig.RebuildOptsExt{
153 RebuildOptsBuilder: base,
154 DiskConfig: opts.DiskConfig,
155 }
156
157 return drive.ToServerRebuildMap()
158}