File: datacenters.go

package info (click to toggle)
golang-github-joyent-gosdc 0.0~git20161202.ec8b350-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 472 kB
  • sloc: makefile: 3
file content (41 lines) | stat: -rw-r--r-- 1,275 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package cloudapi

import (
	"net/http"

	"github.com/joyent/gocommon/client"
	"github.com/joyent/gocommon/errors"
)

// ListDatacenters provides a list of all datacenters this cloud is aware of.
// See API docs: http://apidocs.joyent.com/cloudapi/#ListDatacenters
func (c *Client) ListDatacenters() (map[string]interface{}, error) {
	var resp map[string]interface{}
	req := request{
		method: client.GET,
		url:    apiDatacenters,
		resp:   &resp,
	}
	if _, err := c.sendRequest(req); err != nil {
		return nil, errors.Newf(err, "failed to get list of datcenters")
	}
	return resp, nil
}

// GetDatacenter gets an individual datacenter by name. Returns an HTTP redirect
// to your client, the datacenter URL is in the Location header.
// See API docs: http://apidocs.joyent.com/cloudapi/#GetDatacenter
func (c *Client) GetDatacenter(datacenterName string) (string, error) {
	var respHeader http.Header
	req := request{
		method:         client.GET,
		url:            makeURL(apiDatacenters, datacenterName),
		respHeader:     &respHeader,
		expectedStatus: http.StatusFound,
	}
	respData, err := c.sendRequest(req)
	if err != nil {
		return "", errors.Newf(err, "failed to get datacenter with name: %s", datacenterName)
	}
	return respData.RespHeaders.Get("Location"), nil
}