Skip to content

Commit f5adfab

Browse files
committed
Updating Catalog stuff
1 parent 03bd49e commit f5adfab

File tree

5 files changed

+189
-5
lines changed

5 files changed

+189
-5
lines changed

docs/CATALOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# PHP Consul API Catalog
2+
3+
*This class is still under heavy development*
4+
5+
All interactions with the [`v1/catalog`](https://www.consul.io/docs/agent/http/catalog.html) endpoint are done
6+
via the [CatalogClient](./src/Catalog/CatalogClient.php) class.
7+
8+
If you have constructed a [Client](./src/Client.php) object, this is done as so:
9+
10+
```php
11+
$catalog = $client->Catalog();
12+
```
13+
14+
## Actions
15+
16+
###

src/Catalog/CatalogClient.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,105 @@
2020
use DCarbone\PHPConsulAPI\QueryMeta;
2121
use DCarbone\PHPConsulAPI\QueryOptions;
2222
use DCarbone\PHPConsulAPI\Request;
23+
use DCarbone\PHPConsulAPI\WriteMeta;
24+
use DCarbone\PHPConsulAPI\WriteOptions;
2325

2426
/**
2527
* Class CatalogClient
2628
* @package DCarbone\PHPConsulAPI\Catalog
2729
*/
2830
class CatalogClient extends AbstractConsulClient
2931
{
32+
/**
33+
* @param CatalogRegistration $catalogRegistration
34+
* @param WriteOptions|null $writeOptions
35+
* @return array(
36+
* @type WriteMeta write meta data
37+
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
38+
* )
39+
*/
40+
public function register(CatalogRegistration $catalogRegistration, WriteOptions $writeOptions = null)
41+
{
42+
$r = new Request('put', 'v1/catalog/register', $this->_Config, $catalogRegistration);
43+
$r->setWriteOptions($writeOptions);
44+
45+
list($duration, $_, $err) = $this->requireOK($this->doRequest($r));
46+
$wm = $this->buildWriteMeta($duration);
47+
48+
return [$wm, $err];
49+
}
50+
51+
/**
52+
* @param CatalogDeregistration $catalogDeregistration
53+
* @param WriteOptions|null $writeOptions
54+
* @return array(
55+
* @type WriteMeta write meta data
56+
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
57+
* )
58+
*/
59+
public function deregister(CatalogDeregistration $catalogDeregistration, WriteOptions $writeOptions = null)
60+
{
61+
$r = new Request('put', 'v1/catalog/deregister', $this->_Config, $catalogDeregistration);
62+
$r->setWriteOptions($writeOptions);
63+
64+
list($duration, $_, $err) = $this->requireOK($this->doRequest($r));
65+
$wm = $this->buildWriteMeta($duration);
66+
67+
return [$wm, $err];
68+
}
69+
70+
/**
71+
* @return array(
72+
* @type string[]|null list of datacenters or null on error
73+
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
74+
* )
75+
*/
76+
public function datacenters()
77+
{
78+
$r = new Request('get', 'v1/catalog/datacenters', $this->_Config);
79+
80+
list($_, $response, $err) = $this->requireOK($this->doRequest($r));
81+
82+
if (null !== $err)
83+
return [null, $err];
84+
85+
return $this->decodeBody($response);
86+
}
87+
88+
/**
89+
* @param QueryOptions|null $queryOptions
90+
* @return array(
91+
* @type CatalogNode[]|null array of catalog nodes or null on error
92+
* @type QueryMeta query metadata
93+
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
94+
* )
95+
*/
96+
public function nodes(QueryOptions $queryOptions = null)
97+
{
98+
$r = new Request('get', 'v1/catalog/nodes', $this->_Config);
99+
$r->setQueryOptions($queryOptions);
100+
101+
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
102+
$qm = $this->buildQueryMeta($duration, $response);
103+
104+
if (null !== $err)
105+
return [null, $qm, $err];
106+
107+
list($data, $err) = $this->decodeBody($response);
108+
109+
if (null !== $err)
110+
return [null, $qm, $err];
111+
112+
$nodes = array();
113+
foreach($data as $v)
114+
{
115+
$node = new CatalogNode($v);
116+
$nodes[$node->getNode()] = $node;
117+
}
118+
119+
return [$nodes, $qm, null];
120+
}
121+
30122
/**
31123
* @param QueryOptions|null $queryOptions
32124
* @return array(
@@ -50,4 +142,70 @@ public function services(QueryOptions $queryOptions = null)
50142

51143
return [$data, $qm, $err];
52144
}
145+
146+
/**
147+
* @param string $service
148+
* @param string $tag
149+
* @param QueryOptions|null $queryOptions
150+
* @return array(
151+
* @type CatalogService[]|null array of services or null on error
152+
* @type QueryMeta query metadata
153+
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
154+
* )
155+
*/
156+
public function service($service, $tag = '', QueryOptions $queryOptions = null)
157+
{
158+
$r = new Request('get', sprintf('v1/catalog/service/%s', rawurlencode($service)), $this->_Config);
159+
$r->setQueryOptions($queryOptions);
160+
if ('' !== $tag)
161+
$r->params()->set('tag', $tag);
162+
163+
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
164+
$qm = $this->buildQueryMeta($duration, $response);
165+
166+
if (null !== $err)
167+
return [null, $qm, $err];
168+
169+
list($data, $err) = $this->decodeBody($response);
170+
171+
if (null !== $err)
172+
return [null, $qm, $err];
173+
174+
$services = array();
175+
foreach($data as $v)
176+
{
177+
$service = new CatalogService($v);
178+
$services[$service->getServiceID()] = $service;
179+
}
180+
181+
return [$services, $qm, null];
182+
}
183+
184+
/**
185+
* @param string $node
186+
* @param QueryOptions|null $queryOptions
187+
* @return array(
188+
* @type CatalogNode node or null on error
189+
* @type QueryMeta query metadata
190+
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
191+
* )
192+
*/
193+
public function node($node, QueryOptions $queryOptions = null)
194+
{
195+
$r = new Request('get', sprintf('v1/catalog/node/%s', rawurlencode($node)), $this->_Config);
196+
$r->setQueryOptions($queryOptions);
197+
198+
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
199+
$qm = $this->buildQueryMeta($duration, $response);
200+
201+
if (null !== $err)
202+
return [null, $qm, $err];
203+
204+
list($data, $err) = $this->decodeBody($response);
205+
206+
if (null !== $err)
207+
return [null, $qm, $err];
208+
209+
return [new CatalogNode($data), $qm, null];
210+
}
53211
}

src/DateTime.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ class DateTime extends \DateTime implements \JsonSerializable
2525
/** @var string */
2626
private static $_defaultFormat = 'Y-m-d\TH:i:s.uO';
2727

28+
/**
29+
* @return string
30+
*/
31+
public static function getDefaultFormat()
32+
{
33+
return self::$_defaultFormat;
34+
}
35+
2836
/**
2937
* @param string $format
3038
*/
@@ -52,7 +60,7 @@ public static function setDefaultFormat($format)
5260
/**
5361
* @return string
5462
*/
55-
public function defaultFormat()
63+
public function formatDefault()
5664
{
5765
return $this->format(self::$_defaultFormat);
5866
}

src/Error.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818

1919
/**
20+
* TODO: Make this better...
21+
*
2022
* Class Error
2123
* @package DCarbone\PHPConsulAPI
2224
*/

src/HttpResponse.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
class HttpResponse
2626
{
2727
/** @var string */
28-
public $url = null;
28+
public $url = '';
2929
/** @var int */
3030
public $httpCode = 0;
3131
/** @var string */
@@ -77,9 +77,6 @@ class HttpResponse
7777
/** @var string */
7878
public $requestHeader = '';
7979

80-
/** @var array */
81-
private $_requestHeaderArray = null;
82-
8380
/** @var array */
8481
public $responseHeaders = array();
8582
/** @var string */
@@ -88,6 +85,9 @@ class HttpResponse
8885
/** @var string */
8986
public $curlError;
9087

88+
/** @var array */
89+
private $_requestHeaderArray = null;
90+
9191
/**
9292
* HttpResponse constructor.
9393
* @param string|bool $response

0 commit comments

Comments
 (0)