blob: f9424dfc252bfa45e33ab1251d0168053cd98667 [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
Jon Perrittad5f1cb2015-05-20 10:38:13 -060015 // ImageRef [optional; required if ImageName is not provided] is the ID or full
16 // URL to the image that contains the server's OS and initial state.
17 // Also optional if using the boot-from-volume extension.
Ash Wilsonae0ca652014-10-23 12:30:12 -040018 ImageRef string
19
Jon Perrittad5f1cb2015-05-20 10:38:13 -060020 // ImageName [optional; required if ImageRef is not provided] is the name of the
21 // image that contains the server's OS and initial state.
22 // Also optional if using the boot-from-volume extension.
23 ImageName string
24
25 // FlavorRef [optional; required if FlavorName is not provided] is the ID or
26 // full URL to the flavor that describes the server's specs.
Ash Wilsonae0ca652014-10-23 12:30:12 -040027 FlavorRef string
28
Jon Perrittad5f1cb2015-05-20 10:38:13 -060029 // FlavorName [optional; required if FlavorRef is not provided] is the name of
30 // the flavor that describes the server's specs.
31 FlavorName string
32
Ash Wilsonae0ca652014-10-23 12:30:12 -040033 // SecurityGroups [optional] lists the names of the security groups to which this server should belong.
34 SecurityGroups []string
35
36 // UserData [optional] contains configuration information or scripts to use upon launch.
37 // Create will base64-encode it for you.
38 UserData []byte
39
40 // AvailabilityZone [optional] in which to launch the server.
41 AvailabilityZone string
42
43 // Networks [optional] dictates how this server will be attached to available networks.
44 // By default, the server will be attached to all isolated networks for the tenant.
45 Networks []os.Network
46
47 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
48 Metadata map[string]string
49
Kevin Pike92e10b52015-04-10 15:16:57 -070050 // Personality [optional] includes files to inject into the server at launch.
51 // Create will base64-encode file contents for you.
52 Personality os.Personality
Ash Wilsonae0ca652014-10-23 12:30:12 -040053
54 // ConfigDrive [optional] enables metadata injection through a configuration drive.
55 ConfigDrive bool
56
Jon Perrittf3b2e142014-11-04 16:00:19 -060057 // AdminPass [optional] sets the root user password. If not set, a randomly-generated
58 // password will be created and returned in the response.
59 AdminPass string
60
Ash Wilsonae0ca652014-10-23 12:30:12 -040061 // Rackspace-specific extensions begin here.
62
63 // KeyPair [optional] specifies the name of the SSH KeyPair to be injected into the newly launched
64 // server. See the "keypairs" extension in OpenStack compute v2.
65 KeyPair string
66
67 // DiskConfig [optional] controls how the created server's disk is partitioned. See the "diskconfig"
68 // extension in OpenStack compute v2.
69 DiskConfig diskconfig.DiskConfig
Jon Perrittd9a4bf72014-10-23 23:44:04 -050070
71 // BlockDevice [optional] will create the server from a volume, which is created from an image,
jrperrittfab1f3d2015-05-22 11:14:58 -060072 // a snapshot, or another volume.
Jon Perritt01686cd2014-10-24 14:10:16 -050073 BlockDevice []bootfromvolume.BlockDevice
Ash Wilsonae0ca652014-10-23 12:30:12 -040074}
75
76// ToServerCreateMap constructs a request body using all of the OpenStack extensions that are
77// active on Rackspace.
Jon Perritt4149d7c2014-10-23 21:23:46 -050078func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Ash Wilsonae0ca652014-10-23 12:30:12 -040079 base := os.CreateOpts{
80 Name: opts.Name,
81 ImageRef: opts.ImageRef,
Jon Perrittad5f1cb2015-05-20 10:38:13 -060082 ImageName: opts.ImageName,
Ash Wilsonae0ca652014-10-23 12:30:12 -040083 FlavorRef: opts.FlavorRef,
Jon Perrittad5f1cb2015-05-20 10:38:13 -060084 FlavorName: opts.FlavorName,
Ash Wilsonae0ca652014-10-23 12:30:12 -040085 SecurityGroups: opts.SecurityGroups,
86 UserData: opts.UserData,
87 AvailabilityZone: opts.AvailabilityZone,
88 Networks: opts.Networks,
89 Metadata: opts.Metadata,
90 Personality: opts.Personality,
91 ConfigDrive: opts.ConfigDrive,
Jon Perrittf3b2e142014-11-04 16:00:19 -060092 AdminPass: opts.AdminPass,
Ash Wilsonae0ca652014-10-23 12:30:12 -040093 }
94
95 drive := diskconfig.CreateOptsExt{
96 CreateOptsBuilder: base,
97 DiskConfig: opts.DiskConfig,
98 }
99
Jon Perritt4149d7c2014-10-23 21:23:46 -0500100 res, err := drive.ToServerCreateMap()
101 if err != nil {
102 return nil, err
103 }
Ash Wilsonae0ca652014-10-23 12:30:12 -0400104
Jon Perritt01686cd2014-10-24 14:10:16 -0500105 if len(opts.BlockDevice) != 0 {
106 bfv := bootfromvolume.CreateOptsExt{
107 CreateOptsBuilder: drive,
108 BlockDevice: opts.BlockDevice,
109 }
110
111 res, err = bfv.ToServerCreateMap()
112 if err != nil {
113 return nil, err
114 }
Jon Perritt8dd49db2014-10-24 12:39:07 -0500115 }
Jon Perrittd9a4bf72014-10-23 23:44:04 -0500116
Jon Perrittaafafd52014-10-24 14:23:53 -0500117 // key_name doesn't actually come from the extension (or at least isn't documented there) so
118 // we need to add it manually.
119 serverMap := res["server"].(map[string]interface{})
120 serverMap["key_name"] = opts.KeyPair
121
Jon Perritt4149d7c2014-10-23 21:23:46 -0500122 return res, nil
Ash Wilsonae0ca652014-10-23 12:30:12 -0400123}
Ash Wilsond7814a32014-10-23 12:49:25 -0400124
125// RebuildOpts represents all of the configuration options used in a server rebuild operation that
126// are supported by Rackspace.
127type RebuildOpts struct {
128 // Required. The ID of the image you want your server to be provisioned on
129 ImageID string
130
131 // Name to set the server to
132 Name string
133
134 // Required. The server's admin password
135 AdminPass string
136
137 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
138 AccessIPv4 string
139
140 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
141 AccessIPv6 string
142
143 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
144 Metadata map[string]string
145
Kevin Pike92e10b52015-04-10 15:16:57 -0700146 // Personality [optional] includes files to inject into the server at launch.
147 // Rebuild will base64-encode file contents for you.
148 Personality os.Personality
Ash Wilsond7814a32014-10-23 12:49:25 -0400149
150 // Rackspace-specific stuff begins here.
151
152 // DiskConfig [optional] controls how the created server's disk is partitioned. See the "diskconfig"
153 // extension in OpenStack compute v2.
154 DiskConfig diskconfig.DiskConfig
155}
156
157// ToServerRebuildMap constructs a request body using all of the OpenStack extensions that are
158// active on Rackspace.
159func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
160 base := os.RebuildOpts{
161 ImageID: opts.ImageID,
162 Name: opts.Name,
163 AdminPass: opts.AdminPass,
164 AccessIPv4: opts.AccessIPv4,
165 AccessIPv6: opts.AccessIPv6,
166 Metadata: opts.Metadata,
167 Personality: opts.Personality,
168 }
169
170 drive := diskconfig.RebuildOptsExt{
171 RebuildOptsBuilder: base,
172 DiskConfig: opts.DiskConfig,
173 }
174
175 return drive.ToServerRebuildMap()
176}