forked from cloudfoundry-attic/receptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
246 lines (198 loc) · 7.53 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
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
package receptor
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"github.com/cloudfoundry-incubator/cf_http"
"github.com/tedsuo/rata"
"github.com/vito/go-sse/sse"
)
var ErrReadFromClosedSource = errors.New("read from closed source")
var ErrSendToClosedSource = errors.New("send to closed source")
var ErrSourceAlreadyClosed = errors.New("source already closed")
var ErrSlowConsumer = errors.New("slow consumer")
var ErrSubscribedToClosedHub = errors.New("subscribed to closed hub")
var ErrHubAlreadyClosed = errors.New("hub already closed")
//go:generate counterfeiter -o fake_receptor/fake_client.go . Client
type Client interface {
CreateTask(TaskCreateRequest) error
Tasks() ([]TaskResponse, error)
TasksByDomain(domain string) ([]TaskResponse, error)
GetTask(taskId string) (TaskResponse, error)
DeleteTask(taskId string) error
CancelTask(taskId string) error
CreateDesiredLRP(DesiredLRPCreateRequest) error
GetDesiredLRP(processGuid string) (DesiredLRPResponse, error)
UpdateDesiredLRP(processGuid string, update DesiredLRPUpdateRequest) error
DeleteDesiredLRP(processGuid string) error
DesiredLRPs() ([]DesiredLRPResponse, error)
DesiredLRPsByDomain(domain string) ([]DesiredLRPResponse, error)
ActualLRPs() ([]ActualLRPResponse, error)
ActualLRPsByDomain(domain string) ([]ActualLRPResponse, error)
ActualLRPsByProcessGuid(processGuid string) ([]ActualLRPResponse, error)
ActualLRPByProcessGuidAndIndex(processGuid string, index int) (ActualLRPResponse, error)
KillActualLRPByProcessGuidAndIndex(processGuid string, index int) error
SubscribeToEvents() (EventSource, error)
Cells() ([]CellResponse, error)
UpsertDomain(domain string, ttl time.Duration) error
Domains() ([]string, error)
}
func NewClient(url string) Client {
return &client{
httpClient: cf_http.NewClient(),
streamingHTTPClient: cf_http.NewStreamingClient(),
reqGen: rata.NewRequestGenerator(url, Routes),
}
}
type client struct {
httpClient *http.Client
streamingHTTPClient *http.Client
reqGen *rata.RequestGenerator
}
func (c *client) CreateTask(request TaskCreateRequest) error {
return c.doRequest(CreateTaskRoute, nil, nil, request, nil)
}
func (c *client) Tasks() ([]TaskResponse, error) {
tasks := []TaskResponse{}
err := c.doRequest(TasksRoute, nil, nil, nil, &tasks)
return tasks, err
}
func (c *client) TasksByDomain(domain string) ([]TaskResponse, error) {
tasks := []TaskResponse{}
err := c.doRequest(TasksRoute, nil, url.Values{"domain": []string{domain}}, nil, &tasks)
return tasks, err
}
func (c *client) GetTask(taskId string) (TaskResponse, error) {
task := TaskResponse{}
err := c.doRequest(GetTaskRoute, rata.Params{"task_guid": taskId}, nil, nil, &task)
return task, err
}
func (c *client) DeleteTask(taskId string) error {
return c.doRequest(DeleteTaskRoute, rata.Params{"task_guid": taskId}, nil, nil, nil)
}
func (c *client) CancelTask(taskId string) error {
return c.doRequest(CancelTaskRoute, rata.Params{"task_guid": taskId}, nil, nil, nil)
}
func (c *client) CreateDesiredLRP(req DesiredLRPCreateRequest) error {
return c.doRequest(CreateDesiredLRPRoute, nil, nil, req, nil)
}
func (c *client) GetDesiredLRP(processGuid string) (DesiredLRPResponse, error) {
var desiredLRP DesiredLRPResponse
err := c.doRequest(GetDesiredLRPRoute, rata.Params{"process_guid": processGuid}, nil, nil, &desiredLRP)
return desiredLRP, err
}
func (c *client) UpdateDesiredLRP(processGuid string, req DesiredLRPUpdateRequest) error {
return c.doRequest(UpdateDesiredLRPRoute, rata.Params{"process_guid": processGuid}, nil, req, nil)
}
func (c *client) DeleteDesiredLRP(processGuid string) error {
return c.doRequest(DeleteDesiredLRPRoute, rata.Params{"process_guid": processGuid}, nil, nil, nil)
}
func (c *client) DesiredLRPs() ([]DesiredLRPResponse, error) {
var desiredLRPs []DesiredLRPResponse
err := c.doRequest(DesiredLRPsRoute, nil, nil, nil, &desiredLRPs)
return desiredLRPs, err
}
func (c *client) DesiredLRPsByDomain(domain string) ([]DesiredLRPResponse, error) {
var desiredLRPs []DesiredLRPResponse
err := c.doRequest(DesiredLRPsRoute, nil, url.Values{"domain": []string{domain}}, nil, &desiredLRPs)
return desiredLRPs, err
}
func (c *client) ActualLRPs() ([]ActualLRPResponse, error) {
var actualLRPs []ActualLRPResponse
err := c.doRequest(ActualLRPsRoute, nil, nil, nil, &actualLRPs)
return actualLRPs, err
}
func (c *client) ActualLRPsByDomain(domain string) ([]ActualLRPResponse, error) {
var actualLRPs []ActualLRPResponse
err := c.doRequest(ActualLRPsRoute, nil, url.Values{"domain": []string{domain}}, nil, &actualLRPs)
return actualLRPs, err
}
func (c *client) ActualLRPsByProcessGuid(processGuid string) ([]ActualLRPResponse, error) {
var actualLRPs []ActualLRPResponse
err := c.doRequest(ActualLRPsByProcessGuidRoute, rata.Params{"process_guid": processGuid}, nil, nil, &actualLRPs)
return actualLRPs, err
}
func (c *client) ActualLRPByProcessGuidAndIndex(processGuid string, index int) (ActualLRPResponse, error) {
var actualLRP ActualLRPResponse
err := c.doRequest(ActualLRPByProcessGuidAndIndexRoute, rata.Params{"process_guid": processGuid, "index": strconv.Itoa(index)}, nil, nil, &actualLRP)
return actualLRP, err
}
func (c *client) KillActualLRPByProcessGuidAndIndex(processGuid string, index int) error {
err := c.doRequest(KillActualLRPByProcessGuidAndIndexRoute, rata.Params{"process_guid": processGuid, "index": strconv.Itoa(index)}, nil, nil, nil)
return err
}
func (c *client) SubscribeToEvents() (EventSource, error) {
eventSource, err := sse.Connect(c.streamingHTTPClient, time.Second, func() *http.Request {
request, err := c.reqGen.CreateRequest(EventStream, nil, nil)
if err != nil {
panic(err) // totally shouldn't happen
}
return request
})
if err != nil {
return nil, err
}
return NewEventSource(eventSource), nil
}
func (c *client) Cells() ([]CellResponse, error) {
var cells []CellResponse
err := c.doRequest(CellsRoute, nil, nil, nil, &cells)
return cells, err
}
func (c *client) UpsertDomain(domain string, ttl time.Duration) error {
req, err := c.createRequest(UpsertDomainRoute, rata.Params{"domain": domain}, nil, nil)
if err != nil {
return err
}
if ttl != 0 {
req.Header.Set("Cache-Control", fmt.Sprintf("max-age=%d", int(ttl.Seconds())))
}
return c.do(req, nil)
}
func (c *client) Domains() ([]string, error) {
var domains []string
err := c.doRequest(DomainsRoute, nil, nil, nil, &domains)
return domains, err
}
func (c *client) createRequest(requestName string, params rata.Params, queryParams url.Values, request interface{}) (*http.Request, error) {
requestJson, err := json.Marshal(request)
if err != nil {
return nil, err
}
req, err := c.reqGen.CreateRequest(requestName, params, bytes.NewReader(requestJson))
if err != nil {
return nil, err
}
req.URL.RawQuery = queryParams.Encode()
req.ContentLength = int64(len(requestJson))
req.Header.Set("Content-Type", "application/json")
return req, nil
}
func (c *client) doRequest(requestName string, params rata.Params, queryParams url.Values, request, response interface{}) error {
req, err := c.createRequest(requestName, params, queryParams, request)
if err != nil {
return err
}
return c.do(req, response)
}
func (c *client) do(req *http.Request, response interface{}) error {
res, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode > 299 {
errResponse := Error{}
json.NewDecoder(res.Body).Decode(&errResponse)
return errResponse
}
if response != nil {
return json.NewDecoder(res.Body).Decode(response)
}
return nil
}