forked from michaelklishin/rabbit-hole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shovels.go
217 lines (180 loc) · 6.48 KB
/
shovels.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
package rabbithole
import (
"encoding/json"
"net/http"
"net/url"
"strconv"
)
// ShovelInfo contains the configuration of a dynamic Shovel
type ShovelInfo struct {
// Shovel name
Name string `json:"name"`
// Virtual host this Shovel belongs to
Vhost string `json:"vhost"`
// Runtime component of the Shovel
Component string `json:"component"`
// Details the configuration values of the Shovel
Definition ShovelDefinition `json:"value"`
}
// ShovelStatus represents the status of a Shovel
type ShovelStatus struct {
// Shovel name
Name string `json:"name"`
// Virtual host this shovel belongs to
Vhost string `json:"vhost"`
// Type of this shovel
Type string `json:"type"`
// State of this shovel
State string `json:"state"`
// Timestamp is the moment when this Shovel last reported its state change (e.g. was started)
Timestamp string `json:"timestamp"`
}
// DeleteAfter after can hold a delete-after value which may be a string (eg. "never") or an integer
type DeleteAfter string
// MarshalJSON can marshal a string or an integer
func (d DeleteAfter) MarshalJSON() ([]byte, error) {
deleteAfterInt, err := strconv.Atoi(string(d))
if err != nil {
return json.Marshal(string(d))
}
return json.Marshal(deleteAfterInt)
}
// UnmarshalJSON can unmarshal a string or an integer
func (d *DeleteAfter) UnmarshalJSON(b []byte) error {
// delete-after is a string, such as "never"
if b[0] == '"' {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
*d = DeleteAfter(s)
return nil
}
// delete-after is a number
var i int
if err := json.Unmarshal(b, &i); err != nil {
return err
}
*d = DeleteAfter(strconv.Itoa(i))
return nil
}
// ShovelDefinition contains the details of the shovel configuration
type ShovelDefinition struct {
DestinationURI URISet `json:"dest-uri"`
SourceURI URISet `json:"src-uri"`
AckMode string `json:"ack-mode,omitempty"`
AddForwardHeaders bool `json:"add-forward-headers,omitempty"`
DeleteAfter DeleteAfter `json:"delete-after,omitempty"`
DestinationAddForwardHeaders bool `json:"dest-add-forward-headers,omitempty"`
DestinationAddTimestampHeader bool `json:"dest-add-timestamp-header,omitempty"`
DestinationAddress string `json:"dest-address,omitempty"`
DestinationApplicationProperties string `json:"dest-application-properties,omitempty"`
DestinationExchange string `json:"dest-exchange,omitempty"`
DestinationExchangeKey string `json:"dest-exchange-key,omitempty"`
DestinationProperties string `json:"dest-properties,omitempty"`
DestinationProtocol string `json:"dest-protocol,omitempty"`
DestinationPublishProperties map[string]interface{} `json:"dest-publish-properties,omitempty"`
DestinationQueue string `json:"dest-queue,omitempty"`
PrefetchCount int `json:"prefetch-count,omitempty"`
ReconnectDelay int `json:"reconnect-delay,omitempty"`
SourceAddress string `json:"src-address,omitempty"`
SourceDeleteAfter DeleteAfter `json:"src-delete-after,omitempty"`
SourceExchange string `json:"src-exchange,omitempty"`
SourceExchangeKey string `json:"src-exchange-key,omitempty"`
SourcePrefetchCount int `json:"src-prefetch-count,omitempty"`
SourceProtocol string `json:"src-protocol,omitempty"`
SourceQueue string `json:"src-queue,omitempty"`
}
// ShovelDefinitionDTO provides a data transfer object
type ShovelDefinitionDTO struct {
Definition ShovelDefinition `json:"value"`
}
//
// GET /api/parameters/shovel
//
// ListShovels returns all shovels
func (c *Client) ListShovels() (rec []ShovelInfo, err error) {
req, err := newGETRequest(c, "parameters/shovel")
if err != nil {
return []ShovelInfo{}, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return []ShovelInfo{}, err
}
return rec, nil
}
//
// GET /api/parameters/shovel/{vhost}
//
// ListShovelsIn returns all shovels in a vhost
func (c *Client) ListShovelsIn(vhost string) (rec []ShovelInfo, err error) {
req, err := newGETRequest(c, "parameters/shovel/"+url.PathEscape(vhost))
if err != nil {
return []ShovelInfo{}, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return []ShovelInfo{}, err
}
return rec, nil
}
//
// GET /api/parameters/shovel/{vhost}/{name}
//
// GetShovel returns a shovel configuration
func (c *Client) GetShovel(vhost, shovel string) (rec *ShovelInfo, err error) {
req, err := newGETRequest(c, "parameters/shovel/"+url.PathEscape(vhost)+"/"+url.PathEscape(shovel))
if err != nil {
return nil, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return nil, err
}
return rec, nil
}
//
// PUT /api/parameters/shovel/{vhost}/{name}
//
// DeclareShovel creates a shovel
func (c *Client) DeclareShovel(vhost, shovel string, info ShovelDefinition) (res *http.Response, err error) {
shovelDTO := ShovelDefinitionDTO{Definition: info}
body, err := json.Marshal(shovelDTO)
if err != nil {
return nil, err
}
req, err := newRequestWithBody(c, "PUT", "parameters/shovel/"+url.PathEscape(vhost)+"/"+url.PathEscape(shovel), body)
if err != nil {
return nil, err
}
if res, err = executeRequest(c, req); err != nil {
return nil, err
}
return res, nil
}
//
// DELETE /api/parameters/shovel/{vhost}/{name}
//
// DeleteShovel a shovel
func (c *Client) DeleteShovel(vhost, shovel string) (res *http.Response, err error) {
req, err := newRequestWithBody(c, "DELETE", "parameters/shovel/"+url.PathEscape(vhost)+"/"+url.PathEscape(shovel), nil)
if err != nil {
return nil, err
}
if res, err = executeRequest(c, req); err != nil {
return nil, err
}
return res, nil
}
//
// GET /api/shovels/{vhost}
//
// ListShovelStatus returns status of all shovels in a vhost
func (c *Client) ListShovelStatus(vhost string) (rec []ShovelStatus, err error) {
req, err := newGETRequest(c, "shovels/"+url.PathEscape(vhost))
if err != nil {
return []ShovelStatus{}, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return []ShovelStatus{}, err
}
return rec, nil
}