Skip to content

Commit 3e4dcfe

Browse files
committed
Minor cleanup and features
- 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"
1 parent aaa9fbf commit 3e4dcfe

19 files changed

+199
-76
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ Require Entry:
3030
## Configuration
3131

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

3636
### Default Configuration
3737

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

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

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

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

9999
Once constructed, you interact with each Consul API via it's corresponding Client class:

phpunit.local.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
<file>./tests/Usage/ConfigUsageTest.php</file>
2828
</testsuite>
2929

30+
<testsuite name="usage-consul">
31+
<file>./tests/Usage/ConsulUsageTest.php</file>
32+
</testsuite>
33+
3034
<testsuite name="usage-kv">
3135
<file>./tests/usage/KV/KVClientCRUDTests.php</file>
3236
</testsuite>
@@ -38,7 +42,7 @@
3842

3943
<filter>
4044
<whitelist addUncoveredFilesFromWhitelist="true">
41-
<directory suffix=".php">./tests</directory>
45+
<directory suffix=".php">./src</directory>
4246
</whitelist>
4347
</filter>
4448

phpunit.xml.dist

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,22 @@
2727
<file>./tests/Usage/ConfigUsageTest.php</file>
2828
</testsuite>
2929

30+
<testsuite name="usage-consul">
31+
<file>./tests/Usage/ConsulUsageTest.php</file>
32+
</testsuite>
33+
3034
<testsuite name="usage-kv">
3135
<file>./tests/usage/KV/KVClientCRUDTests.php</file>
36+
</testsuite>
3237

38+
<testsuite name="usage-agent">
39+
<file>./tests/Usage/Agent/AgentClientUsageTests.php</file>
3340
</testsuite>
3441
</testsuites>
3542

3643
<filter>
3744
<whitelist addUncoveredFilesFromWhitelist="true">
38-
<directory suffix=".php">./tests</directory>
45+
<directory suffix=".php">./src</directory>
3946
</whitelist>
4047
</filter>
4148

src/ACL/ACLClient.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ACLClient extends AbstractClient {
3636
* )
3737
*/
3838
public function create(ACLEntry $acl, WriteOptions $options = null) {
39-
$r = new Request('PUT', 'v1/acl/create', $this->c, $acl);
39+
$r = new Request('PUT', 'v1/acl/create', $this->config, $acl);
4040
$r->setWriteOptions($options);
4141

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

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

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

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

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

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

208208
/** @var \Psr\Http\Message\ResponseInterface $response */

src/AbstractClient.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,23 @@
2727
*/
2828
abstract class AbstractClient {
2929
/** @var Config */
30-
protected $c;
30+
protected $config;
3131

3232
/**
3333
* AbstractConsulClient constructor.
3434
* @param Config $config
3535
*/
3636
public function __construct(Config $config) {
37-
$this->c = $config;
37+
// TODO: Clone config?
38+
39+
$this->config = $config;
40+
}
41+
42+
/**
43+
* @return \DCarbone\PHPConsulAPI\Config
44+
*/
45+
public function getConfig() {
46+
return $this->config;
3847
}
3948

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

161+
if ('' !== ($h = $response->getHeaderLine('X-Consul-Translate-Addresses'))) {
162+
$qm->AddressTranslationEnabled = (bool)$h;
163+
}
164+
151165
return $qm;
152166
}
153167

src/Agent/AgentClient.php

+17-17
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class AgentClient extends AbstractClient {
3737
* )
3838
*/
3939
public function self() {
40-
$r = new Request('GET', 'v1/agent/self', $this->c);
40+
$r = new Request('GET', 'v1/agent/self', $this->config);
4141

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

6767
return $this->requireOK($this->doRequest($r))[2];
6868
}
@@ -95,7 +95,7 @@ public function nodeName() {
9595
* )
9696
*/
9797
public function checks() {
98-
$r = new Request('GET', 'v1/agent/checks', $this->c);
98+
$r = new Request('GET', 'v1/agent/checks', $this->config);
9999

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

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

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

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

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

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

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

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

291291
return $this->requireOK($this->doRequest($r))[2];
292292
}
@@ -297,7 +297,7 @@ public function checkDeregister($checkID) {
297297
* @return \DCarbone\PHPConsulAPI\Error|null
298298
*/
299299
public function join($addr, $wan = false) {
300-
$r = new Request('PUT', sprintf('v1/agent/join/%s', $addr), $this->c);
300+
$r = new Request('PUT', sprintf('v1/agent/join/%s', $addr), $this->config);
301301
if ($wan) {
302302
$r->params->set('wan', '1');
303303
}
@@ -312,7 +312,7 @@ public function join($addr, $wan = false) {
312312
* @return \DCarbone\PHPConsulAPI\Error|null
313313
*/
314314
public function forceLeave($node) {
315-
$r = new Request('PUT', sprintf('v1/agent/force-leave/%s', $node), $this->c);
315+
$r = new Request('PUT', sprintf('v1/agent/force-leave/%s', $node), $this->config);
316316

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)