/
client.go
203 lines (173 loc) · 4.2 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
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
package doomsday
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
"time"
"github.com/doomsday-project/doomsday/server/auth"
)
type Client struct {
Client *http.Client
URL url.URL
Token string
Trace io.Writer
}
func (c *Client) doRequest(
method, path string,
input, output interface{}) error {
var client = c.Client
if c.Client == nil {
client = http.DefaultClient
}
var reqBody io.Reader
if input != nil {
reqBody = &bytes.Buffer{}
jEncoder := json.NewEncoder(reqBody.(*bytes.Buffer))
err := jEncoder.Encode(&input)
if err != nil {
panic("Could not encode object for request body")
}
}
req, err := http.NewRequest(method, fmt.Sprintf("%s%s", c.URL.String(), path), reqBody)
if err != nil {
return err
}
req.Header.Set("X-Doomsday-Token", c.Token)
if c.Trace != nil {
dump, err := httputil.DumpRequest(req, true)
if err != nil {
return err
}
_, err = c.Trace.Write(dump)
if err != nil {
return err
}
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if c.Trace != nil {
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
return err
}
_, err = c.Trace.Write(dump)
if err != nil {
return err
}
}
if (resp.StatusCode / 100) != 2 {
return parseError(resp.StatusCode)
}
if output != nil {
jDecoder := json.NewDecoder(resp.Body)
err := jDecoder.Decode(&output)
return err
}
return nil
}
//UserpassAuth attempts to authenticate with the doomsday server. If successful,
// the response is stored into the client
func (c *Client) UserpassAuth(username, password string) error {
output := struct {
Token string `json:"token"`
}{}
err := c.doRequest("POST", "/v1/auth", map[string]string{
"username": username,
"password": password,
}, &output)
c.Token = output.Token
return err
}
type CacheItem struct {
Paths []CacheItemPath `json:"paths"`
CommonName string `json:"common_name"`
NotAfter int64 `json:"not_after"`
}
type CacheItemPath struct {
Backend string `json:"backend"`
Location string `json:"location"`
}
type CacheItems []CacheItem
type CacheItemFilter struct {
Beyond *time.Duration
Within *time.Duration
}
//Filter only works if the given CacheItems is sorted by NotAfter
func (c CacheItems) Filter(filter CacheItemFilter) CacheItems {
ret := make(CacheItems, 0, len(c))
for _, v := range c {
ret = append(ret, v)
}
if filter.Beyond != nil {
cutoff := time.Now().Add(*filter.Beyond)
for i, v := range ret {
if cutoff.Before(time.Unix(v.NotAfter, 0)) {
ret = ret[i:]
break
}
}
}
if filter.Within != nil {
cutoff := time.Now().Add(*filter.Within)
for i, v := range ret {
if cutoff.Before(time.Unix(v.NotAfter, 0)) {
ret = ret[:i]
break
}
}
}
return ret
}
type GetCacheResponse struct {
Content CacheItems `json:"content"`
}
//GetCache gets the cache list
func (c *Client) GetCache() (CacheItems, error) {
resp := GetCacheResponse{}
err := c.doRequest("GET", "/v1/cache", nil, &resp)
return resp.Content, err
}
//RefreshCache makes a request to asynchronously refresh the server cache
func (c *Client) RefreshCache() error {
return c.doRequest("POST", "/v1/cache/refresh", nil, nil)
}
type InfoResponse struct {
Version string `json:"version"`
AuthType auth.AuthType `json:"auth_type"`
}
func (c *Client) Info() (*InfoResponse, error) {
resp := InfoResponse{}
err := c.doRequest("GET", "/v1/info", nil, &resp)
return &resp, err
}
type GetSchedulerResponse struct {
Running []GetSchedulerTask `json:"running"`
Pending []GetSchedulerTask `json:"pending"`
Workers []GetSchedulerWorker `json:"workers"`
}
type GetSchedulerTask struct {
At int64 `json:"at"`
Backend string `json:"backend"`
Reason string `json:"reason"`
Kind string `json:"kind"`
ID uint `json:"id"`
State string `json:"state"`
WorkerID int `json:"worker_id"`
}
type GetSchedulerWorker struct {
ID uint `json:"id"`
State string `json:"state"`
StateAt int64 `json:"state_at"`
}
func (c *Client) GetSchedulerState() (*GetSchedulerResponse, error) {
resp := GetSchedulerResponse{}
err := c.doRequest("GET", "/v1/scheduler", nil, &resp)
return &resp, err
}