blob: 8c79f94f64fef1590765d6a912c4bfb8c0f88929 [file] [log] [blame]
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08001package servers
2
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"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08008)
9
10// Client abstracts the connection information needed to make API requests for OpenStack compute endpoints.
11type Client struct {
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080012 endpoint string
13 authority identity.AuthResults
Ash Wilson2ebb21c2014-09-08 15:01:32 -040014 options gophercloud.AuthOptions
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080015 token *identity.Token
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080016}
17
18// NewClient creates a new Client structure to use when issuing requests to the server.
Ash Wilson2ebb21c2014-09-08 15:01:32 -040019func NewClient(e string, a identity.AuthResults, o gophercloud.AuthOptions) *Client {
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080020 return &Client{
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080021 endpoint: e,
22 authority: a,
23 options: o,
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080024 }
25}
26
27func (c *Client) getListUrl() string {
28 return fmt.Sprintf("%s/servers/detail", c.endpoint)
29}
30
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080031func (c *Client) getCreateUrl() string {
32 return fmt.Sprintf("%s/servers", c.endpoint)
33}
34
35func (c *Client) getDeleteUrl(id string) string {
36 return fmt.Sprintf("%s/servers/%s", c.endpoint, id)
37}
38
39func (c *Client) getDetailUrl(id string) string {
40 return c.getDeleteUrl(id)
41}
42
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -080043func (c *Client) getUpdateUrl(id string) string {
44 return c.getDeleteUrl(id)
45}
46
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -070047func (c *Client) getActionUrl(id string) string {
48 return fmt.Sprintf("%s/servers/%s/action", c.endpoint, id)
49}
50
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080051func (c *Client) getListHeaders() (map[string]string, error) {
52 t, err := c.getAuthToken()
53 if err != nil {
54 return map[string]string{}, err
55 }
56
57 return map[string]string{
58 "X-Auth-Token": t,
59 }, nil
60}
61
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080062func (c *Client) getCreateHeaders() (map[string]string, error) {
63 return c.getListHeaders()
64}
65
66func (c *Client) getDeleteHeaders() (map[string]string, error) {
67 return c.getListHeaders()
68}
69
70func (c *Client) getDetailHeaders() (map[string]string, error) {
71 return c.getListHeaders()
72}
73
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -080074func (c *Client) getUpdateHeaders() (map[string]string, error) {
75 return c.getListHeaders()
76}
77
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -070078func (c *Client) getActionHeaders() (map[string]string, error) {
79 return c.getListHeaders()
80}
81
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080082func (c *Client) getAuthToken() (string, error) {
83 var err error
84
85 if c.token == nil {
86 c.token, err = identity.GetToken(c.authority)
87 if err != nil {
88 return "", err
89 }
90 }
91
Ash Wilson2ebb21c2014-09-08 15:01:32 -040092 return c.token.ID, err
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080093}