blob: a66e9f2561ded6f82c0cb92cdaaa702a3ca8a54e [file] [log] [blame]
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -07001// TODO(sfalvo): Remove Rackspace-specific Server structure fields and refactor them into a provider-specific access method.
2// Be sure to update godocs accordingly.
3
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -07004package gophercloud
5
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -07006import (
Samuel A. Falvo II5c305e12013-07-25 19:19:43 -07007 "fmt"
Samuel A. Falvo IId3617102014-01-20 18:27:42 -08008 "github.com/mitchellh/mapstructure"
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -07009 "github.com/racker/perigee"
Mark Peek6b57c232013-08-24 19:03:26 -070010 "strings"
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -070011)
12
Samuel A. Falvo II1dd740a2013-07-08 15:48:40 -070013// genericServersProvider structures provide the implementation for generic OpenStack-compatible
14// CloudServersProvider interfaces.
15type genericServersProvider struct {
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070016 // endpoint refers to the provider's API endpoint base URL. This will be used to construct
17 // and issue queries.
18 endpoint string
19
20 // Test context (if any) in which to issue requests.
21 context *Context
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -070022
23 // access associates this API provider with a set of credentials,
24 // which may be automatically renewed if they near expiration.
25 access AccessProvider
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070026}
27
Samuel A. Falvo II1dd740a2013-07-08 15:48:40 -070028// See the CloudServersProvider interface for details.
Samuel A. Falvo IIa0a55842013-07-24 13:14:17 -070029func (gcp *genericServersProvider) ListServersLinksOnly() ([]Server, error) {
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -070030 var ss []Server
31
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -070032 err := gcp.context.WithReauth(gcp.access, func() error {
33 url := gcp.endpoint + "/servers"
34 return perigee.Get(url, perigee.Options{
35 CustomClient: gcp.context.httpClient,
36 Results: &struct{ Servers *[]Server }{&ss},
37 MoreHeaders: map[string]string{
38 "X-Auth-Token": gcp.access.AuthToken(),
39 },
40 })
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -070041 })
42 return ss, err
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070043}
44
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070045// See the CloudServersProvider interface for details.
Samuel A. Falvo IIa0a55842013-07-24 13:14:17 -070046func (gcp *genericServersProvider) ListServers() ([]Server, error) {
47 var ss []Server
48
49 err := gcp.context.WithReauth(gcp.access, func() error {
50 url := gcp.endpoint + "/servers/detail"
51 return perigee.Get(url, perigee.Options{
52 CustomClient: gcp.context.httpClient,
53 Results: &struct{ Servers *[]Server }{&ss},
54 MoreHeaders: map[string]string{
55 "X-Auth-Token": gcp.access.AuthToken(),
56 },
57 })
58 })
Samuel A. Falvo IId3617102014-01-20 18:27:42 -080059
60 // Compatibility with v0.0.x -- we "map" our public and private
61 // addresses into a legacy structure field for the benefit of
62 // earlier software.
63
64 if err != nil {
65 return ss, err
66 }
67
68 for _, s := range ss {
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -080069 err = mapstructure.Decode(s.RawAddresses, &s.Addresses)
Samuel A. Falvo IId3617102014-01-20 18:27:42 -080070 if err != nil {
71 return ss, err
72 }
73 }
74
Samuel A. Falvo IIa0a55842013-07-24 13:14:17 -070075 return ss, err
76}
77
78// See the CloudServersProvider interface for details.
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070079func (gsp *genericServersProvider) ServerById(id string) (*Server, error) {
80 var s *Server
81
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -070082 err := gsp.context.WithReauth(gsp.access, func() error {
83 url := gsp.endpoint + "/servers/" + id
84 return perigee.Get(url, perigee.Options{
85 Results: &struct{ Server **Server }{&s},
86 MoreHeaders: map[string]string{
87 "X-Auth-Token": gsp.access.AuthToken(),
88 },
89 })
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070090 })
Samuel A. Falvo IId3617102014-01-20 18:27:42 -080091
92 // Compatibility with v0.0.x -- we "map" our public and private
93 // addresses into a legacy structure field for the benefit of
94 // earlier software.
95
96 if err != nil {
97 return s, err
98 }
99
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800100 err = mapstructure.Decode(s.RawAddresses, &s.Addresses)
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800101
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -0700102 return s, err
103}
104
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700105// See the CloudServersProvider interface for details.
106func (gsp *genericServersProvider) CreateServer(ns NewServer) (*NewServer, error) {
107 var s *NewServer
108
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -0700109 err := gsp.context.WithReauth(gsp.access, func() error {
110 ep := gsp.endpoint + "/servers"
111 return perigee.Post(ep, perigee.Options{
112 ReqBody: &struct {
113 Server *NewServer `json:"server"`
114 }{&ns},
115 Results: &struct{ Server **NewServer }{&s},
116 MoreHeaders: map[string]string{
117 "X-Auth-Token": gsp.access.AuthToken(),
118 },
119 OkCodes: []int{202},
120 })
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700121 })
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -0700122
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700123 return s, err
124}
125
Samuel A. Falvo II286e4de2013-07-12 11:33:31 -0700126// See the CloudServersProvider interface for details.
127func (gsp *genericServersProvider) DeleteServerById(id string) error {
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -0700128 err := gsp.context.WithReauth(gsp.access, func() error {
129 url := gsp.endpoint + "/servers/" + id
130 return perigee.Delete(url, perigee.Options{
131 MoreHeaders: map[string]string{
132 "X-Auth-Token": gsp.access.AuthToken(),
133 },
134 OkCodes: []int{204},
135 })
Samuel A. Falvo II286e4de2013-07-12 11:33:31 -0700136 })
137 return err
138}
139
Samuel A. Falvo II5c305e12013-07-25 19:19:43 -0700140// See the CloudServersProvider interface for details.
141func (gsp *genericServersProvider) SetAdminPassword(id, pw string) error {
142 err := gsp.context.WithReauth(gsp.access, func() error {
143 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
144 return perigee.Post(url, perigee.Options{
145 ReqBody: &struct {
146 ChangePassword struct {
147 AdminPass string `json:"adminPass"`
148 } `json:"changePassword"`
149 }{
150 struct {
151 AdminPass string `json:"adminPass"`
152 }{pw},
153 },
154 OkCodes: []int{202},
155 MoreHeaders: map[string]string{
156 "X-Auth-Token": gsp.access.AuthToken(),
157 },
158 })
159 })
160 return err
161}
162
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700163// See the CloudServersProvider interface for details.
164func (gsp *genericServersProvider) ResizeServer(id, newName, newFlavor, newDiskConfig string) error {
165 err := gsp.context.WithReauth(gsp.access, func() error {
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700166 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
167 rr := ResizeRequest{
168 Name: newName,
169 FlavorRef: newFlavor,
170 DiskConfig: newDiskConfig,
171 }
172 return perigee.Post(url, perigee.Options{
173 ReqBody: &struct {
174 Resize ResizeRequest `json:"resize"`
175 }{rr},
176 OkCodes: []int{202},
177 MoreHeaders: map[string]string{
178 "X-Auth-Token": gsp.access.AuthToken(),
179 },
180 })
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700181 })
182 return err
183}
184
185// See the CloudServersProvider interface for details.
186func (gsp *genericServersProvider) RevertResize(id string) error {
187 err := gsp.context.WithReauth(gsp.access, func() error {
188 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
189 return perigee.Post(url, perigee.Options{
190 ReqBody: &struct {
191 RevertResize *int `json:"revertResize"`
192 }{nil},
193 OkCodes: []int{202},
194 MoreHeaders: map[string]string{
195 "X-Auth-Token": gsp.access.AuthToken(),
196 },
197 })
198 })
199 return err
200}
201
202// See the CloudServersProvider interface for details.
203func (gsp *genericServersProvider) ConfirmResize(id string) error {
204 err := gsp.context.WithReauth(gsp.access, func() error {
205 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
206 return perigee.Post(url, perigee.Options{
207 ReqBody: &struct {
208 ConfirmResize *int `json:"confirmResize"`
209 }{nil},
210 OkCodes: []int{204},
211 MoreHeaders: map[string]string{
212 "X-Auth-Token": gsp.access.AuthToken(),
213 },
214 })
215 })
216 return err
217}
218
Samuel A. Falvo IIadbecf92013-07-30 13:13:59 -0700219// See the CloudServersProvider interface for details
220func (gsp *genericServersProvider) RebootServer(id string, hard bool) error {
221 return gsp.context.WithReauth(gsp.access, func() error {
222 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
223 types := map[bool]string{false: "SOFT", true: "HARD"}
224 return perigee.Post(url, perigee.Options{
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700225 ReqBody: &struct {
Samuel A. Falvo IIadbecf92013-07-30 13:13:59 -0700226 Reboot struct {
227 Type string `json:"type"`
228 } `json:"reboot"`
229 }{
230 struct {
231 Type string `json:"type"`
232 }{types[hard]},
233 },
234 OkCodes: []int{202},
235 MoreHeaders: map[string]string{
236 "X-Auth-Token": gsp.access.AuthToken(),
237 },
238 })
239 })
240}
241
Samuel A. Falvo II15da6ab2013-07-30 14:02:11 -0700242// See the CloudServersProvider interface for details
243func (gsp *genericServersProvider) RescueServer(id string) (string, error) {
244 var pw *string
245
246 err := gsp.context.WithReauth(gsp.access, func() error {
247 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
248 return perigee.Post(url, perigee.Options{
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700249 ReqBody: &struct {
Samuel A. Falvo II15da6ab2013-07-30 14:02:11 -0700250 Rescue string `json:"rescue"`
251 }{"none"},
252 MoreHeaders: map[string]string{
253 "X-Auth-Token": gsp.access.AuthToken(),
254 },
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700255 Results: &struct {
Samuel A. Falvo II15da6ab2013-07-30 14:02:11 -0700256 AdminPass **string `json:"adminPass"`
257 }{&pw},
258 })
259 })
260 return *pw, err
261}
262
263// See the CloudServersProvider interface for details
264func (gsp *genericServersProvider) UnrescueServer(id string) error {
265 return gsp.context.WithReauth(gsp.access, func() error {
266 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
267 return perigee.Post(url, perigee.Options{
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700268 ReqBody: &struct {
Samuel A. Falvo II15da6ab2013-07-30 14:02:11 -0700269 Unrescue *int `json:"unrescue"`
270 }{nil},
271 MoreHeaders: map[string]string{
272 "X-Auth-Token": gsp.access.AuthToken(),
273 },
274 OkCodes: []int{202},
275 })
276 })
277}
278
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700279// See the CloudServersProvider interface for details
280func (gsp *genericServersProvider) UpdateServer(id string, changes NewServerSettings) (*Server, error) {
281 var svr *Server
282 err := gsp.context.WithReauth(gsp.access, func() error {
283 url := fmt.Sprintf("%s/servers/%s", gsp.endpoint, id)
284 return perigee.Put(url, perigee.Options{
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700285 ReqBody: &struct {
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700286 Server NewServerSettings `json:"server"`
287 }{changes},
288 MoreHeaders: map[string]string{
289 "X-Auth-Token": gsp.access.AuthToken(),
290 },
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700291 Results: &struct {
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700292 Server **Server `json:"server"`
293 }{&svr},
294 })
295 })
296 return svr, err
297}
298
Samuel A. Falvo II414c15c2013-08-01 15:16:46 -0700299// See the CloudServersProvider interface for details.
Samuel A. Falvo IIf3391602013-08-14 14:53:32 -0700300func (gsp *genericServersProvider) RebuildServer(id string, ns NewServer) (*Server, error) {
Samuel A. Falvo II414c15c2013-08-01 15:16:46 -0700301 var s *Server
302
303 err := gsp.context.WithReauth(gsp.access, func() error {
304 ep := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
305 return perigee.Post(ep, perigee.Options{
306 ReqBody: &struct {
307 Rebuild *NewServer `json:"rebuild"`
308 }{&ns},
309 Results: &struct{ Server **Server }{&s},
310 MoreHeaders: map[string]string{
311 "X-Auth-Token": gsp.access.AuthToken(),
312 },
313 OkCodes: []int{202},
314 })
315 })
316
317 return s, err
318}
319
Samuel A. Falvo IIe21808f2013-08-14 14:48:09 -0700320// See the CloudServersProvider interface for details.
321func (gsp *genericServersProvider) ListAddresses(id string) (AddressSet, error) {
322 var pas *AddressSet
323 var statusCode int
324
325 err := gsp.context.WithReauth(gsp.access, func() error {
326 ep := fmt.Sprintf("%s/servers/%s/ips", gsp.endpoint, id)
327 return perigee.Get(ep, perigee.Options{
328 Results: &struct{ Addresses **AddressSet }{&pas},
329 MoreHeaders: map[string]string{
330 "X-Auth-Token": gsp.access.AuthToken(),
331 },
Samuel A. Falvo IIf3391602013-08-14 14:53:32 -0700332 OkCodes: []int{200, 203},
Samuel A. Falvo IIe21808f2013-08-14 14:48:09 -0700333 StatusCode: &statusCode,
334 })
335 })
336
337 if err != nil {
338 if statusCode == 203 {
339 err = WarnUnauthoritative
340 }
341 }
342
343 return *pas, err
344}
345
Mark Peek6b57c232013-08-24 19:03:26 -0700346// See the CloudServersProvider interface for details.
Jon Perritt0c1629d2013-12-06 19:51:36 -0600347func (gsp *genericServersProvider) ListAddressesByNetwork(id, networkLabel string) (NetworkAddress, error) {
Jon Perrittb1ead742013-10-29 16:03:40 -0500348 pas := make(NetworkAddress)
Jon Perritt499dce12013-10-29 15:41:14 -0500349 var statusCode int
350
351 err := gsp.context.WithReauth(gsp.access, func() error {
352 ep := fmt.Sprintf("%s/servers/%s/ips/%s", gsp.endpoint, id, networkLabel)
353 return perigee.Get(ep, perigee.Options{
354 Results: &pas,
355 MoreHeaders: map[string]string{
356 "X-Auth-Token": gsp.access.AuthToken(),
357 },
Jon Perritt0c1629d2013-12-06 19:51:36 -0600358 OkCodes: []int{200, 203},
Jon Perritt499dce12013-10-29 15:41:14 -0500359 StatusCode: &statusCode,
360 })
361 })
362
363 if err != nil {
364 if statusCode == 203 {
365 err = WarnUnauthoritative
366 }
367 }
368
369 return pas, err
370}
371
372// See the CloudServersProvider interface for details.
Mark Peek6b57c232013-08-24 19:03:26 -0700373func (gsp *genericServersProvider) CreateImage(id string, ci CreateImage) (string, error) {
374 response, err := gsp.context.ResponseWithReauth(gsp.access, func() (*perigee.Response, error) {
375 ep := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
376 return perigee.Request("POST", ep, perigee.Options{
377 ReqBody: &struct {
378 CreateImage *CreateImage `json:"createImage"`
379 }{&ci},
380 MoreHeaders: map[string]string{
381 "X-Auth-Token": gsp.access.AuthToken(),
382 },
Mark Peek7d3e09d2013-08-27 07:57:18 -0700383 OkCodes: []int{200, 202},
Mark Peek6b57c232013-08-24 19:03:26 -0700384 })
385 })
386
387 if err != nil {
388 return "", err
389 }
390 location, err := response.HttpResponse.Location()
391 if err != nil {
392 return "", err
393 }
394
395 // Return the last element of the location which is the image id
396 locationArr := strings.Split(location.Path, "/")
397 return locationArr[len(locationArr)-1], err
398}
399
Samuel A. Falvo IIf52bdf82014-02-01 16:26:21 -0800400// See the CloudServersProvider interface for details.
401func (gsp *genericServersProvider) ListSecurityGroups() ([]SecurityGroup, error) {
402 var sgs []SecurityGroup
403
404 err := gsp.context.WithReauth(gsp.access, func() error {
405 ep := fmt.Sprintf("%s/os-security-groups", gsp.endpoint)
406 return perigee.Get(ep, perigee.Options{
407 MoreHeaders: map[string]string{
408 "X-Auth-Token": gsp.access.AuthToken(),
409 },
410 Results: &struct {
411 SecurityGroups *[]SecurityGroup `json:"security_groups"`
412 }{&sgs},
413 })
414 })
415 return sgs, err
416}
417
418// See the CloudServersProvider interface for details.
419func (gsp *genericServersProvider) CreateSecurityGroup(desired SecurityGroup) (*SecurityGroup, error) {
420 var actual *SecurityGroup
421
422 err := gsp.context.WithReauth(gsp.access, func() error {
423 ep := fmt.Sprintf("%s/os-security-groups", gsp.endpoint)
424 return perigee.Post(ep, perigee.Options{
425 ReqBody: struct {
Samuel A. Falvo II7b8ee8a2014-02-25 11:30:52 -0800426 AddSecurityGroup SecurityGroup `json:"security_group"`
Samuel A. Falvo IIf52bdf82014-02-01 16:26:21 -0800427 }{desired},
428 MoreHeaders: map[string]string{
429 "X-Auth-Token": gsp.access.AuthToken(),
430 },
431 Results: &struct {
432 SecurityGroup **SecurityGroup `json:"security_group"`
433 }{&actual},
434 })
435 })
436 return actual, err
437}
438
439// See the CloudServersProvider interface for details.
440func (gsp *genericServersProvider) ListSecurityGroupsByServerId(id string) ([]SecurityGroup, error) {
441 var sgs []SecurityGroup
442
443 err := gsp.context.WithReauth(gsp.access, func() error {
Samuel A. Falvo II7b8ee8a2014-02-25 11:30:52 -0800444 ep := fmt.Sprintf("%s/servers/%s/os-security-groups", gsp.endpoint, id)
Samuel A. Falvo IIf52bdf82014-02-01 16:26:21 -0800445 return perigee.Get(ep, perigee.Options{
446 MoreHeaders: map[string]string{
447 "X-Auth-Token": gsp.access.AuthToken(),
448 },
449 Results: &struct {
450 SecurityGroups *[]SecurityGroup `json:"security_groups"`
451 }{&sgs},
452 })
453 })
454 return sgs, err
455}
456
457// See the CloudServersProvider interface for details.
458func (gsp *genericServersProvider) SecurityGroupById(id int) (*SecurityGroup, error) {
459 var actual *SecurityGroup
460
461 err := gsp.context.WithReauth(gsp.access, func() error {
462 ep := fmt.Sprintf("%s/os-security-groups/%d", gsp.endpoint, id)
463 return perigee.Get(ep, perigee.Options{
464 MoreHeaders: map[string]string{
465 "X-Auth-Token": gsp.access.AuthToken(),
466 },
467 Results: &struct {
468 SecurityGroup **SecurityGroup `json:"security_group"`
469 }{&actual},
470 })
471 })
472 return actual, err
473}
474
475// See the CloudServersProvider interface for details.
476func (gsp *genericServersProvider) DeleteSecurityGroupById(id int) error {
477 err := gsp.context.WithReauth(gsp.access, func() error {
478 ep := fmt.Sprintf("%s/os-security-groups/%d", gsp.endpoint, id)
479 return perigee.Delete(ep, perigee.Options{
480 MoreHeaders: map[string]string{
481 "X-Auth-Token": gsp.access.AuthToken(),
482 },
483 OkCodes: []int{202},
484 })
485 })
486 return err
487}
488
489// SecurityGroup provides a description of a security group, including all its rules.
490type SecurityGroup struct {
491 Description string `json:"description,omitempty"`
492 Id int `json:"id,omitempty"`
493 Name string `json:"name,omitempty"`
494 Rules []SGRule `json:"rules,omitempty"`
495 TenantId string `json:"tenant_id,omitempty"`
496}
497
498// SGRule encapsulates a single rule which applies to a security group.
499// This definition is just a guess, based on the documentation found in another extension here: http://docs.openstack.org/api/openstack-compute/2/content/GET_os-security-group-default-rules-v2_listSecGroupDefaultRules_v2__tenant_id__os-security-group-rules_ext-os-security-group-default-rules.html
500type SGRule struct {
501 FromPort int `json:"from_port,omitempty"`
502 Id int `json:"id,omitempty"`
503 IpProtocol string `json:"ip_protocol,omitempty"`
504 IpRange map[string]interface{} `json:"ip_range,omitempty"`
505 ToPort int `json:"to_port,omitempty"`
506}
507
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700508// RaxBandwidth provides measurement of server bandwidth consumed over a given audit interval.
509type RaxBandwidth struct {
510 AuditPeriodEnd string `json:"audit_period_end"`
511 AuditPeriodStart string `json:"audit_period_start"`
512 BandwidthInbound int64 `json:"bandwidth_inbound"`
513 BandwidthOutbound int64 `json:"bandwidth_outbound"`
514 Interface string `json:"interface"`
515}
516
517// A VersionedAddress denotes either an IPv4 or IPv6 (depending on version indicated)
518// address.
519type VersionedAddress struct {
520 Addr string `json:"addr"`
521 Version int `json:"version"`
522}
523
524// An AddressSet provides a set of public and private IP addresses for a resource.
525// Each address has a version to identify if IPv4 or IPv6.
526type AddressSet struct {
527 Public []VersionedAddress `json:"public"`
528 Private []VersionedAddress `json:"private"`
529}
530
Jon Perritt499dce12013-10-29 15:41:14 -0500531type NetworkAddress map[string][]VersionedAddress
532
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700533// Server records represent (virtual) hardware instances (not configurations) accessible by the user.
534//
535// The AccessIPv4 / AccessIPv6 fields provides IP addresses for the server in the IPv4 or IPv6 format, respectively.
536//
537// Addresses provides addresses for any attached isolated networks.
538// The version field indicates whether the IP address is version 4 or 6.
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800539// Note: only public and private pools appear here.
540// To get the complete set, use the AllAddressPools() method instead.
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700541//
542// Created tells when the server entity was created.
543//
544// The Flavor field includes the flavor ID and flavor links.
545//
546// The compute provisioning algorithm has an anti-affinity property that
547// attempts to spread customer VMs across hosts.
548// Under certain situations,
549// VMs from the same customer might be placed on the same host.
550// The HostId field represents the host your server runs on and
551// can be used to determine this scenario if it is relevant to your application.
552// Note that HostId is unique only per account; it is not globally unique.
Mark Peeka2818af2013-08-24 15:01:12 -0700553//
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700554// Id provides the server's unique identifier.
555// This field must be treated opaquely.
556//
557// Image indicates which image is installed on the server.
558//
559// Links provides one or more means of accessing the server.
560//
561// Metadata provides a small key-value store for application-specific information.
562//
563// Name provides a human-readable name for the server.
564//
565// Progress indicates how far along it is towards being provisioned.
566// 100 represents complete, while 0 represents just beginning.
567//
568// Status provides an indication of what the server's doing at the moment.
569// A server will be in ACTIVE state if it's ready for use.
570//
571// OsDcfDiskConfig indicates the server's boot volume configuration.
572// Valid values are:
573// AUTO
574// ----
575// The server is built with a single partition the size of the target flavor disk.
576// The file system is automatically adjusted to fit the entire partition.
577// This keeps things simple and automated.
578// AUTO is valid only for images and servers with a single partition that use the EXT3 file system.
579// This is the default setting for applicable Rackspace base images.
580//
581// MANUAL
582// ------
583// The server is built using whatever partition scheme and file system is in the source image.
584// If the target flavor disk is larger,
585// the remaining disk space is left unpartitioned.
586// This enables images to have non-EXT3 file systems, multiple partitions, and so on,
587// and enables you to manage the disk configuration.
588//
589// RaxBandwidth provides measures of the server's inbound and outbound bandwidth per interface.
590//
591// OsExtStsPowerState provides an indication of the server's power.
592// This field appears to be a set of flag bits:
593//
594// ... 4 3 2 1 0
595// +--//--+---+---+---+---+
596// | .... | 0 | S | 0 | I |
597// +--//--+---+---+---+---+
598// | |
599// | +--- 0=Instance is down.
600// | 1=Instance is up.
601// |
602// +----------- 0=Server is switched ON.
603// 1=Server is switched OFF.
604// (note reverse logic.)
605//
606// Unused bits should be ignored when read, and written as 0 for future compatibility.
607//
608// OsExtStsTaskState and OsExtStsVmState work together
609// to provide visibility in the provisioning process for the instance.
610// Consult Rackspace documentation at
611// http://docs.rackspace.com/servers/api/v2/cs-devguide/content/ch_extensions.html#ext_status
612// for more details. It's too lengthy to include here.
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700613type Server struct {
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800614 AccessIPv4 string `json:"accessIPv4"`
615 AccessIPv6 string `json:"accessIPv6"`
616 Addresses AddressSet
Mark Peek22efb6c2013-08-26 13:50:22 -0700617 Created string `json:"created"`
618 Flavor FlavorLink `json:"flavor"`
619 HostId string `json:"hostId"`
620 Id string `json:"id"`
621 Image ImageLink `json:"image"`
622 Links []Link `json:"links"`
623 Metadata map[string]string `json:"metadata"`
624 Name string `json:"name"`
625 Progress int `json:"progress"`
626 Status string `json:"status"`
627 TenantId string `json:"tenant_id"`
628 Updated string `json:"updated"`
629 UserId string `json:"user_id"`
630 OsDcfDiskConfig string `json:"OS-DCF:diskConfig"`
631 RaxBandwidth []RaxBandwidth `json:"rax-bandwidth:bandwidth"`
632 OsExtStsPowerState int `json:"OS-EXT-STS:power_state"`
633 OsExtStsTaskState string `json:"OS-EXT-STS:task_state"`
634 OsExtStsVmState string `json:"OS-EXT-STS:vm_state"`
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800635
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800636 RawAddresses map[string]interface{} `json:"addresses"`
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800637}
638
639// AllAddressPools returns a complete set of address pools available on the server.
640// The name of each pool supported keys the map.
641// The value of the map contains the addresses provided in the corresponding pool.
642func (s *Server) AllAddressPools() (map[string][]VersionedAddress, error) {
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800643 pools := make(map[string][]VersionedAddress, 0)
644 for pool, subtree := range s.RawAddresses {
645 addresses := make([]VersionedAddress, 0)
646 err := mapstructure.Decode(subtree, &addresses)
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800647 if err != nil {
648 return nil, err
649 }
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800650 pools[pool] = addresses
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800651 }
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800652 return pools, nil
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700653}
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700654
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700655// NewServerSettings structures record those fields of the Server structure to change
656// when updating a server (see UpdateServer method).
657type NewServerSettings struct {
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700658 Name string `json:"name,omitempty"`
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700659 AccessIPv4 string `json:"accessIPv4,omitempty"`
660 AccessIPv6 string `json:"accessIPv6,omitempty"`
661}
662
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700663// NewServer structures are used for both requests and responses.
664// The fields discussed below are relevent for server-creation purposes.
665//
666// The Name field contains the desired name of the server.
667// Note that (at present) Rackspace permits more than one server with the same name;
668// however, software should not depend on this.
669// Not only will Rackspace support thank you, so will your own devops engineers.
670// A name is required.
671//
672// The ImageRef field contains the ID of the desired software image to place on the server.
673// This ID must be found in the image slice returned by the Images() function.
674// This field is required.
675//
676// The FlavorRef field contains the ID of the server configuration desired for deployment.
677// This ID must be found in the flavor slice returned by the Flavors() function.
678// This field is required.
679//
680// For OsDcfDiskConfig, refer to the Image or Server structure documentation.
681// This field defaults to "AUTO" if not explicitly provided.
682//
683// Metadata contains a small key/value association of arbitrary data.
684// Neither Rackspace nor OpenStack places significance on this field in any way.
685// This field defaults to an empty map if not provided.
686//
687// Personality specifies the contents of certain files in the server's filesystem.
688// The files and their contents are mapped through a slice of FileConfig structures.
689// If not provided, all filesystem entities retain their image-specific configuration.
690//
691// Networks specifies an affinity for the server's various networks and interfaces.
692// Networks are identified through UUIDs; see NetworkConfig structure documentation for more details.
693// If not provided, network affinity is determined automatically.
694//
695// The AdminPass field may be used to provide a root- or administrator-password
696// during the server provisioning process.
697// If not provided, a random password will be automatically generated and returned in this field.
698//
699// The following fields are intended to be used to communicate certain results about the server being provisioned.
700// When attempting to create a new server, these fields MUST not be provided.
701// They'll be filled in by the response received from the Rackspace APIs.
702//
703// The Id field contains the server's unique identifier.
704// The identifier's scope is best assumed to be bound by the user's account, unless other arrangements have been made with Rackspace.
705//
Kgespadaab69ab22014-01-22 11:43:17 -0800706// The SecurityGroup field allows the user to specify a security group at launch.
707//
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700708// Any Links provided are used to refer to the server specifically by URL.
709// These links are useful for making additional REST calls not explicitly supported by Gorax.
710type NewServer struct {
Kgespadaab69ab22014-01-22 11:43:17 -0800711 Name string `json:"name,omitempty"`
712 ImageRef string `json:"imageRef,omitempty"`
713 FlavorRef string `json:"flavorRef,omitempty"`
714 Metadata map[string]string `json:"metadata,omitempty"`
715 Personality []FileConfig `json:"personality,omitempty"`
716 Networks []NetworkConfig `json:"networks,omitempty"`
717 AdminPass string `json:"adminPass,omitempty"`
718 KeyPairName string `json:"key_name,omitempty"`
719 Id string `json:"id,omitempty"`
720 Links []Link `json:"links,omitempty"`
721 OsDcfDiskConfig string `json:"OS-DCF:diskConfig,omitempty"`
722 SecurityGroup []map[string]interface{} `json:"security_groups,omitempty"`
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700723}
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700724
725// ResizeRequest structures are used internally to encode to JSON the parameters required to resize a server instance.
726// Client applications will not use this structure (no API accepts an instance of this structure).
727// See the Region method ResizeServer() for more details on how to resize a server.
728type ResizeRequest struct {
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700729 Name string `json:"name,omitempty"`
730 FlavorRef string `json:"flavorRef"`
731 DiskConfig string `json:"OS-DCF:diskConfig,omitempty"`
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700732}
Mark Peek6b57c232013-08-24 19:03:26 -0700733
734type CreateImage struct {
735 Name string `json:"name"`
736 Metadata map[string]string `json:"metadata,omitempty"`
737}