Skip to content

Commit ae19c2c

Browse files
committed
remove helper class and add parsing to builder
1 parent fd5abc5 commit ae19c2c

File tree

3 files changed

+29
-206
lines changed

3 files changed

+29
-206
lines changed

src/Elasticsearch/ClientBuilder.php

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Elasticsearch\Common\Exceptions\InvalidArgumentException;
88
use Elasticsearch\Common\Exceptions\RuntimeException;
9+
use Elasticsearch\Common\Exceptions\ElasticCloudIdParseException;
910
use Elasticsearch\ConnectionPool\AbstractConnectionPool;
1011
use Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector;
1112
use Elasticsearch\ConnectionPool\Selectors\SelectorInterface;
@@ -17,7 +18,6 @@
1718
use Elasticsearch\Serializers\SerializerInterface;
1819
use Elasticsearch\ConnectionPool\Selectors;
1920
use Elasticsearch\Serializers\SmartSerializer;
20-
use Elasticsearch\Helper\ElasticCloudIdParser;
2121
use GuzzleHttp\Ring\Client\CurlHandler;
2222
use GuzzleHttp\Ring\Client\CurlMultiHandler;
2323
use GuzzleHttp\Ring\Client\Middleware;
@@ -375,30 +375,20 @@ public function setConnectionParams(array $params): ClientBuilder
375375
/**
376376
* Set Elastic Cloud ID to connect to Elastic Cloud
377377
*
378-
* <b>No authentication is provided</b>
379-
*
380-
* - set Hostname
381-
* - set best practices for the connection
378+
* @link https://elastic.co/cloud
382379
*
383380
* @param string $cloudId
384-
* @param string $username, optional if using Basic Authentication
385-
* @param string $password, optional if using Basic Authentication
386381
*/
387-
public function setElasticCloudId(string $cloudId, ?string $username = null, ?string $password = null)
382+
public function setElasticCloudId(string $cloudId)
388383
{
389-
$cloud = new ElasticCloudIdParser($cloudId);
390-
$hosts = [
384+
// Register the Hosts array
385+
$this->setHosts([
391386
[
392-
'host' => $cloud->getClusterDns(),
387+
'host' => $this->parseElasticCloudId($cloudId),
393388
'port' => '',
394389
'scheme' => 'https',
395-
'user' => $username,
396-
'pass' => $password,
397390
]
398-
];
399-
400-
// Register the Hosts array
401-
$this->setHosts($hosts);
391+
]);
402392

403393
// Merge best practices for the connection
404394
$this->setConnectionParams([
@@ -692,4 +682,26 @@ private function prependMissingScheme(string $host): string
692682

693683
return $host;
694684
}
685+
686+
/**
687+
* Parse the Elastic Cloud Params from the CloudId
688+
*
689+
* @param string $cloudId
690+
*
691+
* @return string
692+
*
693+
* @throws ElasticCloudIdParseException
694+
*/
695+
private function parseElasticCloudId(string $cloudId): string
696+
{
697+
try {
698+
list($name, $encoded) = explode(':', $cloudId);
699+
list($uri, $uuids) = explode('$', base64_decode($encoded));
700+
list($es,) = explode(':', $uuids);
701+
702+
return $es . '.' . $uri;
703+
} catch (\Throwable $t) {
704+
throw new ElasticCloudIdParseException('could not parse the Cloud ID:' . $cloudId);
705+
}
706+
}
695707
}

src/Elasticsearch/Helper/ElasticCloudIdParser.php

Lines changed: 0 additions & 97 deletions
This file was deleted.

tests/Elasticsearch/Tests/Helper/ElasticCloudIdParserTest.php

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)