Jon Perritt | b6b1d02 | 2014-04-14 21:50:45 -0500 | [diff] [blame^] | 1 | package v1 |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "github.com/rackspace/gophercloud/openstack/identity" |
| 6 | ) |
| 7 | |
| 8 | // Client is a structure that contains information for communicating with a provider. |
| 9 | type Client struct { |
| 10 | endpoint string |
| 11 | authority identity.AuthResults |
| 12 | options identity.AuthOptions |
| 13 | token *identity.Token |
| 14 | } |
| 15 | |
| 16 | func NewClient(e string, a identity.AuthResults, o identity.AuthOptions) *Client { |
| 17 | return &Client{ |
| 18 | endpoint: e, |
| 19 | authority: a, |
| 20 | options: o, |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func (c *Client) GetAccountURL() string { |
| 25 | return fmt.Sprintf("%s", c.endpoint) |
| 26 | } |
| 27 | |
| 28 | func (c *Client) GetContainerURL(container string) string { |
| 29 | return fmt.Sprintf("%s/%s", c.endpoint, container) |
| 30 | } |
| 31 | |
| 32 | func (c *Client) GetObjectURL(container, object string) string { |
| 33 | return fmt.Sprintf("%s/%s/%s", c.endpoint, container, object) |
| 34 | } |
| 35 | |
| 36 | // GetHeaders is a function that sets the header for token authentication against a client's endpoint. |
| 37 | func (c *Client) GetHeaders() (map[string]string, error) { |
| 38 | t, err := c.getAuthToken() |
| 39 | if err != nil { |
| 40 | return map[string]string{}, err |
| 41 | } |
| 42 | |
| 43 | return map[string]string{ |
| 44 | "X-Auth-Token": t, |
| 45 | }, nil |
| 46 | } |
| 47 | |
| 48 | // getAuthToken is a function that tries to retrieve an authentication token from a client's endpoint. |
| 49 | func (c *Client) getAuthToken() (string, error) { |
| 50 | var err error |
| 51 | |
| 52 | if c.token == nil { |
| 53 | c.token, err = identity.GetToken(c.authority) |
| 54 | if err != nil { |
| 55 | return "", err |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return c.token.Id, err |
| 60 | } |