Skip to content

Commit e201997

Browse files
committed
elastic cloud id helper with tests
1 parent ee0c3a8 commit e201997

File tree

3 files changed

+317
-0
lines changed

3 files changed

+317
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types = 1);
2+
3+
// Licensed to Elasticsearch B.V under one or more agreements.
4+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
5+
// See the LICENSE file in the project root for more information
6+
7+
namespace Elasticsearch\Common\Exceptions;
8+
9+
/**
10+
* RuntimeException
11+
*
12+
* @category Elasticsearch
13+
* @package Elasticsearch\Common\Exceptions
14+
* @author Philip Krauss <[email protected]>
15+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
16+
* @link http://elastic.co
17+
*/
18+
class ElasticCloudIdParseException extends \RuntimeException implements ElasticsearchException
19+
{
20+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php declare(strict_types = 1);
2+
3+
// Licensed to Elasticsearch B.V under one or more agreements.
4+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
5+
// See the LICENSE file in the project root for more information
6+
7+
namespace Elasticsearch\Helper;
8+
9+
use Elasticsearch\Common\Exceptions\ElasticCloudIdParseException;
10+
11+
/**
12+
* Class ElasticCloudHelper
13+
*
14+
* @category Elasticsearch
15+
* @package Elasticsearch\Helper
16+
* @author Philip Krauss <[email protected]>
17+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
18+
* @link http://elastic.co
19+
*/
20+
class ElasticCloudHelper
21+
{
22+
23+
/**
24+
* @var string
25+
*/
26+
private $cloudId;
27+
28+
/**
29+
* @var string
30+
*/
31+
private $clusterName;
32+
33+
/**
34+
* @var string
35+
*/
36+
private $clusterDns;
37+
38+
/**
39+
* @var array
40+
*/
41+
private $basicAuth = [];
42+
43+
/**
44+
* @param string $cloudId
45+
*/
46+
public function __construct(string $cloudId)
47+
{
48+
$this->cloudId = $cloudId;
49+
$this->parse();
50+
}
51+
52+
/**
53+
* Get the Elastic Cloud Id
54+
*
55+
* @return string
56+
*/
57+
public function getCloudId(): string
58+
{
59+
return $this->cloudId;
60+
}
61+
62+
/**
63+
* Get the Name of the Elastic Cloud Cluster
64+
*
65+
* @return string
66+
*/
67+
public function getClusterName(): string
68+
{
69+
return $this->clusterName;
70+
}
71+
72+
/**
73+
* Get the DNS of the Elasticsearch Cluster
74+
*
75+
* @return string
76+
*/
77+
public function getClusterDns(): string
78+
{
79+
return $this->clusterDns;
80+
}
81+
82+
/**
83+
* Get Elastic Cloud Host Array
84+
*
85+
* @link https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/configuration.html#_extended_host_configuration
86+
*
87+
* @return array
88+
*/
89+
public function getHost(): array
90+
{
91+
return [
92+
[
93+
'host' => $this->clusterDns,
94+
'port' => '',
95+
'scheme' => 'https',
96+
] + $this->basicAuth
97+
];
98+
}
99+
100+
/**
101+
* Set the Basic Authentication Credentials
102+
*
103+
* @param string $username
104+
* @param string $password
105+
*
106+
* @return void
107+
*/
108+
public function setBasicAuthentication(string $username, string $password): void
109+
{
110+
$this->basicAuth = [
111+
'user' => $username,
112+
'pass' => $password,
113+
];
114+
}
115+
116+
/**
117+
* Parse the Elastic Cloud Params from the CloudId
118+
*
119+
* @return void
120+
*
121+
* @throws Elasticsearch\Common\Exceptions\ElasticCloudIdParseException
122+
*/
123+
private function parse(): void
124+
{
125+
try
126+
{
127+
list($name, $encoded) = explode(':', $this->cloudId);
128+
list($uri, $uuids) = explode('$', base64_decode($encoded));
129+
list($es,) = explode(':', $uuids);
130+
131+
$this->clusterName = $name;
132+
$this->clusterDns = $es . '.' . $uri;
133+
}
134+
catch(\Throwable $t)
135+
{
136+
throw new ElasticCloudIdParseException('could not parse the Cloud ID:' . $this->cloudId);
137+
}
138+
}
139+
140+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php declare(strict_types = 1);
2+
3+
// Licensed to Elasticsearch B.V under one or more agreements.
4+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
5+
// See the LICENSE file in the project root for more information
6+
7+
namespace Elasticsearch\Tests\Helper;
8+
9+
use Elasticsearch\Helper\ElasticCloudHelper;
10+
use Elasticsearch\Common\Exceptions\ElasticCloudIdParseException;
11+
12+
/**
13+
* Class ElasticCloudHelperTest
14+
*
15+
* @package Elasticsearch\Tests\Helper
16+
* @author Philip Krauss <[email protected]>
17+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
18+
* @link https://elastic.co
19+
*/
20+
class ElasticCloudHelperTest extends \PHPUnit\Framework\TestCase
21+
{
22+
23+
/**
24+
* @return array
25+
*/
26+
public function cloudIdsProvider()
27+
{
28+
return [
29+
['name' => 'my-cluster-001', 'domain' => 'd-001.com', 'uuids' => 'elasticsearch'],
30+
['name' => 'my-cluster-002', 'domain' => 'd-002.net', 'uuids' => 'elasticsearch:kibana'],
31+
['name' => 'my-cluster-003', 'domain' => 'd-003.org', 'uuids' => 'elasticsearch:kibana:apm'],
32+
];
33+
}
34+
35+
/**
36+
* @return array
37+
*/
38+
public function hostArrayProvider()
39+
{
40+
return [
41+
[
42+
[
43+
'host' => 'es.d.co',
44+
'port' => '',
45+
'scheme' => 'https',
46+
]
47+
]
48+
];
49+
}
50+
51+
/**
52+
* @dataProvider cloudIdsProvider
53+
*
54+
* @covers \ElasticCloudHelperTest::parse
55+
* @covers \ElasticCloudHelperTest::getCloudId
56+
* @covers \ElasticCloudHelperTest::getClusterName
57+
* @covers \ElasticCloudHelperTest::getClusterDns
58+
*/
59+
public function testElasticCloudParserAndAccessors($name, $domain, $uuids)
60+
{
61+
$cloudId = sprintf('%s:%s', $name, base64_encode(sprintf('%s$%s', $domain, $uuids)));
62+
$dns = sprintf('elasticsearch.%s', $domain);
63+
64+
$cloud = new ElasticCloudHelper($cloudId);
65+
66+
$this->assertEquals($cloud->getCloudId(), $cloudId);
67+
$this->assertEquals($cloud->getClusterName(), $name);
68+
$this->assertEquals($cloud->getClusterDns(), $dns);
69+
}
70+
71+
/**
72+
* @depends testElasticCloudParserAndAccessors
73+
*
74+
* @dataProvider hostArrayProvider
75+
*
76+
* @covers \ElasticCloudHelperTest::getHost
77+
*/
78+
public function testGetHostWithoutBasicAuthentication($expected)
79+
{
80+
$cloudId = sprintf('name:%s', base64_encode(sprintf('d.co$es')));
81+
82+
$cloud = new ElasticCloudHelper($cloudId);
83+
84+
$host = $cloud->getHost();
85+
86+
$this->assertArrayHasKey(0, $host);
87+
$this->assertEquals(1, count($host));
88+
$this->assertEquals($expected, $host[0]);
89+
90+
$this->assertArrayNotHasKey('user', $host[0]);
91+
$this->assertArrayNotHasKey('pass', $host[0]);
92+
}
93+
94+
/**
95+
* @depends testGetHostWithoutBasicAuthentication
96+
*
97+
* @dataProvider hostArrayProvider
98+
*
99+
* @covers \ElasticCloudHelperTest::getHost
100+
*/
101+
public function testGetHostWithBasicAuthentication($expected)
102+
{
103+
$cloudId = sprintf('name:%s', base64_encode(sprintf('d.co$es')));
104+
105+
$cloud = new ElasticCloudHelper($cloudId);
106+
107+
// Hook Basic Auth Credentials in the Host config
108+
$expected['user'] = 'username';
109+
$expected['pass'] = 'super-secret-password';
110+
$cloud->setBasicAuthentication($expected['user'], $expected['pass']);
111+
112+
$host = $cloud->getHost();
113+
114+
$this->assertArrayHasKey(0, $host);
115+
$this->assertEquals(1, count($host));
116+
$this->assertEquals($expected, $host[0]);
117+
}
118+
119+
/**
120+
* @covers \ElasticCloudHelperTest::parse
121+
*/
122+
public function testCloudIdParseExceptionWithMissingCloudId()
123+
{
124+
$this->expectException(ElasticCloudIdParseException::class);
125+
$cloud = new ElasticCloudHelper('');
126+
}
127+
128+
/**
129+
* @covers \ElasticCloudHelperTest::parse
130+
*/
131+
public function testCloudIdParseExceptionWithMissingHostPart()
132+
{
133+
$this->expectException(ElasticCloudIdParseException::class);
134+
$cloud = new ElasticCloudHelper('foo:');
135+
}
136+
137+
/**
138+
* @covers \ElasticCloudHelperTest::parse
139+
*/
140+
public function testCloudIdParseExceptionWithMissingClusterName()
141+
{
142+
$this->expectException(ElasticCloudIdParseException::class);
143+
$cloud = new ElasticCloudHelper(':bar');
144+
}
145+
146+
/**
147+
* @covers \ElasticCloudHelperTest::parse
148+
*/
149+
public function testCloudIdParseExceptionWithInvalidHostPart()
150+
{
151+
$cloudId = sprintf('name:%s', base64_encode('invalid-format'));
152+
153+
$this->expectException(ElasticCloudIdParseException::class);
154+
$cloud = new ElasticCloudHelper($cloudId);
155+
}
156+
157+
}

0 commit comments

Comments
 (0)