-
Notifications
You must be signed in to change notification settings - Fork 0
/
thorin.go
63 lines (49 loc) · 1.14 KB
/
thorin.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
59
60
61
62
63
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
)
type payload struct {
Type string `json:"type"`
Name string `json:"name"`
}
func main() {
// Variables
apiKey := os.Getenv("DO_API_KEY")
dropletID := os.Getenv("DO_DROPLET_ID")
dropletName, err := os.Hostname()
if err != nil {
panic(err)
}
dateTime := time.Now().Format("02012006_150406")
snapshotName := dropletName + "_" + dateTime
apiCreateSnapshot := "https://api.digitalocean.com/v2/droplets/" + string(dropletID) + "/actions"
data := payload{
Type: "snapshot",
Name: snapshotName,
}
payloadBytes, err := json.Marshal(data)
if err != nil {
panic(err)
}
body := bytes.NewReader(payloadBytes)
req, err := http.NewRequest("POST", apiCreateSnapshot, body)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
if resp.StatusCode != http.StatusCreated {
fmt.Printf("Error - Status returned: %v\n", resp.Status)
os.Exit(1)
}
fmt.Printf("Success - Status returned: %v\n", resp.Status)
}