Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 1 | package blockstorage |
| 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 | |
Jon Perritt | 3baca06 | 2014-04-30 23:33:04 -0500 | [diff] [blame^] | 25 | func (c *Client) GetVolumesURL() string { |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 26 | return fmt.Sprintf("%s/volumes", c.endpoint) |
| 27 | } |
| 28 | |
Jon Perritt | 3baca06 | 2014-04-30 23:33:04 -0500 | [diff] [blame^] | 29 | func (c *Client) GetVolumeURL(id string) string { |
| 30 | return fmt.Sprintf("%s/volumes/%s", c.endpoint, id) |
| 31 | } |
| 32 | |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 33 | func (c *Client) GetHeaders() (map[string]string, error) { |
| 34 | t, err := c.getAuthToken() |
| 35 | if err != nil { |
| 36 | return map[string]string{}, err |
| 37 | } |
| 38 | |
| 39 | return map[string]string{ |
| 40 | "X-Auth-Token": t, |
| 41 | }, nil |
| 42 | } |
| 43 | |
| 44 | func (c *Client) getAuthToken() (string, error) { |
| 45 | var err error |
| 46 | |
| 47 | if c.token == nil { |
| 48 | c.token, err = identity.GetToken(c.authority) |
| 49 | if err != nil { |
| 50 | return "", err |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return c.token.Id, err |
| 55 | } |