Skip to content

Commit 5e62d81

Browse files
committed
Misc cleanup, adding Health endpoints
1 parent 5234af8 commit 5e62d81

File tree

5 files changed

+371
-10
lines changed

5 files changed

+371
-10
lines changed

src/Catalog/CatalogClient.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
*/
1818

1919
use DCarbone\PHPConsulAPI\AbstractConsulClient;
20-
use DCarbone\PHPConsulAPI\QueryMeta;
2120
use DCarbone\PHPConsulAPI\QueryOptions;
2221
use DCarbone\PHPConsulAPI\Request;
23-
use DCarbone\PHPConsulAPI\WriteMeta;
2422
use DCarbone\PHPConsulAPI\WriteOptions;
2523

2624
/**
@@ -33,7 +31,7 @@ class CatalogClient extends AbstractConsulClient
3331
* @param CatalogRegistration $catalogRegistration
3432
* @param WriteOptions|null $writeOptions
3533
* @return array(
36-
* @type WriteMeta write meta data
34+
* @type \DCarbone\PHPConsulAPI\WriteMeta write meta data
3735
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
3836
* )
3937
*/
@@ -52,7 +50,7 @@ public function register(CatalogRegistration $catalogRegistration, WriteOptions
5250
* @param CatalogDeregistration $catalogDeregistration
5351
* @param WriteOptions|null $writeOptions
5452
* @return array(
55-
* @type WriteMeta write meta data
53+
* @type \DCarbone\PHPConsulAPI\WriteMeta write meta data
5654
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
5755
* )
5856
*/

src/Health/HealthCheck.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php namespace DCarbone\PHPConsulAPI\Health;
2+
3+
/*
4+
Copyright 2016 Daniel Carbone ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
use DCarbone\PHPConsulAPI\AbstractDefinedCollection;
20+
21+
/**
22+
* Class HealthCheck
23+
* @package DCarbone\PHPConsulAPI\Health
24+
*/
25+
class HealthCheck extends AbstractDefinedCollection
26+
{
27+
/**
28+
* @return array
29+
*/
30+
protected function getDefinition()
31+
{
32+
return array(
33+
'Node' => null,
34+
'CheckID' => null,
35+
'Name' => null,
36+
'Status' => null,
37+
'Notes' => null,
38+
'Output' => null,
39+
'ServiceID' => null,
40+
'ServiceName' => null
41+
);
42+
}
43+
44+
/**
45+
* @return string
46+
*/
47+
public function getNode()
48+
{
49+
return (string)$this->_storage['Node'];
50+
}
51+
52+
/**
53+
* @return string
54+
*/
55+
public function getCheckID()
56+
{
57+
return (string)$this->_storage['CheckID'];
58+
}
59+
60+
/**
61+
* @return string
62+
*/
63+
public function getName()
64+
{
65+
return (string)$this->_storage['Name'];
66+
}
67+
68+
/**
69+
* @return string
70+
*/
71+
public function getStatus()
72+
{
73+
return (string)$this->_storage['Status'];
74+
}
75+
76+
/**
77+
* @return string
78+
*/
79+
public function getOutput()
80+
{
81+
return (string)$this->_storage['Output'];
82+
}
83+
84+
/**
85+
* @return string
86+
*/
87+
public function getServiceID()
88+
{
89+
return (string)$this->_storage['ServiceID'];
90+
}
91+
92+
/**
93+
* @return string
94+
*/
95+
public function getServiceName()
96+
{
97+
return (string)$this->_storage['ServiceName'];
98+
}
99+
}

src/Health/HealthClient.php

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<?php namespace DCarbone\PHPConsulAPI\Health;
2+
3+
/*
4+
Copyright 2016 Daniel Carbone ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
use DCarbone\PHPConsulAPI\AbstractConsulClient;
20+
use DCarbone\PHPConsulAPI\Error;
21+
use DCarbone\PHPConsulAPI\QueryOptions;
22+
use DCarbone\PHPConsulAPI\Request;
23+
24+
/**
25+
* Class HealthClient
26+
* @package DCarbone\PHPConsulAPI\Health
27+
*/
28+
class HealthClient extends AbstractConsulClient
29+
{
30+
/**
31+
* @param string $node
32+
* @param QueryOptions|null $queryOptions
33+
* @return array(
34+
* @type HealthCheck[]|null list of health checks or null on error
35+
* @type \DCarbone\PHPConsulAPI\QueryMeta query meta
36+
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
37+
* )
38+
*/
39+
public function node($node, QueryOptions $queryOptions = null)
40+
{
41+
if (!is_string($node))
42+
{
43+
return [null, null, new Error(sprintf(
44+
'%s::node - $node must be string, %s seen.',
45+
get_class($this),
46+
gettype($node)
47+
))];
48+
}
49+
50+
$r = new Request('get', sprintf('v1/health/node/%s', rawurlencode($node)), $this->_Config);
51+
$r->setQueryOptions($queryOptions);
52+
53+
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
54+
$qm = $this->buildQueryMeta($duration, $response);
55+
56+
if (null !== $err)
57+
return [null, $qm, $err];
58+
59+
list($data, $err) = $this->decodeBody($response);
60+
61+
if (null !== $err)
62+
return [null, $qm, $err];
63+
64+
$checks = array();
65+
foreach($data as $check)
66+
{
67+
$checks[] = new HealthCheck($check);
68+
}
69+
70+
return [$checks, $qm, null];
71+
}
72+
73+
/**
74+
* @param string $service
75+
* @param QueryOptions|null $queryOptions
76+
* @return array(
77+
* @type HealthCheck[]|null list of health checks or null on error
78+
* @type \DCarbone\PHPConsulAPI\QueryMeta query metadata
79+
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
80+
* )
81+
*/
82+
public function checks($service, QueryOptions $queryOptions = null)
83+
{
84+
if (!is_string($service))
85+
{
86+
return [null, null, new Error(sprintf(
87+
'%s::checks - $service must be string, %s seen.',
88+
get_class($this),
89+
gettype($service)
90+
))];
91+
}
92+
93+
$r = new Request('get', sprintf('v1/health/checks/%s', rawurlencode($service)), $this->_Config);
94+
$r->setQueryOptions($queryOptions);
95+
96+
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
97+
$qm = $this->buildQueryMeta($duration, $response);
98+
99+
if (null !== $err)
100+
return [null, $qm, $err];
101+
102+
list($data, $err) = $this->decodeBody($response);
103+
104+
if (null !== $err)
105+
return [null, $qm, $err];
106+
107+
$checks = array();
108+
foreach($data as $check)
109+
{
110+
$checks[] = new HealthCheck($check);
111+
}
112+
113+
return [$checks, $qm, null];
114+
}
115+
116+
/**
117+
* @param string $service
118+
* @param QueryOptions|null $queryOptions
119+
* @return array(
120+
* @type ServiceEntry[]|null list of service entries or null on error
121+
* @type \DCarbone\PHPConsulAPI\QueryMeta query metadata
122+
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
123+
* )
124+
*/
125+
public function service($service, QueryOptions $queryOptions = null)
126+
{
127+
if (!is_string($service))
128+
{
129+
return [null, null, new Error(sprintf(
130+
'%s::service - $service must be string, %s seen.',
131+
get_class($this),
132+
gettype($service)
133+
))];
134+
}
135+
136+
$r = new Request('get', sprintf('v1/health/checks/%s', rawurlencode($service)), $this->_Config);
137+
$r->setQueryOptions($queryOptions);
138+
139+
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
140+
$qm = $this->buildQueryMeta($duration, $response);
141+
142+
if (null !== $err)
143+
return [null, $qm, $err];
144+
145+
list($data, $err) = $this->decodeBody($response);
146+
147+
if (null !== $err)
148+
return [null, $qm, $err];
149+
150+
$services = array();
151+
foreach($data as $service)
152+
{
153+
$services[] = new ServiceEntry($service);
154+
}
155+
156+
return [$services, $qm, null];
157+
}
158+
159+
/**
160+
* @param string $state
161+
* @param QueryOptions|null $queryOptions
162+
* @return array(
163+
* @type HealthCheck[]|null array of heath checks or null on error
164+
* @type \DCarbone\PHPConsulAPI\QueryMeta|null query metadata or null on error
165+
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
166+
* )
167+
*/
168+
public function state($state, QueryOptions $queryOptions = null)
169+
{
170+
static $validStates = array('any', 'warning', 'critical', 'passing', 'unknown');
171+
172+
if (!is_string($state) || !in_array($state, $validStates, true))
173+
{
174+
return [null, null, new Error(sprintf(
175+
'%s::state - "$state" must be string with value of ["%s"]. %s seen.',
176+
get_class($this),
177+
implode('", "', $validStates),
178+
is_string($state) ? $state : gettype($state)
179+
))];
180+
}
181+
182+
$r = new Request('get', sprintf('v1/health/state/%s', rawurlencode($state)), $this->_Config);
183+
$r->setQueryOptions($queryOptions);
184+
185+
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
186+
$qm = $this->buildQueryMeta($duration, $response);
187+
188+
if (null !== $err)
189+
return [null, $qm, $err];
190+
191+
list($data, $err) = $this->decodeBody($response);
192+
193+
if (null !== $err)
194+
return [null, $qm, $err];
195+
196+
$checks = array();
197+
foreach($data as $check)
198+
{
199+
$checks[] = new HealthCheck($check);
200+
}
201+
202+
return [$checks, $qm, null];
203+
}
204+
}

src/Health/ServiceEntry.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php namespace DCarbone\PHPConsulAPI\Health;
2+
3+
/*
4+
Copyright 2016 Daniel Carbone ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
use DCarbone\PHPConsulAPI\AbstractDefinedCollection;
20+
21+
/**
22+
* Class ServiceEntry
23+
* @package DCarbone\PHPConsulAPI\Health
24+
*/
25+
class ServiceEntry extends AbstractDefinedCollection
26+
{
27+
/**
28+
* @return array
29+
*/
30+
protected function getDefinition()
31+
{
32+
return array(
33+
'Node' => null,
34+
'Service' => null,
35+
'Checks' => array()
36+
);
37+
}
38+
39+
/**
40+
* @return \DCarbone\PHPConsulAPI\Catalog\CatalogNode
41+
*/
42+
public function getNode()
43+
{
44+
return $this->_storage['Node'];
45+
}
46+
47+
/**
48+
* @return \DCarbone\PHPConsulAPI\Agent\AgentService
49+
*/
50+
public function getService()
51+
{
52+
return $this->_storage['Service'];
53+
}
54+
55+
/**
56+
* @return \DCarbone\PHPConsulAPI\Health\HealCheck[]
57+
*/
58+
public function getChecks()
59+
{
60+
return $this->_storage['Checks'];
61+
}
62+
}

0 commit comments

Comments
 (0)