blob: 28abd1c82ab530c3b70a8b94d477ad65d7846511 [file] [log] [blame]
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08001package servers
2
3import (
4 "github.com/racker/perigee"
5)
6
7// ListResult abstracts the raw results of making a List() request against the
8// API. As OpenStack extensions may freely alter the response bodies of
9// structures returned to the client, you may only safely access the data
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080010// provided through separate, type-safe accessors or methods.
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080011type ListResult map[string]interface{}
12
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080013type ServerResult map[string]interface{}
14
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080015// List makes a request against the API to list servers accessible to you.
16func List(c *Client) (ListResult, error) {
17 var lr ListResult
18
19 h, err := c.getListHeaders()
20 if err != nil {
21 return nil, err
22 }
23
24 err = perigee.Get(c.getListUrl(), perigee.Options{
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080025 Results: &lr,
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080026 MoreHeaders: h,
27 })
28 return lr, err
29}
30
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080031// Create requests a server to be provisioned to the user in the current tenant.
32func Create(c *Client, opts map[string]interface{}) (ServerResult, error) {
33 var sr ServerResult
34
35 h, err := c.getCreateHeaders()
36 if err != nil {
37 return nil, err
38 }
39
40 err = perigee.Post(c.getCreateUrl(), perigee.Options{
41 Results: &sr,
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080042 ReqBody: map[string]interface{}{
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080043 "server": opts,
44 },
45 MoreHeaders: h,
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080046 OkCodes: []int{202},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080047 })
48 return sr, err
49}
50
51// Delete requests that a server previously provisioned be removed from your account.
52func Delete(c *Client, id string) error {
53 h, err := c.getDeleteHeaders()
54 if err != nil {
55 return err
56 }
57
58 err = perigee.Delete(c.getDeleteUrl(id), perigee.Options{
59 MoreHeaders: h,
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080060 OkCodes: []int{204},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080061 })
62 return err
63}
64
65// GetDetail requests details on a single server, by ID.
66func GetDetail(c *Client, id string) (ServerResult, error) {
67 var sr ServerResult
68
69 h, err := c.getDetailHeaders()
70 if err != nil {
71 return nil, err
72 }
73
74 err = perigee.Get(c.getDetailUrl(id), perigee.Options{
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080075 Results: &sr,
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080076 MoreHeaders: h,
77 })
78 return sr, err
79}
80
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -080081// Update requests that various attributes of the indicated server be changed.
82func Update(c *Client, id string, opts map[string]interface{}) (ServerResult, error) {
83 var sr ServerResult
84
85 h, err := c.getUpdateHeaders()
86 if err != nil {
87 return nil, err
88 }
89
90 err = perigee.Put(c.getUpdateUrl(id), perigee.Options{
91 Results: &sr,
92 ReqBody: map[string]interface{}{
93 "server": opts,
94 },
95 MoreHeaders: h,
96 })
97 return sr, err
98}