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+ }
0 commit comments