|
6 | 6 |
|
7 | 7 | use Elasticsearch\Common\Exceptions\InvalidArgumentException;
|
8 | 8 | use Elasticsearch\Common\Exceptions\RuntimeException;
|
| 9 | +use Elasticsearch\Common\Exceptions\ElasticCloudIdParseException; |
9 | 10 | use Elasticsearch\ConnectionPool\AbstractConnectionPool;
|
10 | 11 | use Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector;
|
11 | 12 | use Elasticsearch\ConnectionPool\Selectors\SelectorInterface;
|
|
17 | 18 | use Elasticsearch\Serializers\SerializerInterface;
|
18 | 19 | use Elasticsearch\ConnectionPool\Selectors;
|
19 | 20 | use Elasticsearch\Serializers\SmartSerializer;
|
20 |
| -use Elasticsearch\Helper\ElasticCloudIdParser; |
21 | 21 | use GuzzleHttp\Ring\Client\CurlHandler;
|
22 | 22 | use GuzzleHttp\Ring\Client\CurlMultiHandler;
|
23 | 23 | use GuzzleHttp\Ring\Client\Middleware;
|
@@ -375,30 +375,20 @@ public function setConnectionParams(array $params): ClientBuilder
|
375 | 375 | /**
|
376 | 376 | * Set Elastic Cloud ID to connect to Elastic Cloud
|
377 | 377 | *
|
378 |
| - * <b>No authentication is provided</b> |
379 |
| - * |
380 |
| - * - set Hostname |
381 |
| - * - set best practices for the connection |
| 378 | + * @link https://elastic.co/cloud |
382 | 379 | *
|
383 | 380 | * @param string $cloudId
|
384 |
| - * @param string $username, optional if using Basic Authentication |
385 |
| - * @param string $password, optional if using Basic Authentication |
386 | 381 | */
|
387 |
| - public function setElasticCloudId(string $cloudId, ?string $username = null, ?string $password = null) |
| 382 | + public function setElasticCloudId(string $cloudId) |
388 | 383 | {
|
389 |
| - $cloud = new ElasticCloudIdParser($cloudId); |
390 |
| - $hosts = [ |
| 384 | + // Register the Hosts array |
| 385 | + $this->setHosts([ |
391 | 386 | [
|
392 |
| - 'host' => $cloud->getClusterDns(), |
| 387 | + 'host' => $this->parseElasticCloudId($cloudId), |
393 | 388 | 'port' => '',
|
394 | 389 | 'scheme' => 'https',
|
395 |
| - 'user' => $username, |
396 |
| - 'pass' => $password, |
397 | 390 | ]
|
398 |
| - ]; |
399 |
| - |
400 |
| - // Register the Hosts array |
401 |
| - $this->setHosts($hosts); |
| 391 | + ]); |
402 | 392 |
|
403 | 393 | // Merge best practices for the connection
|
404 | 394 | $this->setConnectionParams([
|
@@ -692,4 +682,26 @@ private function prependMissingScheme(string $host): string
|
692 | 682 |
|
693 | 683 | return $host;
|
694 | 684 | }
|
| 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 | + } |
695 | 707 | }
|
0 commit comments