forked from cloudfoundry/bbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
809 lines (682 loc) · 26.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
package bbs
import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"mime"
"net/http"
"net/url"
"time"
"github.com/cloudfoundry-incubator/bbs/events"
"github.com/cloudfoundry-incubator/bbs/models"
"github.com/cloudfoundry-incubator/cf_http"
"github.com/gogo/protobuf/proto"
"github.com/pivotal-golang/lager"
"github.com/tedsuo/rata"
"github.com/vito/go-sse/sse"
)
const (
ContentTypeHeader = "Content-Type"
XCfRouterErrorHeader = "X-Cf-Routererror"
ProtoContentType = "application/x-protobuf"
KeepContainer = true
DeleteContainer = false
)
//go:generate counterfeiter -o fake_bbs/fake_internal_client.go . InternalClient
//go:generate counterfeiter -o fake_bbs/fake_client.go . Client
/*
The InternalClient interface exposes all available endpoints of the BBS server,
including private endpoints which should be used exclusively by internal Diego
components. To interact with the BBS from outside of Diego, the Client
should be used instead.
*/
type InternalClient interface {
Client
ExperimentalExternalEventClient
ClaimActualLRP(logger lager.Logger, processGuid string, index int, instanceKey *models.ActualLRPInstanceKey) error
StartActualLRP(logger lager.Logger, key *models.ActualLRPKey, instanceKey *models.ActualLRPInstanceKey, netInfo *models.ActualLRPNetInfo) error
CrashActualLRP(logger lager.Logger, key *models.ActualLRPKey, instanceKey *models.ActualLRPInstanceKey, errorMessage string) error
FailActualLRP(logger lager.Logger, key *models.ActualLRPKey, errorMessage string) error
RemoveActualLRP(logger lager.Logger, processGuid string, index int, instanceKey *models.ActualLRPInstanceKey) error
EvacuateClaimedActualLRP(lager.Logger, *models.ActualLRPKey, *models.ActualLRPInstanceKey) (bool, error)
EvacuateRunningActualLRP(lager.Logger, *models.ActualLRPKey, *models.ActualLRPInstanceKey, *models.ActualLRPNetInfo, uint64) (bool, error)
EvacuateStoppedActualLRP(lager.Logger, *models.ActualLRPKey, *models.ActualLRPInstanceKey) (bool, error)
EvacuateCrashedActualLRP(lager.Logger, *models.ActualLRPKey, *models.ActualLRPInstanceKey, string) (bool, error)
RemoveEvacuatingActualLRP(lager.Logger, *models.ActualLRPKey, *models.ActualLRPInstanceKey) error
ConvergeLRPs(logger lager.Logger) error
ConvergeTasks(logger lager.Logger, kickTaskDuration, expirePendingTaskDuration, expireCompletedTaskDuration time.Duration) error
StartTask(logger lager.Logger, taskGuid string, cellID string) (bool, error)
FailTask(logger lager.Logger, taskGuid, failureReason string) error
CompleteTask(logger lager.Logger, taskGuid, cellId string, failed bool, failureReason, result string) error
}
/*
The External InternalClient can be used to access the BBS's public functionality.
It exposes methods for basic LRP and Task Lifecycles, Domain manipulation, and
event subscription.
*/
type Client interface {
ExternalTaskClient
ExternalDomainClient
ExternalActualLRPClient
ExternalDesiredLRPClient
ExternalEventClient
// Returns true if the BBS server is reachable
Ping(logger lager.Logger) bool
// Lists all Cells
Cells(logger lager.Logger) ([]*models.CellPresence, error)
}
/*
The ExternalTaskClient is used to access Diego's ability to run one-off tasks.
More information about this API can be found in the bbs docs:
https://github.com/cloudfoundry-incubator/bbs/tree/master/doc/tasks.md
*/
type ExternalTaskClient interface {
// Creates a Task from the given TaskDefinition
DesireTask(logger lager.Logger, guid, domain string, def *models.TaskDefinition) error
// Lists all Tasks
Tasks(logger lager.Logger) ([]*models.Task, error)
// Lists all Tasks of the given domain
TasksByDomain(logger lager.Logger, domain string) ([]*models.Task, error)
// Lists all Tasks on the given cell
TasksByCellID(logger lager.Logger, cellId string) ([]*models.Task, error)
// Returns the Task with the given guid
TaskByGuid(logger lager.Logger, guid string) (*models.Task, error)
// Cancels the Task with the given task guid
CancelTask(logger lager.Logger, taskGuid string) error
// Resolves a Task with the given guid
ResolvingTask(logger lager.Logger, taskGuid string) error
// Deletes a completed task with the given guid
DeleteTask(logger lager.Logger, taskGuid string) error
}
/*
The ExternalDomainClient is used to access and update Diego's domains.
*/
type ExternalDomainClient interface {
// Lists the active domains
Domains(logger lager.Logger) ([]string, error)
// Creates a domain or bumps the ttl on an existing domain
UpsertDomain(logger lager.Logger, domain string, ttl time.Duration) error
}
/*
The ExternalActualLRPClient is used to access and retire Actual LRPs
*/
type ExternalActualLRPClient interface {
// Returns all ActualLRPGroups matching the given ActualLRPFilter
ActualLRPGroups(lager.Logger, models.ActualLRPFilter) ([]*models.ActualLRPGroup, error)
// Returns all ActualLRPGroups that have the given process guid
ActualLRPGroupsByProcessGuid(logger lager.Logger, processGuid string) ([]*models.ActualLRPGroup, error)
// Returns the ActualLRPGroup with the given process guid and instance index
ActualLRPGroupByProcessGuidAndIndex(logger lager.Logger, processGuid string, index int) (*models.ActualLRPGroup, error)
// Shuts down the ActualLRP matching the given ActualLRPKey, but does not modify the desired state
RetireActualLRP(logger lager.Logger, key *models.ActualLRPKey) error
}
/*
The ExternalDesiredLRPClient is used to access and manipulate Disired LRPs.
*/
type ExternalDesiredLRPClient interface {
// Lists all DesiredLRPs that match the given DesiredLRPFilter
DesiredLRPs(lager.Logger, models.DesiredLRPFilter) ([]*models.DesiredLRP, error)
// Returns the DesiredLRP with the given process guid
DesiredLRPByProcessGuid(logger lager.Logger, processGuid string) (*models.DesiredLRP, error)
// Returns all DesiredLRPSchedulingInfos that match the given DesiredLRPFilter
DesiredLRPSchedulingInfos(lager.Logger, models.DesiredLRPFilter) ([]*models.DesiredLRPSchedulingInfo, error)
// Creates the given DesiredLRP and its corresponding ActualLRPs
DesireLRP(lager.Logger, *models.DesiredLRP) error
// Updates the DesiredLRP matching the given process guid
UpdateDesiredLRP(logger lager.Logger, processGuid string, update *models.DesiredLRPUpdate) error
// Removes the DesiredLRP matching the given process guid
RemoveDesiredLRP(logger lager.Logger, processGuid string) error
}
/*
The ExternalEventClient is used to subscribe to groups of Events.
*/
type ExternalEventClient interface {
SubscribeToEvents(logger lager.Logger) (events.EventSource, error)
}
/*
** EXPERIMENTAL **
The ExperimentalExternalEventClient is used to subscribe to groups of Events.
*/
type ExperimentalExternalEventClient interface {
// Returns an EventSource for watching changes to DesiredLRPs
SubscribeToDesiredLRPEvents(logger lager.Logger) (events.EventSource, error)
// Returns an EventSource for watching changes to ActualLRPs
SubscribeToActualLRPEvents(logger lager.Logger) (events.EventSource, error)
}
func newClient(url string) *client {
return &client{
httpClient: cf_http.NewClient(),
streamingHTTPClient: cf_http.NewStreamingClient(),
reqGen: rata.NewRequestGenerator(url, Routes),
}
}
func NewClient(url string) InternalClient {
return newClient(url)
}
func NewSecureClient(url, caFile, certFile, keyFile string, clientSessionCacheSize, maxIdleConnsPerHost int) (InternalClient, error) {
return newSecureClient(url, caFile, certFile, keyFile, clientSessionCacheSize, maxIdleConnsPerHost, false)
}
func NewSecureSkipVerifyClient(url, certFile, keyFile string, clientSessionCacheSize, maxIdleConnsPerHost int) (InternalClient, error) {
return newSecureClient(url, "", certFile, keyFile, clientSessionCacheSize, maxIdleConnsPerHost, true)
}
func newSecureClient(url, caFile, certFile, keyFile string, clientSessionCacheSize, maxIdleConnsPerHost int, skipVerify bool) (InternalClient, error) {
client := newClient(url)
tlsConfig, err := cf_http.NewTLSConfig(certFile, keyFile, caFile)
if err != nil {
return nil, err
}
tlsConfig.ClientSessionCache = tls.NewLRUClientSessionCache(clientSessionCacheSize)
tlsConfig.InsecureSkipVerify = skipVerify
if tr, ok := client.httpClient.Transport.(*http.Transport); ok {
tr.TLSClientConfig = tlsConfig
tr.MaxIdleConnsPerHost = maxIdleConnsPerHost
} else {
return nil, errors.New("Invalid transport")
}
if tr, ok := client.streamingHTTPClient.Transport.(*http.Transport); ok {
tr.TLSClientConfig = tlsConfig
tr.MaxIdleConnsPerHost = maxIdleConnsPerHost
} else {
return nil, errors.New("Invalid transport")
}
return client, nil
}
type client struct {
httpClient *http.Client
streamingHTTPClient *http.Client
reqGen *rata.RequestGenerator
}
func (c *client) Ping(logger lager.Logger) bool {
response := models.PingResponse{}
err := c.doRequest(logger, PingRoute, nil, nil, nil, &response)
if err != nil {
return false
}
return response.Available
}
func (c *client) Domains(logger lager.Logger) ([]string, error) {
response := models.DomainsResponse{}
err := c.doRequest(logger, DomainsRoute, nil, nil, nil, &response)
if err != nil {
return nil, err
}
return response.Domains, response.Error.ToError()
}
func (c *client) UpsertDomain(logger lager.Logger, domain string, ttl time.Duration) error {
request := models.UpsertDomainRequest{
Domain: domain,
Ttl: uint32(ttl.Seconds()),
}
response := models.UpsertDomainResponse{}
err := c.doRequest(logger, UpsertDomainRoute, nil, nil, &request, &response)
if err != nil {
return err
}
return response.Error.ToError()
}
func (c *client) ActualLRPGroups(logger lager.Logger, filter models.ActualLRPFilter) ([]*models.ActualLRPGroup, error) {
request := models.ActualLRPGroupsRequest{
Domain: filter.Domain,
CellId: filter.CellID,
}
response := models.ActualLRPGroupsResponse{}
err := c.doRequest(logger, ActualLRPGroupsRoute, nil, nil, &request, &response)
if err != nil {
return nil, err
}
return response.ActualLrpGroups, response.Error.ToError()
}
func (c *client) ActualLRPGroupsByProcessGuid(logger lager.Logger, processGuid string) ([]*models.ActualLRPGroup, error) {
request := models.ActualLRPGroupsByProcessGuidRequest{
ProcessGuid: processGuid,
}
response := models.ActualLRPGroupsResponse{}
err := c.doRequest(logger, ActualLRPGroupsByProcessGuidRoute, nil, nil, &request, &response)
if err != nil {
return nil, err
}
return response.ActualLrpGroups, response.Error.ToError()
}
func (c *client) ActualLRPGroupByProcessGuidAndIndex(logger lager.Logger, processGuid string, index int) (*models.ActualLRPGroup, error) {
request := models.ActualLRPGroupByProcessGuidAndIndexRequest{
ProcessGuid: processGuid,
Index: int32(index),
}
response := models.ActualLRPGroupResponse{}
err := c.doRequest(logger, ActualLRPGroupByProcessGuidAndIndexRoute, nil, nil, &request, &response)
if err != nil {
return nil, err
}
return response.ActualLrpGroup, response.Error.ToError()
}
func (c *client) ClaimActualLRP(logger lager.Logger, processGuid string, index int, instanceKey *models.ActualLRPInstanceKey) error {
request := models.ClaimActualLRPRequest{
ProcessGuid: processGuid,
Index: int32(index),
ActualLrpInstanceKey: instanceKey,
}
response := models.ActualLRPLifecycleResponse{}
err := c.doRequest(logger, ClaimActualLRPRoute, nil, nil, &request, &response)
if err != nil {
return err
}
return response.Error.ToError()
}
func (c *client) StartActualLRP(logger lager.Logger, key *models.ActualLRPKey, instanceKey *models.ActualLRPInstanceKey, netInfo *models.ActualLRPNetInfo) error {
request := models.StartActualLRPRequest{
ActualLrpKey: key,
ActualLrpInstanceKey: instanceKey,
ActualLrpNetInfo: netInfo,
}
response := models.ActualLRPLifecycleResponse{}
err := c.doRequest(logger, StartActualLRPRoute, nil, nil, &request, &response)
if err != nil {
return err
}
return response.Error.ToError()
}
func (c *client) CrashActualLRP(logger lager.Logger, key *models.ActualLRPKey, instanceKey *models.ActualLRPInstanceKey, errorMessage string) error {
request := models.CrashActualLRPRequest{
ActualLrpKey: key,
ActualLrpInstanceKey: instanceKey,
ErrorMessage: errorMessage,
}
response := models.ActualLRPLifecycleResponse{}
err := c.doRequest(logger, CrashActualLRPRoute, nil, nil, &request, &response)
if err != nil {
return err
}
return response.Error.ToError()
}
func (c *client) FailActualLRP(logger lager.Logger, key *models.ActualLRPKey, errorMessage string) error {
request := models.FailActualLRPRequest{
ActualLrpKey: key,
ErrorMessage: errorMessage,
}
response := models.ActualLRPLifecycleResponse{}
err := c.doRequest(logger, FailActualLRPRoute, nil, nil, &request, &response)
if err != nil {
return err
}
return response.Error.ToError()
}
func (c *client) RetireActualLRP(logger lager.Logger, key *models.ActualLRPKey) error {
request := models.RetireActualLRPRequest{
ActualLrpKey: key,
}
response := models.ActualLRPLifecycleResponse{}
err := c.doRequest(logger, RetireActualLRPRoute, nil, nil, &request, &response)
if err != nil {
return err
}
return response.Error.ToError()
}
func (c *client) RemoveActualLRP(logger lager.Logger, processGuid string, index int, instanceKey *models.ActualLRPInstanceKey) error {
request := models.RemoveActualLRPRequest{
ProcessGuid: processGuid,
Index: int32(index),
ActualLrpInstanceKey: instanceKey,
}
response := models.ActualLRPLifecycleResponse{}
err := c.doRequest(logger, RemoveActualLRPRoute, nil, nil, &request, &response)
if err != nil {
return err
}
return response.Error.ToError()
}
func (c *client) EvacuateClaimedActualLRP(logger lager.Logger, key *models.ActualLRPKey, instanceKey *models.ActualLRPInstanceKey) (bool, error) {
return c.doEvacRequest(logger, EvacuateClaimedActualLRPRoute, KeepContainer, &models.EvacuateClaimedActualLRPRequest{
ActualLrpKey: key,
ActualLrpInstanceKey: instanceKey,
})
}
func (c *client) EvacuateCrashedActualLRP(logger lager.Logger, key *models.ActualLRPKey, instanceKey *models.ActualLRPInstanceKey, errorMessage string) (bool, error) {
return c.doEvacRequest(logger, EvacuateCrashedActualLRPRoute, DeleteContainer, &models.EvacuateCrashedActualLRPRequest{
ActualLrpKey: key,
ActualLrpInstanceKey: instanceKey,
ErrorMessage: errorMessage,
})
}
func (c *client) EvacuateStoppedActualLRP(logger lager.Logger, key *models.ActualLRPKey, instanceKey *models.ActualLRPInstanceKey) (bool, error) {
return c.doEvacRequest(logger, EvacuateStoppedActualLRPRoute, DeleteContainer, &models.EvacuateStoppedActualLRPRequest{
ActualLrpKey: key,
ActualLrpInstanceKey: instanceKey,
})
}
func (c *client) EvacuateRunningActualLRP(logger lager.Logger, key *models.ActualLRPKey, instanceKey *models.ActualLRPInstanceKey, netInfo *models.ActualLRPNetInfo, ttl uint64) (bool, error) {
return c.doEvacRequest(logger, EvacuateRunningActualLRPRoute, KeepContainer, &models.EvacuateRunningActualLRPRequest{
ActualLrpKey: key,
ActualLrpInstanceKey: instanceKey,
ActualLrpNetInfo: netInfo,
Ttl: ttl,
})
}
func (c *client) RemoveEvacuatingActualLRP(logger lager.Logger, key *models.ActualLRPKey, instanceKey *models.ActualLRPInstanceKey) error {
request := models.RemoveEvacuatingActualLRPRequest{
ActualLrpKey: key,
ActualLrpInstanceKey: instanceKey,
}
response := models.RemoveEvacuatingActualLRPResponse{}
err := c.doRequest(logger, RemoveEvacuatingActualLRPRoute, nil, nil, &request, &response)
if err != nil {
return err
}
return response.Error.ToError()
}
func (c *client) DesiredLRPs(logger lager.Logger, filter models.DesiredLRPFilter) ([]*models.DesiredLRP, error) {
request := models.DesiredLRPsRequest{
Domain: filter.Domain,
}
response := models.DesiredLRPsResponse{}
err := c.doRequest(logger, DesiredLRPsRoute, nil, nil, &request, &response)
if err != nil {
return nil, err
}
return response.DesiredLrps, response.Error.ToError()
}
func (c *client) DesiredLRPByProcessGuid(logger lager.Logger, processGuid string) (*models.DesiredLRP, error) {
request := models.DesiredLRPByProcessGuidRequest{
ProcessGuid: processGuid,
}
response := models.DesiredLRPResponse{}
err := c.doRequest(logger, DesiredLRPByProcessGuidRoute, nil, nil, &request, &response)
if err != nil {
return nil, err
}
return response.DesiredLrp, response.Error.ToError()
}
func (c *client) DesiredLRPSchedulingInfos(logger lager.Logger, filter models.DesiredLRPFilter) ([]*models.DesiredLRPSchedulingInfo, error) {
request := models.DesiredLRPsRequest{
Domain: filter.Domain,
}
response := models.DesiredLRPSchedulingInfosResponse{}
err := c.doRequest(logger, DesiredLRPSchedulingInfosRoute, nil, nil, &request, &response)
if err != nil {
return nil, err
}
return response.DesiredLrpSchedulingInfos, response.Error.ToError()
}
func (c *client) doDesiredLRPLifecycleRequest(logger lager.Logger, route string, request proto.Message) error {
response := models.DesiredLRPLifecycleResponse{}
err := c.doRequest(logger, route, nil, nil, request, &response)
if err != nil {
return err
}
return response.Error.ToError()
}
func (c *client) DesireLRP(logger lager.Logger, desiredLRP *models.DesiredLRP) error {
request := models.DesireLRPRequest{
DesiredLrp: desiredLRP,
}
return c.doDesiredLRPLifecycleRequest(logger, DesireDesiredLRPRoute, &request)
}
func (c *client) UpdateDesiredLRP(logger lager.Logger, processGuid string, update *models.DesiredLRPUpdate) error {
request := models.UpdateDesiredLRPRequest{
ProcessGuid: processGuid,
Update: update,
}
return c.doDesiredLRPLifecycleRequest(logger, UpdateDesiredLRPRoute, &request)
}
func (c *client) RemoveDesiredLRP(logger lager.Logger, processGuid string) error {
request := models.RemoveDesiredLRPRequest{
ProcessGuid: processGuid,
}
return c.doDesiredLRPLifecycleRequest(logger, RemoveDesiredLRPRoute, &request)
}
func (c *client) ConvergeLRPs(logger lager.Logger) error {
route := ConvergeLRPsRoute
response := models.ConvergeLRPsResponse{}
err := c.doRequest(logger, route, nil, nil, nil, &response)
if err != nil {
return err
}
return response.Error.ToError()
}
func (c *client) Tasks(logger lager.Logger) ([]*models.Task, error) {
request := models.TasksRequest{}
response := models.TasksResponse{}
err := c.doRequest(logger, TasksRoute, nil, nil, &request, &response)
if err != nil {
return nil, err
}
return response.Tasks, response.Error.ToError()
}
func (c *client) TasksByDomain(logger lager.Logger, domain string) ([]*models.Task, error) {
request := models.TasksRequest{
Domain: domain,
}
response := models.TasksResponse{}
err := c.doRequest(logger, TasksRoute, nil, nil, &request, &response)
if err != nil {
return nil, err
}
return response.Tasks, response.Error.ToError()
}
func (c *client) TasksByCellID(logger lager.Logger, cellId string) ([]*models.Task, error) {
request := models.TasksRequest{
CellId: cellId,
}
response := models.TasksResponse{}
err := c.doRequest(logger, TasksRoute, nil, nil, &request, &response)
if err != nil {
return nil, err
}
return response.Tasks, response.Error.ToError()
}
func (c *client) TaskByGuid(logger lager.Logger, taskGuid string) (*models.Task, error) {
request := models.TaskByGuidRequest{
TaskGuid: taskGuid,
}
response := models.TaskResponse{}
err := c.doRequest(logger, TaskByGuidRoute, nil, nil, &request, &response)
if err != nil {
return nil, err
}
return response.Task, response.Error.ToError()
}
func (c *client) doTaskLifecycleRequest(logger lager.Logger, route string, request proto.Message) error {
response := models.TaskLifecycleResponse{}
err := c.doRequest(logger, route, nil, nil, request, &response)
if err != nil {
return err
}
return response.Error.ToError()
}
func (c *client) DesireTask(logger lager.Logger, taskGuid, domain string, taskDef *models.TaskDefinition) error {
route := DesireTaskRoute
request := models.DesireTaskRequest{
TaskGuid: taskGuid,
Domain: domain,
TaskDefinition: taskDef,
}
return c.doTaskLifecycleRequest(logger, route, &request)
}
func (c *client) StartTask(logger lager.Logger, taskGuid string, cellId string) (bool, error) {
request := &models.StartTaskRequest{
TaskGuid: taskGuid,
CellId: cellId,
}
response := &models.StartTaskResponse{}
err := c.doRequest(logger, StartTaskRoute, nil, nil, request, response)
if err != nil {
return false, err
}
return response.ShouldStart, response.Error.ToError()
}
func (c *client) CancelTask(logger lager.Logger, taskGuid string) error {
request := models.TaskGuidRequest{
TaskGuid: taskGuid,
}
route := CancelTaskRoute
return c.doTaskLifecycleRequest(logger, route, &request)
}
func (c *client) ResolvingTask(logger lager.Logger, taskGuid string) error {
request := models.TaskGuidRequest{
TaskGuid: taskGuid,
}
route := ResolvingTaskRoute
return c.doTaskLifecycleRequest(logger, route, &request)
}
func (c *client) DeleteTask(logger lager.Logger, taskGuid string) error {
request := models.TaskGuidRequest{
TaskGuid: taskGuid,
}
route := DeleteTaskRoute
return c.doTaskLifecycleRequest(logger, route, &request)
}
func (c *client) FailTask(logger lager.Logger, taskGuid, failureReason string) error {
request := models.FailTaskRequest{
TaskGuid: taskGuid,
FailureReason: failureReason,
}
route := FailTaskRoute
return c.doTaskLifecycleRequest(logger, route, &request)
}
func (c *client) CompleteTask(logger lager.Logger, taskGuid, cellId string, failed bool, failureReason, result string) error {
request := models.CompleteTaskRequest{
TaskGuid: taskGuid,
CellId: cellId,
Failed: failed,
FailureReason: failureReason,
Result: result,
}
route := CompleteTaskRoute
return c.doTaskLifecycleRequest(logger, route, &request)
}
func (c *client) ConvergeTasks(logger lager.Logger, kickTaskDuration, expirePendingTaskDuration, expireCompletedTaskDuration time.Duration) error {
request := &models.ConvergeTasksRequest{
KickTaskDuration: kickTaskDuration.Nanoseconds(),
ExpirePendingTaskDuration: expirePendingTaskDuration.Nanoseconds(),
ExpireCompletedTaskDuration: expireCompletedTaskDuration.Nanoseconds(),
}
response := models.ConvergeTasksResponse{}
route := ConvergeTasksRoute
err := c.doRequest(logger, route, nil, nil, request, &response)
if err != nil {
return err
}
return response.Error.ToError()
}
func (c *client) subscribeToEvents(route string) (events.EventSource, error) {
eventSource, err := sse.Connect(c.streamingHTTPClient, time.Second, func() *http.Request {
request, err := c.reqGen.CreateRequest(route, nil, nil)
if err != nil {
panic(err) // totally shouldn't happen
}
return request
})
if err != nil {
return nil, err
}
return events.NewEventSource(eventSource), nil
}
func (c *client) SubscribeToEvents(logger lager.Logger) (events.EventSource, error) {
return c.subscribeToEvents(EventStreamRoute_r0)
}
func (c *client) SubscribeToDesiredLRPEvents(logger lager.Logger) (events.EventSource, error) {
return c.subscribeToEvents(DesiredLRPEventStreamRoute)
}
func (c *client) SubscribeToActualLRPEvents(logger lager.Logger) (events.EventSource, error) {
return c.subscribeToEvents(ActualLRPEventStreamRoute)
}
func (c *client) Cells(logger lager.Logger) ([]*models.CellPresence, error) {
response := models.CellsResponse{}
err := c.doRequest(logger, CellsRoute, nil, nil, nil, &response)
if err != nil {
return nil, err
}
return response.Cells, response.Error.ToError()
}
func (c *client) createRequest(requestName string, params rata.Params, queryParams url.Values, message proto.Message) (*http.Request, error) {
var messageBody []byte
var err error
if message != nil {
messageBody, err = proto.Marshal(message)
if err != nil {
return nil, err
}
}
request, err := c.reqGen.CreateRequest(requestName, params, bytes.NewReader(messageBody))
if err != nil {
return nil, err
}
request.URL.RawQuery = queryParams.Encode()
request.ContentLength = int64(len(messageBody))
request.Header.Set("Content-Type", ProtoContentType)
return request, nil
}
func (c *client) doEvacRequest(logger lager.Logger, route string, defaultKeepContainer bool, request proto.Message) (bool, error) {
var response models.EvacuationResponse
err := c.doRequest(logger, route, nil, nil, request, &response)
if err != nil {
return defaultKeepContainer, err
}
return response.KeepContainer, response.Error.ToError()
}
func (c *client) doRequest(logger lager.Logger, requestName string, params rata.Params, queryParams url.Values, requestBody, responseBody proto.Message) error {
logger = logger.Session("do-request")
var err error
var request *http.Request
for attempts := 0; attempts < 3; attempts++ {
logger.Debug("creating-request", lager.Data{"attempt": attempts + 1})
request, err = c.createRequest(requestName, params, queryParams, requestBody)
if err != nil {
logger.Error("failed-creating-request", err)
return err
}
logger.Debug("doing-request", lager.Data{"attempt": attempts + 1})
err = c.do(request, responseBody)
if err != nil {
logger.Error("failed-doing-request", err)
time.Sleep(500 * time.Millisecond)
} else {
logger.Debug("complete")
break
}
}
return err
}
func (c *client) do(request *http.Request, responseObject proto.Message) error {
response, err := c.httpClient.Do(request)
if err != nil {
return err
}
defer func() {
// don't worry about errors when closing the body
_ = response.Body.Close()
}()
var parsedContentType string
if contentType, ok := response.Header[ContentTypeHeader]; ok {
parsedContentType, _, _ = mime.ParseMediaType(contentType[0])
}
if routerError, ok := response.Header[XCfRouterErrorHeader]; ok {
return models.NewError(models.Error_RouterError, routerError[0])
}
if parsedContentType == ProtoContentType {
return handleProtoResponse(response, responseObject)
} else {
return handleNonProtoResponse(response)
}
}
func handleProtoResponse(response *http.Response, responseObject proto.Message) error {
if responseObject == nil {
return models.NewError(models.Error_InvalidRequest, "responseObject cannot be nil")
}
buf, err := ioutil.ReadAll(response.Body)
if err != nil {
return models.NewError(models.Error_InvalidResponse, fmt.Sprint("failed to read body: ", err.Error()))
}
err = proto.Unmarshal(buf, responseObject)
if err != nil {
return models.NewError(models.Error_InvalidProtobufMessage, fmt.Sprintf("failed to unmarshal proto", err.Error()))
}
return nil
}
func handleNonProtoResponse(response *http.Response) error {
if response.StatusCode > 299 {
return models.NewError(models.Error_InvalidResponse, fmt.Sprintf("Invalid Response with status code: %d", response.StatusCode))
}
return nil
}