blob: 611ff22a2da2deb6725085178e0a8e72e360edfc [file] [log] [blame]
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08001package servers
2
3import (
4 "fmt"
Jon Perritt5eb55b12014-08-18 14:48:23 -05005 identity "github.com/rackspace/gophercloud/openstack/identity/v2"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08006)
7
8// Client abstracts the connection information needed to make API requests for OpenStack compute endpoints.
9type Client struct {
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080010 endpoint string
11 authority identity.AuthResults
12 options identity.AuthOptions
13 token *identity.Token
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080014}
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{
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080019 endpoint: e,
20 authority: a,
21 options: o,
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080022 }
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 IIca5f9a32014-03-11 17:52:58 -070045func (c *Client) getActionUrl(id string) string {
46 return fmt.Sprintf("%s/servers/%s/action", c.endpoint, id)
47}
48
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080049func (c *Client) getListHeaders() (map[string]string, error) {
50 t, err := c.getAuthToken()
51 if err != nil {
52 return map[string]string{}, err
53 }
54
55 return map[string]string{
56 "X-Auth-Token": t,
57 }, nil
58}
59
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080060func (c *Client) getCreateHeaders() (map[string]string, error) {
61 return c.getListHeaders()
62}
63
64func (c *Client) getDeleteHeaders() (map[string]string, error) {
65 return c.getListHeaders()
66}
67
68func (c *Client) getDetailHeaders() (map[string]string, error) {
69 return c.getListHeaders()
70}
71
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -080072func (c *Client) getUpdateHeaders() (map[string]string, error) {
73 return c.getListHeaders()
74}
75
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -070076func (c *Client) getActionHeaders() (map[string]string, error) {
77 return c.getListHeaders()
78}
79
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080080func (c *Client) getAuthToken() (string, error) {
81 var err error
82
83 if c.token == nil {
84 c.token, err = identity.GetToken(c.authority)
85 if err != nil {
86 return "", err
87 }
88 }
89
90 return c.token.Id, err
91}