2013-06-13 10:03:10 -04:00
|
|
|
// All of the methods used to communicate with the digital_ocean API
|
|
|
|
// are here. Their API is on a path to V2, so just plain JSON is used
|
|
|
|
// in place of a proper client library for now.
|
|
|
|
|
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-06-19 00:54:15 -04:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2013-06-13 10:03:10 -04:00
|
|
|
"io/ioutil"
|
2013-06-14 09:26:03 -04:00
|
|
|
"log"
|
2013-06-13 10:03:10 -04:00
|
|
|
"net/http"
|
2013-06-14 09:26:03 -04:00
|
|
|
"net/url"
|
2013-06-19 16:22:48 -04:00
|
|
|
"strings"
|
2013-09-05 00:20:41 -04:00
|
|
|
"time"
|
2013-06-13 10:03:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const DIGITALOCEAN_API_URL = "https://api.digitalocean.com"
|
|
|
|
|
2013-06-19 00:54:15 -04:00
|
|
|
type Image struct {
|
|
|
|
Id uint
|
|
|
|
Name string
|
|
|
|
Distribution string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ImagesResp struct {
|
|
|
|
Images []Image
|
|
|
|
}
|
|
|
|
|
2013-06-13 10:03:10 -04:00
|
|
|
type DigitalOceanClient struct {
|
|
|
|
// The http client for communicating
|
2013-06-13 11:58:06 -04:00
|
|
|
client *http.Client
|
2013-06-13 10:03:10 -04:00
|
|
|
|
|
|
|
// The base URL of the API
|
|
|
|
BaseURL string
|
|
|
|
|
|
|
|
// Credentials
|
|
|
|
ClientID string
|
|
|
|
APIKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new client for communicating with DO
|
2013-06-13 11:58:06 -04:00
|
|
|
func (d DigitalOceanClient) New(client string, key string) *DigitalOceanClient {
|
2013-06-13 10:03:10 -04:00
|
|
|
c := &DigitalOceanClient{
|
2013-08-18 22:29:54 -04:00
|
|
|
client: &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
},
|
|
|
|
},
|
2013-06-13 10:03:10 -04:00
|
|
|
BaseURL: DIGITALOCEAN_API_URL,
|
|
|
|
ClientID: client,
|
|
|
|
APIKey: key,
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates an SSH Key and returns it's id
|
|
|
|
func (d DigitalOceanClient) CreateKey(name string, pub string) (uint, error) {
|
2013-08-16 13:47:22 -04:00
|
|
|
params := url.Values{}
|
|
|
|
params.Set("name", name)
|
|
|
|
params.Set("ssh_pub_key", pub)
|
2013-06-13 10:03:10 -04:00
|
|
|
|
|
|
|
body, err := NewRequest(d, "ssh_keys/new", params)
|
|
|
|
if err != nil {
|
2013-06-13 11:58:06 -04:00
|
|
|
return 0, err
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the SSH key's ID we just created
|
|
|
|
key := body["ssh_key"].(map[string]interface{})
|
|
|
|
keyId := key["id"].(float64)
|
|
|
|
return uint(keyId), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroys an SSH key
|
|
|
|
func (d DigitalOceanClient) DestroyKey(id uint) error {
|
2013-06-14 09:26:03 -04:00
|
|
|
path := fmt.Sprintf("ssh_keys/%v/destroy", id)
|
2013-08-16 13:47:22 -04:00
|
|
|
_, err := NewRequest(d, path, url.Values{})
|
2013-06-13 10:03:10 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a droplet and returns it's id
|
|
|
|
func (d DigitalOceanClient) CreateDroplet(name string, size uint, image uint, region uint, keyId uint) (uint, error) {
|
2013-08-16 13:47:22 -04:00
|
|
|
params := url.Values{}
|
|
|
|
params.Set("name", name)
|
|
|
|
params.Set("size_id", fmt.Sprintf("%v", size))
|
|
|
|
params.Set("image_id", fmt.Sprintf("%v", image))
|
|
|
|
params.Set("region_id", fmt.Sprintf("%v", region))
|
|
|
|
params.Set("ssh_key_ids", fmt.Sprintf("%v", keyId))
|
2013-06-13 10:03:10 -04:00
|
|
|
|
|
|
|
body, err := NewRequest(d, "droplets/new", params)
|
|
|
|
if err != nil {
|
2013-06-13 11:58:06 -04:00
|
|
|
return 0, err
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the Droplets ID
|
|
|
|
droplet := body["droplet"].(map[string]interface{})
|
|
|
|
dropletId := droplet["id"].(float64)
|
2013-06-13 11:58:06 -04:00
|
|
|
return uint(dropletId), err
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destroys a droplet
|
|
|
|
func (d DigitalOceanClient) DestroyDroplet(id uint) error {
|
2013-06-14 09:26:03 -04:00
|
|
|
path := fmt.Sprintf("droplets/%v/destroy", id)
|
2013-08-16 13:47:22 -04:00
|
|
|
_, err := NewRequest(d, path, url.Values{})
|
2013-06-13 10:03:10 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Powers off a droplet
|
2013-06-13 11:58:06 -04:00
|
|
|
func (d DigitalOceanClient) PowerOffDroplet(id uint) error {
|
2013-06-14 09:26:03 -04:00
|
|
|
path := fmt.Sprintf("droplets/%v/power_off", id)
|
2013-06-13 10:03:10 -04:00
|
|
|
|
2013-08-16 13:47:22 -04:00
|
|
|
_, err := NewRequest(d, path, url.Values{})
|
2013-06-13 10:03:10 -04:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-08-24 07:04:51 -04:00
|
|
|
// Shutsdown a droplet. This is a "soft" shutdown.
|
|
|
|
func (d DigitalOceanClient) ShutdownDroplet(id uint) error {
|
|
|
|
path := fmt.Sprintf("droplets/%v/shutdown", id)
|
|
|
|
|
|
|
|
_, err := NewRequest(d, path, url.Values{})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-06-13 10:03:10 -04:00
|
|
|
// Creates a snaphot of a droplet by it's ID
|
|
|
|
func (d DigitalOceanClient) CreateSnapshot(id uint, name string) error {
|
2013-06-14 09:26:03 -04:00
|
|
|
path := fmt.Sprintf("droplets/%v/snapshot", id)
|
2013-08-16 13:47:22 -04:00
|
|
|
|
|
|
|
params := url.Values{}
|
|
|
|
params.Set("name", name)
|
2013-06-13 10:03:10 -04:00
|
|
|
|
|
|
|
_, err := NewRequest(d, path, params)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-06-19 00:54:15 -04:00
|
|
|
// Returns all available images.
|
|
|
|
func (d DigitalOceanClient) Images() ([]Image, error) {
|
2013-08-16 13:47:22 -04:00
|
|
|
resp, err := NewRequest(d, "images", url.Values{})
|
2013-06-19 00:54:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result ImagesResp
|
|
|
|
if err := mapstructure.Decode(resp, &result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.Images, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroys an image by its ID.
|
|
|
|
func (d DigitalOceanClient) DestroyImage(id uint) error {
|
|
|
|
path := fmt.Sprintf("images/%d/destroy", id)
|
2013-08-16 13:47:22 -04:00
|
|
|
_, err := NewRequest(d, path, url.Values{})
|
2013-06-19 00:54:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-06-13 10:03:10 -04:00
|
|
|
// Returns DO's string representation of status "off" "new" "active" etc.
|
2013-06-13 12:48:19 -04:00
|
|
|
func (d DigitalOceanClient) DropletStatus(id uint) (string, string, error) {
|
2013-06-14 09:26:03 -04:00
|
|
|
path := fmt.Sprintf("droplets/%v", id)
|
2013-06-13 10:03:10 -04:00
|
|
|
|
2013-08-16 13:47:22 -04:00
|
|
|
body, err := NewRequest(d, path, url.Values{})
|
2013-06-13 10:03:10 -04:00
|
|
|
if err != nil {
|
2013-06-13 12:48:19 -04:00
|
|
|
return "", "", err
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|
|
|
|
|
2013-06-14 09:26:03 -04:00
|
|
|
var ip string
|
|
|
|
|
2013-06-13 10:03:10 -04:00
|
|
|
// Read the droplet's "status"
|
|
|
|
droplet := body["droplet"].(map[string]interface{})
|
|
|
|
status := droplet["status"].(string)
|
2013-06-14 09:26:03 -04:00
|
|
|
|
|
|
|
if droplet["ip_address"] != nil {
|
|
|
|
ip = droplet["ip_address"].(string)
|
|
|
|
}
|
2013-06-13 10:03:10 -04:00
|
|
|
|
2013-06-13 12:48:19 -04:00
|
|
|
return ip, status, err
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sends an api request and returns a generic map[string]interface of
|
|
|
|
// the response.
|
2013-08-16 13:47:22 -04:00
|
|
|
func NewRequest(d DigitalOceanClient, path string, params url.Values) (map[string]interface{}, error) {
|
2013-06-13 10:03:10 -04:00
|
|
|
client := d.client
|
2013-08-16 13:47:22 -04:00
|
|
|
|
|
|
|
// Add the authentication parameters
|
|
|
|
params.Set("client_id", d.ClientID)
|
|
|
|
params.Set("api_key", d.APIKey)
|
|
|
|
|
|
|
|
url := fmt.Sprintf("%s/%s?%s", DIGITALOCEAN_API_URL, path, params.Encode())
|
2013-06-13 10:03:10 -04:00
|
|
|
|
2013-06-19 16:18:53 -04:00
|
|
|
// Do some basic scrubbing so sensitive information doesn't appear in logs
|
2013-06-19 16:22:48 -04:00
|
|
|
scrubbedUrl := strings.Replace(url, d.ClientID, "CLIENT_ID", -1)
|
|
|
|
scrubbedUrl = strings.Replace(scrubbedUrl, d.APIKey, "API_KEY", -1)
|
2013-06-19 16:18:53 -04:00
|
|
|
log.Printf("sending new request to digitalocean: %s", scrubbedUrl)
|
2013-06-14 09:26:03 -04:00
|
|
|
|
2013-09-05 00:20:41 -04:00
|
|
|
var lastErr error
|
|
|
|
for attempts := 1; attempts < 5; attempts++ {
|
|
|
|
resp, err := client.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-06-13 10:03:10 -04:00
|
|
|
|
2013-09-05 00:20:41 -04:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-06-13 10:03:10 -04:00
|
|
|
|
2013-09-05 00:20:41 -04:00
|
|
|
log.Printf("response from digitalocean: %s", body)
|
2013-06-17 08:31:47 -04:00
|
|
|
|
2013-09-05 00:20:41 -04:00
|
|
|
var decodedResponse map[string]interface{}
|
|
|
|
err = json.Unmarshal(body, &decodedResponse)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New(fmt.Sprintf("Failed to decode JSON response (HTTP %v) from DigitalOcean: %s",
|
|
|
|
resp.StatusCode, body))
|
|
|
|
return decodedResponse, err
|
|
|
|
}
|
2013-06-13 10:03:10 -04:00
|
|
|
|
2013-09-05 00:20:41 -04:00
|
|
|
// Check for errors sent by digitalocean
|
|
|
|
status := decodedResponse["status"].(string)
|
|
|
|
if status == "OK" {
|
|
|
|
return decodedResponse, nil
|
|
|
|
}
|
2013-06-13 10:03:10 -04:00
|
|
|
|
2013-06-17 08:22:29 -04:00
|
|
|
if status == "ERROR" {
|
2013-09-05 00:20:41 -04:00
|
|
|
status = decodedResponse["error_message"].(string)
|
2013-06-17 08:22:29 -04:00
|
|
|
}
|
2013-09-05 00:20:41 -04:00
|
|
|
|
|
|
|
lastErr = errors.New(fmt.Sprintf("Received error from DigitalOcean (%d): %s",
|
|
|
|
resp.StatusCode, status))
|
|
|
|
log.Println(lastErr)
|
2013-09-05 00:29:07 -04:00
|
|
|
if strings.Contains(status, "a pending event") {
|
2013-09-05 00:20:41 -04:00
|
|
|
// Retry, DigitalOcean sends these dumb "pending event"
|
|
|
|
// errors all the time.
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some other kind of error. Just return.
|
|
|
|
return decodedResponse, lastErr
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|
|
|
|
|
2013-09-05 00:20:41 -04:00
|
|
|
return nil, lastErr
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|