blob: 8b7d1fbcd4ce8260f522e1b9df4b93af43fc0b03 [file] [log] [blame]
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08001package servers
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
25func (c *Client) getListUrl() string {
26 return fmt.Sprintf("%s/servers/detail", c.endpoint)
27}
28
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080029func (c *Client) getCreateUrl() string {
30 return fmt.Sprintf("%s/servers", c.endpoint)
31}
32
33func (c *Client) getDeleteUrl(id string) string {
34 return fmt.Sprintf("%s/servers/%s", c.endpoint, id)
35}
36
37func (c *Client) getDetailUrl(id string) string {
38 return c.getDeleteUrl(id)
39}
40
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080041func (c *Client) getListHeaders() (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
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080052func (c *Client) getCreateHeaders() (map[string]string, error) {
53 return c.getListHeaders()
54}
55
56func (c *Client) getDeleteHeaders() (map[string]string, error) {
57 return c.getListHeaders()
58}
59
60func (c *Client) getDetailHeaders() (map[string]string, error) {
61 return c.getListHeaders()
62}
63
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080064func (c *Client) getAuthToken() (string, error) {
65 var err error
66
67 if c.token == nil {
68 c.token, err = identity.GetToken(c.authority)
69 if err != nil {
70 return "", err
71 }
72 }
73
74 return c.token.Id, err
75}
76