Skip to content

Commit

Permalink
Minor cleanup and features
Browse files Browse the repository at this point in the history
- Making both phpunit config files uniform
- Adding new "ConsulUsageTest" class
- Renaming "AbstractClient::$c" to "AbstractClient::$config"
- Updating README slightly
- The "Pretty" param in "QueryOptions" will now be output correctly
- The "QueryMeta" object now supports "AddressTranslationEnabled"
  • Loading branch information
dcarbone committed Jun 7, 2017
1 parent aaa9fbf commit 3e4dcfe
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 76 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Require Entry:
## Configuration

First, construct a [Config](./src/Config.php). This class is modeled quite closely after the
[Config Struct](https://github.com/hashicorp/consul/blob/v0.7.0/api/api.go#L104) present in the
[Consul API Subpackage](https://github.com/hashicorp/consul/blob/v0.7.0/api).
[Config Struct](https://github.com/hashicorp/consul/blob/v0.8.3/api/api.go#L161) present in the
[Consul API Subpackage](https://github.com/hashicorp/consul/blob/v0.8.3/api).

### Default Configuration

Expand Down Expand Up @@ -66,7 +66,7 @@ $config = new \DCarbone\PHPConsulAPI\Config([
#### Configuration Note:

By default, this client will attempt to locate a series of environment variables to describe much of the above
configuration properties. See [here](./src/Config.php#L446) for that list, and see [here](./src/Consul.php#L36) for
configuration properties. See [here](./src/Config.php#L450) for that list, and see [here](./src/Consul.php#L36) for
a list of the env var names.

For more advanced client configuration, such as proxy configuration, you must construct your own GuzzleHttp client
Expand All @@ -93,7 +93,7 @@ Next, construct a [Consul](./src/Consul.php) object:
$consul = new \DCarbone\PHPConsulAPI\Consul($config);
```

*NOTE*: If you do not create your own config object, [Consul](./src/Consul.php#L75) will create it's own
*NOTE*: If you do not create your own config object, [Consul](./src/Consul.php#L78) will create it's own
using [Config::newDefaultConfig()](./src/Config.php#L147) and attempt to locate a suitable HTTP Client.

Once constructed, you interact with each Consul API via it's corresponding Client class:
Expand Down
6 changes: 5 additions & 1 deletion phpunit.local.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<file>./tests/Usage/ConfigUsageTest.php</file>
</testsuite>

<testsuite name="usage-consul">
<file>./tests/Usage/ConsulUsageTest.php</file>
</testsuite>

<testsuite name="usage-kv">
<file>./tests/usage/KV/KVClientCRUDTests.php</file>
</testsuite>
Expand All @@ -38,7 +42,7 @@

<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./tests</directory>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>

Expand Down
9 changes: 8 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,22 @@
<file>./tests/Usage/ConfigUsageTest.php</file>
</testsuite>

<testsuite name="usage-consul">
<file>./tests/Usage/ConsulUsageTest.php</file>
</testsuite>

<testsuite name="usage-kv">
<file>./tests/usage/KV/KVClientCRUDTests.php</file>
</testsuite>

<testsuite name="usage-agent">
<file>./tests/Usage/Agent/AgentClientUsageTests.php</file>
</testsuite>
</testsuites>

<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./tests</directory>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>

Expand Down
14 changes: 7 additions & 7 deletions src/ACL/ACLClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ACLClient extends AbstractClient {
* )
*/
public function create(ACLEntry $acl, WriteOptions $options = null) {
$r = new Request('PUT', 'v1/acl/create', $this->c, $acl);
$r = new Request('PUT', 'v1/acl/create', $this->config, $acl);
$r->setWriteOptions($options);

/** @var \Psr\Http\Message\ResponseInterface $response */
Expand Down Expand Up @@ -64,7 +64,7 @@ public function create(ACLEntry $acl, WriteOptions $options = null) {
* )
*/
public function update(ACLEntry $acl, WriteOptions $options = null) {
$r = new Request('PUT', 'v1/acl/update', $this->c, $acl);
$r = new Request('PUT', 'v1/acl/update', $this->config, $acl);
$r->setWriteOptions($options);

list($duration, $_, $err) = $this->requireOK($this->doRequest($r));
Expand All @@ -85,7 +85,7 @@ public function update(ACLEntry $acl, WriteOptions $options = null) {
* )
*/
public function destroy($id, WriteOptions $options = null) {
$r = new Request('PUT', sprintf('v1/acl/destroy/%s', $id), $this->c);
$r = new Request('PUT', sprintf('v1/acl/destroy/%s', $id), $this->config);
$r->setWriteOptions($options);

list($duration, $_, $err) = $this->requireOK($this->doRequest($r));
Expand All @@ -107,7 +107,7 @@ public function destroy($id, WriteOptions $options = null) {
* )
*/
public function cloneACL($id, WriteOptions $options = null) {
$r = new Request('PUT', sprintf('v1/acl/clone/%s', $id), $this->c);
$r = new Request('PUT', sprintf('v1/acl/clone/%s', $id), $this->config);
$r->setWriteOptions($options);

/** @var \Psr\Http\Message\ResponseInterface $response */
Expand Down Expand Up @@ -136,7 +136,7 @@ public function cloneACL($id, WriteOptions $options = null) {
* )
*/
public function info($id, QueryOptions $options = null) {
$r = new Request('GET', sprintf('v1/acl/info/%s', $id), $this->c);
$r = new Request('GET', sprintf('v1/acl/info/%s', $id), $this->config);
$r->setQueryOptions($options);

/** @var \Psr\Http\Message\ResponseInterface $response */
Expand Down Expand Up @@ -169,7 +169,7 @@ public function info($id, QueryOptions $options = null) {
* )
*/
public function listACLs(QueryOptions $options = null) {
$r = new Request('GET', 'v1/acl/list', $this->c);
$r = new Request('GET', 'v1/acl/list', $this->config);
$r->setQueryOptions($options);

/** @var \Psr\Http\Message\ResponseInterface $response */
Expand Down Expand Up @@ -202,7 +202,7 @@ public function listACLs(QueryOptions $options = null) {
* )
*/
public function replication(QueryOptions $options = null) {
$r = new Request('GET', '/v1/acl/replication', $this->c);
$r = new Request('GET', '/v1/acl/replication', $this->config);
$r->setQueryOptions($options);

/** @var \Psr\Http\Message\ResponseInterface $response */
Expand Down
22 changes: 18 additions & 4 deletions src/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,23 @@
*/
abstract class AbstractClient {
/** @var Config */
protected $c;
protected $config;

/**
* AbstractConsulClient constructor.
* @param Config $config
*/
public function __construct(Config $config) {
$this->c = $config;
// TODO: Clone config?
$this->config = $config;
}

/**
* @return \DCarbone\PHPConsulAPI\Config
*/
public function getConfig() {
return $this->config;
}

/**
Expand Down Expand Up @@ -100,8 +109,9 @@ protected function doRequest(Request $r) {
$err = null;
try {
// If we actually have a client defined...
if (isset($this->c->HttpClient) && $this->c->HttpClient instanceof ClientInterface) {
$response = $this->c->HttpClient->send($r->toPsrRequest(), $this->c->getGuzzleRequestOptions());
if (isset($this->config->HttpClient) && $this->config->HttpClient instanceof ClientInterface) {
$response =
$this->config->HttpClient->send($r->toPsrRequest(), $this->config->getGuzzleRequestOptions());
} // Otherwise, throw error to be caught below
else {
throw new \RuntimeException('Unable to execute query as no HttpClient has been defined.');
Expand Down Expand Up @@ -148,6 +158,10 @@ protected function buildQueryMeta($duration, ResponseInterface $response, UriInt
$qm->LastContact = (int)$h;
}

if ('' !== ($h = $response->getHeaderLine('X-Consul-Translate-Addresses'))) {
$qm->AddressTranslationEnabled = (bool)$h;
}

return $qm;
}

Expand Down
34 changes: 17 additions & 17 deletions src/Agent/AgentClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AgentClient extends AbstractClient {
* )
*/
public function self() {
$r = new Request('GET', 'v1/agent/self', $this->c);
$r = new Request('GET', 'v1/agent/self', $this->config);

/** @var \Psr\Http\Message\ResponseInterface $response */
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
Expand All @@ -62,7 +62,7 @@ public function self() {
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function reload() {
$r = new Request('PUT', 'v1/agent/reload', $this->c);
$r = new Request('PUT', 'v1/agent/reload', $this->config);

return $this->requireOK($this->doRequest($r))[2];
}
Expand Down Expand Up @@ -95,7 +95,7 @@ public function nodeName() {
* )
*/
public function checks() {
$r = new Request('GET', 'v1/agent/checks', $this->c);
$r = new Request('GET', 'v1/agent/checks', $this->config);

/** @var \Psr\Http\Message\ResponseInterface $response */
list($_, $response, $err) = $this->requireOK($this->doRequest($r));
Expand Down Expand Up @@ -125,7 +125,7 @@ public function checks() {
* )
*/
public function services() {
$r = new Request('GET', 'v1/agent/services', $this->c);
$r = new Request('GET', 'v1/agent/services', $this->config);

/** @var \Psr\Http\Message\ResponseInterface $response */
list($_, $response, $err) = $this->requireOK($this->doRequest($r));
Expand Down Expand Up @@ -155,7 +155,7 @@ public function services() {
* )
*/
public function members() {
$r = new Request('GET', 'v1/agent/members', $this->c);
$r = new Request('GET', 'v1/agent/members', $this->config);

/** @var \Psr\Http\Message\ResponseInterface $response */
list($_, $response, $err) = $this->requireOK($this->doRequest($r));
Expand Down Expand Up @@ -185,7 +185,7 @@ public function members() {
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function serviceRegister(AgentServiceRegistration $agentServiceRegistration) {
$r = new Request('PUT', 'v1/agent/service/register', $this->c, $agentServiceRegistration);
$r = new Request('PUT', 'v1/agent/service/register', $this->config, $agentServiceRegistration);

return $this->requireOK($this->doRequest($r))[2];
}
Expand All @@ -197,7 +197,7 @@ public function serviceRegister(AgentServiceRegistration $agentServiceRegistrati
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function serviceDeregister($serviceID) {
$r = new Request('PUT', sprintf('v1/agent/service/deregister/%s', $serviceID), $this->c);
$r = new Request('PUT', sprintf('v1/agent/service/deregister/%s', $serviceID), $this->config);

return $this->requireOK($this->doRequest($r))[2];
}
Expand Down Expand Up @@ -265,7 +265,7 @@ public function updateTTL($checkID, $output, $status) {

$r = new Request('PUT',
sprintf('v1/agent/check/update/%s', $checkID),
$this->c,
$this->config,
new AgentCheckUpdate(['Output' => $output, 'Status' => $status]));

return $this->requireOK($this->doRequest($r))[2];
Expand All @@ -276,7 +276,7 @@ public function updateTTL($checkID, $output, $status) {
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function checkRegister(AgentCheckRegistration $agentCheckRegistration) {
$r = new Request('PUT', 'v1/agent/check/register', $this->c, $agentCheckRegistration);
$r = new Request('PUT', 'v1/agent/check/register', $this->config, $agentCheckRegistration);

return $this->requireOK($this->doRequest($r))[2];
}
Expand All @@ -286,7 +286,7 @@ public function checkRegister(AgentCheckRegistration $agentCheckRegistration) {
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function checkDeregister($checkID) {
$r = new Request('PUT', sprintf('v1/agent/check/deregister/%s', $checkID), $this->c);
$r = new Request('PUT', sprintf('v1/agent/check/deregister/%s', $checkID), $this->config);

return $this->requireOK($this->doRequest($r))[2];
}
Expand All @@ -297,7 +297,7 @@ public function checkDeregister($checkID) {
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function join($addr, $wan = false) {
$r = new Request('PUT', sprintf('v1/agent/join/%s', $addr), $this->c);
$r = new Request('PUT', sprintf('v1/agent/join/%s', $addr), $this->config);
if ($wan) {
$r->params->set('wan', '1');
}
Expand All @@ -312,7 +312,7 @@ public function join($addr, $wan = false) {
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function forceLeave($node) {
$r = new Request('PUT', sprintf('v1/agent/force-leave/%s', $node), $this->c);
$r = new Request('PUT', sprintf('v1/agent/force-leave/%s', $node), $this->config);

list($_, $_, $err) = $this->requireOK($this->doRequest($r));

Expand All @@ -325,7 +325,7 @@ public function forceLeave($node) {
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function enableServiceMaintenance($serviceID, $reason = '') {
$r = new Request('PUT', sprintf('v1/agent/service/maintenance/%s', $serviceID), $this->c);
$r = new Request('PUT', sprintf('v1/agent/service/maintenance/%s', $serviceID), $this->config);
$r->params->set('enable', 'true');
$r->params->set('reason', $reason);

Expand All @@ -339,7 +339,7 @@ public function enableServiceMaintenance($serviceID, $reason = '') {
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function disableServiceMaintenance($serviceID) {
$r = new Request('PUT', sprintf('v1/agent/service/maintenance/%s', $serviceID), $this->c);
$r = new Request('PUT', sprintf('v1/agent/service/maintenance/%s', $serviceID), $this->config);
$r->params->set('enable', 'false');

list($_, $_, $err) = $this->requireOK($this->doRequest($r));
Expand All @@ -352,7 +352,7 @@ public function disableServiceMaintenance($serviceID) {
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function enableNodeMaintenance($reason = '') {
$r = new Request('PUT', 'v1/agent/maintenance', $this->c);
$r = new Request('PUT', 'v1/agent/maintenance', $this->config);
$r->params->set('enable', 'true');
$r->params->set('reason', $reason);

Expand All @@ -365,7 +365,7 @@ public function enableNodeMaintenance($reason = '') {
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function disableNodeMaintenance() {
$r = new Request('PUT', 'v1/agent/maintenance', $this->c);
$r = new Request('PUT', 'v1/agent/maintenance', $this->config);
$r->params->set('enable', 'false');

list($_, $_, $err) = $this->requireOK($this->doRequest($r));
Expand All @@ -377,7 +377,7 @@ public function disableNodeMaintenance() {
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function leave() {
$r = new Request('PUT', 'v1/agent/leave', $this->c);
$r = new Request('PUT', 'v1/agent/leave', $this->config);

list($_, $_, $err) = $this->requireOK($this->doRequest($r));

Expand Down
Loading

0 comments on commit 3e4dcfe

Please sign in to comment.