blob: 425853b8ae5bce91c8a2998a71e8e86055604a68 [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 },
Samuel A. Falvo II40444fb2014-06-30 16:00:17 -070089 OkCodes: []int{200},
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -070090 })
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070091 })
Samuel A. Falvo IId3617102014-01-20 18:27:42 -080092
93 // Compatibility with v0.0.x -- we "map" our public and private
94 // addresses into a legacy structure field for the benefit of
95 // earlier software.
96
97 if err != nil {
98 return s, err
99 }
100
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800101 err = mapstructure.Decode(s.RawAddresses, &s.Addresses)
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800102
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -0700103 return s, err
104}
105
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700106// See the CloudServersProvider interface for details.
107func (gsp *genericServersProvider) CreateServer(ns NewServer) (*NewServer, error) {
108 var s *NewServer
109
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -0700110 err := gsp.context.WithReauth(gsp.access, func() error {
111 ep := gsp.endpoint + "/servers"
112 return perigee.Post(ep, perigee.Options{
113 ReqBody: &struct {
114 Server *NewServer `json:"server"`
115 }{&ns},
116 Results: &struct{ Server **NewServer }{&s},
117 MoreHeaders: map[string]string{
118 "X-Auth-Token": gsp.access.AuthToken(),
119 },
120 OkCodes: []int{202},
121 })
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700122 })
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -0700123
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700124 return s, err
125}
126
Samuel A. Falvo II286e4de2013-07-12 11:33:31 -0700127// See the CloudServersProvider interface for details.
128func (gsp *genericServersProvider) DeleteServerById(id string) error {
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -0700129 err := gsp.context.WithReauth(gsp.access, func() error {
130 url := gsp.endpoint + "/servers/" + id
131 return perigee.Delete(url, perigee.Options{
132 MoreHeaders: map[string]string{
133 "X-Auth-Token": gsp.access.AuthToken(),
134 },
135 OkCodes: []int{204},
136 })
Samuel A. Falvo II286e4de2013-07-12 11:33:31 -0700137 })
138 return err
139}
140
Samuel A. Falvo II5c305e12013-07-25 19:19:43 -0700141// See the CloudServersProvider interface for details.
142func (gsp *genericServersProvider) SetAdminPassword(id, pw string) error {
143 err := gsp.context.WithReauth(gsp.access, func() error {
144 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
145 return perigee.Post(url, perigee.Options{
146 ReqBody: &struct {
147 ChangePassword struct {
148 AdminPass string `json:"adminPass"`
149 } `json:"changePassword"`
150 }{
151 struct {
152 AdminPass string `json:"adminPass"`
153 }{pw},
154 },
155 OkCodes: []int{202},
156 MoreHeaders: map[string]string{
157 "X-Auth-Token": gsp.access.AuthToken(),
158 },
159 })
160 })
161 return err
162}
163
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700164// See the CloudServersProvider interface for details.
165func (gsp *genericServersProvider) ResizeServer(id, newName, newFlavor, newDiskConfig string) error {
166 err := gsp.context.WithReauth(gsp.access, func() error {
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700167 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
168 rr := ResizeRequest{
169 Name: newName,
170 FlavorRef: newFlavor,
171 DiskConfig: newDiskConfig,
172 }
173 return perigee.Post(url, perigee.Options{
174 ReqBody: &struct {
175 Resize ResizeRequest `json:"resize"`
176 }{rr},
177 OkCodes: []int{202},
178 MoreHeaders: map[string]string{
179 "X-Auth-Token": gsp.access.AuthToken(),
180 },
181 })
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700182 })
183 return err
184}
185
186// See the CloudServersProvider interface for details.
187func (gsp *genericServersProvider) RevertResize(id string) error {
188 err := gsp.context.WithReauth(gsp.access, func() error {
189 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
190 return perigee.Post(url, perigee.Options{
191 ReqBody: &struct {
192 RevertResize *int `json:"revertResize"`
193 }{nil},
194 OkCodes: []int{202},
195 MoreHeaders: map[string]string{
196 "X-Auth-Token": gsp.access.AuthToken(),
197 },
198 })
199 })
200 return err
201}
202
203// See the CloudServersProvider interface for details.
204func (gsp *genericServersProvider) ConfirmResize(id string) error {
205 err := gsp.context.WithReauth(gsp.access, func() error {
206 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
207 return perigee.Post(url, perigee.Options{
208 ReqBody: &struct {
209 ConfirmResize *int `json:"confirmResize"`
210 }{nil},
211 OkCodes: []int{204},
212 MoreHeaders: map[string]string{
213 "X-Auth-Token": gsp.access.AuthToken(),
214 },
215 })
216 })
217 return err
218}
219
Samuel A. Falvo IIadbecf92013-07-30 13:13:59 -0700220// See the CloudServersProvider interface for details
221func (gsp *genericServersProvider) RebootServer(id string, hard bool) error {
222 return gsp.context.WithReauth(gsp.access, func() error {
223 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
224 types := map[bool]string{false: "SOFT", true: "HARD"}
225 return perigee.Post(url, perigee.Options{
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700226 ReqBody: &struct {
Samuel A. Falvo IIadbecf92013-07-30 13:13:59 -0700227 Reboot struct {
228 Type string `json:"type"`
229 } `json:"reboot"`
230 }{
231 struct {
232 Type string `json:"type"`
233 }{types[hard]},
234 },
235 OkCodes: []int{202},
236 MoreHeaders: map[string]string{
237 "X-Auth-Token": gsp.access.AuthToken(),
238 },
239 })
240 })
241}
242
Samuel A. Falvo II15da6ab2013-07-30 14:02:11 -0700243// See the CloudServersProvider interface for details
244func (gsp *genericServersProvider) RescueServer(id string) (string, error) {
245 var pw *string
246
247 err := gsp.context.WithReauth(gsp.access, func() error {
248 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
249 return perigee.Post(url, perigee.Options{
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700250 ReqBody: &struct {
Samuel A. Falvo II15da6ab2013-07-30 14:02:11 -0700251 Rescue string `json:"rescue"`
252 }{"none"},
253 MoreHeaders: map[string]string{
254 "X-Auth-Token": gsp.access.AuthToken(),
255 },
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700256 Results: &struct {
Samuel A. Falvo II15da6ab2013-07-30 14:02:11 -0700257 AdminPass **string `json:"adminPass"`
258 }{&pw},
259 })
260 })
261 return *pw, err
262}
263
264// See the CloudServersProvider interface for details
265func (gsp *genericServersProvider) UnrescueServer(id string) error {
266 return gsp.context.WithReauth(gsp.access, func() error {
267 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
268 return perigee.Post(url, perigee.Options{
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700269 ReqBody: &struct {
Samuel A. Falvo II15da6ab2013-07-30 14:02:11 -0700270 Unrescue *int `json:"unrescue"`
271 }{nil},
272 MoreHeaders: map[string]string{
273 "X-Auth-Token": gsp.access.AuthToken(),
274 },
275 OkCodes: []int{202},
276 })
277 })
278}
279
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700280// See the CloudServersProvider interface for details
281func (gsp *genericServersProvider) UpdateServer(id string, changes NewServerSettings) (*Server, error) {
282 var svr *Server
283 err := gsp.context.WithReauth(gsp.access, func() error {
284 url := fmt.Sprintf("%s/servers/%s", gsp.endpoint, id)
285 return perigee.Put(url, perigee.Options{
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700286 ReqBody: &struct {
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700287 Server NewServerSettings `json:"server"`
288 }{changes},
289 MoreHeaders: map[string]string{
290 "X-Auth-Token": gsp.access.AuthToken(),
291 },
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700292 Results: &struct {
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700293 Server **Server `json:"server"`
294 }{&svr},
295 })
296 })
297 return svr, err
298}
299
Samuel A. Falvo II414c15c2013-08-01 15:16:46 -0700300// See the CloudServersProvider interface for details.
Samuel A. Falvo IIf3391602013-08-14 14:53:32 -0700301func (gsp *genericServersProvider) RebuildServer(id string, ns NewServer) (*Server, error) {
Samuel A. Falvo II414c15c2013-08-01 15:16:46 -0700302 var s *Server
303
304 err := gsp.context.WithReauth(gsp.access, func() error {
305 ep := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
306 return perigee.Post(ep, perigee.Options{
307 ReqBody: &struct {
308 Rebuild *NewServer `json:"rebuild"`
309 }{&ns},
310 Results: &struct{ Server **Server }{&s},
311 MoreHeaders: map[string]string{
312 "X-Auth-Token": gsp.access.AuthToken(),
313 },
314 OkCodes: []int{202},
315 })
316 })
317
318 return s, err
319}
320
Samuel A. Falvo IIe21808f2013-08-14 14:48:09 -0700321// See the CloudServersProvider interface for details.
322func (gsp *genericServersProvider) ListAddresses(id string) (AddressSet, error) {
323 var pas *AddressSet
324 var statusCode int
325
326 err := gsp.context.WithReauth(gsp.access, func() error {
327 ep := fmt.Sprintf("%s/servers/%s/ips", gsp.endpoint, id)
328 return perigee.Get(ep, perigee.Options{
329 Results: &struct{ Addresses **AddressSet }{&pas},
330 MoreHeaders: map[string]string{
331 "X-Auth-Token": gsp.access.AuthToken(),
332 },
Samuel A. Falvo IIf3391602013-08-14 14:53:32 -0700333 OkCodes: []int{200, 203},
Samuel A. Falvo IIe21808f2013-08-14 14:48:09 -0700334 StatusCode: &statusCode,
335 })
336 })
337
338 if err != nil {
339 if statusCode == 203 {
340 err = WarnUnauthoritative
341 }
342 }
343
344 return *pas, err
345}
346
Mark Peek6b57c232013-08-24 19:03:26 -0700347// See the CloudServersProvider interface for details.
Jon Perritt0c1629d2013-12-06 19:51:36 -0600348func (gsp *genericServersProvider) ListAddressesByNetwork(id, networkLabel string) (NetworkAddress, error) {
Jon Perrittb1ead742013-10-29 16:03:40 -0500349 pas := make(NetworkAddress)
Jon Perritt499dce12013-10-29 15:41:14 -0500350 var statusCode int
351
352 err := gsp.context.WithReauth(gsp.access, func() error {
353 ep := fmt.Sprintf("%s/servers/%s/ips/%s", gsp.endpoint, id, networkLabel)
354 return perigee.Get(ep, perigee.Options{
355 Results: &pas,
356 MoreHeaders: map[string]string{
357 "X-Auth-Token": gsp.access.AuthToken(),
358 },
Jon Perritt0c1629d2013-12-06 19:51:36 -0600359 OkCodes: []int{200, 203},
Jon Perritt499dce12013-10-29 15:41:14 -0500360 StatusCode: &statusCode,
361 })
362 })
363
364 if err != nil {
365 if statusCode == 203 {
366 err = WarnUnauthoritative
367 }
368 }
369
370 return pas, err
371}
372
373// See the CloudServersProvider interface for details.
Mark Peek6b57c232013-08-24 19:03:26 -0700374func (gsp *genericServersProvider) CreateImage(id string, ci CreateImage) (string, error) {
375 response, err := gsp.context.ResponseWithReauth(gsp.access, func() (*perigee.Response, error) {
376 ep := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
377 return perigee.Request("POST", ep, perigee.Options{
378 ReqBody: &struct {
379 CreateImage *CreateImage `json:"createImage"`
380 }{&ci},
381 MoreHeaders: map[string]string{
382 "X-Auth-Token": gsp.access.AuthToken(),
383 },
Mark Peek7d3e09d2013-08-27 07:57:18 -0700384 OkCodes: []int{200, 202},
Mark Peek6b57c232013-08-24 19:03:26 -0700385 })
386 })
387
388 if err != nil {
389 return "", err
390 }
391 location, err := response.HttpResponse.Location()
392 if err != nil {
393 return "", err
394 }
395
396 // Return the last element of the location which is the image id
397 locationArr := strings.Split(location.Path, "/")
398 return locationArr[len(locationArr)-1], err
399}
400
Samuel A. Falvo IIf52bdf82014-02-01 16:26:21 -0800401// See the CloudServersProvider interface for details.
402func (gsp *genericServersProvider) ListSecurityGroups() ([]SecurityGroup, error) {
403 var sgs []SecurityGroup
404
405 err := gsp.context.WithReauth(gsp.access, func() error {
406 ep := fmt.Sprintf("%s/os-security-groups", gsp.endpoint)
407 return perigee.Get(ep, perigee.Options{
408 MoreHeaders: map[string]string{
409 "X-Auth-Token": gsp.access.AuthToken(),
410 },
411 Results: &struct {
412 SecurityGroups *[]SecurityGroup `json:"security_groups"`
413 }{&sgs},
414 })
415 })
416 return sgs, err
417}
418
419// See the CloudServersProvider interface for details.
420func (gsp *genericServersProvider) CreateSecurityGroup(desired SecurityGroup) (*SecurityGroup, error) {
421 var actual *SecurityGroup
422
423 err := gsp.context.WithReauth(gsp.access, func() error {
424 ep := fmt.Sprintf("%s/os-security-groups", gsp.endpoint)
425 return perigee.Post(ep, perigee.Options{
426 ReqBody: struct {
Samuel A. Falvo II7b8ee8a2014-02-25 11:30:52 -0800427 AddSecurityGroup SecurityGroup `json:"security_group"`
Samuel A. Falvo IIf52bdf82014-02-01 16:26:21 -0800428 }{desired},
429 MoreHeaders: map[string]string{
430 "X-Auth-Token": gsp.access.AuthToken(),
431 },
432 Results: &struct {
433 SecurityGroup **SecurityGroup `json:"security_group"`
434 }{&actual},
435 })
436 })
437 return actual, err
438}
439
440// See the CloudServersProvider interface for details.
441func (gsp *genericServersProvider) ListSecurityGroupsByServerId(id string) ([]SecurityGroup, error) {
442 var sgs []SecurityGroup
443
444 err := gsp.context.WithReauth(gsp.access, func() error {
Samuel A. Falvo II7b8ee8a2014-02-25 11:30:52 -0800445 ep := fmt.Sprintf("%s/servers/%s/os-security-groups", gsp.endpoint, id)
Samuel A. Falvo IIf52bdf82014-02-01 16:26:21 -0800446 return perigee.Get(ep, perigee.Options{
447 MoreHeaders: map[string]string{
448 "X-Auth-Token": gsp.access.AuthToken(),
449 },
450 Results: &struct {
451 SecurityGroups *[]SecurityGroup `json:"security_groups"`
452 }{&sgs},
453 })
454 })
455 return sgs, err
456}
457
458// See the CloudServersProvider interface for details.
459func (gsp *genericServersProvider) SecurityGroupById(id int) (*SecurityGroup, error) {
460 var actual *SecurityGroup
461
462 err := gsp.context.WithReauth(gsp.access, func() error {
463 ep := fmt.Sprintf("%s/os-security-groups/%d", gsp.endpoint, id)
464 return perigee.Get(ep, perigee.Options{
465 MoreHeaders: map[string]string{
466 "X-Auth-Token": gsp.access.AuthToken(),
467 },
468 Results: &struct {
469 SecurityGroup **SecurityGroup `json:"security_group"`
470 }{&actual},
471 })
472 })
473 return actual, err
474}
475
476// See the CloudServersProvider interface for details.
477func (gsp *genericServersProvider) DeleteSecurityGroupById(id int) error {
478 err := gsp.context.WithReauth(gsp.access, func() error {
479 ep := fmt.Sprintf("%s/os-security-groups/%d", gsp.endpoint, id)
480 return perigee.Delete(ep, perigee.Options{
481 MoreHeaders: map[string]string{
482 "X-Auth-Token": gsp.access.AuthToken(),
483 },
484 OkCodes: []int{202},
485 })
486 })
487 return err
488}
489
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800490// See the CloudServersProvider interface for details.
491func (gsp *genericServersProvider) ListDefaultSGRules() ([]SGRule, error) {
492 var sgrs []SGRule
493 err := gsp.context.WithReauth(gsp.access, func() error {
Samuel A. Falvo IIc61289e2014-03-04 13:13:52 -0800494 ep := fmt.Sprintf("%s/os-security-group-default-rules", gsp.endpoint)
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800495 return perigee.Get(ep, perigee.Options{
496 MoreHeaders: map[string]string{
497 "X-Auth-Token": gsp.access.AuthToken(),
498 },
499 Results: &struct{Security_group_default_rules *[]SGRule}{&sgrs},
500 })
501 })
502 return sgrs, err
503}
504
505// See the CloudServersProvider interface for details.
Samuel A. Falvo IIda422ea2014-02-25 13:38:12 -0800506func (gsp *genericServersProvider) CreateDefaultSGRule(r SGRule) (*SGRule, error) {
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800507 var sgr *SGRule
508 err := gsp.context.WithReauth(gsp.access, func() error {
Samuel A. Falvo IIc61289e2014-03-04 13:13:52 -0800509 ep := fmt.Sprintf("%s/os-security-group-default-rules", gsp.endpoint)
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800510 return perigee.Post(ep, perigee.Options{
511 MoreHeaders: map[string]string{
512 "X-Auth-Token": gsp.access.AuthToken(),
513 },
514 Results: &struct{Security_group_default_rule **SGRule}{&sgr},
515 ReqBody: struct{Security_group_default_rule SGRule `json:"security_group_default_rule"`}{r},
516 })
517 })
518 return sgr, err
519}
520
521// See the CloudServersProvider interface for details.
522func (gsp *genericServersProvider) GetSGRule(id string) (*SGRule, error) {
523 var sgr *SGRule
524 err := gsp.context.WithReauth(gsp.access, func() error {
Samuel A. Falvo IIc61289e2014-03-04 13:13:52 -0800525 ep := fmt.Sprintf("%s/os-security-group-default-rules/%s", gsp.endpoint, id)
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800526 return perigee.Get(ep, perigee.Options{
527 MoreHeaders: map[string]string{
528 "X-Auth-Token": gsp.access.AuthToken(),
529 },
Samuel A. Falvo IIf30d51e2014-02-25 12:55:22 -0800530 Results: &struct{Security_group_default_rule **SGRule}{&sgr},
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800531 })
532 })
533 return sgr, err
534}
535
Samuel A. Falvo IIf52bdf82014-02-01 16:26:21 -0800536// SecurityGroup provides a description of a security group, including all its rules.
537type SecurityGroup struct {
538 Description string `json:"description,omitempty"`
539 Id int `json:"id,omitempty"`
540 Name string `json:"name,omitempty"`
541 Rules []SGRule `json:"rules,omitempty"`
542 TenantId string `json:"tenant_id,omitempty"`
543}
544
545// SGRule encapsulates a single rule which applies to a security group.
546// 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
547type SGRule struct {
548 FromPort int `json:"from_port,omitempty"`
549 Id int `json:"id,omitempty"`
550 IpProtocol string `json:"ip_protocol,omitempty"`
551 IpRange map[string]interface{} `json:"ip_range,omitempty"`
552 ToPort int `json:"to_port,omitempty"`
553}
554
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700555// RaxBandwidth provides measurement of server bandwidth consumed over a given audit interval.
556type RaxBandwidth struct {
557 AuditPeriodEnd string `json:"audit_period_end"`
558 AuditPeriodStart string `json:"audit_period_start"`
559 BandwidthInbound int64 `json:"bandwidth_inbound"`
560 BandwidthOutbound int64 `json:"bandwidth_outbound"`
561 Interface string `json:"interface"`
562}
563
564// A VersionedAddress denotes either an IPv4 or IPv6 (depending on version indicated)
565// address.
566type VersionedAddress struct {
567 Addr string `json:"addr"`
568 Version int `json:"version"`
569}
570
571// An AddressSet provides a set of public and private IP addresses for a resource.
572// Each address has a version to identify if IPv4 or IPv6.
573type AddressSet struct {
574 Public []VersionedAddress `json:"public"`
575 Private []VersionedAddress `json:"private"`
576}
577
Jon Perritt499dce12013-10-29 15:41:14 -0500578type NetworkAddress map[string][]VersionedAddress
579
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700580// Server records represent (virtual) hardware instances (not configurations) accessible by the user.
581//
582// The AccessIPv4 / AccessIPv6 fields provides IP addresses for the server in the IPv4 or IPv6 format, respectively.
583//
584// Addresses provides addresses for any attached isolated networks.
585// The version field indicates whether the IP address is version 4 or 6.
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800586// Note: only public and private pools appear here.
587// To get the complete set, use the AllAddressPools() method instead.
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700588//
589// Created tells when the server entity was created.
590//
591// The Flavor field includes the flavor ID and flavor links.
592//
593// The compute provisioning algorithm has an anti-affinity property that
594// attempts to spread customer VMs across hosts.
595// Under certain situations,
596// VMs from the same customer might be placed on the same host.
597// The HostId field represents the host your server runs on and
598// can be used to determine this scenario if it is relevant to your application.
599// Note that HostId is unique only per account; it is not globally unique.
Mark Peeka2818af2013-08-24 15:01:12 -0700600//
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700601// Id provides the server's unique identifier.
602// This field must be treated opaquely.
603//
604// Image indicates which image is installed on the server.
605//
606// Links provides one or more means of accessing the server.
607//
608// Metadata provides a small key-value store for application-specific information.
609//
610// Name provides a human-readable name for the server.
611//
612// Progress indicates how far along it is towards being provisioned.
613// 100 represents complete, while 0 represents just beginning.
614//
615// Status provides an indication of what the server's doing at the moment.
616// A server will be in ACTIVE state if it's ready for use.
617//
618// OsDcfDiskConfig indicates the server's boot volume configuration.
619// Valid values are:
620// AUTO
621// ----
622// The server is built with a single partition the size of the target flavor disk.
623// The file system is automatically adjusted to fit the entire partition.
624// This keeps things simple and automated.
625// AUTO is valid only for images and servers with a single partition that use the EXT3 file system.
626// This is the default setting for applicable Rackspace base images.
627//
628// MANUAL
629// ------
630// The server is built using whatever partition scheme and file system is in the source image.
631// If the target flavor disk is larger,
632// the remaining disk space is left unpartitioned.
633// This enables images to have non-EXT3 file systems, multiple partitions, and so on,
634// and enables you to manage the disk configuration.
635//
636// RaxBandwidth provides measures of the server's inbound and outbound bandwidth per interface.
637//
638// OsExtStsPowerState provides an indication of the server's power.
639// This field appears to be a set of flag bits:
640//
641// ... 4 3 2 1 0
642// +--//--+---+---+---+---+
643// | .... | 0 | S | 0 | I |
644// +--//--+---+---+---+---+
645// | |
646// | +--- 0=Instance is down.
647// | 1=Instance is up.
648// |
649// +----------- 0=Server is switched ON.
650// 1=Server is switched OFF.
651// (note reverse logic.)
652//
653// Unused bits should be ignored when read, and written as 0 for future compatibility.
654//
655// OsExtStsTaskState and OsExtStsVmState work together
656// to provide visibility in the provisioning process for the instance.
657// Consult Rackspace documentation at
658// http://docs.rackspace.com/servers/api/v2/cs-devguide/content/ch_extensions.html#ext_status
659// for more details. It's too lengthy to include here.
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700660type Server struct {
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800661 AccessIPv4 string `json:"accessIPv4"`
662 AccessIPv6 string `json:"accessIPv6"`
663 Addresses AddressSet
Mark Peek22efb6c2013-08-26 13:50:22 -0700664 Created string `json:"created"`
665 Flavor FlavorLink `json:"flavor"`
666 HostId string `json:"hostId"`
667 Id string `json:"id"`
668 Image ImageLink `json:"image"`
669 Links []Link `json:"links"`
670 Metadata map[string]string `json:"metadata"`
671 Name string `json:"name"`
672 Progress int `json:"progress"`
673 Status string `json:"status"`
674 TenantId string `json:"tenant_id"`
675 Updated string `json:"updated"`
676 UserId string `json:"user_id"`
677 OsDcfDiskConfig string `json:"OS-DCF:diskConfig"`
678 RaxBandwidth []RaxBandwidth `json:"rax-bandwidth:bandwidth"`
679 OsExtStsPowerState int `json:"OS-EXT-STS:power_state"`
680 OsExtStsTaskState string `json:"OS-EXT-STS:task_state"`
681 OsExtStsVmState string `json:"OS-EXT-STS:vm_state"`
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800682
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800683 RawAddresses map[string]interface{} `json:"addresses"`
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800684}
685
686// AllAddressPools returns a complete set of address pools available on the server.
687// The name of each pool supported keys the map.
688// The value of the map contains the addresses provided in the corresponding pool.
689func (s *Server) AllAddressPools() (map[string][]VersionedAddress, error) {
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800690 pools := make(map[string][]VersionedAddress, 0)
691 for pool, subtree := range s.RawAddresses {
692 addresses := make([]VersionedAddress, 0)
693 err := mapstructure.Decode(subtree, &addresses)
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800694 if err != nil {
695 return nil, err
696 }
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800697 pools[pool] = addresses
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800698 }
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800699 return pools, nil
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700700}
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700701
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700702// NewServerSettings structures record those fields of the Server structure to change
703// when updating a server (see UpdateServer method).
704type NewServerSettings struct {
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700705 Name string `json:"name,omitempty"`
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700706 AccessIPv4 string `json:"accessIPv4,omitempty"`
707 AccessIPv6 string `json:"accessIPv6,omitempty"`
708}
709
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700710// NewServer structures are used for both requests and responses.
711// The fields discussed below are relevent for server-creation purposes.
712//
713// The Name field contains the desired name of the server.
714// Note that (at present) Rackspace permits more than one server with the same name;
715// however, software should not depend on this.
716// Not only will Rackspace support thank you, so will your own devops engineers.
717// A name is required.
718//
719// The ImageRef field contains the ID of the desired software image to place on the server.
720// This ID must be found in the image slice returned by the Images() function.
721// This field is required.
722//
723// The FlavorRef field contains the ID of the server configuration desired for deployment.
724// This ID must be found in the flavor slice returned by the Flavors() function.
725// This field is required.
726//
727// For OsDcfDiskConfig, refer to the Image or Server structure documentation.
728// This field defaults to "AUTO" if not explicitly provided.
729//
730// Metadata contains a small key/value association of arbitrary data.
731// Neither Rackspace nor OpenStack places significance on this field in any way.
732// This field defaults to an empty map if not provided.
733//
734// Personality specifies the contents of certain files in the server's filesystem.
735// The files and their contents are mapped through a slice of FileConfig structures.
736// If not provided, all filesystem entities retain their image-specific configuration.
737//
738// Networks specifies an affinity for the server's various networks and interfaces.
739// Networks are identified through UUIDs; see NetworkConfig structure documentation for more details.
740// If not provided, network affinity is determined automatically.
741//
742// The AdminPass field may be used to provide a root- or administrator-password
743// during the server provisioning process.
744// If not provided, a random password will be automatically generated and returned in this field.
745//
746// The following fields are intended to be used to communicate certain results about the server being provisioned.
747// When attempting to create a new server, these fields MUST not be provided.
748// They'll be filled in by the response received from the Rackspace APIs.
749//
750// The Id field contains the server's unique identifier.
751// The identifier's scope is best assumed to be bound by the user's account, unless other arrangements have been made with Rackspace.
752//
Kgespadaab69ab22014-01-22 11:43:17 -0800753// The SecurityGroup field allows the user to specify a security group at launch.
754//
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700755// Any Links provided are used to refer to the server specifically by URL.
756// These links are useful for making additional REST calls not explicitly supported by Gorax.
757type NewServer struct {
Kgespadaab69ab22014-01-22 11:43:17 -0800758 Name string `json:"name,omitempty"`
759 ImageRef string `json:"imageRef,omitempty"`
760 FlavorRef string `json:"flavorRef,omitempty"`
761 Metadata map[string]string `json:"metadata,omitempty"`
762 Personality []FileConfig `json:"personality,omitempty"`
763 Networks []NetworkConfig `json:"networks,omitempty"`
764 AdminPass string `json:"adminPass,omitempty"`
765 KeyPairName string `json:"key_name,omitempty"`
766 Id string `json:"id,omitempty"`
767 Links []Link `json:"links,omitempty"`
768 OsDcfDiskConfig string `json:"OS-DCF:diskConfig,omitempty"`
769 SecurityGroup []map[string]interface{} `json:"security_groups,omitempty"`
Alex Polvi1800d8f2014-04-05 22:21:18 -0700770 ConfigDrive bool `json:"config_drive"`
771 UserData string `json:"user_data"`
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700772}
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700773
774// ResizeRequest structures are used internally to encode to JSON the parameters required to resize a server instance.
775// Client applications will not use this structure (no API accepts an instance of this structure).
776// See the Region method ResizeServer() for more details on how to resize a server.
777type ResizeRequest struct {
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700778 Name string `json:"name,omitempty"`
779 FlavorRef string `json:"flavorRef"`
780 DiskConfig string `json:"OS-DCF:diskConfig,omitempty"`
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700781}
Mark Peek6b57c232013-08-24 19:03:26 -0700782
783type CreateImage struct {
784 Name string `json:"name"`
785 Metadata map[string]string `json:"metadata,omitempty"`
786}