Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "github.com/rackspace/gophercloud/openstack/identity" |
| 6 | ) |
| 7 | |
| 8 | // Client abstracts the connection information needed to make API requests for OpenStack compute endpoints. |
| 9 | type Client struct { |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame^] | 10 | endpoint string |
| 11 | authority identity.AuthResults |
| 12 | options identity.AuthOptions |
| 13 | token *identity.Token |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | // NewClient creates a new Client structure to use when issuing requests to the server. |
| 17 | func NewClient(e string, a identity.AuthResults, o identity.AuthOptions) *Client { |
| 18 | return &Client{ |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame^] | 19 | endpoint: e, |
| 20 | authority: a, |
| 21 | options: o, |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 22 | } |
| 23 | } |
| 24 | |
| 25 | func (c *Client) getListUrl() string { |
| 26 | return fmt.Sprintf("%s/servers/detail", c.endpoint) |
| 27 | } |
| 28 | |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 29 | func (c *Client) getCreateUrl() string { |
| 30 | return fmt.Sprintf("%s/servers", c.endpoint) |
| 31 | } |
| 32 | |
| 33 | func (c *Client) getDeleteUrl(id string) string { |
| 34 | return fmt.Sprintf("%s/servers/%s", c.endpoint, id) |
| 35 | } |
| 36 | |
| 37 | func (c *Client) getDetailUrl(id string) string { |
| 38 | return c.getDeleteUrl(id) |
| 39 | } |
| 40 | |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 41 | func (c *Client) getUpdateUrl(id string) string { |
| 42 | return c.getDeleteUrl(id) |
| 43 | } |
| 44 | |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 45 | func (c *Client) getListHeaders() (map[string]string, error) { |
| 46 | t, err := c.getAuthToken() |
| 47 | if err != nil { |
| 48 | return map[string]string{}, err |
| 49 | } |
| 50 | |
| 51 | return map[string]string{ |
| 52 | "X-Auth-Token": t, |
| 53 | }, nil |
| 54 | } |
| 55 | |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 56 | func (c *Client) getCreateHeaders() (map[string]string, error) { |
| 57 | return c.getListHeaders() |
| 58 | } |
| 59 | |
| 60 | func (c *Client) getDeleteHeaders() (map[string]string, error) { |
| 61 | return c.getListHeaders() |
| 62 | } |
| 63 | |
| 64 | func (c *Client) getDetailHeaders() (map[string]string, error) { |
| 65 | return c.getListHeaders() |
| 66 | } |
| 67 | |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 68 | func (c *Client) getUpdateHeaders() (map[string]string, error) { |
| 69 | return c.getListHeaders() |
| 70 | } |
| 71 | |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 72 | func (c *Client) getAuthToken() (string, error) { |
| 73 | var err error |
| 74 | |
| 75 | if c.token == nil { |
| 76 | c.token, err = identity.GetToken(c.authority) |
| 77 | if err != nil { |
| 78 | return "", err |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return c.token.Id, err |
| 83 | } |