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" |
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" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 8 | ) |
| 9 | |
| 10 | // Client is a structure that contains information for communicating with a provider. |
| 11 | type Client struct { |
| 12 | endpoint string |
| 13 | authority identity.AuthResults |
Ash Wilson | 2ebb21c | 2014-09-08 15:01:32 -0400 | [diff] [blame] | 14 | options gophercloud.AuthOptions |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 15 | token *identity.Token |
| 16 | } |
| 17 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 18 | // NewClient creates and returns a *Client. |
Ash Wilson | 2ebb21c | 2014-09-08 15:01:32 -0400 | [diff] [blame] | 19 | func NewClient(e string, a identity.AuthResults, o gophercloud.AuthOptions) *Client { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 20 | return &Client{ |
| 21 | endpoint: e, |
| 22 | authority: a, |
| 23 | options: o, |
| 24 | } |
| 25 | } |
| 26 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 27 | // GetAccountURL returns the URI for making Account requests. This function is exported to allow |
| 28 | // the 'Accounts' subpackage to use it. It is not meant for public consumption. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 29 | func (c *Client) GetAccountURL() string { |
| 30 | return fmt.Sprintf("%s", c.endpoint) |
| 31 | } |
| 32 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 33 | // GetContainerURL returns the URI for making Container requests. This function is exported to allow |
| 34 | // the 'Containers' subpackage to use it. It is not meant for public consumption. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 35 | func (c *Client) GetContainerURL(container string) string { |
| 36 | return fmt.Sprintf("%s/%s", c.endpoint, container) |
| 37 | } |
| 38 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 39 | // GetObjectURL returns the URI for making Object requests. This function is exported to allow |
| 40 | // the 'Objects' subpackage to use it. It is not meant for public consumption. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 41 | func (c *Client) GetObjectURL(container, object string) string { |
| 42 | return fmt.Sprintf("%s/%s/%s", c.endpoint, container, object) |
| 43 | } |
| 44 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 45 | // GetHeaders is a function that gets the header for token authentication against a client's endpoint. |
| 46 | // This function is exported to allow the subpackages to use it. It is not meant for public consumption. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 47 | func (c *Client) GetHeaders() (map[string]string, error) { |
| 48 | t, err := c.getAuthToken() |
| 49 | if err != nil { |
| 50 | return map[string]string{}, err |
| 51 | } |
| 52 | |
| 53 | return map[string]string{ |
| 54 | "X-Auth-Token": t, |
| 55 | }, nil |
| 56 | } |
| 57 | |
| 58 | // getAuthToken is a function that tries to retrieve an authentication token from a client's endpoint. |
| 59 | func (c *Client) getAuthToken() (string, error) { |
| 60 | var err error |
| 61 | |
| 62 | if c.token == nil { |
| 63 | c.token, err = identity.GetToken(c.authority) |
| 64 | if err != nil { |
| 65 | return "", err |
| 66 | } |
| 67 | } |
| 68 | |
Ash Wilson | 2ebb21c | 2014-09-08 15:01:32 -0400 | [diff] [blame] | 69 | return c.token.ID, err |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 70 | } |