blob: 52477ffabecc966f7596f8b622572e739f479f6d [file] [log] [blame]
Jon Perrittee6074f2014-04-30 18:42:32 -05001package blockstorage
2
3import (
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.
9type 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.
17func 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 Perritt3baca062014-04-30 23:33:04 -050025func (c *Client) GetVolumesURL() string {
Jon Perrittee6074f2014-04-30 18:42:32 -050026 return fmt.Sprintf("%s/volumes", c.endpoint)
27}
28
Jon Perritt3baca062014-04-30 23:33:04 -050029func (c *Client) GetVolumeURL(id string) string {
30 return fmt.Sprintf("%s/volumes/%s", c.endpoint, id)
31}
32
Jon Perritt687c7d02014-05-05 18:44:54 -050033func (c *Client) GetSnapshotsURL() string {
34 return fmt.Sprintf("%s/snapshots", c.endpoint)
35}
36
Jon Perritt982c86d2014-05-05 21:13:54 -050037func (c *Client) GetSnapshotURL(id string) string {
38 return fmt.Sprintf("%s/snapshots/%s", c.endpoint, id)
39}
40
Jon Perrittee6074f2014-04-30 18:42:32 -050041func (c *Client) GetHeaders() (map[string]string, error) {
42 t, err := c.getAuthToken()
43 if err != nil {
44 return map[string]string{}, err
45 }
46
47 return map[string]string{
48 "X-Auth-Token": t,
49 }, nil
50}
51
52func (c *Client) getAuthToken() (string, error) {
53 var err error
54
55 if c.token == nil {
56 c.token, err = identity.GetToken(c.authority)
57 if err != nil {
58 return "", err
59 }
60 }
61
62 return c.token.Id, err
63}