-
Notifications
You must be signed in to change notification settings - Fork 14
/
rest_client_logging_cache.go
123 lines (99 loc) · 3.4 KB
/
rest_client_logging_cache.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
package remote
import (
"context"
"net/http"
"time"
"github.com/evergreen-ci/gimlet"
"github.com/mongodb/jasper/options"
"github.com/pkg/errors"
)
// restLoggingCache is the client-side representation of a jasper.LoggingCache
// for making requests to the remote REST service.
type restLoggingCache struct {
client *restClient
ctx context.Context
}
func (lc *restLoggingCache) Create(id string, opts *options.Output) (*options.CachedLogger, error) {
body, err := makeBody(opts)
if err != nil {
return nil, errors.Wrap(err, "building request")
}
resp, err := lc.client.doRequest(lc.ctx, http.MethodPost, lc.client.getURL("/logging/id/%s", id), body)
if err != nil {
return nil, errors.Wrap(err, "making request")
}
defer resp.Body.Close()
if err = handleError(resp); err != nil {
return nil, errors.WithStack(err)
}
out := &options.CachedLogger{}
if err = gimlet.GetJSON(resp.Body, out); err != nil {
return nil, errors.Wrap(err, "getting cached logger info from response")
}
return out, nil
}
func (lc *restLoggingCache) Put(id string, cl *options.CachedLogger) error {
return errors.New("operation not supported for remote managers")
}
func (lc *restLoggingCache) Get(id string) (*options.CachedLogger, error) {
resp, err := lc.client.doRequest(lc.ctx, http.MethodGet, lc.client.getURL("/logging/id/%s", id), nil)
if err != nil {
return nil, errors.Wrap(err, "making request")
}
defer resp.Body.Close()
if err = handleError(resp); err != nil {
return nil, errors.WithStack(err)
}
out := &options.CachedLogger{}
if err = gimlet.GetJSON(resp.Body, out); err != nil {
return nil, errors.Wrap(err, "getting cached logger info from response")
}
return out, nil
}
func (lc *restLoggingCache) Remove(id string) error {
resp, err := lc.client.doRequest(lc.ctx, http.MethodDelete, lc.client.getURL("/logging/id/%s", id), nil)
if err != nil {
return errors.Wrap(err, "making request")
}
defer resp.Body.Close()
return errors.WithStack(handleError(resp))
}
func (lc *restLoggingCache) CloseAndRemove(ctx context.Context, id string) error {
resp, err := lc.client.doRequest(ctx, http.MethodDelete, lc.client.getURL("/logging/id/%s/close", id), nil)
if err != nil {
return errors.Wrap(err, "making request")
}
defer resp.Body.Close()
return errors.WithStack(handleError(resp))
}
func (lc *restLoggingCache) Clear(ctx context.Context) error {
resp, err := lc.client.doRequest(ctx, http.MethodDelete, lc.client.getURL("/logging/clear"), nil)
if err != nil {
return errors.Wrap(err, "making request")
}
defer resp.Body.Close()
return errors.WithStack(handleError(resp))
}
func (lc *restLoggingCache) Prune(ts time.Time) error {
resp, err := lc.client.doRequest(lc.ctx, http.MethodDelete, lc.client.getURL("/logging/prune/%s", ts.Format(time.RFC3339)), nil)
if err != nil {
return errors.Wrap(err, "making request")
}
defer resp.Body.Close()
return errors.WithStack(handleError(resp))
}
func (lc *restLoggingCache) Len() (int, error) {
resp, err := lc.client.doRequest(lc.ctx, http.MethodGet, lc.client.getURL("/logging/len"), nil)
if err != nil {
return -1, errors.Wrap(err, "making request")
}
defer resp.Body.Close()
if err := handleError(resp); err != nil {
return -1, errors.WithStack(err)
}
out := restLoggingCacheLen{}
if err = gimlet.GetJSON(resp.Body, &out); err != nil {
return -1, errors.Wrap(err, "getting logging cache length from response")
}
return out.Len, nil
}