Skip to content

Commit

Permalink
elastic cloud provider connection credential implemented and config t…
Browse files Browse the repository at this point in the history
…ests improved
  • Loading branch information
hkulekci committed Jun 16, 2022
1 parent ee71b0f commit e7ab09e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
4 changes: 4 additions & 0 deletions src/ElasticSearch/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

/**
* @method static array hosts()
* @method static user()
* @method static password()
* @method static elasticCloudId()
* @method static apiKey()
*/
class Config
{
Expand Down
16 changes: 16 additions & 0 deletions src/ElasticSearch/Config/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/ElasticSearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down
48 changes: 48 additions & 0 deletions tests/Unit/ElasticSearch/Config/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Tests\Unit\ElasticSearch\Config;

use Illuminate\Support\Facades\Config;
use Matchish\ScoutElasticSearch\ElasticSearch\Config\Config as ScoutConfig;
use Tests\TestCase;

class ConfigTest extends TestCase
{
public function test_parse_host(): void
{
Config::set('elasticsearch.host', 'http://localhost:9200');

$config = new ScoutConfig();
$this->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());
}
}

0 comments on commit e7ab09e

Please sign in to comment.