Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
| 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 II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame^] | 10 | // provided through separate, type-safe accessors or methods. |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 11 | type ListResult map[string]interface{} |
| 12 | |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 13 | type ServerResult map[string]interface{} |
| 14 | |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 15 | // List makes a request against the API to list servers accessible to you. |
| 16 | func 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 II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame^] | 25 | Results: &lr, |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 26 | MoreHeaders: h, |
| 27 | }) |
| 28 | return lr, err |
| 29 | } |
| 30 | |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 31 | // Create requests a server to be provisioned to the user in the current tenant. |
| 32 | func 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 II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame^] | 42 | ReqBody: map[string]interface{}{ |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 43 | "server": opts, |
| 44 | }, |
| 45 | MoreHeaders: h, |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame^] | 46 | OkCodes: []int{202}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 47 | }) |
| 48 | return sr, err |
| 49 | } |
| 50 | |
| 51 | // Delete requests that a server previously provisioned be removed from your account. |
| 52 | func 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 II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame^] | 60 | OkCodes: []int{204}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 61 | }) |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | // GetDetail requests details on a single server, by ID. |
| 66 | func 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 II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame^] | 75 | Results: &sr, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 76 | MoreHeaders: h, |
| 77 | }) |
| 78 | return sr, err |
| 79 | } |
| 80 | |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 81 | // Update requests that various attributes of the indicated server be changed. |
| 82 | func 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 | } |