Skip to content

Commit

Permalink
Minor updates
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
dcarbone committed Apr 19, 2017
1 parent 4b8735d commit e05405f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 22 deletions.
29 changes: 26 additions & 3 deletions src/Agent/AgentClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/

use DCarbone\PHPConsulAPI\AbstractClient;
use DCarbone\PHPConsulAPI\Consul;
use DCarbone\PHPConsulAPI\Error;
use DCarbone\PHPConsulAPI\Request;

/**
Expand Down Expand Up @@ -199,7 +201,7 @@ public function serviceDeregister($serviceID) {
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function passTTL($checkID, $note) {
return $this->updateTTL($checkID, $note, 'passing');
return $this->updateTTL($checkID, $note, 'pass');
}

/**
Expand All @@ -210,7 +212,7 @@ public function passTTL($checkID, $note) {
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function warnTTL($checkID, $note) {
return $this->updateTTL($checkID, $note, 'warning');
return $this->updateTTL($checkID, $note, 'warn');
}

/**
Expand All @@ -221,7 +223,7 @@ public function warnTTL($checkID, $note) {
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function failTTL($checkID, $note) {
return $this->updateTTL($checkID, $note, 'critical');
return $this->updateTTL($checkID, $note, 'fail');
}

/**
Expand All @@ -233,6 +235,27 @@ public function failTTL($checkID, $note) {
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function updateTTL($checkID, $output, $status) {
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(sprintf(
'%s is not a valid status. Allowed: ["pass", "warn", "fail"]',
is_string($status) ? $status : gettype($status)
));
}

$r = new Request('put',
sprintf('v1/agent/check/update/%s', $checkID),
$this->c,
Expand Down
54 changes: 54 additions & 0 deletions src/Agent/AgentServiceCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class AgentServiceCheck extends AbstractModel {
public $TCP = '';
/** @var string */
public $Status = '';
/** @var string */
public $Notes = '';
/** @var bool */
public $TLSSkipVerify = false;
/** @var string */
public $DeregisterCriticalServiceAfter = '';

/**
* @return string
Expand Down Expand Up @@ -185,4 +191,52 @@ public function setStatus($Status) {
$this->Status = $Status;
return $this;
}

/**
* @return string
*/
public function getNotes() {
return $this->Notes;
}

/**
* @param string $Notes
* @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck
*/
public function setNotes($Notes) {
$this->Notes = $Notes;
return $this;
}

/**
* @return bool
*/
public function isTLSSkipVerify() {
return $this->TLSSkipVerify;
}

/**
* @param bool $TLSSkipVerify
* @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck
*/
public function setTLSSkipVerify($TLSSkipVerify) {
$this->TLSSkipVerify = $TLSSkipVerify;
return $this;
}

/**
* @return string
*/
public function getDeregisterCriticalServiceAfter() {
return $this->DeregisterCriticalServiceAfter;
}

/**
* @param string $DeregisterCriticalServiceAfter
* @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck
*/
public function setDeregisterCriticalServiceAfter($DeregisterCriticalServiceAfter) {
$this->DeregisterCriticalServiceAfter = $DeregisterCriticalServiceAfter;
return $this;
}
}
6 changes: 6 additions & 0 deletions src/Consul.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ class Consul {
const HTTPSSLEnvName = 'CONSUL_HTTP_SSL';
const HTTPSSLVerifyEnvName = 'CONSUL_HTTP_SSL_VERIFY';

const HealthAny = 'any';
const HealthPassing = 'passing';
const HealthWarning = 'warning';
const HealthCritical = 'critical';
const HealthMaint = 'maintenance';

/** @var \DCarbone\PHPConsulAPI\KV\KVClient */
public $KV;
/** @var \DCarbone\PHPConsulAPI\Agent\AgentClient */
Expand Down
32 changes: 13 additions & 19 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,54 +24,48 @@
*/
class Error implements \JsonSerializable {
/** @var DateTime */
private $_timestamp;
private $time;

/** @var string */
private $_message;
private $message;

/**
* Error constructor.
* @param string $message
*/
public function __construct($message) {
$this->_timestamp = new DateTime();
$this->_message = $message;
$this->time = new DateTime();
$this->message = $message;
}

/**
* @return DateTime
*/
public function getTimestamp() {
return $this->_timestamp;
public function getTime() {
return $this->time;
}

/**
* @return string
*/
public function getMessage() {
return $this->_message;
return $this->message;
}

/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by json_encode, which is a value of any type other than a resource.
* @return array
*/
public function jsonSerialize() {
return array(
'message' => $this->_message,
'timestamp' => $this->_timestamp
);
return [
'message' => $this->message,
'timestamp' => $this->time
];
}

/**
* @return string
*/
public function __toString() {
return sprintf(
'[error] - %s - %s',
$this->_timestamp,
$this->_message
);
return $this->message;
}
}

0 comments on commit e05405f

Please sign in to comment.