blob: 9afb396a8a97379fa28e7e4018b8f7a23bd38a0e [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
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800489// See the CloudServersProvider interface for details.
490func (gsp *genericServersProvider) ListDefaultSGRules() ([]SGRule, error) {
491 var sgrs []SGRule
492 err := gsp.context.WithReauth(gsp.access, func() error {
493 ep := fmt.Sprintf("%s/os-security-group-rules", gsp.endpoint)
494 return perigee.Get(ep, perigee.Options{
495 MoreHeaders: map[string]string{
496 "X-Auth-Token": gsp.access.AuthToken(),
497 },
498 Results: &struct{Security_group_default_rules *[]SGRule}{&sgrs},
499 })
500 })
501 return sgrs, err
502}
503
504// See the CloudServersProvider interface for details.
505func (gsp *genericServersProvider) CreateDefaultSGRule(r SGRule) error {
506 var sgr *SGRule
507 err := gsp.context.WithReauth(gsp.access, func() error {
508 ep := fmt.Sprintf("%s/os-security-group-rules", gsp.endpoint)
509 return perigee.Post(ep, perigee.Options{
510 MoreHeaders: map[string]string{
511 "X-Auth-Token": gsp.access.AuthToken(),
512 },
513 Results: &struct{Security_group_default_rule **SGRule}{&sgr},
514 ReqBody: struct{Security_group_default_rule SGRule `json:"security_group_default_rule"`}{r},
515 })
516 })
517 return sgr, err
518}
519
520// See the CloudServersProvider interface for details.
521func (gsp *genericServersProvider) GetSGRule(id string) (*SGRule, error) {
522 var sgr *SGRule
523 err := gsp.context.WithReauth(gsp.access, func() error {
524 ep := fmt.Sprintf("%s/os-security-group-rules/%s", gsp.endpoint, id)
525 return perigee.Get(ep, perigee.Options{
526 MoreHeaders: map[string]string{
527 "X-Auth-Token": gsp.access.AuthToken(),
528 },
Samuel A. Falvo IIf30d51e2014-02-25 12:55:22 -0800529 Results: &struct{Security_group_default_rule **SGRule}{&sgr},
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800530 })
531 })
532 return sgr, err
533}
534
Samuel A. Falvo IIf52bdf82014-02-01 16:26:21 -0800535// SecurityGroup provides a description of a security group, including all its rules.
536type SecurityGroup struct {
537 Description string `json:"description,omitempty"`
538 Id int `json:"id,omitempty"`
539 Name string `json:"name,omitempty"`
540 Rules []SGRule `json:"rules,omitempty"`
541 TenantId string `json:"tenant_id,omitempty"`
542}
543
544// SGRule encapsulates a single rule which applies to a security group.
545// 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
546type SGRule struct {
547 FromPort int `json:"from_port,omitempty"`
548 Id int `json:"id,omitempty"`
549 IpProtocol string `json:"ip_protocol,omitempty"`
550 IpRange map[string]interface{} `json:"ip_range,omitempty"`
551 ToPort int `json:"to_port,omitempty"`
552}
553
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700554// RaxBandwidth provides measurement of server bandwidth consumed over a given audit interval.
555type RaxBandwidth struct {
556 AuditPeriodEnd string `json:"audit_period_end"`
557 AuditPeriodStart string `json:"audit_period_start"`
558 BandwidthInbound int64 `json:"bandwidth_inbound"`
559 BandwidthOutbound int64 `json:"bandwidth_outbound"`
560 Interface string `json:"interface"`
561}
562
563// A VersionedAddress denotes either an IPv4 or IPv6 (depending on version indicated)
564// address.
565type VersionedAddress struct {
566 Addr string `json:"addr"`
567 Version int `json:"version"`
568}
569
570// An AddressSet provides a set of public and private IP addresses for a resource.
571// Each address has a version to identify if IPv4 or IPv6.
572type AddressSet struct {
573 Public []VersionedAddress `json:"public"`
574 Private []VersionedAddress `json:"private"`
575}
576
Jon Perritt499dce12013-10-29 15:41:14 -0500577type NetworkAddress map[string][]VersionedAddress
578
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700579// Server records represent (virtual) hardware instances (not configurations) accessible by the user.
580//
581// The AccessIPv4 / AccessIPv6 fields provides IP addresses for the server in the IPv4 or IPv6 format, respectively.
582//
583// Addresses provides addresses for any attached isolated networks.
584// The version field indicates whether the IP address is version 4 or 6.
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800585// Note: only public and private pools appear here.
586// To get the complete set, use the AllAddressPools() method instead.
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700587//
588// Created tells when the server entity was created.
589//
590// The Flavor field includes the flavor ID and flavor links.
591//
592// The compute provisioning algorithm has an anti-affinity property that
593// attempts to spread customer VMs across hosts.
594// Under certain situations,
595// VMs from the same customer might be placed on the same host.
596// The HostId field represents the host your server runs on and
597// can be used to determine this scenario if it is relevant to your application.
598// Note that HostId is unique only per account; it is not globally unique.
Mark Peeka2818af2013-08-24 15:01:12 -0700599//
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700600// Id provides the server's unique identifier.
601// This field must be treated opaquely.
602//
603// Image indicates which image is installed on the server.
604//
605// Links provides one or more means of accessing the server.
606//
607// Metadata provides a small key-value store for application-specific information.
608//
609// Name provides a human-readable name for the server.
610//
611// Progress indicates how far along it is towards being provisioned.
612// 100 represents complete, while 0 represents just beginning.
613//
614// Status provides an indication of what the server's doing at the moment.
615// A server will be in ACTIVE state if it's ready for use.
616//
617// OsDcfDiskConfig indicates the server's boot volume configuration.
618// Valid values are:
619// AUTO
620// ----
621// The server is built with a single partition the size of the target flavor disk.
622// The file system is automatically adjusted to fit the entire partition.
623// This keeps things simple and automated.
624// AUTO is valid only for images and servers with a single partition that use the EXT3 file system.
625// This is the default setting for applicable Rackspace base images.
626//
627// MANUAL
628// ------
629// The server is built using whatever partition scheme and file system is in the source image.
630// If the target flavor disk is larger,
631// the remaining disk space is left unpartitioned.
632// This enables images to have non-EXT3 file systems, multiple partitions, and so on,
633// and enables you to manage the disk configuration.
634//
635// RaxBandwidth provides measures of the server's inbound and outbound bandwidth per interface.
636//
637// OsExtStsPowerState provides an indication of the server's power.
638// This field appears to be a set of flag bits:
639//
640// ... 4 3 2 1 0
641// +--//--+---+---+---+---+
642// | .... | 0 | S | 0 | I |
643// +--//--+---+---+---+---+
644// | |
645// | +--- 0=Instance is down.
646// | 1=Instance is up.
647// |
648// +----------- 0=Server is switched ON.
649// 1=Server is switched OFF.
650// (note reverse logic.)
651//
652// Unused bits should be ignored when read, and written as 0 for future compatibility.
653//
654// OsExtStsTaskState and OsExtStsVmState work together
655// to provide visibility in the provisioning process for the instance.
656// Consult Rackspace documentation at
657// http://docs.rackspace.com/servers/api/v2/cs-devguide/content/ch_extensions.html#ext_status
658// for more details. It's too lengthy to include here.
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700659type Server struct {
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800660 AccessIPv4 string `json:"accessIPv4"`
661 AccessIPv6 string `json:"accessIPv6"`
662 Addresses AddressSet
Mark Peek22efb6c2013-08-26 13:50:22 -0700663 Created string `json:"created"`
664 Flavor FlavorLink `json:"flavor"`
665 HostId string `json:"hostId"`
666 Id string `json:"id"`
667 Image ImageLink `json:"image"`
668 Links []Link `json:"links"`
669 Metadata map[string]string `json:"metadata"`
670 Name string `json:"name"`
671 Progress int `json:"progress"`
672 Status string `json:"status"`
673 TenantId string `json:"tenant_id"`
674 Updated string `json:"updated"`
675 UserId string `json:"user_id"`
676 OsDcfDiskConfig string `json:"OS-DCF:diskConfig"`
677 RaxBandwidth []RaxBandwidth `json:"rax-bandwidth:bandwidth"`
678 OsExtStsPowerState int `json:"OS-EXT-STS:power_state"`
679 OsExtStsTaskState string `json:"OS-EXT-STS:task_state"`
680 OsExtStsVmState string `json:"OS-EXT-STS:vm_state"`
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800681
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800682 RawAddresses map[string]interface{} `json:"addresses"`
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800683}
684
685// AllAddressPools returns a complete set of address pools available on the server.
686// The name of each pool supported keys the map.
687// The value of the map contains the addresses provided in the corresponding pool.
688func (s *Server) AllAddressPools() (map[string][]VersionedAddress, error) {
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800689 pools := make(map[string][]VersionedAddress, 0)
690 for pool, subtree := range s.RawAddresses {
691 addresses := make([]VersionedAddress, 0)
692 err := mapstructure.Decode(subtree, &addresses)
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800693 if err != nil {
694 return nil, err
695 }
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800696 pools[pool] = addresses
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800697 }
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800698 return pools, nil
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700699}
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700700
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700701// NewServerSettings structures record those fields of the Server structure to change
702// when updating a server (see UpdateServer method).
703type NewServerSettings struct {
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700704 Name string `json:"name,omitempty"`
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700705 AccessIPv4 string `json:"accessIPv4,omitempty"`
706 AccessIPv6 string `json:"accessIPv6,omitempty"`
707}
708
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700709// NewServer structures are used for both requests and responses.
710// The fields discussed below are relevent for server-creation purposes.
711//
712// The Name field contains the desired name of the server.
713// Note that (at present) Rackspace permits more than one server with the same name;
714// however, software should not depend on this.
715// Not only will Rackspace support thank you, so will your own devops engineers.
716// A name is required.
717//
718// The ImageRef field contains the ID of the desired software image to place on the server.
719// This ID must be found in the image slice returned by the Images() function.
720// This field is required.
721//
722// The FlavorRef field contains the ID of the server configuration desired for deployment.
723// This ID must be found in the flavor slice returned by the Flavors() function.
724// This field is required.
725//
726// For OsDcfDiskConfig, refer to the Image or Server structure documentation.
727// This field defaults to "AUTO" if not explicitly provided.
728//
729// Metadata contains a small key/value association of arbitrary data.
730// Neither Rackspace nor OpenStack places significance on this field in any way.
731// This field defaults to an empty map if not provided.
732//
733// Personality specifies the contents of certain files in the server's filesystem.
734// The files and their contents are mapped through a slice of FileConfig structures.
735// If not provided, all filesystem entities retain their image-specific configuration.
736//
737// Networks specifies an affinity for the server's various networks and interfaces.
738// Networks are identified through UUIDs; see NetworkConfig structure documentation for more details.
739// If not provided, network affinity is determined automatically.
740//
741// The AdminPass field may be used to provide a root- or administrator-password
742// during the server provisioning process.
743// If not provided, a random password will be automatically generated and returned in this field.
744//
745// The following fields are intended to be used to communicate certain results about the server being provisioned.
746// When attempting to create a new server, these fields MUST not be provided.
747// They'll be filled in by the response received from the Rackspace APIs.
748//
749// The Id field contains the server's unique identifier.
750// The identifier's scope is best assumed to be bound by the user's account, unless other arrangements have been made with Rackspace.
751//
Kgespadaab69ab22014-01-22 11:43:17 -0800752// The SecurityGroup field allows the user to specify a security group at launch.
753//
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700754// Any Links provided are used to refer to the server specifically by URL.
755// These links are useful for making additional REST calls not explicitly supported by Gorax.
756type NewServer struct {
Kgespadaab69ab22014-01-22 11:43:17 -0800757 Name string `json:"name,omitempty"`
758 ImageRef string `json:"imageRef,omitempty"`
759 FlavorRef string `json:"flavorRef,omitempty"`
760 Metadata map[string]string `json:"metadata,omitempty"`
761 Personality []FileConfig `json:"personality,omitempty"`
762 Networks []NetworkConfig `json:"networks,omitempty"`
763 AdminPass string `json:"adminPass,omitempty"`
764 KeyPairName string `json:"key_name,omitempty"`
765 Id string `json:"id,omitempty"`
766 Links []Link `json:"links,omitempty"`
767 OsDcfDiskConfig string `json:"OS-DCF:diskConfig,omitempty"`
768 SecurityGroup []map[string]interface{} `json:"security_groups,omitempty"`
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700769}
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700770
771// ResizeRequest structures are used internally to encode to JSON the parameters required to resize a server instance.
772// Client applications will not use this structure (no API accepts an instance of this structure).
773// See the Region method ResizeServer() for more details on how to resize a server.
774type ResizeRequest struct {
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700775 Name string `json:"name,omitempty"`
776 FlavorRef string `json:"flavorRef"`
777 DiskConfig string `json:"OS-DCF:diskConfig,omitempty"`
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700778}
Mark Peek6b57c232013-08-24 19:03:26 -0700779
780type CreateImage struct {
781 Name string `json:"name"`
782 Metadata map[string]string `json:"metadata,omitempty"`
783}