-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodepicnic.go
403 lines (374 loc) · 10.3 KB
/
codepicnic.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package codepicnic
import (
"bytes"
"encoding/json"
"errors"
//"fmt"
"github.com/Jeffail/gabs"
"io"
"io/ioutil"
"mime/multipart"
"net"
"net/http"
"os"
"strings"
"time"
)
const ERROR_NOT_AUTHORIZED = "Not Authorized"
const ERROR_NOT_CONNECTED = "Disconnected"
const ERROR_CONNECTION_REFUSED = "Connection Refused"
const ERROR_TCP_TIMEOUT = "TCP Timeout"
const ERROR_CLIENT_TIMEOUT = "Client Timeout"
const ERROR_TLS_TIMEOUT = "TLS Handshake Timeout"
const ERROR_DNS_LOOKUP = "Host not found"
const ERROR_NOT_FOUND = "Not Found"
const ERROR_EMPTY_CREDENTIALS = "No Credentials"
const ERROR_EMPTY_TOKEN = "No Token"
const ERROR_INVALID_TOKEN = "Invalid Token"
const ERROR_RENEW_TOKEN = "Token not Renewed"
const ERROR_USAGE_EXCEEDED = "Usage Exceeded"
const ERROR_NETWORK_UNREACHABLE = "Network Unreachable"
const ERROR_CONSOLE = "Console Error"
type codepicnic struct {
ClientId string
ClientSecret string
Token string
UserAgent string
}
var cp codepicnic
type TokenJson struct {
Access string `json:"access_token"`
Type string `json:"token_type"`
Expires string `json:"expires_in"`
Created string `json:"created_at"`
}
type StackJson struct {
Identifier string `json:"identifier"`
Name string `json:"name"`
ShortName string `json:"short_name"`
Version string `json:"version"`
ImageName string `json:"image_name"`
Group string `json:"group"`
}
type StackCollection struct {
Stacks []StackJson `json:"container_types"`
}
type CommandJson struct {
command string
result string
}
type FileJson struct {
Name string `json:"name"`
Path string `json:"path"`
Type string `json:"type"`
Size float64 `json:"size"`
}
type ApiRequest struct {
Method string
Endpoint string
Payload string
Timeout time.Duration
}
func Init(client_id string, client_secret string) error {
//TODO: return expiration
cp = codepicnic{
ClientId: client_id,
ClientSecret: client_secret,
UserAgent: "CodePicnic GO",
}
var token TokenJson
body, err := oauthRequest()
if err != nil {
return err
}
err = json.Unmarshal(body, &token)
cp.Token = token.Access
return nil
}
func RefreshToken() error {
var token TokenJson
body, err := oauthRequest()
if err != nil {
return err
}
err = json.Unmarshal(body, &token)
cp.Token = token.Access
return nil
}
func GetToken() string {
return cp.Token
}
func SetUserAgent(user_agent string) error {
cp.UserAgent = user_agent
return nil
}
func ListConsoles() ([]Console, error) {
var console_collection ConsoleCollection
api := ApiRequest{
Endpoint: "/consoles/all.json",
Method: "GET",
}
body, err := api.Send()
if err != nil {
return console_collection.Consoles, err
}
err = json.Unmarshal(body, &console_collection)
if err != nil {
return console_collection.Consoles, err
}
return console_collection.Consoles, nil
}
func GetConsole(console_id string) (Console, error) {
var console Console
cp_api_path := "/consoles/" + console_id
api := ApiRequest{
Endpoint: cp_api_path,
Method: "GET",
}
body, err := api.Send()
if err != nil {
return console, err
}
jsonBody, err := gabs.ParseJSON(body)
if err != nil {
return console, err
}
console_json, err := jsonBody.Search("console").ChildrenMap()
if err != nil {
return console, err
}
var console_json_title string
if console_json["title"].Data() == nil {
console_json_title = ""
} else {
console_json_title = console_json["title"].Data().(string)
}
console = Console{
id: int(console_json["id"].Data().(float64)),
content: sanitize(console_json["content"].Data()),
title: sanitize(console_json_title),
name: sanitize(console_json["name"].Data().(string)),
containerName: sanitize(console_json["container_name"].Data().(string)),
containerType: sanitize(console_json["container_type"].Data().(string)),
customImage: sanitize(console_json["created_at"].Data().(string)),
createdAt: sanitize(console_json["created_at"].Data().(string)),
permalink: sanitize(console_json["permalink"].Data().(string)),
url: sanitize(console_json["url"].Data().(string)),
embedUrl: sanitize(console_json["embed_url"].Data().(string)),
terminalUrl: sanitize(console_json["terminal_url"].Data().(string)),
}
return console, nil
}
func CreateConsole(console_req ConsoleRequest) (Console, error) {
var console Console
if console_req.Size == "" {
console_req.Size = "medium"
}
if console_req.Type == "" {
console_req.Type = "bash"
}
if console_req.Mode == "" {
console_req.Mode = "draft"
}
cp_api_path := "/consoles"
cp_payload := ` { "console": { "container_size": "` + console_req.Size + `", "container_type": "` + console_req.Type + `", "title": "` + console_req.Title + `" , "hostname": "` + console_req.Hostname + `", "current_mode": "` + console_req.Mode + `" } }`
api := ApiRequest{
Endpoint: cp_api_path,
Method: "POST",
Payload: cp_payload,
}
body, err := api.Send()
if err != nil {
return console, err
}
jsonBody, err := gabs.ParseJSON(body)
if err != nil {
return console, err
}
container_name, ok := jsonBody.Search("container_name").Data().(string)
if ok != true {
return console, errors.New(ERROR_CONSOLE)
}
console, err = GetConsole(container_name)
if err != nil {
return console, err
}
return console, nil
}
func ListStacks() ([]StackJson, error) {
var stack_collection StackCollection
api := ApiRequest{
Endpoint: "/container_types.json",
Method: "GET",
}
body, err := api.Send()
err = json.Unmarshal(body, &stack_collection)
if err != nil {
return stack_collection.Stacks, err
}
return stack_collection.Stacks, nil
}
func (api *ApiRequest) Send() ([]byte, error) {
var codepicnic_api = "https://codepicnic.com/api"
var req *http.Request
cp_api_url := codepicnic_api + api.Endpoint
if len(api.Payload) > 0 {
var jsonStr = []byte(api.Payload)
req, _ = http.NewRequest(api.Method, cp_api_url, bytes.NewBuffer(jsonStr))
} else {
req, _ = http.NewRequest(api.Method, cp_api_url, nil)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+cp.Token)
req.Header.Set("User-Agent", cp.UserAgent)
/*
var api_timeout time.Duration
if api.Timeout > time.Second * 0 {
api_timeout = api.Timeout
} else {
//default timeout
api_timeout = time.Second * 60
}*/
var api_transport = &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
client := &http.Client{
Timeout: time.Second * 20,
Transport: api_transport,
}
resp, err := client.Do(req)
if err != nil {
if strings.Contains(err.Error(), "no such host") {
return nil, errors.New(ERROR_DNS_LOOKUP)
} else if strings.Contains(err.Error(), "connection refused") {
return nil, errors.New(ERROR_CONNECTION_REFUSED)
} else if strings.Contains(err.Error(), "dial tcp: i/o timeout") {
return nil, errors.New(ERROR_TCP_TIMEOUT)
} else if strings.Contains(err.Error(), "exceeded while awaiting headers") {
return nil, errors.New(ERROR_CLIENT_TIMEOUT)
} else if strings.Contains(err.Error(), "TLS handshake timeout") {
return nil, errors.New(ERROR_TLS_TIMEOUT)
} else if strings.Contains(err.Error(), "network is unreachable") {
return nil, errors.New(ERROR_NETWORK_UNREACHABLE)
} else {
return nil, err
}
}
defer resp.Body.Close()
if resp.StatusCode == 401 {
err = RefreshToken()
if err != nil {
return nil, errors.New(ERROR_RENEW_TOKEN)
} else {
req.Header.Set("Authorization", "Bearer "+cp.Token)
resp, err = client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
}
//return nil, errors.New(ERROR_INVALID_TOKEN)
} else if resp.StatusCode == 429 {
return nil, errors.New(ERROR_USAGE_EXCEEDED)
} else if resp.StatusCode == 404 {
return nil, errors.New(ERROR_NOT_FOUND)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func (api *ApiRequest) Upload(src_file string, dst_file string) ([]byte, error) {
var codepicnic_api = "https://codepicnic.com/api"
var req *http.Request
cp_api_url := codepicnic_api + api.Endpoint
var b bytes.Buffer
w := multipart.NewWriter(&b)
temp_file, err := os.Open(src_file)
if err != nil {
return nil, err
}
fw, err := w.CreateFormFile("file", temp_file.Name())
if _, err = io.Copy(fw, temp_file); err != nil {
return nil, err
}
if fw, err = w.CreateFormField("path"); err != nil {
return nil, err
}
if _, err = fw.Write([]byte("/app/" + dst_file)); err != nil {
return nil, err
}
w.Close()
req, err = http.NewRequest("POST", cp_api_url, &b)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", w.FormDataContentType())
req.Header.Set("Authorization", "Bearer "+cp.Token)
req.Header.Set("User-Agent", cp.UserAgent)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == 401 {
err = RefreshToken()
if err != nil {
return nil, errors.New(ERROR_RENEW_TOKEN)
} else {
req.Header.Set("Authorization", "Bearer "+cp.Token)
resp, err = client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
}
} else if resp.StatusCode == 429 {
return nil, errors.New(ERROR_USAGE_EXCEEDED)
} else if resp.StatusCode == 404 {
return nil, errors.New(ERROR_NOT_FOUND)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func oauthRequest() ([]byte, error) {
var codepicnic_oauth = "https://codepicnic.com/oauth/token"
cp_payload := `{ "grant_type": "client_credentials","client_id": "` + cp.ClientId + `", "client_secret": "` + cp.ClientSecret + `"}`
var jsonStr = []byte(cp_payload)
req, err := http.NewRequest("POST", codepicnic_oauth, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", cp.UserAgent)
client := &http.Client{
Timeout: time.Second * 10,
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if err != nil {
if strings.Contains(err.Error(), "read: connection refused") {
return nil, errors.New(ERROR_NOT_CONNECTED)
}
}
if resp.StatusCode == 401 {
return nil, errors.New(ERROR_NOT_AUTHORIZED)
}
return body, nil
}
func sanitize(i interface{}) string {
if i == nil {
return ""
} else {
return i.(string)
}
}