-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from City-of-Helsinki/UHF-10990
Add elasticsearch connector plugin
- Loading branch information
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
config/schema/elasticsearch_connector.connector.helfi_connector.schema.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
plugin.plugin_configuration.elasticsearch_connector.helfi_connector: | ||
type: plugin.plugin_configuration.elasticsearch_connector.basicauth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_api_base\Plugin\ElasticSearch\Connector; | ||
|
||
use Drupal\elasticsearch_connector\Plugin\ElasticSearch\Connector\BasicAuthConnector; | ||
use Elastic\Elasticsearch\Client; | ||
use Elastic\Elasticsearch\ClientBuilder; | ||
|
||
/** | ||
* Provides an ElasticSearch connector that accepts self-signed certificates. | ||
* | ||
* @ElasticSearchConnector( | ||
* id = "helfi_connector", | ||
* label = @Translation("Helfi Connector"), | ||
* description = @Translation("ElasticSearch connector with HTTP Basic Auth that accepts self signed certificates.") | ||
* ) | ||
*/ | ||
class HelfiConnector extends BasicAuthConnector { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getClient(): Client { | ||
return ClientBuilder::create() | ||
->setHosts([$this->configuration['url']]) | ||
->setBasicAuthentication($this->configuration['username'], $this->configuration['password']) | ||
->setSSLVerification(FALSE) | ||
->build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\Tests\helfi_api_base\Kernel; | ||
|
||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\KernelTests\KernelTestBase; | ||
use Drupal\elasticsearch_connector\Plugin\search_api\backend\ElasticSearchBackend; | ||
use Drupal\helfi_api_base\Plugin\ElasticSearch\Connector\HelfiConnector; | ||
use Elastic\Elasticsearch\Client; | ||
|
||
/** | ||
* Test for elasticsearch connector plugin. | ||
*/ | ||
class ElasticsearchConnectorTest extends KernelTestBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected static $modules = [ | ||
'search_api', | ||
'elasticsearch_connector', | ||
'helfi_api_base', | ||
]; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->installEntitySchema('search_api_server'); | ||
} | ||
|
||
/** | ||
* Tests custom elasticsearch connector. | ||
*/ | ||
public function testSearchApiConnector() { | ||
// Add elasticsearch server configuration. | ||
$config = $this->config('search_api.server.default'); | ||
$config->setData([ | ||
'status' => TRUE, | ||
'id' => 'default', | ||
'name' => 'elasticsearch_server', | ||
'description' => 'Test server', | ||
'backend' => 'elasticsearch', | ||
'backend_config' => [ | ||
'connector' => 'helfi_connector', | ||
'connector_config' => [ | ||
'url' => 'http://elasticsearch.example.com:9200', | ||
'username' => '123', | ||
'password' => '456', | ||
], | ||
], | ||
]); | ||
$config->save(); | ||
|
||
/** @var \Drupal\search_api\ServerInterface $server */ | ||
$server = $this->container | ||
->get(EntityTypeManagerInterface::class) | ||
->getStorage('search_api_server') | ||
->load('default'); | ||
|
||
$backend = $server->getBackend(); | ||
assert($backend instanceof ElasticSearchBackend); | ||
$connector = $backend->getConnector(); | ||
$this->assertInstanceOf(HelfiConnector::class, $connector); | ||
$this->assertInstanceOf(Client::class, $connector->getClient()); | ||
} | ||
|
||
} |