Skip to content

Commit

Permalink
adding agent service health endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Nov 25, 2020
1 parent 11b5c1c commit 55d7047
Show file tree
Hide file tree
Showing 10 changed files with 431 additions and 70 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"php": "^7.2",
"ext-json": "*",
"dcarbone/gotime": "0.4.*",
"dcarbone/gohttp": "v0.3.*",
"guzzlehttp/guzzle": "~7",
"guzzlehttp/psr7": "~1"
},
Expand Down
13 changes: 8 additions & 5 deletions src/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
* Class AbstractModel
* @package DCarbone\PHPConsulAPI
*/
abstract class AbstractModel implements \JsonSerializable {

abstract class AbstractModel implements \JsonSerializable
{
/**
* AbstractModel constructor.
*
Expand All @@ -32,7 +32,8 @@ abstract class AbstractModel implements \JsonSerializable {
*
* @param array $data
*/
public function __construct(array $data = []) {
public function __construct(array $data = [])
{
foreach ($data as $k => $v) {
$this->{$k} = $v;
}
Expand All @@ -45,14 +46,16 @@ public function __construct(array $data = []) {
*
* @return array
*/
function jsonSerialize() {
public function jsonSerialize()
{
return array_filter((array)$this);
}

/**
* @return string
*/
public function __toString() {
public function __toString()
{
return get_class($this);
}
}
95 changes: 56 additions & 39 deletions src/AbstractModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
* Class AbstractModels
* @package DCarbone\PHPConsulAPI
*/
abstract class AbstractModels implements \Iterator, \ArrayAccess, \Countable, \JsonSerializable {
abstract class AbstractModels implements \Iterator, \ArrayAccess, \Countable, \JsonSerializable
{
/** @var \DCarbone\PHPConsulAPI\AbstractModel[] */
protected $_list = [];

Expand All @@ -31,118 +32,134 @@ abstract class AbstractModels implements \Iterator, \ArrayAccess, \Countable, \J

/**
* AbstractModels constructor.
* @param array $children
* @param array|null $children
*/
public function __construct($children = []) {
public function __construct(?array $children = [])
{
if (is_array($children)) {
foreach (array_filter($children) as $child) {
if (is_array($child)) {
$this->_list[] = $this->newChild($child);
} else if ($child instanceof $this->containedClass) {
} elseif ($child instanceof $this->containedClass) {
$this->_list[] = $child;
} else {
throw new \InvalidArgumentException(sprintf(
get_class($this).' accepts only '.$this->containedClass.' as a child, saw %s',
is_object($child) ? get_class($child) : gettype($child)));
throw new \InvalidArgumentException(
sprintf(
get_class($this) . ' accepts only ' . $this->containedClass . ' as a child, saw %s',
is_object($child) ? get_class($child) : gettype($child)
)
);
}
}
} else if (null === $children) {
// do nothin
} else {
throw new \InvalidArgumentException(get_class($this).
'::__construct accepts only array of '.
$this->containedClass.
'\'s or null as values');
}
}

/**
* @param \DCarbone\PHPConsulAPI\AbstractModel|null $value
*/
public function append(AbstractModel $value = null) {
public function append(AbstractModel $value = null)
{
if (null === $value || $value instanceof $this->containedClass) {
$this->_list[] = $value;
} else {
throw new \InvalidArgumentException(get_class($this).
' accepts only objects of type '.
$this->containedClass.
' or null as values');
throw new \InvalidArgumentException(
get_class($this) .
' accepts only objects of type ' .
$this->containedClass .
' or null as values'
);
}
}

public function current() {
/**
* @return \DCarbone\PHPConsulAPI\AbstractModel|mixed
*/
public function current()
{
return current($this->_list);
}

public function next() {
public function next()
{
next($this->_list);
}

/**
* @return int|null
*/
public function key() {
public function key()
{
return key($this->_list);
}

/**
* @return bool
*/
public function valid() {
public function valid()
{
return null !== key($this->_list);
}

public function rewind() {
public function rewind()
{
reset($this->_list);
}

/**
* @param int $offset
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset) {
public function offsetExists($offset)
{
return is_int($offset) && isset($this->_list[$offset]);
}

public function offsetGet($offset) {
public function offsetGet($offset)
{
if (is_int($offset) && isset($this->_list[$offset])) {
return $this->_list[$offset];
}
throw new \OutOfRangeException(sprintf(
'Offset %s does not exist in this list',
is_int($offset) ? (string)$offset : gettype($offset)
));
throw new \OutOfRangeException(
sprintf(
'Offset %s does not exist in this list',
is_int($offset) ? (string)$offset : gettype($offset)
)
);
}

/**
* @param int $offset
* @param \DCarbone\PHPConsulAPI\KV\KVPair $value
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value) {
public function offsetSet($offset, $value)
{
if (!is_int($offset)) {
throw new \InvalidArgumentException('Offset must be int');
}
if (null !== $value && !($value instanceof $this->containedClass)) {
throw new \InvalidArgumentException('Value must be instance of '.$this->containedClass);
throw new \InvalidArgumentException('Value must be instance of ' . $this->containedClass);
}
$this->_list[$offset] = $value;
}

/**
* @param int $offset
* @param mixed $offset
*/
public function offsetUnset($offset) {
public function offsetUnset($offset)
{
unset($this->_list[$offset]);
}

/**
* @return int
*/
public function count() {
public function count()
{
return count($this->_list);
}

public function jsonSerialize() {
public function jsonSerialize()
{
return $this->_list;
}

Expand Down
87 changes: 87 additions & 0 deletions src/Agent/AgentClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
limitations under the License.
*/

use DCarbone\Go\HTTP;
use DCarbone\PHPConsulAPI\AbstractClient;
use DCarbone\PHPConsulAPI\Consul;
use DCarbone\PHPConsulAPI\Error;
Expand Down Expand Up @@ -176,6 +177,92 @@ public function Services(): AgentServicesResponse
return $this->ServicesWithFilter('');
}

/**
* @param string $service
* @return \DCarbone\PHPConsulAPI\Agent\AgentHealthServiceResponse
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function AgentHealthServiceByName(string $service): AgentHealthServiceResponse
{
$r = new Request('GET', sprintf('v1/agent/health/service/name/%s', urlencode($service)), $this->config);
$r->params->add('format', 'json');
$r->header->set('Accept', 'application/json');

$res = $this->doRequest($r);
if (null !== $res->Err) {
return new AgentHealthServiceResponse(Consul::HealthCritical, null, $res->Err);
}

if (HTTP\StatusNotFound === $res->Response->getStatusCode()) {
return new AgentHealthServiceResponse(Consul::HealthCritical, null, null);
}

[$data, $err] = $this->decodeBody($res->Response->getBody());
if (null !== $err) {
return new AgentHealthServiceResponse(Consul::HealthCritical, null, $res->Err);
}

switch ($res->Response->getStatusCode()) {
case HTTP\StatusOK:
$status = Consul::HealthPassing;
break;
case HTTP\StatusTooManyRequests:
$status = Consul::HealthWarning;
break;
case HTTP\StatusServiceUnavailable:
$status = Consul::HealthCritical;
break;

default:
$status = Consul::HealthCritical;
}

return new AgentHealthServiceResponse($status, $data, null);
}

/**
* @param string $id
* @return \DCarbone\PHPConsulAPI\Agent\AgentHealthServiceResponse
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function AgentHealthServiceByID(string $id): AgentHealthServiceResponse
{
$r = new Request('GET', sprintf('v1/agent/health/service/id/%s', $id), $this->config);
$r->params->add('format', 'json');
$r->header->set('Accept', 'application/json');

$res = $this->doRequest($r);
if (null !== $res->Err) {
return new AgentHealthServiceResponse(Consul::HealthCritical, null, $res->Err);
}

if (HTTP\StatusNotFound === $res->Response->getStatusCode()) {
return new AgentHealthServiceResponse(Consul::HealthCritical, null, null);
}

[$data, $err] = $this->decodeBody($res->Response->getBody());
if (null !== $err) {
return new AgentHealthServiceResponse(Consul::HealthCritical, null, $res->Err);
}

switch ($res->Response->getStatusCode()) {
case HTTP\StatusOK:
$status = Consul::HealthPassing;
break;
case HTTP\StatusTooManyRequests:
$status = Consul::HealthWarning;
break;
case HTTP\StatusServiceUnavailable:
$status = Consul::HealthCritical;
break;

default:
$status = Consul::HealthCritical;
}

return new AgentHealthServiceResponse($status, $data, null);
}

/**
* @return \DCarbone\PHPConsulAPI\Agent\AgentMembersResponse
* @throws \GuzzleHttp\Exception\GuzzleException
Expand Down
Loading

0 comments on commit 55d7047

Please sign in to comment.