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