forked from dcarbone/php-consul-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HealthClient.php
174 lines (145 loc) · 6.06 KB
/
HealthClient.php
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
<?php namespace DCarbone\PHPConsulAPI\Health;
/*
Copyright 2016-2018 Daniel Carbone ([email protected])
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\PHPConsulAPI\AbstractClient;
use DCarbone\PHPConsulAPI\Error;
use DCarbone\PHPConsulAPI\QueryOptions;
use DCarbone\PHPConsulAPI\Request;
/**
* Class HealthClient
* @package DCarbone\PHPConsulAPI\Health
*/
class HealthClient extends AbstractClient {
/**
* @param string $node
* @param \DCarbone\PHPConsulAPI\QueryOptions|null $options
* @return array(
* @type \DCarbone\PHPConsulAPI\Health\HealthChecks|null list of health checks or null on error
* @type \DCarbone\PHPConsulAPI\QueryMeta query meta
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
* )
*/
public function Node(string $node, QueryOptions $options = null): array {
$r = new Request('GET', sprintf('v1/health/node/%s', $node), $this->config);
$r->setQueryOptions($options);
/** @var \Psr\Http\Message\ResponseInterface $response */
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
if (null !== $err) {
return [null, null, $err];
}
list($data, $err) = $this->decodeBody($response->getBody());
if (null !== $err) {
return [null, null, $err];
}
$qm = $this->buildQueryMeta($duration, $response, $r->getUri());
return [new HealthChecks($data), $qm, null];
}
/**
* @param string $service
* @param \DCarbone\PHPConsulAPI\QueryOptions|null $options
* @return array(
* @type \DCarbone\PHPConsulAPI\Health\HealthChecks|null list of health checks or null on error
* @type \DCarbone\PHPConsulAPI\QueryMeta query metadata
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
* )
*/
public function Checks(string $service, QueryOptions $options = null): array {
/** @var \Psr\Http\Message\ResponseInterface $response */
$r = new Request('GET', sprintf('v1/health/checks/%s', $service), $this->config);
$r->setQueryOptions($options);
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
if (null !== $err) {
return [null, null, $err];
}
list($data, $err) = $this->decodeBody($response->getBody());
if (null !== $err) {
return [null, null, $err];
}
$qm = $this->buildQueryMeta($duration, $response, $r->getUri());
return [new HealthChecks($data), $qm, null];
}
/**
* @param string $service
* @param string $tag
* @param bool $passingOnly
* @param \DCarbone\PHPConsulAPI\QueryOptions|null $options
* @return array (
* @type \DCarbone\PHPConsulAPI\Health\ServiceEntry[]|null list of service entries or null on error
* @type \DCarbone\PHPConsulAPI\QueryMeta query metadata
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
* )
*/
public function Service(string $service,
string $tag = '',
bool $passingOnly = false,
QueryOptions $options = null): array {
$r = new Request('GET', sprintf('v1/health/service/%s', $service), $this->config);
$r->setQueryOptions($options);
if ('' !== $tag) {
$r->Params->set('tag', $tag);
}
if ($passingOnly) {
$r->Params->set('passing', '1');
}
/** @var \Psr\Http\Message\ResponseInterface $response */
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
if (null !== $err) {
return [null, null, $err];
}
$qm = $this->buildQueryMeta($duration, $response, $r->getUri());
list($data, $err) = $this->decodeBody($response->getBody());
if (null !== $err) {
return [null, $qm, $err];
}
$services = [];
foreach ($data as $service) {
$services[] = new ServiceEntry($service);
}
return [$services, $qm, null];
}
/**
* @param string $state
* @param \DCarbone\PHPConsulAPI\QueryOptions|null $options
* @return array(
* @type \DCarbone\PHPConsulAPI\Health\HealthChecks|null array of heath checks or null on error
* @type \DCarbone\PHPConsulAPI\QueryMeta|null query metadata or null on error
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
* )
*/
public function State(string $state, QueryOptions $options = null): array {
static $validStates = ['any', 'warning', 'critical', 'passing', 'unknown'];
if (!in_array($state, $validStates, true)) {
return [null,
null,
new Error(sprintf(
'%s::state - "$state" must be string with value of ["%s"]. %s seen.',
get_class($this),
implode('", "', $validStates),
$state
))];
}
$r = new Request('GET', sprintf('v1/health/state/%s', $state), $this->config);
$r->setQueryOptions($options);
/** @var \Psr\Http\Message\ResponseInterface $response */
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
if (null !== $err) {
return [null, null, $err];
}
list($data, $err) = $this->decodeBody($response->getBody());
if (null !== $err) {
return [null, null, $err];
}
$qm = $this->buildQueryMeta($duration, $response, $r->getUri());
return [new HealthChecks($data), $qm, null];
}
}