-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathAgentConsulClient.java
More file actions
330 lines (262 loc) · 10.6 KB
/
AgentConsulClient.java
File metadata and controls
330 lines (262 loc) · 10.6 KB
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
package com.ecwid.consul.v1.agent;
import com.ecwid.consul.SingleUrlParameters;
import com.ecwid.consul.UrlParameters;
import com.ecwid.consul.json.GsonFactory;
import com.ecwid.consul.transport.HttpResponse;
import com.ecwid.consul.transport.TLSConfig;
import com.ecwid.consul.v1.ConsulRawClient;
import com.ecwid.consul.v1.OperationException;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.agent.model.*;
import com.google.gson.reflect.TypeToken;
import java.util.List;
import java.util.Map;
/**
* @author Vasily Vasilkov (vgv@ecwid.com)
*/
public final class AgentConsulClient implements AgentClient {
private final ConsulRawClient rawClient;
public AgentConsulClient(ConsulRawClient rawClient) {
this.rawClient = rawClient;
}
public AgentConsulClient() {
this(new ConsulRawClient());
}
public AgentConsulClient(TLSConfig tlsConfig) {
this(new ConsulRawClient(tlsConfig));
}
public AgentConsulClient(String agentHost) {
this(new ConsulRawClient(agentHost));
}
public AgentConsulClient(String agentHost, TLSConfig tlsConfig) {
this(new ConsulRawClient(agentHost, tlsConfig));
}
public AgentConsulClient(String agentHost, int agentPort) {
this(new ConsulRawClient(agentHost, agentPort));
}
public AgentConsulClient(String agentHost, int agentPort, TLSConfig tlsConfig) {
this(new ConsulRawClient(agentHost, agentPort, tlsConfig));
}
@Override
public Response<Map<String, Check>> getAgentChecks() {
HttpResponse httpResponse = rawClient.makeGetRequest("/v1/agent/checks");
if (httpResponse.getStatusCode() == 200) {
Map<String, Check> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<Map<String, Check>>() {
}.getType());
return new Response<Map<String, Check>>(value, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response<Map<String, Service>> getAgentServices() {
HttpResponse httpResponse = rawClient.makeGetRequest("/v1/agent/services");
if (httpResponse.getStatusCode() == 200) {
Map<String, Service> agentServices = GsonFactory.getGson().fromJson(httpResponse.getContent(),
new TypeToken<Map<String, Service>>() {
}.getType());
return new Response<Map<String, Service>>(agentServices, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response<List<Member>> getAgentMembers() {
HttpResponse httpResponse = rawClient.makeGetRequest("/v1/agent/members");
if (httpResponse.getStatusCode() == 200) {
List<Member> members = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Member>>() {
}.getType());
return new Response<List<Member>>(members, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response<Self> getAgentSelf() {
return getAgentSelf(null);
}
@Override
public Response<Self> getAgentSelf(String token) {
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makeGetRequest("/v1/agent/self", tokenParam);
if (httpResponse.getStatusCode() == 200) {
Self self = GsonFactory.getGson().fromJson(httpResponse.getContent(), Self.class);
return new Response<Self>(self, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response<Void> agentSetMaintenance(boolean maintenanceEnabled) {
return agentSetMaintenance(maintenanceEnabled, null);
}
@Override
public Response<Void> agentSetMaintenance(boolean maintenanceEnabled, String reason) {
UrlParameters maintenanceParameter = new SingleUrlParameters("enable", Boolean.toString(maintenanceEnabled));
UrlParameters reasonParameter = reason != null ? new SingleUrlParameters("reason", reason) : null;
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/maintenance", "", maintenanceParameter, reasonParameter);
if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response<Void> agentJoin(String address, boolean wan) {
UrlParameters wanParams = wan ? new SingleUrlParameters("wan", "1") : null;
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/join/" + address, "", wanParams);
if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response<Void> agentForceLeave(String node) {
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/force-leave/" + node, "");
if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response<Void> agentCheckRegister(NewCheck newCheck) {
return agentCheckRegister(newCheck, null);
}
@Override
public Response<Void> agentCheckRegister(NewCheck newCheck, String token) {
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
String json = GsonFactory.getGson().toJson(newCheck);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/register", json, tokenParam);
if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response<Void> agentCheckDeregister(String checkId) {
return agentCheckDeregister(checkId, null);
}
@Override
public Response<Void> agentCheckDeregister(String checkId, String token) {
UrlParameters tokenParameter = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/deregister/" + checkId, "", tokenParameter);
if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response<Void> agentCheckPass(String checkId) {
return agentCheckPass(checkId, null);
}
@Override
public Response<Void> agentCheckPass(String checkId, String note) {
return agentCheckPass(checkId, note, null);
}
@Override
public Response<Void> agentCheckPass(String checkId, String note, String token) {
UrlParameters noteParameter = note != null ? new SingleUrlParameters("note", note) : null;
UrlParameters tokenParameter = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/pass/" + checkId, "", noteParameter, tokenParameter);
if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response<Void> agentCheckWarn(String checkId) {
return agentCheckWarn(checkId, null);
}
@Override
public Response<Void> agentCheckWarn(String checkId, String note) {
return agentCheckWarn(checkId, note, null);
}
@Override
public Response<Void> agentCheckWarn(String checkId, String note, String token) {
UrlParameters noteParameter = note != null ? new SingleUrlParameters("note", note) : null;
UrlParameters tokenParameter = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/warn/" + checkId, "", noteParameter, tokenParameter);
if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response<Void> agentCheckFail(String checkId) {
return agentCheckFail(checkId, null);
}
@Override
public Response<Void> agentCheckFail(String checkId, String note) {
return agentCheckFail(checkId, note, null);
}
@Override
public Response<Void> agentCheckFail(String checkId, String note, String token) {
UrlParameters noteParameter = note != null ? new SingleUrlParameters("note", note) : null;
UrlParameters tokenParameter = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/fail/" + checkId, "", noteParameter, tokenParameter);
if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response<Void> agentServiceRegister(NewService newService) {
return agentServiceRegister(newService, null);
}
@Override
public Response<Void> agentServiceRegister(NewService newService, String token) {
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
String json = GsonFactory.getGson().toJson(newService);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/service/register", json, tokenParam);
if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response<Void> agentServiceDeregister(String serviceId) {
return agentServiceDeregister(serviceId, null);
}
@Override
public Response<Void> agentServiceDeregister(String serviceId, String token) {
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/service/deregister/" + serviceId, "", tokenParam);
if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response<Void> agentServiceSetMaintenance(String serviceId, boolean maintenanceEnabled) {
return agentServiceSetMaintenance(serviceId, maintenanceEnabled, null);
}
@Override
public Response<Void> agentServiceSetMaintenance(String serviceId, boolean maintenanceEnabled, String reason) {
UrlParameters maintenanceParameter = new SingleUrlParameters("enable", Boolean.toString(maintenanceEnabled));
UrlParameters reasonParameter = reason != null ? new SingleUrlParameters("reason", reason) : null;
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/service/maintenance/" + serviceId, "", maintenanceParameter, reasonParameter);
if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response<Void> agentReload() {
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/reload", "");
if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
}