forked from bitcoin-software/nubectl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
157 lines (124 loc) · 4.08 KB
/
api.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
)
var (
cloudkey = flag.String("cloudkey", "", "Path to cloudkey ( or CLOUDKEY environment )")
cloudurl = flag.String("cloudurl", "", "Cloud URL ( or CLOUDURL environment )")
sshkey = flag.String("sshkey", "~/.ssh/id_ed25519", "SSH keypath for ssh ( or SSHKEY environment )")
)
var cloudUrl string
var sshKey string
func createJail(disksize string, key string, name string) {
httpposturl := cloudUrl + "/api/v1/create/" + name
//fmt.Println("HTTP JSON POST URL:", httpposturl)
var jsonData = []byte(`{
"type": "jail",
"imgsize": "` + disksize + `",
"pubkey": "` + key + `"
}`)
fmt.Println(string(jsonData))
request, error := http.NewRequest("POST", httpposturl, bytes.NewBuffer(jsonData))
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
client := &http.Client{}
response, error := client.Do(request)
if error != nil {
panic(error)
}
defer response.Body.Close()
fmt.Println("response Status:", response.Status)
//fmt.Println("response Headers:", response.Header)
body, _ := ioutil.ReadAll(response.Body)
fmt.Println("response Body:", string(body))
}
func createVM(image string, cores int, ramsize string, disksize string, key string, name string) {
httpposturl := cloudUrl + "/api/v1/create/" + name
fmt.Println("HTTP JSON POST URL:", httpposturl)
var jsonData = []byte(`{
"type": "bhyve",
"imgsize": "` + disksize + `",
"ram": "` + ramsize + `",
"cpus": "` + strconv.Itoa(cores) + `",
"img": "` + image + `",
"pubkey": "` + key + `"
}`)
//fmt.Println(string(jsonData))
request, error := http.NewRequest("POST", httpposturl, bytes.NewBuffer(jsonData))
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
client := &http.Client{}
response, error := client.Do(request)
if error != nil {
panic(error)
}
defer response.Body.Close()
fmt.Println("response Status:", response.Status)
//fmt.Println("response Headers:", response.Header)
body, _ := ioutil.ReadAll(response.Body)
fmt.Println("response Body:", string(body))
}
// show environment (when name is set) or cluster status
func getStatus(name string, keyID string) {
var statusurl string
if len(name) > 1 {
//get status for 'name' environment
statusurl = cloudUrl + "/api/v1/status/" + name
} else {
// when 'name' is absent then get cluster status
statusurl = cloudUrl + "/api/v1/cluster"
}
//fmt.Println("HTTP JSON POST URL:", statusurl)
request, error := http.NewRequest("GET", statusurl, nil)
request.Header.Set("cid", keyID)
//fmt.Println("cid:", keyID)
client := &http.Client{}
response, error := client.Do(request)
if error != nil {
panic(error)
}
defer response.Body.Close()
fmt.Fprintln(os.Stderr, "response Status:", response.Status)
//fmt.Println("response Headers:", response.Header)
body, _ := ioutil.ReadAll(response.Body)
fmt.Fprintln(os.Stderr, "response Body:")
fmt.Println(string(body))
}
func listCluster(keyID string) {
statusurl := cloudUrl + "/api/v1/cluster"
//fmt.Println("HTTP JSON POST URL:", statusurl)
request, error := http.NewRequest("GET", statusurl, nil)
request.Header.Set("cid", keyID)
//fmt.Println("cid:", keyID)
client := &http.Client{}
response, error := client.Do(request)
if error != nil {
panic(error)
}
defer response.Body.Close()
fmt.Println("response Status:", response.Status)
//fmt.Println("response Headers:", response.Header)
body, _ := ioutil.ReadAll(response.Body)
fmt.Println("response Body:", string(body))
}
func destroyResource(name string, keyID string) {
destroyurl := cloudUrl + "/api/v1/destroy/" + name
//fmt.Println("HTTP JSON POST URL:", destroyurl)
request, error := http.NewRequest("GET", destroyurl, nil)
request.Header.Set("cid", keyID)
//fmt.Println("cid:", keyID)
client := &http.Client{}
response, error := client.Do(request)
if error != nil {
panic(error)
}
defer response.Body.Close()
fmt.Println("response Status:", response.Status)
//fmt.Println("response Headers:", response.Header)
body, _ := ioutil.ReadAll(response.Body)
fmt.Println("response Body:", string(body))
}