From e7ab09e8a96b1d7b6a662ed51612df0b311084dc Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Thu, 16 Jun 2022 22:05:37 +0300 Subject: [PATCH] elastic cloud provider connection credential implemented and config tests improved --- config/elasticsearch.php | 2 + src/ElasticSearch/Config/Config.php | 4 ++ src/ElasticSearch/Config/Storage.php | 16 +++++++ src/ElasticSearchServiceProvider.php | 5 ++ .../Unit/ElasticSearch/Config/ConfigTest.php | 48 +++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 tests/Unit/ElasticSearch/Config/ConfigTest.php diff --git a/config/elasticsearch.php b/config/elasticsearch.php index 9f9826b0..58c3a91b 100644 --- a/config/elasticsearch.php +++ b/config/elasticsearch.php @@ -4,6 +4,8 @@ 'host' => env('ELASTICSEARCH_HOST'), 'user' => env('ELASTICSEARCH_USER'), 'password' => env('ELASTICSEARCH_PASSWORD'), + 'cloud_id' => env('ELASTICSEARCH_CLOUD_ID'), + 'api_key' => env('ELASTICSEARCH_API_KEY'), 'indices' => [ 'mappings' => [ 'default' => [ diff --git a/src/ElasticSearch/Config/Config.php b/src/ElasticSearch/Config/Config.php index cb2de016..f892eefd 100644 --- a/src/ElasticSearch/Config/Config.php +++ b/src/ElasticSearch/Config/Config.php @@ -4,6 +4,10 @@ /** * @method static array hosts() + * @method static user() + * @method static password() + * @method static elasticCloudId() + * @method static apiKey() */ class Config { diff --git a/src/ElasticSearch/Config/Storage.php b/src/ElasticSearch/Config/Storage.php index 5bcf29be..d583c5c9 100644 --- a/src/ElasticSearch/Config/Storage.php +++ b/src/ElasticSearch/Config/Storage.php @@ -47,6 +47,22 @@ public function password(): ?string return $this->loadConfig('password'); } + /** + * @return ?string + */ + public function elasticCloudId(): ?string + { + return $this->loadConfig('cloud_id'); + } + + /** + * @return ?string + */ + public function apiKey(): ?string + { + return $this->loadConfig('api_key'); + } + /** * @param string $path * @return mixed diff --git a/src/ElasticSearchServiceProvider.php b/src/ElasticSearchServiceProvider.php index a1ace161..6fb5dd87 100644 --- a/src/ElasticSearchServiceProvider.php +++ b/src/ElasticSearchServiceProvider.php @@ -26,6 +26,11 @@ public function register(): void $clientBuilder->setBasicAuthentication($user, Config::password()); } + if ($cloudId = Config::elasticCloudId()) { + $clientBuilder->setElasticCloudId($cloudId) + ->setApiKey(Config::apiKey()); + } + return $clientBuilder->build(); }); diff --git a/tests/Unit/ElasticSearch/Config/ConfigTest.php b/tests/Unit/ElasticSearch/Config/ConfigTest.php new file mode 100644 index 00000000..d45ce9e7 --- /dev/null +++ b/tests/Unit/ElasticSearch/Config/ConfigTest.php @@ -0,0 +1,48 @@ +assertEquals(['http://localhost:9200'], $config::hosts()); + } + + public function test_parse_multihost(): void + { + Config::set('elasticsearch.host', 'http://localhost:9200,http://localhost:9201'); + + $config = new ScoutConfig(); + $this->assertEquals(['http://localhost:9200', 'http://localhost:9201'], $config::hosts()); + } + + public function test_parse_username_password(): void + { + Config::set('elasticsearch.host', 'http://localhost:9200,http://localhost:9201'); + Config::set('elasticsearch.user', 'elastic'); + Config::set('elasticsearch.password', 'pass'); + + $config = new ScoutConfig(); + $this->assertEquals('elastic', $config::user()); + $this->assertEquals('pass', $config::password()); + } + + public function test_parse_elastic_cloud_id(): void + { + Config::set('elasticsearch.host', 'http://localhost:9200,http://localhost:9201'); + Config::set('elasticsearch.cloud_id', 'cloud-id'); + Config::set('elasticsearch.api_key', '123456'); + + $config = new ScoutConfig(); + $this->assertEquals('cloud-id', $config::elasticCloudId()); + $this->assertEquals('123456', $config::apiKey()); + } +}