blob: 1f6a7a47882fb94034111ccea3892c42a1644357 [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"
Fatih Arslan41e3a702014-07-09 13:05:33 -07008 "net/url"
9 "strings"
10
Samuel A. Falvo IId3617102014-01-20 18:27:42 -080011 "github.com/mitchellh/mapstructure"
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -070012 "github.com/racker/perigee"
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -070013)
14
Samuel A. Falvo II1dd740a2013-07-08 15:48:40 -070015// genericServersProvider structures provide the implementation for generic OpenStack-compatible
16// CloudServersProvider interfaces.
17type genericServersProvider struct {
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070018 // endpoint refers to the provider's API endpoint base URL. This will be used to construct
19 // and issue queries.
20 endpoint string
21
22 // Test context (if any) in which to issue requests.
23 context *Context
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -070024
25 // access associates this API provider with a set of credentials,
26 // which may be automatically renewed if they near expiration.
27 access AccessProvider
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070028}
29
Samuel A. Falvo II1dd740a2013-07-08 15:48:40 -070030// See the CloudServersProvider interface for details.
Fatih Arslan41e3a702014-07-09 13:05:33 -070031func (gcp *genericServersProvider) ListServersByFilter(filter url.Values) ([]Server, error) {
32 var ss []Server
33
34 err := gcp.context.WithReauth(gcp.access, func() error {
35 url := gcp.endpoint + "/servers/detail?" + filter.Encode()
36 return perigee.Get(url, perigee.Options{
37 CustomClient: gcp.context.httpClient,
38 Results: &struct{ Servers *[]Server }{&ss},
39 MoreHeaders: map[string]string{
40 "X-Auth-Token": gcp.access.AuthToken(),
41 },
42 })
43 })
44 return ss, err
45}
46
47// See the CloudServersProvider interface for details.
Samuel A. Falvo IIa0a55842013-07-24 13:14:17 -070048func (gcp *genericServersProvider) ListServersLinksOnly() ([]Server, error) {
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -070049 var ss []Server
50
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -070051 err := gcp.context.WithReauth(gcp.access, func() error {
52 url := gcp.endpoint + "/servers"
53 return perigee.Get(url, perigee.Options{
54 CustomClient: gcp.context.httpClient,
55 Results: &struct{ Servers *[]Server }{&ss},
56 MoreHeaders: map[string]string{
57 "X-Auth-Token": gcp.access.AuthToken(),
58 },
59 })
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -070060 })
61 return ss, err
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070062}
63
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070064// See the CloudServersProvider interface for details.
Samuel A. Falvo IIa0a55842013-07-24 13:14:17 -070065func (gcp *genericServersProvider) ListServers() ([]Server, error) {
66 var ss []Server
67
68 err := gcp.context.WithReauth(gcp.access, func() error {
69 url := gcp.endpoint + "/servers/detail"
70 return perigee.Get(url, perigee.Options{
71 CustomClient: gcp.context.httpClient,
72 Results: &struct{ Servers *[]Server }{&ss},
73 MoreHeaders: map[string]string{
74 "X-Auth-Token": gcp.access.AuthToken(),
75 },
76 })
77 })
Samuel A. Falvo IId3617102014-01-20 18:27:42 -080078
79 // Compatibility with v0.0.x -- we "map" our public and private
80 // addresses into a legacy structure field for the benefit of
81 // earlier software.
82
83 if err != nil {
84 return ss, err
85 }
86
87 for _, s := range ss {
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -080088 err = mapstructure.Decode(s.RawAddresses, &s.Addresses)
Samuel A. Falvo IId3617102014-01-20 18:27:42 -080089 if err != nil {
90 return ss, err
91 }
92 }
93
Samuel A. Falvo IIa0a55842013-07-24 13:14:17 -070094 return ss, err
95}
96
97// See the CloudServersProvider interface for details.
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070098func (gsp *genericServersProvider) ServerById(id string) (*Server, error) {
99 var s *Server
100
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -0700101 err := gsp.context.WithReauth(gsp.access, func() error {
102 url := gsp.endpoint + "/servers/" + id
103 return perigee.Get(url, perigee.Options{
104 Results: &struct{ Server **Server }{&s},
105 MoreHeaders: map[string]string{
106 "X-Auth-Token": gsp.access.AuthToken(),
107 },
Samuel A. Falvo II40444fb2014-06-30 16:00:17 -0700108 OkCodes: []int{200},
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -0700109 })
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -0700110 })
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800111
112 // Compatibility with v0.0.x -- we "map" our public and private
113 // addresses into a legacy structure field for the benefit of
114 // earlier software.
115
116 if err != nil {
117 return s, err
118 }
119
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800120 err = mapstructure.Decode(s.RawAddresses, &s.Addresses)
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800121
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -0700122 return s, err
123}
124
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700125// See the CloudServersProvider interface for details.
126func (gsp *genericServersProvider) CreateServer(ns NewServer) (*NewServer, error) {
127 var s *NewServer
128
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -0700129 err := gsp.context.WithReauth(gsp.access, func() error {
130 ep := gsp.endpoint + "/servers"
131 return perigee.Post(ep, perigee.Options{
132 ReqBody: &struct {
133 Server *NewServer `json:"server"`
134 }{&ns},
135 Results: &struct{ Server **NewServer }{&s},
136 MoreHeaders: map[string]string{
137 "X-Auth-Token": gsp.access.AuthToken(),
138 },
139 OkCodes: []int{202},
140 })
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700141 })
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -0700142
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700143 return s, err
144}
145
Samuel A. Falvo II286e4de2013-07-12 11:33:31 -0700146// See the CloudServersProvider interface for details.
147func (gsp *genericServersProvider) DeleteServerById(id string) error {
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -0700148 err := gsp.context.WithReauth(gsp.access, func() error {
149 url := gsp.endpoint + "/servers/" + id
150 return perigee.Delete(url, perigee.Options{
151 MoreHeaders: map[string]string{
152 "X-Auth-Token": gsp.access.AuthToken(),
153 },
154 OkCodes: []int{204},
155 })
Samuel A. Falvo II286e4de2013-07-12 11:33:31 -0700156 })
157 return err
158}
159
Samuel A. Falvo II5c305e12013-07-25 19:19:43 -0700160// See the CloudServersProvider interface for details.
161func (gsp *genericServersProvider) SetAdminPassword(id, pw string) error {
162 err := gsp.context.WithReauth(gsp.access, func() error {
163 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
164 return perigee.Post(url, perigee.Options{
165 ReqBody: &struct {
166 ChangePassword struct {
167 AdminPass string `json:"adminPass"`
168 } `json:"changePassword"`
169 }{
170 struct {
171 AdminPass string `json:"adminPass"`
172 }{pw},
173 },
174 OkCodes: []int{202},
175 MoreHeaders: map[string]string{
176 "X-Auth-Token": gsp.access.AuthToken(),
177 },
178 })
179 })
180 return err
181}
182
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700183// See the CloudServersProvider interface for details.
184func (gsp *genericServersProvider) ResizeServer(id, newName, newFlavor, newDiskConfig string) error {
185 err := gsp.context.WithReauth(gsp.access, func() error {
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700186 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
187 rr := ResizeRequest{
188 Name: newName,
189 FlavorRef: newFlavor,
190 DiskConfig: newDiskConfig,
191 }
192 return perigee.Post(url, perigee.Options{
193 ReqBody: &struct {
194 Resize ResizeRequest `json:"resize"`
195 }{rr},
196 OkCodes: []int{202},
197 MoreHeaders: map[string]string{
198 "X-Auth-Token": gsp.access.AuthToken(),
199 },
200 })
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700201 })
202 return err
203}
204
205// See the CloudServersProvider interface for details.
206func (gsp *genericServersProvider) RevertResize(id string) error {
207 err := gsp.context.WithReauth(gsp.access, func() error {
208 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
209 return perigee.Post(url, perigee.Options{
210 ReqBody: &struct {
211 RevertResize *int `json:"revertResize"`
212 }{nil},
213 OkCodes: []int{202},
214 MoreHeaders: map[string]string{
215 "X-Auth-Token": gsp.access.AuthToken(),
216 },
217 })
218 })
219 return err
220}
221
222// See the CloudServersProvider interface for details.
223func (gsp *genericServersProvider) ConfirmResize(id string) error {
224 err := gsp.context.WithReauth(gsp.access, func() error {
225 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
226 return perigee.Post(url, perigee.Options{
227 ReqBody: &struct {
228 ConfirmResize *int `json:"confirmResize"`
229 }{nil},
230 OkCodes: []int{204},
231 MoreHeaders: map[string]string{
232 "X-Auth-Token": gsp.access.AuthToken(),
233 },
234 })
235 })
236 return err
237}
238
Samuel A. Falvo IIadbecf92013-07-30 13:13:59 -0700239// See the CloudServersProvider interface for details
240func (gsp *genericServersProvider) RebootServer(id string, hard bool) error {
241 return gsp.context.WithReauth(gsp.access, func() error {
242 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
243 types := map[bool]string{false: "SOFT", true: "HARD"}
244 return perigee.Post(url, perigee.Options{
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700245 ReqBody: &struct {
Samuel A. Falvo IIadbecf92013-07-30 13:13:59 -0700246 Reboot struct {
247 Type string `json:"type"`
248 } `json:"reboot"`
249 }{
250 struct {
251 Type string `json:"type"`
252 }{types[hard]},
253 },
254 OkCodes: []int{202},
255 MoreHeaders: map[string]string{
256 "X-Auth-Token": gsp.access.AuthToken(),
257 },
258 })
259 })
260}
261
Samuel A. Falvo II15da6ab2013-07-30 14:02:11 -0700262// See the CloudServersProvider interface for details
263func (gsp *genericServersProvider) RescueServer(id string) (string, error) {
264 var pw *string
265
266 err := 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 Rescue string `json:"rescue"`
271 }{"none"},
272 MoreHeaders: map[string]string{
273 "X-Auth-Token": gsp.access.AuthToken(),
274 },
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700275 Results: &struct {
Samuel A. Falvo II15da6ab2013-07-30 14:02:11 -0700276 AdminPass **string `json:"adminPass"`
277 }{&pw},
278 })
279 })
280 return *pw, err
281}
282
283// See the CloudServersProvider interface for details
284func (gsp *genericServersProvider) UnrescueServer(id string) error {
285 return gsp.context.WithReauth(gsp.access, func() error {
286 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
287 return perigee.Post(url, perigee.Options{
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700288 ReqBody: &struct {
Samuel A. Falvo II15da6ab2013-07-30 14:02:11 -0700289 Unrescue *int `json:"unrescue"`
290 }{nil},
291 MoreHeaders: map[string]string{
292 "X-Auth-Token": gsp.access.AuthToken(),
293 },
294 OkCodes: []int{202},
295 })
296 })
297}
298
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700299// See the CloudServersProvider interface for details
300func (gsp *genericServersProvider) UpdateServer(id string, changes NewServerSettings) (*Server, error) {
301 var svr *Server
302 err := gsp.context.WithReauth(gsp.access, func() error {
303 url := fmt.Sprintf("%s/servers/%s", gsp.endpoint, id)
304 return perigee.Put(url, perigee.Options{
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700305 ReqBody: &struct {
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700306 Server NewServerSettings `json:"server"`
307 }{changes},
308 MoreHeaders: map[string]string{
309 "X-Auth-Token": gsp.access.AuthToken(),
310 },
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700311 Results: &struct {
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700312 Server **Server `json:"server"`
313 }{&svr},
314 })
315 })
316 return svr, err
317}
318
Samuel A. Falvo II414c15c2013-08-01 15:16:46 -0700319// See the CloudServersProvider interface for details.
Samuel A. Falvo IIf3391602013-08-14 14:53:32 -0700320func (gsp *genericServersProvider) RebuildServer(id string, ns NewServer) (*Server, error) {
Samuel A. Falvo II414c15c2013-08-01 15:16:46 -0700321 var s *Server
322
323 err := gsp.context.WithReauth(gsp.access, func() error {
324 ep := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
325 return perigee.Post(ep, perigee.Options{
326 ReqBody: &struct {
327 Rebuild *NewServer `json:"rebuild"`
328 }{&ns},
329 Results: &struct{ Server **Server }{&s},
330 MoreHeaders: map[string]string{
331 "X-Auth-Token": gsp.access.AuthToken(),
332 },
333 OkCodes: []int{202},
334 })
335 })
336
337 return s, err
338}
339
Samuel A. Falvo IIe21808f2013-08-14 14:48:09 -0700340// See the CloudServersProvider interface for details.
341func (gsp *genericServersProvider) ListAddresses(id string) (AddressSet, error) {
342 var pas *AddressSet
343 var statusCode int
344
345 err := gsp.context.WithReauth(gsp.access, func() error {
346 ep := fmt.Sprintf("%s/servers/%s/ips", gsp.endpoint, id)
347 return perigee.Get(ep, perigee.Options{
348 Results: &struct{ Addresses **AddressSet }{&pas},
349 MoreHeaders: map[string]string{
350 "X-Auth-Token": gsp.access.AuthToken(),
351 },
Samuel A. Falvo IIf3391602013-08-14 14:53:32 -0700352 OkCodes: []int{200, 203},
Samuel A. Falvo IIe21808f2013-08-14 14:48:09 -0700353 StatusCode: &statusCode,
354 })
355 })
356
357 if err != nil {
358 if statusCode == 203 {
359 err = WarnUnauthoritative
360 }
361 }
362
363 return *pas, err
364}
365
Mark Peek6b57c232013-08-24 19:03:26 -0700366// See the CloudServersProvider interface for details.
Jon Perritt0c1629d2013-12-06 19:51:36 -0600367func (gsp *genericServersProvider) ListAddressesByNetwork(id, networkLabel string) (NetworkAddress, error) {
Jon Perrittb1ead742013-10-29 16:03:40 -0500368 pas := make(NetworkAddress)
Jon Perritt499dce12013-10-29 15:41:14 -0500369 var statusCode int
370
371 err := gsp.context.WithReauth(gsp.access, func() error {
372 ep := fmt.Sprintf("%s/servers/%s/ips/%s", gsp.endpoint, id, networkLabel)
373 return perigee.Get(ep, perigee.Options{
374 Results: &pas,
375 MoreHeaders: map[string]string{
376 "X-Auth-Token": gsp.access.AuthToken(),
377 },
Jon Perritt0c1629d2013-12-06 19:51:36 -0600378 OkCodes: []int{200, 203},
Jon Perritt499dce12013-10-29 15:41:14 -0500379 StatusCode: &statusCode,
380 })
381 })
382
383 if err != nil {
384 if statusCode == 203 {
385 err = WarnUnauthoritative
386 }
387 }
388
389 return pas, err
390}
391
392// See the CloudServersProvider interface for details.
Mark Peek6b57c232013-08-24 19:03:26 -0700393func (gsp *genericServersProvider) CreateImage(id string, ci CreateImage) (string, error) {
394 response, err := gsp.context.ResponseWithReauth(gsp.access, func() (*perigee.Response, error) {
395 ep := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
396 return perigee.Request("POST", ep, perigee.Options{
397 ReqBody: &struct {
398 CreateImage *CreateImage `json:"createImage"`
399 }{&ci},
400 MoreHeaders: map[string]string{
401 "X-Auth-Token": gsp.access.AuthToken(),
402 },
Mark Peek7d3e09d2013-08-27 07:57:18 -0700403 OkCodes: []int{200, 202},
Mark Peek6b57c232013-08-24 19:03:26 -0700404 })
405 })
406
407 if err != nil {
408 return "", err
409 }
410 location, err := response.HttpResponse.Location()
411 if err != nil {
412 return "", err
413 }
414
415 // Return the last element of the location which is the image id
416 locationArr := strings.Split(location.Path, "/")
417 return locationArr[len(locationArr)-1], err
418}
419
Samuel A. Falvo IIf52bdf82014-02-01 16:26:21 -0800420// See the CloudServersProvider interface for details.
421func (gsp *genericServersProvider) ListSecurityGroups() ([]SecurityGroup, error) {
422 var sgs []SecurityGroup
423
424 err := gsp.context.WithReauth(gsp.access, func() error {
425 ep := fmt.Sprintf("%s/os-security-groups", gsp.endpoint)
426 return perigee.Get(ep, perigee.Options{
427 MoreHeaders: map[string]string{
428 "X-Auth-Token": gsp.access.AuthToken(),
429 },
430 Results: &struct {
431 SecurityGroups *[]SecurityGroup `json:"security_groups"`
432 }{&sgs},
433 })
434 })
435 return sgs, err
436}
437
438// See the CloudServersProvider interface for details.
439func (gsp *genericServersProvider) CreateSecurityGroup(desired SecurityGroup) (*SecurityGroup, error) {
440 var actual *SecurityGroup
441
442 err := gsp.context.WithReauth(gsp.access, func() error {
443 ep := fmt.Sprintf("%s/os-security-groups", gsp.endpoint)
444 return perigee.Post(ep, perigee.Options{
445 ReqBody: struct {
Samuel A. Falvo II7b8ee8a2014-02-25 11:30:52 -0800446 AddSecurityGroup SecurityGroup `json:"security_group"`
Samuel A. Falvo IIf52bdf82014-02-01 16:26:21 -0800447 }{desired},
448 MoreHeaders: map[string]string{
449 "X-Auth-Token": gsp.access.AuthToken(),
450 },
451 Results: &struct {
452 SecurityGroup **SecurityGroup `json:"security_group"`
453 }{&actual},
454 })
455 })
456 return actual, err
457}
458
459// See the CloudServersProvider interface for details.
460func (gsp *genericServersProvider) ListSecurityGroupsByServerId(id string) ([]SecurityGroup, error) {
461 var sgs []SecurityGroup
462
463 err := gsp.context.WithReauth(gsp.access, func() error {
Samuel A. Falvo II7b8ee8a2014-02-25 11:30:52 -0800464 ep := fmt.Sprintf("%s/servers/%s/os-security-groups", gsp.endpoint, id)
Samuel A. Falvo IIf52bdf82014-02-01 16:26:21 -0800465 return perigee.Get(ep, perigee.Options{
466 MoreHeaders: map[string]string{
467 "X-Auth-Token": gsp.access.AuthToken(),
468 },
469 Results: &struct {
470 SecurityGroups *[]SecurityGroup `json:"security_groups"`
471 }{&sgs},
472 })
473 })
474 return sgs, err
475}
476
477// See the CloudServersProvider interface for details.
478func (gsp *genericServersProvider) SecurityGroupById(id int) (*SecurityGroup, error) {
479 var actual *SecurityGroup
480
481 err := gsp.context.WithReauth(gsp.access, func() error {
482 ep := fmt.Sprintf("%s/os-security-groups/%d", gsp.endpoint, id)
483 return perigee.Get(ep, perigee.Options{
484 MoreHeaders: map[string]string{
485 "X-Auth-Token": gsp.access.AuthToken(),
486 },
487 Results: &struct {
488 SecurityGroup **SecurityGroup `json:"security_group"`
489 }{&actual},
490 })
491 })
492 return actual, err
493}
494
495// See the CloudServersProvider interface for details.
496func (gsp *genericServersProvider) DeleteSecurityGroupById(id int) error {
497 err := gsp.context.WithReauth(gsp.access, func() error {
498 ep := fmt.Sprintf("%s/os-security-groups/%d", gsp.endpoint, id)
499 return perigee.Delete(ep, perigee.Options{
500 MoreHeaders: map[string]string{
501 "X-Auth-Token": gsp.access.AuthToken(),
502 },
503 OkCodes: []int{202},
504 })
505 })
506 return err
507}
508
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800509// See the CloudServersProvider interface for details.
510func (gsp *genericServersProvider) ListDefaultSGRules() ([]SGRule, error) {
511 var sgrs []SGRule
512 err := gsp.context.WithReauth(gsp.access, func() error {
Samuel A. Falvo IIc61289e2014-03-04 13:13:52 -0800513 ep := fmt.Sprintf("%s/os-security-group-default-rules", gsp.endpoint)
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800514 return perigee.Get(ep, perigee.Options{
515 MoreHeaders: map[string]string{
516 "X-Auth-Token": gsp.access.AuthToken(),
517 },
Fatih Arslan41e3a702014-07-09 13:05:33 -0700518 Results: &struct{ Security_group_default_rules *[]SGRule }{&sgrs},
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800519 })
520 })
521 return sgrs, err
522}
523
524// See the CloudServersProvider interface for details.
Samuel A. Falvo IIda422ea2014-02-25 13:38:12 -0800525func (gsp *genericServersProvider) CreateDefaultSGRule(r SGRule) (*SGRule, error) {
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800526 var sgr *SGRule
527 err := gsp.context.WithReauth(gsp.access, func() error {
Samuel A. Falvo IIc61289e2014-03-04 13:13:52 -0800528 ep := fmt.Sprintf("%s/os-security-group-default-rules", gsp.endpoint)
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800529 return perigee.Post(ep, perigee.Options{
530 MoreHeaders: map[string]string{
531 "X-Auth-Token": gsp.access.AuthToken(),
532 },
Fatih Arslan41e3a702014-07-09 13:05:33 -0700533 Results: &struct{ Security_group_default_rule **SGRule }{&sgr},
534 ReqBody: struct {
535 Security_group_default_rule SGRule `json:"security_group_default_rule"`
536 }{r},
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800537 })
538 })
539 return sgr, err
540}
541
542// See the CloudServersProvider interface for details.
543func (gsp *genericServersProvider) GetSGRule(id string) (*SGRule, error) {
544 var sgr *SGRule
545 err := gsp.context.WithReauth(gsp.access, func() error {
Samuel A. Falvo IIc61289e2014-03-04 13:13:52 -0800546 ep := fmt.Sprintf("%s/os-security-group-default-rules/%s", gsp.endpoint, id)
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800547 return perigee.Get(ep, perigee.Options{
548 MoreHeaders: map[string]string{
549 "X-Auth-Token": gsp.access.AuthToken(),
550 },
Fatih Arslan41e3a702014-07-09 13:05:33 -0700551 Results: &struct{ Security_group_default_rule **SGRule }{&sgr},
Samuel A. Falvo IId825e1c2014-02-25 12:48:03 -0800552 })
553 })
554 return sgr, err
555}
556
Samuel A. Falvo IIf52bdf82014-02-01 16:26:21 -0800557// SecurityGroup provides a description of a security group, including all its rules.
558type SecurityGroup struct {
559 Description string `json:"description,omitempty"`
560 Id int `json:"id,omitempty"`
561 Name string `json:"name,omitempty"`
562 Rules []SGRule `json:"rules,omitempty"`
563 TenantId string `json:"tenant_id,omitempty"`
564}
565
566// SGRule encapsulates a single rule which applies to a security group.
567// 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
568type SGRule struct {
569 FromPort int `json:"from_port,omitempty"`
570 Id int `json:"id,omitempty"`
571 IpProtocol string `json:"ip_protocol,omitempty"`
572 IpRange map[string]interface{} `json:"ip_range,omitempty"`
573 ToPort int `json:"to_port,omitempty"`
574}
575
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700576// RaxBandwidth provides measurement of server bandwidth consumed over a given audit interval.
577type RaxBandwidth struct {
578 AuditPeriodEnd string `json:"audit_period_end"`
579 AuditPeriodStart string `json:"audit_period_start"`
580 BandwidthInbound int64 `json:"bandwidth_inbound"`
581 BandwidthOutbound int64 `json:"bandwidth_outbound"`
582 Interface string `json:"interface"`
583}
584
585// A VersionedAddress denotes either an IPv4 or IPv6 (depending on version indicated)
586// address.
587type VersionedAddress struct {
588 Addr string `json:"addr"`
589 Version int `json:"version"`
590}
591
592// An AddressSet provides a set of public and private IP addresses for a resource.
593// Each address has a version to identify if IPv4 or IPv6.
594type AddressSet struct {
595 Public []VersionedAddress `json:"public"`
596 Private []VersionedAddress `json:"private"`
597}
598
Jon Perritt499dce12013-10-29 15:41:14 -0500599type NetworkAddress map[string][]VersionedAddress
600
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700601// Server records represent (virtual) hardware instances (not configurations) accessible by the user.
602//
603// The AccessIPv4 / AccessIPv6 fields provides IP addresses for the server in the IPv4 or IPv6 format, respectively.
604//
605// Addresses provides addresses for any attached isolated networks.
606// The version field indicates whether the IP address is version 4 or 6.
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800607// Note: only public and private pools appear here.
608// To get the complete set, use the AllAddressPools() method instead.
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700609//
610// Created tells when the server entity was created.
611//
612// The Flavor field includes the flavor ID and flavor links.
613//
614// The compute provisioning algorithm has an anti-affinity property that
615// attempts to spread customer VMs across hosts.
616// Under certain situations,
617// VMs from the same customer might be placed on the same host.
618// The HostId field represents the host your server runs on and
619// can be used to determine this scenario if it is relevant to your application.
620// Note that HostId is unique only per account; it is not globally unique.
Mark Peeka2818af2013-08-24 15:01:12 -0700621//
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700622// Id provides the server's unique identifier.
623// This field must be treated opaquely.
624//
625// Image indicates which image is installed on the server.
626//
627// Links provides one or more means of accessing the server.
628//
629// Metadata provides a small key-value store for application-specific information.
630//
631// Name provides a human-readable name for the server.
632//
633// Progress indicates how far along it is towards being provisioned.
634// 100 represents complete, while 0 represents just beginning.
635//
636// Status provides an indication of what the server's doing at the moment.
637// A server will be in ACTIVE state if it's ready for use.
638//
639// OsDcfDiskConfig indicates the server's boot volume configuration.
640// Valid values are:
641// AUTO
642// ----
643// The server is built with a single partition the size of the target flavor disk.
644// The file system is automatically adjusted to fit the entire partition.
645// This keeps things simple and automated.
646// AUTO is valid only for images and servers with a single partition that use the EXT3 file system.
647// This is the default setting for applicable Rackspace base images.
648//
649// MANUAL
650// ------
651// The server is built using whatever partition scheme and file system is in the source image.
652// If the target flavor disk is larger,
653// the remaining disk space is left unpartitioned.
654// This enables images to have non-EXT3 file systems, multiple partitions, and so on,
655// and enables you to manage the disk configuration.
656//
657// RaxBandwidth provides measures of the server's inbound and outbound bandwidth per interface.
658//
659// OsExtStsPowerState provides an indication of the server's power.
660// This field appears to be a set of flag bits:
661//
662// ... 4 3 2 1 0
663// +--//--+---+---+---+---+
664// | .... | 0 | S | 0 | I |
665// +--//--+---+---+---+---+
666// | |
667// | +--- 0=Instance is down.
668// | 1=Instance is up.
669// |
670// +----------- 0=Server is switched ON.
671// 1=Server is switched OFF.
672// (note reverse logic.)
673//
674// Unused bits should be ignored when read, and written as 0 for future compatibility.
675//
676// OsExtStsTaskState and OsExtStsVmState work together
677// to provide visibility in the provisioning process for the instance.
678// Consult Rackspace documentation at
679// http://docs.rackspace.com/servers/api/v2/cs-devguide/content/ch_extensions.html#ext_status
680// for more details. It's too lengthy to include here.
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700681type Server struct {
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800682 AccessIPv4 string `json:"accessIPv4"`
683 AccessIPv6 string `json:"accessIPv6"`
684 Addresses AddressSet
Mark Peek22efb6c2013-08-26 13:50:22 -0700685 Created string `json:"created"`
686 Flavor FlavorLink `json:"flavor"`
687 HostId string `json:"hostId"`
688 Id string `json:"id"`
689 Image ImageLink `json:"image"`
690 Links []Link `json:"links"`
691 Metadata map[string]string `json:"metadata"`
692 Name string `json:"name"`
693 Progress int `json:"progress"`
694 Status string `json:"status"`
695 TenantId string `json:"tenant_id"`
696 Updated string `json:"updated"`
697 UserId string `json:"user_id"`
698 OsDcfDiskConfig string `json:"OS-DCF:diskConfig"`
699 RaxBandwidth []RaxBandwidth `json:"rax-bandwidth:bandwidth"`
700 OsExtStsPowerState int `json:"OS-EXT-STS:power_state"`
701 OsExtStsTaskState string `json:"OS-EXT-STS:task_state"`
702 OsExtStsVmState string `json:"OS-EXT-STS:vm_state"`
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800703
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800704 RawAddresses map[string]interface{} `json:"addresses"`
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800705}
706
707// AllAddressPools returns a complete set of address pools available on the server.
708// The name of each pool supported keys the map.
709// The value of the map contains the addresses provided in the corresponding pool.
710func (s *Server) AllAddressPools() (map[string][]VersionedAddress, error) {
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800711 pools := make(map[string][]VersionedAddress, 0)
712 for pool, subtree := range s.RawAddresses {
713 addresses := make([]VersionedAddress, 0)
714 err := mapstructure.Decode(subtree, &addresses)
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800715 if err != nil {
716 return nil, err
717 }
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800718 pools[pool] = addresses
Samuel A. Falvo IId3617102014-01-20 18:27:42 -0800719 }
Samuel A. Falvo IIecae0ac2014-01-21 11:02:21 -0800720 return pools, nil
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700721}
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700722
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700723// NewServerSettings structures record those fields of the Server structure to change
724// when updating a server (see UpdateServer method).
725type NewServerSettings struct {
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700726 Name string `json:"name,omitempty"`
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700727 AccessIPv4 string `json:"accessIPv4,omitempty"`
728 AccessIPv6 string `json:"accessIPv6,omitempty"`
729}
730
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700731// NewServer structures are used for both requests and responses.
732// The fields discussed below are relevent for server-creation purposes.
733//
734// The Name field contains the desired name of the server.
735// Note that (at present) Rackspace permits more than one server with the same name;
736// however, software should not depend on this.
737// Not only will Rackspace support thank you, so will your own devops engineers.
738// A name is required.
739//
740// The ImageRef field contains the ID of the desired software image to place on the server.
741// This ID must be found in the image slice returned by the Images() function.
742// This field is required.
743//
744// The FlavorRef field contains the ID of the server configuration desired for deployment.
745// This ID must be found in the flavor slice returned by the Flavors() function.
746// This field is required.
747//
748// For OsDcfDiskConfig, refer to the Image or Server structure documentation.
749// This field defaults to "AUTO" if not explicitly provided.
750//
751// Metadata contains a small key/value association of arbitrary data.
752// Neither Rackspace nor OpenStack places significance on this field in any way.
753// This field defaults to an empty map if not provided.
754//
755// Personality specifies the contents of certain files in the server's filesystem.
756// The files and their contents are mapped through a slice of FileConfig structures.
757// If not provided, all filesystem entities retain their image-specific configuration.
758//
759// Networks specifies an affinity for the server's various networks and interfaces.
760// Networks are identified through UUIDs; see NetworkConfig structure documentation for more details.
761// If not provided, network affinity is determined automatically.
762//
763// The AdminPass field may be used to provide a root- or administrator-password
764// during the server provisioning process.
765// If not provided, a random password will be automatically generated and returned in this field.
766//
767// The following fields are intended to be used to communicate certain results about the server being provisioned.
768// When attempting to create a new server, these fields MUST not be provided.
769// They'll be filled in by the response received from the Rackspace APIs.
770//
771// The Id field contains the server's unique identifier.
772// The identifier's scope is best assumed to be bound by the user's account, unless other arrangements have been made with Rackspace.
773//
Kgespadaab69ab22014-01-22 11:43:17 -0800774// The SecurityGroup field allows the user to specify a security group at launch.
775//
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700776// Any Links provided are used to refer to the server specifically by URL.
777// These links are useful for making additional REST calls not explicitly supported by Gorax.
778type NewServer struct {
Kgespadaab69ab22014-01-22 11:43:17 -0800779 Name string `json:"name,omitempty"`
780 ImageRef string `json:"imageRef,omitempty"`
781 FlavorRef string `json:"flavorRef,omitempty"`
782 Metadata map[string]string `json:"metadata,omitempty"`
783 Personality []FileConfig `json:"personality,omitempty"`
784 Networks []NetworkConfig `json:"networks,omitempty"`
785 AdminPass string `json:"adminPass,omitempty"`
786 KeyPairName string `json:"key_name,omitempty"`
787 Id string `json:"id,omitempty"`
788 Links []Link `json:"links,omitempty"`
789 OsDcfDiskConfig string `json:"OS-DCF:diskConfig,omitempty"`
790 SecurityGroup []map[string]interface{} `json:"security_groups,omitempty"`
Alex Polvi1800d8f2014-04-05 22:21:18 -0700791 ConfigDrive bool `json:"config_drive"`
792 UserData string `json:"user_data"`
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700793}
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700794
795// ResizeRequest structures are used internally to encode to JSON the parameters required to resize a server instance.
796// Client applications will not use this structure (no API accepts an instance of this structure).
797// See the Region method ResizeServer() for more details on how to resize a server.
798type ResizeRequest struct {
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700799 Name string `json:"name,omitempty"`
800 FlavorRef string `json:"flavorRef"`
801 DiskConfig string `json:"OS-DCF:diskConfig,omitempty"`
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700802}
Mark Peek6b57c232013-08-24 19:03:26 -0700803
804type CreateImage struct {
805 Name string `json:"name"`
806 Metadata map[string]string `json:"metadata,omitempty"`
807}