-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathAgentClient.php
More file actions
338 lines (286 loc) · 11.4 KB
/
AgentClient.php
File metadata and controls
338 lines (286 loc) · 11.4 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
331
332
333
334
335
336
337
338
<?php
declare(strict_types=1);
namespace DCarbone\PHPConsulAPI\Agent;
/*
Copyright 2016-2025 Daniel Carbone (daniel.p.carbone@gmail.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
use DCarbone\Go\HTTP;
use DCarbone\PHPConsulAPI\AbstractClient;
use DCarbone\PHPConsulAPI\Consul;
use DCarbone\PHPConsulAPI\Error;
use DCarbone\PHPConsulAPI\MapResponse;
use DCarbone\PHPConsulAPI\QueryOptions;
use DCarbone\PHPConsulAPI\Request;
use DCarbone\PHPConsulAPI\ValuedStringResponse;
class AgentClient extends AbstractClient
{
private ?MapResponse $_self = null;
public function Self(bool $refresh = false): MapResponse
{
if (!$refresh && isset($this->_self)) {
return $this->_self;
}
$resp = $this->_requireOK($this->_doGet('v1/agent/self', null));
$ret = new MapResponse();
$this->_unmarshalResponse($resp, $ret);
if (null === $ret->Err) {
$this->_self = $ret;
}
return $ret;
}
public function Host(): MapResponse
{
$resp = $this->_requireOK($this->_doGet('v1/agent/host', null));
$ret = new MapResponse();
$this->_unmarshalResponse($resp, $ret);
return $ret;
}
public function Metrics(): MetricsInfoResponse
{
$resp = $this->_requireOK($this->_doGet('v1/agent/metrics', null));
$ret = new MetricsInfoResponse();
$this->_unmarshalResponse($resp, $ret);
return $ret;
}
public function Reload(): ?Error
{
return $this->_executePut('v1/agent/reload', null, null)->Err;
}
public function NodeName(bool $refresh = false): ValuedStringResponse
{
$self = $this->Self($refresh);
$ret = new ValuedStringResponse();
$ret->Err = $self->Err;
if (null !== $self->Err) {
return $ret;
}
if (isset($self->Map['Config'], $self->Map['Config']['NodeName'])) {
$ret->Value = $self->Map['Config']['NodeName'];
}
return $ret;
}
public function ChecksWithFilter(string $filter): AgentChecksResponse
{
$r = $this->_newGetRequest('v1/agent/checks', null);
$r->filterQuery($filter);
$resp = $this->_requireOK($this->_do($r));
$ret = new AgentChecksResponse();
$this->_unmarshalResponse($resp, $ret);
return $ret;
}
public function Checks(): AgentChecksResponse
{
return $this->checksWithFilter('');
}
public function ServicesWithFilter(string $filter): AgentServicesResponse
{
$r = $this->_newGetRequest('v1/agent/services', null);
$r->filterQuery($filter);
$resp = $this->_requireOK($this->_do($r));
$ret = new AgentServicesResponse();
$this->_unmarshalResponse($resp, $ret);
return $ret;
}
public function Services(): AgentServicesResponse
{
return $this->ServicesWithFilter('');
}
public function AgentHealthServiceByID(string $id): AgentHealthServiceResponse
{
$r = $this->_prepAgentHealthServiceRequest(sprintf('v1/agent/health/service/id/%s', $id));
$resp = $this->_requireOK($this->_do($r));
if (null !== $resp->Err) {
return new AgentHealthServiceResponse(Consul::HealthCritical, null, $resp->Err);
}
if (HTTP\StatusNotFound === $resp->Response->getStatusCode()) {
return new AgentHealthServiceResponse(Consul::HealthCritical, null, null);
}
$dec = $this->_decodeBody($resp->Response->getBody());
if (null !== $dec->Err) {
return new AgentHealthServiceResponse(Consul::HealthCritical, null, $dec->Err);
}
$status = match ($resp->Response->getStatusCode()) {
HTTP\StatusOK => Consul::HealthPassing,
HTTP\StatusTooManyRequests => Consul::HealthWarning,
HTTP\StatusServiceUnavailable => Consul::HealthCritical,
default => Consul::HealthCritical,
};
return new AgentHealthServiceResponse($status, $dec->Decoded, null);
}
public function AgentHealthServiceByName(string $service): AgentHealthServicesResponse
{
$r = $this->_prepAgentHealthServiceRequest(sprintf('v1/agent/health/service/name/%s', urlencode($service)));
$resp = $this->_requireOK($this->_do($r));
if (null !== $resp->Err) {
return new AgentHealthServicesResponse(Consul::HealthCritical, null, $resp->Err);
}
if (HTTP\StatusNotFound === $resp->Response->getStatusCode()) {
return new AgentHealthServicesResponse(Consul::HealthCritical, null, null);
}
$dec = $this->_decodeBody($resp->Response->getBody());
if (null !== $dec->Err) {
return new AgentHealthServicesResponse(Consul::HealthCritical, null, $dec->Err);
}
$status = match ($resp->Response->getStatusCode()) {
HTTP\StatusOK => Consul::HealthPassing,
HTTP\StatusTooManyRequests => Consul::HealthWarning,
HTTP\StatusServiceUnavailable => Consul::HealthCritical,
default => Consul::HealthCritical,
};
return new AgentHealthServicesResponse($status, $dec->Decoded, null);
}
public function Service(string $serviceID, ?QueryOptions $opts = null): AgentServiceResponse
{
$resp = $this->_requireOK($this->_doGet(sprintf('v1/agent/service/%s', $serviceID), $opts));
$ret = new AgentServiceResponse();
$this->_unmarshalResponse($resp, $ret);
return $ret;
}
public function Members(): AgentMembersResponse
{
$resp = $this->_requireOK($this->_doGet('v1/agent/members', null));
$ret = new AgentMembersResponse();
$this->_unmarshalResponse($resp, $ret);
return $ret;
}
public function MemberOpts(MemberOpts $memberOpts): AgentMembersResponse
{
$r = $this->_newGetRequest('v1/agent/members', null);
$r->params->set('segment', $memberOpts->Segment);
if ($memberOpts->WAN) {
$r->params->set('wan', '1');
}
$resp = $this->_requireOK($this->_do($r));
$ret = new AgentMembersResponse();
$this->_unmarshalResponse($resp, $ret);
return $ret;
}
public function ServiceRegisterOpts(AgentServiceRegistration $service, ServiceRegisterOpts $registerOpts): ?Error
{
$r = $this->_newPutRequest('v1/agent/service/register', $service, null);
if ($registerOpts->ReplaceExistingChecks) {
$r->params->set('replace-existing-checks', 'true');
}
return $this->_requireOK($this->_do($r))->Err;
}
public function ServiceRegister(AgentServiceRegistration $service): ?Error
{
return $this->ServiceRegisterOpts($service, new ServiceRegisterOpts(['ReplaceExistingChecks' => false]));
}
public function ServiceDeregister(string $serviceID): ?Error
{
$r = new Request(HTTP\MethodPut, sprintf('v1/agent/service/deregister/%s', $serviceID), $this->_config, null);
return $this->_requireOK($this->_do($r))->Err;
}
public function PassTTL(string $checkID, string $note): ?Error
{
return $this->UpdateTTL($checkID, $note, 'pass');
}
public function WarnTTL(string $checkID, string $note): ?Error
{
return $this->UpdateTTL($checkID, $note, 'warn');
}
public function FailTTL(string $checkID, string $note): ?Error
{
return $this->UpdateTTL($checkID, $note, 'fail');
}
public function UpdateTTL(string $checkID, string $output, string $status): ?Error
{
switch ($status) {
case Consul::HealthPassing:
case Consul::HealthWarning:
case Consul::HealthCritical:
break;
case 'pass':
$status = Consul::HealthPassing;
break;
case 'warn':
$status = Consul::HealthWarning;
break;
case 'fail':
$status = Consul::HealthCritical;
break;
default:
return new Error("\"{$status}\" is not a valid status. Allowed: [\"pass\", \"warn\", \"fail\"]");
}
$r = new Request(
HTTP\MethodPut,
sprintf('v1/agent/check/update/%s', $checkID),
$this->_config,
new AgentCheckUpdate(['Output' => $output, 'Status' => $status])
);
return $this->_requireOK($this->_do($r))->Err;
}
public function CheckRegister(AgentCheckRegistration $check): ?Error
{
return $this->_executePut('v1/agent/check/register', $check, null)->Err;
}
public function CheckDeregister(string $checkID): ?Error
{
return $this->_executePut(sprintf('v1/agent/check/deregister/%s', $checkID), null, null)->Err;
}
public function Join(string $addr, bool $wan = false): ?Error
{
$r = $this->_newPutRequest(sprintf('v1/agent/join/%s', $addr), null, null);
if ($wan) {
$r->params->set('wan', '1');
}
return $this->_requireOK($this->_do($r))->Err;
}
public function Leave(): ?Error
{
return $this->_executePut('v1/agent/leave', null, null)->Err;
}
public function ForceLeave(string $node): ?Error
{
return $this->_executePut(sprintf('v1/agent/force-leave/%s', $node), null, null)->Err;
}
public function ForceLeavePrune(string $node): ?Error
{
$r = $this->_newPutRequest(sprintf('v1/agent/force-leave/%s', $node), null, null);
$r->params->set('prune', '1');
return $this->_requireOK($this->_do($r))->Err;
}
public function EnableServiceMaintenance(string $serviceID, string $reason = ''): ?Error
{
$r = $this->_newPutRequest(sprintf('v1/agent/service/maintenance/%s', $serviceID), null, null);
$r->params->set('enable', 'true');
$r->params->set('reason', $reason);
return $this->_requireOK($this->_do($r))->Err;
}
public function DisableServiceMaintenance(string $serviceID): ?Error
{
$r = $this->_newPutRequest(sprintf('v1/agent/service/maintenance/%s', $serviceID), null, null);
$r->params->set('enable', 'false');
return $this->_requireOK($this->_do($r))->Err;
}
public function EnableNodeMaintenance(string $reason = ''): ?Error
{
$r = $this->_newPutRequest('v1/agent/maintenance', null, null);
$r->params->set('enable', 'true');
$r->params->set('reason', $reason);
return $this->_requireOK($this->_do($r))->Err;
}
public function DisableNodeMaintenance(): ?Error
{
$r = $this->_newPutRequest('v1/agent/maintenance', null, null);
$r->params->set('enable', 'false');
return $this->_requireOK($this->_do($r))->Err;
}
protected function _prepAgentHealthServiceRequest(string $path): Request
{
$r = $this->_newGetRequest($path, null);
$r->params->add('format', 'json');
$r->header->set('Accept', 'application/json');
return $r;
}
}