blob: 03c5c1583306ecfd7525de18a10d57881ba11a85 [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 II22d3b772014-02-13 19:27:05 -080041func (c *Client) getUpdateUrl(id string) string {
42 return c.getDeleteUrl(id)
43}
44
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080045func (c *Client) getListHeaders() (map[string]string, error) {
46 t, err := c.getAuthToken()
47 if err != nil {
48 return map[string]string{}, err
49 }
50
51 return map[string]string{
52 "X-Auth-Token": t,
53 }, nil
54}
55
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080056func (c *Client) getCreateHeaders() (map[string]string, error) {
57 return c.getListHeaders()
58}
59
60func (c *Client) getDeleteHeaders() (map[string]string, error) {
61 return c.getListHeaders()
62}
63
64func (c *Client) getDetailHeaders() (map[string]string, error) {
65 return c.getListHeaders()
66}
67
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -080068func (c *Client) getUpdateHeaders() (map[string]string, error) {
69 return c.getListHeaders()
70}
71
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080072func (c *Client) getAuthToken() (string, error) {
73 var err error
74
75 if c.token == nil {
76 c.token, err = identity.GetToken(c.authority)
77 if err != nil {
78 return "", err
79 }
80 }
81
82 return c.token.Id, err
83}
84