blob: 1ebb8971d26fa2a7aef1c31f2c66aef26cf776b4 [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 Pike92e10b52015-04-10 15:16:57 -070039 // Personality [optional] includes files to inject into the server at launch.
40 // Create will base64-encode file contents for you.
41 Personality os.Personality
Ash Wilsonae0ca652014-10-23 12:30:12 -040042
43 // ConfigDrive [optional] enables metadata injection through a configuration drive.
44 ConfigDrive bool
45
Jon Perrittf3b2e142014-11-04 16:00:19 -060046 // AdminPass [optional] sets the root user password. If not set, a randomly-generated
47 // password will be created and returned in the response.
48 AdminPass string
49
Ash Wilsonae0ca652014-10-23 12:30:12 -040050 // Rackspace-specific extensions begin here.
51
52 // KeyPair [optional] specifies the name of the SSH KeyPair to be injected into the newly launched
53 // server. See the "keypairs" extension in OpenStack compute v2.
54 KeyPair string
55
56 // DiskConfig [optional] controls how the created server's disk is partitioned. See the "diskconfig"
57 // extension in OpenStack compute v2.
58 DiskConfig diskconfig.DiskConfig
Jon Perrittd9a4bf72014-10-23 23:44:04 -050059
60 // BlockDevice [optional] will create the server from a volume, which is created from an image,
61 // a snapshot, or an another volume.
Jon Perritt01686cd2014-10-24 14:10:16 -050062 BlockDevice []bootfromvolume.BlockDevice
Ash Wilsonae0ca652014-10-23 12:30:12 -040063}
64
65// ToServerCreateMap constructs a request body using all of the OpenStack extensions that are
66// active on Rackspace.
Jon Perritt4149d7c2014-10-23 21:23:46 -050067func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Ash Wilsonae0ca652014-10-23 12:30:12 -040068 base := os.CreateOpts{
69 Name: opts.Name,
70 ImageRef: opts.ImageRef,
71 FlavorRef: opts.FlavorRef,
72 SecurityGroups: opts.SecurityGroups,
73 UserData: opts.UserData,
74 AvailabilityZone: opts.AvailabilityZone,
75 Networks: opts.Networks,
76 Metadata: opts.Metadata,
77 Personality: opts.Personality,
78 ConfigDrive: opts.ConfigDrive,
Jon Perrittf3b2e142014-11-04 16:00:19 -060079 AdminPass: opts.AdminPass,
Ash Wilsonae0ca652014-10-23 12:30:12 -040080 }
81
82 drive := diskconfig.CreateOptsExt{
83 CreateOptsBuilder: base,
84 DiskConfig: opts.DiskConfig,
85 }
86
Jon Perritt4149d7c2014-10-23 21:23:46 -050087 res, err := drive.ToServerCreateMap()
88 if err != nil {
89 return nil, err
90 }
Ash Wilsonae0ca652014-10-23 12:30:12 -040091
Jon Perritt01686cd2014-10-24 14:10:16 -050092 if len(opts.BlockDevice) != 0 {
93 bfv := bootfromvolume.CreateOptsExt{
94 CreateOptsBuilder: drive,
95 BlockDevice: opts.BlockDevice,
96 }
97
98 res, err = bfv.ToServerCreateMap()
99 if err != nil {
100 return nil, err
101 }
Jon Perritt8dd49db2014-10-24 12:39:07 -0500102 }
Jon Perrittd9a4bf72014-10-23 23:44:04 -0500103
Jon Perrittaafafd52014-10-24 14:23:53 -0500104 // key_name doesn't actually come from the extension (or at least isn't documented there) so
105 // we need to add it manually.
106 serverMap := res["server"].(map[string]interface{})
107 serverMap["key_name"] = opts.KeyPair
108
Jon Perritt4149d7c2014-10-23 21:23:46 -0500109 return res, nil
Ash Wilsonae0ca652014-10-23 12:30:12 -0400110}
Ash Wilsond7814a32014-10-23 12:49:25 -0400111
112// RebuildOpts represents all of the configuration options used in a server rebuild operation that
113// are supported by Rackspace.
114type RebuildOpts struct {
115 // Required. The ID of the image you want your server to be provisioned on
116 ImageID string
117
118 // Name to set the server to
119 Name string
120
121 // Required. The server's admin password
122 AdminPass string
123
124 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
125 AccessIPv4 string
126
127 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
128 AccessIPv6 string
129
130 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
131 Metadata map[string]string
132
Kevin Pike92e10b52015-04-10 15:16:57 -0700133 // Personality [optional] includes files to inject into the server at launch.
134 // Rebuild will base64-encode file contents for you.
135 Personality os.Personality
Ash Wilsond7814a32014-10-23 12:49:25 -0400136
137 // Rackspace-specific stuff begins here.
138
139 // DiskConfig [optional] controls how the created server's disk is partitioned. See the "diskconfig"
140 // extension in OpenStack compute v2.
141 DiskConfig diskconfig.DiskConfig
142}
143
144// ToServerRebuildMap constructs a request body using all of the OpenStack extensions that are
145// active on Rackspace.
146func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
147 base := os.RebuildOpts{
148 ImageID: opts.ImageID,
149 Name: opts.Name,
150 AdminPass: opts.AdminPass,
151 AccessIPv4: opts.AccessIPv4,
152 AccessIPv6: opts.AccessIPv6,
153 Metadata: opts.Metadata,
154 Personality: opts.Personality,
155 }
156
157 drive := diskconfig.RebuildOptsExt{
158 RebuildOptsBuilder: base,
159 DiskConfig: opts.DiskConfig,
160 }
161
162 return drive.ToServerRebuildMap()
163}