-
Notifications
You must be signed in to change notification settings - Fork 0
/
kaas.go
120 lines (105 loc) · 2.56 KB
/
kaas.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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/google/go-github/github"
"github.com/google/uuid"
)
// Cluster struct
type Cluster struct {
Version string
PubKey string
ID string
Minutes string
Region string
Cfg string
}
type SupportedVersions struct {
Versions []string
}
func kaas(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
versions, err := checkversions()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).Encode(versions.Versions)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
case "POST":
cluster := Cluster{}
err := json.NewDecoder(r.Body).Decode(&cluster)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
supported, err := checkversions()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if stringInSlice(cluster.Version, supported.Versions) {
id, err := uuid.NewUUID()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
cluster.ID = id.String()
cluster.Minutes = "120"
cluster.Region = "nyc3"
cluster.Cfg = "https://storage.googleapis.com/" + cluster.ID + "/cluster-config.gpg"
err = createcluster(cluster)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
js, err := json.Marshal(cluster.Cfg)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(js)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
fmt.Fprintf(w, "Unsupported version")
}
default:
fmt.Fprintf(w, "Sorry, only GET and POST methods are supported.")
}
}
func checkversions() (SupportedVersions, error) {
client := github.NewClient(nil)
taglist, _, err := client.Repositories.ListTags(context.Background(), "poseidon", "typhoon", nil)
if err != nil {
return SupportedVersions{}, err
}
var supported SupportedVersions
for _, tag := range taglist {
supported.Versions = append(supported.Versions, tag.GetName())
}
if err != nil {
return SupportedVersions{}, err
}
return supported, nil
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func faviconHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "favicon.ico")
}