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 { |
| 10 | endpoint string |
| 11 | authority identity.AuthResults |
| 12 | options identity.AuthOptions |
| 13 | token *identity.Token |
| 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{ |
| 19 | endpoint: e, |
| 20 | authority: a, |
| 21 | options: o, |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func (c *Client) getListUrl() string { |
| 26 | return fmt.Sprintf("%s/servers/detail", c.endpoint) |
| 27 | } |
| 28 | |
| 29 | func (c *Client) getListHeaders() (map[string]string, error) { |
| 30 | t, err := c.getAuthToken() |
| 31 | if err != nil { |
| 32 | return map[string]string{}, err |
| 33 | } |
| 34 | |
| 35 | return map[string]string{ |
| 36 | "X-Auth-Token": t, |
| 37 | }, nil |
| 38 | } |
| 39 | |
| 40 | func (c *Client) getAuthToken() (string, error) { |
| 41 | var err error |
| 42 | |
| 43 | if c.token == nil { |
| 44 | c.token, err = identity.GetToken(c.authority) |
| 45 | if err != nil { |
| 46 | return "", err |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return c.token.Id, err |
| 51 | } |
| 52 | |