-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswift.go
93 lines (69 loc) · 1.67 KB
/
swift.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
package helm
import (
"net/http"
"youtab/dashboard/services/assert"
"bytes"
"io/ioutil"
"encoding/json"
"github.com/kataras/iris/core/errors"
"gopkg.in/yaml.v2"
)
type chart struct {
ChartURL string `json:"chart_url"`
}
type update struct {
chart
Values interface{} `json:"values"`
}
type Upgrade struct {
ClusterName string
NS string
}
func (m manager) Install(chartName string) error {
var payload = chart{url + chartName}
resp := m.request(http.MethodPost, payload)
_, err := marshal(resp)
return err
}
func (m manager) Upgrade(chartURL string, values map[string]string) error {
marshalled, err := yaml.Marshal(values)
assert.Nil(err)
inlineMap := string(marshalled)
payload := update{
chart{chartURL},
struct {
Raw string `json:"raw"`
}{Raw: inlineMap},
}
resp := m.request(http.MethodPut, payload)
_, err = marshal(resp)
return err
}
func (m manager) Values() {
resp := m.request(http.MethodPost, nil)
_, err := marshal(resp)
assert.Nil(err)
}
func (m manager) request(method string, body interface{}) []byte {
client := &http.Client{}
assert.True(m.Name != "", errors.New("release name cannot be empty"))
data, err := json.Marshal(body)
assert.Nil(err)
req, err := http.NewRequest(method, url+m.Name+`/json`, bytes.NewBuffer(data))
assert.Nil(err)
resp, err := client.Do(req)
assert.Nil(err)
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
assert.Nil(err)
return respBody
}
func marshal(resp []byte) (Release, error) {
var payload AutoGenerated
err := json.Unmarshal(resp, &payload)
assert.Nil(err)
if payload.Code != 0 {
return Release{}, errors.New(payload.Message)
}
return payload.Release, nil
}