forked from wallix/awless
-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
176 lines (144 loc) · 3.5 KB
/
client.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
package client
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/thunderbird86/awless/model"
)
type Client struct {
ServiceURL *url.URL
serviceInfo *model.ServiceInfo
httpClient *http.Client
}
func New(discoveryURL string) (*Client, error) {
httpClient := &http.Client{Timeout: 3 * time.Second}
resp, err := httpClient.Get(discoveryURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
v := &model.ServiceInfo{}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("cannot read body at discovery enpoint '%s', status code %s. Error: %s", discoveryURL, resp.Status, err)
}
if err := json.Unmarshal(body, v); err != nil {
return nil, fmt.Errorf("cannot unmarshal json at discovery enpoint '%s': %s. Body was:\n%s", discoveryURL, err, body)
}
if v.UnixSockMode {
c := newUnixSock(v.ServiceAddr)
c.serviceInfo = v
return c, nil
}
addr, err := url.Parse(v.ServiceAddr)
if err != nil {
return nil, fmt.Errorf("cannot parse scheduler service addr %s: %s", v.ServiceAddr, err)
}
return &Client{
ServiceURL: addr,
httpClient: httpClient,
serviceInfo: v,
}, nil
}
func newUnixSock(u string) *Client {
return &Client{
ServiceURL: &url.URL{Host: "unixsock", Scheme: "http"}, // context info only
httpClient: &http.Client{
Timeout: 3 * time.Second,
Transport: &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", u)
},
},
},
}
}
type Form struct {
Region, RunIn, RevertIn string
Template string
}
func (c *Client) Ping() error {
addr := *c.ServiceURL
resp, err := c.httpClient.Get(addr.String())
if err != nil {
return err
}
defer resp.Body.Close()
return notOKStatus(addr.String(), resp)
}
func (c *Client) ServiceInfo() model.ServiceInfo {
return *c.serviceInfo
}
func (c *Client) ListTasks() ([]*model.Task, error) {
var tasks []*model.Task
addr := *c.ServiceURL
addr.Path = "tasks"
resp, err := c.httpClient.Get(addr.String())
if err != nil {
return tasks, err
}
defer resp.Body.Close()
if err = notOKStatus(addr.String(), resp); err != nil {
return tasks, err
}
if err = json.NewDecoder(resp.Body).Decode(&tasks); err != nil {
return tasks, err
}
return tasks, nil
}
func (c *Client) ListFailures() ([]*model.Task, error) {
var tasks []*model.Task
addr := *c.ServiceURL
addr.Path = "failures"
resp, err := c.httpClient.Get(addr.String())
if err != nil {
return tasks, err
}
defer resp.Body.Close()
if err = notOKStatus(addr.String(), resp); err != nil {
return tasks, err
}
if err = json.NewDecoder(resp.Body).Decode(&tasks); err != nil {
return tasks, err
}
return tasks, nil
}
func (c *Client) Post(f Form) error {
addr := *c.ServiceURL
addr.Path = "tasks"
query := addr.Query()
query.Add("region", f.Region)
if f.RunIn != "" {
query.Add("run", f.RunIn)
}
if f.RevertIn != "" {
query.Add("revert", f.RevertIn)
}
addr.RawQuery = query.Encode()
resp, err := c.httpClient.Post(
addr.String(),
"application/text",
strings.NewReader(f.Template),
)
if err != nil {
return err
}
defer resp.Body.Close()
if err := notOKStatus(addr.String(), resp); err != nil {
return err
}
return nil
}
func notOKStatus(addr string, resp *http.Response) error {
if code := resp.StatusCode; code != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("Got %d status instead of 200 from '%s': %q", code, addr, body)
}
return nil
}