blob: 51312eba16b01957baee50d9c7734e2153feb4b8 [file] [log] [blame]
Jon Perrittb6b1d022014-04-14 21:50:45 -05001package v1
Jon Perritt816d2a02014-03-11 20:49:46 -05002
3import (
4 "fmt"
Ash Wilson2ebb21c2014-09-08 15:01:32 -04005
6 "github.com/rackspace/gophercloud"
Jon Perritt5eb55b12014-08-18 14:48:23 -05007 identity "github.com/rackspace/gophercloud/openstack/identity/v2"
Jon Perritt816d2a02014-03-11 20:49:46 -05008)
9
10// Client is a structure that contains information for communicating with a provider.
11type Client struct {
12 endpoint string
13 authority identity.AuthResults
Ash Wilson2ebb21c2014-09-08 15:01:32 -040014 options gophercloud.AuthOptions
Jon Perritt816d2a02014-03-11 20:49:46 -050015 token *identity.Token
16}
17
Jon Perrittbef727e2014-05-12 22:41:55 -050018// NewClient creates and returns a *Client.
Ash Wilson2ebb21c2014-09-08 15:01:32 -040019func NewClient(e string, a identity.AuthResults, o gophercloud.AuthOptions) *Client {
Jon Perritt816d2a02014-03-11 20:49:46 -050020 return &Client{
21 endpoint: e,
22 authority: a,
23 options: o,
24 }
25}
26
Jon Perrittbef727e2014-05-12 22:41:55 -050027// 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 Perritt816d2a02014-03-11 20:49:46 -050029func (c *Client) GetAccountURL() string {
30 return fmt.Sprintf("%s", c.endpoint)
31}
32
Jon Perrittbef727e2014-05-12 22:41:55 -050033// 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 Perritt816d2a02014-03-11 20:49:46 -050035func (c *Client) GetContainerURL(container string) string {
36 return fmt.Sprintf("%s/%s", c.endpoint, container)
37}
38
Jon Perrittbef727e2014-05-12 22:41:55 -050039// 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 Perritt816d2a02014-03-11 20:49:46 -050041func (c *Client) GetObjectURL(container, object string) string {
42 return fmt.Sprintf("%s/%s/%s", c.endpoint, container, object)
43}
44
Jon Perrittbef727e2014-05-12 22:41:55 -050045// 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 Perritt816d2a02014-03-11 20:49:46 -050047func (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.
59func (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 Wilson2ebb21c2014-09-08 15:01:32 -040069 return c.token.ID, err
Jon Perritt816d2a02014-03-11 20:49:46 -050070}