Skip to content

Commit e05405f

Browse files
committed
Minor updates
- Adding some input validation to "AgentClient::updateTTL" - Adding various "Health*" constants to "Consul" class - Adding "Notes", "TLSSkipVerify", and "DeregisterCriticalServiceAfter" properties to "AgentServiceCheck" class - "Error" no longer prints out timestamp when __toString is called.
1 parent 4b8735d commit e05405f

File tree

4 files changed

+99
-22
lines changed

4 files changed

+99
-22
lines changed

src/Agent/AgentClient.php

+26-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818

1919
use DCarbone\PHPConsulAPI\AbstractClient;
20+
use DCarbone\PHPConsulAPI\Consul;
21+
use DCarbone\PHPConsulAPI\Error;
2022
use DCarbone\PHPConsulAPI\Request;
2123

2224
/**
@@ -199,7 +201,7 @@ public function serviceDeregister($serviceID) {
199201
* @return \DCarbone\PHPConsulAPI\Error|null
200202
*/
201203
public function passTTL($checkID, $note) {
202-
return $this->updateTTL($checkID, $note, 'passing');
204+
return $this->updateTTL($checkID, $note, 'pass');
203205
}
204206

205207
/**
@@ -210,7 +212,7 @@ public function passTTL($checkID, $note) {
210212
* @return \DCarbone\PHPConsulAPI\Error|null
211213
*/
212214
public function warnTTL($checkID, $note) {
213-
return $this->updateTTL($checkID, $note, 'warning');
215+
return $this->updateTTL($checkID, $note, 'warn');
214216
}
215217

216218
/**
@@ -221,7 +223,7 @@ public function warnTTL($checkID, $note) {
221223
* @return \DCarbone\PHPConsulAPI\Error|null
222224
*/
223225
public function failTTL($checkID, $note) {
224-
return $this->updateTTL($checkID, $note, 'critical');
226+
return $this->updateTTL($checkID, $note, 'fail');
225227
}
226228

227229
/**
@@ -233,6 +235,27 @@ public function failTTL($checkID, $note) {
233235
* @return \DCarbone\PHPConsulAPI\Error|null
234236
*/
235237
public function updateTTL($checkID, $output, $status) {
238+
switch ($status) {
239+
case Consul::HealthPassing:
240+
case Consul::HealthWarning:
241+
case Consul::HealthCritical:
242+
break;
243+
case 'pass':
244+
$status = Consul::HealthPassing;
245+
break;
246+
case 'warn':
247+
$status = Consul::HealthWarning;
248+
break;
249+
case 'fail':
250+
$status = Consul::HealthCritical;
251+
break;
252+
default:
253+
return new Error(sprintf(
254+
'%s is not a valid status. Allowed: ["pass", "warn", "fail"]',
255+
is_string($status) ? $status : gettype($status)
256+
));
257+
}
258+
236259
$r = new Request('put',
237260
sprintf('v1/agent/check/update/%s', $checkID),
238261
$this->c,

src/Agent/AgentServiceCheck.php

+54
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ class AgentServiceCheck extends AbstractModel {
4141
public $TCP = '';
4242
/** @var string */
4343
public $Status = '';
44+
/** @var string */
45+
public $Notes = '';
46+
/** @var bool */
47+
public $TLSSkipVerify = false;
48+
/** @var string */
49+
public $DeregisterCriticalServiceAfter = '';
4450

4551
/**
4652
* @return string
@@ -185,4 +191,52 @@ public function setStatus($Status) {
185191
$this->Status = $Status;
186192
return $this;
187193
}
194+
195+
/**
196+
* @return string
197+
*/
198+
public function getNotes() {
199+
return $this->Notes;
200+
}
201+
202+
/**
203+
* @param string $Notes
204+
* @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck
205+
*/
206+
public function setNotes($Notes) {
207+
$this->Notes = $Notes;
208+
return $this;
209+
}
210+
211+
/**
212+
* @return bool
213+
*/
214+
public function isTLSSkipVerify() {
215+
return $this->TLSSkipVerify;
216+
}
217+
218+
/**
219+
* @param bool $TLSSkipVerify
220+
* @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck
221+
*/
222+
public function setTLSSkipVerify($TLSSkipVerify) {
223+
$this->TLSSkipVerify = $TLSSkipVerify;
224+
return $this;
225+
}
226+
227+
/**
228+
* @return string
229+
*/
230+
public function getDeregisterCriticalServiceAfter() {
231+
return $this->DeregisterCriticalServiceAfter;
232+
}
233+
234+
/**
235+
* @param string $DeregisterCriticalServiceAfter
236+
* @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck
237+
*/
238+
public function setDeregisterCriticalServiceAfter($DeregisterCriticalServiceAfter) {
239+
$this->DeregisterCriticalServiceAfter = $DeregisterCriticalServiceAfter;
240+
return $this;
241+
}
188242
}

src/Consul.php

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ class Consul {
3939
const HTTPSSLEnvName = 'CONSUL_HTTP_SSL';
4040
const HTTPSSLVerifyEnvName = 'CONSUL_HTTP_SSL_VERIFY';
4141

42+
const HealthAny = 'any';
43+
const HealthPassing = 'passing';
44+
const HealthWarning = 'warning';
45+
const HealthCritical = 'critical';
46+
const HealthMaint = 'maintenance';
47+
4248
/** @var \DCarbone\PHPConsulAPI\KV\KVClient */
4349
public $KV;
4450
/** @var \DCarbone\PHPConsulAPI\Agent\AgentClient */

src/Error.php

+13-19
Original file line numberDiff line numberDiff line change
@@ -24,54 +24,48 @@
2424
*/
2525
class Error implements \JsonSerializable {
2626
/** @var DateTime */
27-
private $_timestamp;
27+
private $time;
2828

2929
/** @var string */
30-
private $_message;
30+
private $message;
3131

3232
/**
3333
* Error constructor.
3434
* @param string $message
3535
*/
3636
public function __construct($message) {
37-
$this->_timestamp = new DateTime();
38-
$this->_message = $message;
37+
$this->time = new DateTime();
38+
$this->message = $message;
3939
}
4040

4141
/**
4242
* @return DateTime
4343
*/
44-
public function getTimestamp() {
45-
return $this->_timestamp;
44+
public function getTime() {
45+
return $this->time;
4646
}
4747

4848
/**
4949
* @return string
5050
*/
5151
public function getMessage() {
52-
return $this->_message;
52+
return $this->message;
5353
}
5454

5555
/**
56-
* Specify data which should be serialized to JSON
57-
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
58-
* @return mixed data which can be serialized by json_encode, which is a value of any type other than a resource.
56+
* @return array
5957
*/
6058
public function jsonSerialize() {
61-
return array(
62-
'message' => $this->_message,
63-
'timestamp' => $this->_timestamp
64-
);
59+
return [
60+
'message' => $this->message,
61+
'timestamp' => $this->time
62+
];
6563
}
6664

6765
/**
6866
* @return string
6967
*/
7068
public function __toString() {
71-
return sprintf(
72-
'[error] - %s - %s',
73-
$this->_timestamp,
74-
$this->_message
75-
);
69+
return $this->message;
7670
}
7771
}

0 commit comments

Comments
 (0)