-
Notifications
You must be signed in to change notification settings - Fork 24
/
diginal_ocean.go
58 lines (43 loc) · 1.18 KB
/
diginal_ocean.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"encoding/json"
"github.com/jmoiron/jsonq"
"log"
"net/http"
)
func getDigitalOceanInstances(config map[string]string) (instances Instances) {
instances = make(Instances)
if _, ok := config["client_id"]; !ok {
log.Fatal("Missing client_id for ", config["name"], " DigitalOcean cloud")
}
if _, ok := config["api_key"]; !ok {
log.Fatal("Missing api_key for ", config["name"], " DigitalOcean cloud")
}
resp, err := http.Get("https://api.digitalocean.com/droplets/?client_id=" + config["client_id"] + "&api_key=" + config["api_key"])
if err != nil {
log.Println("DigitalOcean API error:", err)
return
}
defer resp.Body.Close()
data := map[string]interface{}{}
dec := json.NewDecoder(resp.Body)
dec.Decode(&data)
jq := jsonq.NewQuery(data)
status, err := jq.String("status")
if status == "ERROR" {
err_msg, _ := jq.String("error_message")
log.Println("DigitalOcean API error: ", err_msg)
return
}
droplets, err := jq.ArrayOfObjects("droplets")
if err != nil {
log.Println(err)
return
}
for _, droplet := range droplets {
instances[droplet["ip_address"].(string)] = []Tag{
Tag{"Name", droplet["name"].(string)},
}
}
return
}