Skip to content

Commit cd0e0fb

Browse files
Add support for ES scheme and full host path (#78)
1 parent 6d6470a commit cd0e0fb

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

src/Config/SearchEngine.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\MagentoCloud\Service\ElasticSearch;
1515
use Magento\MagentoCloud\Service\OpenSearch;
1616
use Magento\MagentoCloud\Service\ServiceException;
17+
use Magento\MagentoCloud\Service\Search\AbstractService as AbstractSearchService;
1718

1819
/**
1920
* Returns search configuration.
@@ -154,20 +155,12 @@ public function isESFamily(): bool
154155
*/
155156
private function getSearchConfig(): array
156157
{
157-
if ($osConfig = $this->openSearch->getConfiguration()) {
158-
return $this->getElasticSearchFamilyConfiguration(
159-
$this->openSearch->getFullEngineName(),
160-
$osConfig,
161-
$this->openSearch->isAuthEnabled()
162-
);
158+
if ($this->openSearch->getConfiguration()) {
159+
return $this->getElasticSearchFamilyConfiguration($this->openSearch);
163160
}
164161

165-
if ($esConfig = $this->elasticSearch->getConfiguration()) {
166-
return $this->getElasticSearchFamilyConfiguration(
167-
$this->elasticSearch->getFullEngineName(),
168-
$esConfig,
169-
$this->elasticSearch->isAuthEnabled()
170-
);
162+
if ($this->elasticSearch->getConfiguration()) {
163+
return $this->getElasticSearchFamilyConfiguration($this->elasticSearch);
171164
}
172165

173166
$solrConfig = $this->environment->getRelationship('solr');
@@ -199,22 +192,23 @@ private function getSolrConfiguration(array $config): array
199192
/**
200193
* Returns ElasticSearch configuration
201194
*
202-
* @param string $engine Engine name
203-
* @param array $config ElasticSearch/OpenSearch connection configuration
204-
* @param bool $isAuthEnabled
195+
* @param AbstractSearchService $searchService
205196
* @return array
206197
*
207198
* @throws ServiceException
208199
*/
209-
private function getElasticSearchFamilyConfiguration(string $engine, array $config, bool $isAuthEnabled): array
200+
private function getElasticSearchFamilyConfiguration(AbstractSearchService $searchService): array
210201
{
202+
$engine = $searchService->getFullEngineName();
203+
$config = $searchService->getConfiguration();
204+
211205
$elasticSearchConfig = [
212206
'engine' => $engine,
213-
"{$engine}_server_hostname" => $config['host'],
207+
"{$engine}_server_hostname" => $searchService->getHost(),
214208
"{$engine}_server_port" => $config['port'],
215209
];
216210

217-
if ($isAuthEnabled) {
211+
if ($searchService->isAuthEnabled()) {
218212
$elasticSearchConfig["{$engine}_enable_auth"] = 1;
219213
$elasticSearchConfig["{$engine}_username"] = $config['username'];
220214
$elasticSearchConfig["{$engine}_password"] = $config['password'];

src/Service/Search/AbstractService.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ public function getHost(): string
132132
throw new ServiceException(static::ENGINE_SHORT_NAME . ' service is not installed');
133133
}
134134

135+
if (!empty($this->getConfiguration()['scheme'])) {
136+
return $this->getConfiguration()['scheme'] . '://' . $this->getConfiguration()['host'];
137+
}
138+
135139
return (string)$this->getConfiguration()['host'];
136140
}
137141

@@ -204,7 +208,7 @@ public function getTemplate(): array
204208
try {
205209
$templates = $this->call(sprintf(
206210
'%s:%s/_template/platformsh_index_settings',
207-
$config['host'],
211+
$this->getHost(),
208212
$config['port']
209213
));
210214

src/Test/Unit/Config/SearchEngineTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,15 @@ public function testGetWithElasticSearch(
136136
->method('get')
137137
->with(DeployInterface::VAR_SEARCH_CONFIGURATION)
138138
->willReturn($customSearchConfig);
139-
$this->elasticSearchMock->expects($this->once())
139+
$this->elasticSearchMock->expects($this->exactly(2))
140140
->method('getConfiguration')
141141
->willReturn($esServiceConfig);
142142
$this->openSearchMock->expects($this->once())
143143
->method('getConfiguration')
144144
->willReturn([]);
145+
$this->elasticSearchMock->expects($this->once())
146+
->method('getHost')
147+
->willReturn('localhost');
145148
$this->elasticSearchMock->expects($this->once())
146149
->method('isAuthEnabled')
147150
->willReturn($authEnabled);
@@ -152,6 +155,8 @@ public function testGetWithElasticSearch(
152155
->willReturn('elasticsearch');
153156
$this->openSearchMock->expects($this->never())
154157
->method('getFullEngineName');
158+
$this->elasticSearchMock->method('getHost')
159+
->willReturn($esServiceConfig['host']);
155160

156161
$expected = ['system' => ['default' => ['catalog' => ['search' => $expected]]]];
157162

@@ -292,7 +297,7 @@ public function testGetWithOpenSearch(
292297
->method('isAuthEnabled');
293298
$this->elasticSearchMock->expects($this->never())
294299
->method('getFullEngineName');
295-
$this->openSearchMock->expects($this->once())
300+
$this->openSearchMock->expects($this->exactly(2))
296301
->method('getConfiguration')
297302
->willReturn($osServiceConfig);
298303
$this->openSearchMock->expects($this->once())
@@ -301,6 +306,8 @@ public function testGetWithOpenSearch(
301306
$this->openSearchMock->expects($this->once())
302307
->method('getFullEngineName')
303308
->willReturn('opensearch');
309+
$this->openSearchMock->method('getHost')
310+
->willReturn($osServiceConfig['host']);
304311

305312
$expected = ['system' => ['default' => ['catalog' => ['search' => $expected]]]];
306313

@@ -432,7 +439,7 @@ public function testGetWithElasticSuite(
432439
->method('get')
433440
->with(DeployInterface::VAR_SEARCH_CONFIGURATION)
434441
->willReturn($customSearchConfig);
435-
$this->elasticSearchMock->expects($this->once())
442+
$this->elasticSearchMock->expects($this->exactly(2))
436443
->method('getConfiguration')
437444
->willReturn($esServiceConfig);
438445
$this->openSearchMock->expects($this->once())
@@ -449,6 +456,8 @@ public function testGetWithElasticSuite(
449456
$this->elasticSearchMock->expects($this->once())
450457
->method('getFullEngineName')
451458
->willReturn('elasticsearch');
459+
$this->elasticSearchMock->method('getHost')
460+
->willReturn($esServiceConfig['host']);
452461

453462
$expected = ['system' => ['default' => $expected]];
454463

0 commit comments

Comments
 (0)