Skip to content
Snippets Groups Projects
Commit 64549afe authored by OZGCloud's avatar OZGCloud
Browse files

Merge pull request 'OZG-4453 make elasticsearch calls idempotent' (#11) from...

Merge pull request 'OZG-4453 make elasticsearch calls idempotent' (#11) from OZG-4453-make-elasticsearch-configuration-idempotend into master

Reviewed-on: https://git.ozg-sh.de/ozgcloud-devops/operator/pulls/11
parents 8aa9891a 9ca60c8f
Branches
Tags
No related merge requests found
......@@ -54,10 +54,7 @@ public class OzgCloudElasticsearchService {
}
public void createIndexIfMissing(String name) throws Exception {
LOG.debug("{}: Check elasticsearch index...", name);
if (!remoteService.existsIndex(name)) {
remoteService.createIndex(name);
}
remoteService.createOrUpdateIndex(name);
}
public void createSecurityRoleIfMissing(String roleName) throws Exception {
......@@ -81,25 +78,16 @@ public class OzgCloudElasticsearchService {
}
public void deleteSecurityUserIfExists(String userName) throws Exception {
LOG.debug("{}: Check delete elasticsearch user...", userName);
if (remoteService.existsSecurityUser(userName)) {
remoteService.deleteSecurityUser(userName);
}
}
public void deleteSecurityRoleIfExists(String roleName) throws Exception {
LOG.debug("{}: Check delete elasticsearch role...", roleName);
if (remoteService.existsSecurityRole(roleName)) {
remoteService.deleteSecurityRole(roleName);
}
}
public void deleteIndexIfExists(String indexName) throws Exception {
LOG.debug("{}: Check delete elasticsearch index ...", indexName);
if (remoteService.existsIndex(indexName)) {
remoteService.deleteIndex(indexName);
}
}
public void createCertificateIfMissing(String namespace) {
try {
......
package de.ozgcloud.operator.common.elasticsearch;
import java.io.IOException;
import org.springframework.stereotype.Component;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.ElasticsearchException;
import co.elastic.clients.elasticsearch.security.IndicesPrivileges;
import co.elastic.clients.elasticsearch.security.PutRoleRequest;
import co.elastic.clients.elasticsearch.security.PutUserRequest;
......@@ -21,22 +18,11 @@ public class ElasticsearchRemoteService {
private final ElasticsearchClient client;
public boolean existsIndex(String index) throws Exception {
try {
LOG.debug("{}: Test if elasticsearch index exits.", index);
var exists = client.indices().exists(builder -> builder.index(index)).value();
LOG.debug("{}: Elasticsearch index exists: {}", index, exists);
return exists;
} catch (ElasticsearchException | IOException e) {
throw new RuntimeException("Error checking index '" + index, e);
}
}
public void createIndex(String indexName) throws Exception {
public void createOrUpdateIndex(String indexName) throws Exception {
try {
LOG.info("{}: Create elasticsearch index", indexName);
LOG.info("{}: Create or update elasticsearch index", indexName);
client.indices().create(builder -> builder.index(indexName));
LOG.info("{}: Create elasticsearch index successful", indexName);
LOG.info("{}: Create or update elasticsearch index successful", indexName);
} catch (Exception e) {
throw new RuntimeException("Create elasticsearch index " + indexName + "failed.", e);
}
......@@ -102,9 +88,9 @@ public class ElasticsearchRemoteService {
public void deleteIndex(String indexName) throws Exception {
try {
LOG.info("{}: Delete elasticsearch index", indexName);
LOG.info("{}: Delete elasticsearch index if exists", indexName);
client.indices().delete(builder -> builder.index(indexName));
LOG.info("{}: Delete elasticsearch index successful", indexName);
LOG.info("{}: Delete elasticsearch index if exists successful", indexName);
} catch (Exception e) {
throw new RuntimeException("Delete elasticsearch index " + indexName + "failed.", e);
}
......@@ -112,9 +98,9 @@ public class ElasticsearchRemoteService {
public void deleteSecurityRole(String roleName) throws Exception {
try {
LOG.info("{}: Delete elasticsearch role", roleName);
LOG.info("{}: Delete elasticsearch role if exists", roleName);
client.security().deleteRole(builder -> builder.name(roleName));
LOG.info("{}: Delete elasticsearch role successful", roleName);
LOG.info("{}: Delete elasticsearch role if exists successful", roleName);
} catch (Exception e) {
throw new RuntimeException("Delete elasticsearch role " + roleName + "failed.", e);
}
......@@ -122,9 +108,9 @@ public class ElasticsearchRemoteService {
public void deleteSecurityUser(String userName) throws Exception {
try {
LOG.info("{}: Delete elasticsearch user", userName);
LOG.info("{}: Delete elasticsearch user if exists", userName);
client.security().deleteUser(builder -> builder.username(userName));
LOG.info("{}: Delete elasticsearch user successful", userName);
LOG.info("{}: Delete elasticsearch user if exists successful", userName);
} catch (Exception e) {
throw new RuntimeException("Delete elasticsearch user " + userName + "failed.", e);
}
......
......@@ -114,22 +114,12 @@ class OzgCloudElasticsearchServiceTest {
@Nested
class TestCreateIndexIfMissing {
@SneakyThrows
@Test
void shouldCheckIfIndexExists() {
service.createIndexIfMissing(NAMESPACE);
verify(remoteService).existsIndex(NAMESPACE);
}
@SneakyThrows
@Test
void shouldCreateIndexIfMissing() {
when(remoteService.existsIndex(any())).thenReturn(false);
service.createIndexIfMissing(NAMESPACE);
verify(remoteService).createIndex(NAMESPACE);
verify(remoteService).createOrUpdateIndex(NAMESPACE);
}
}
......@@ -236,19 +226,9 @@ class OzgCloudElasticsearchServiceTest {
@Nested
class TestDeleteSecurityUserIfExists {
@SneakyThrows
@Test
void shouldCheckIfSecurityUserExists() {
service.deleteSecurityUserIfExists(PutUserRequestDataTestFactory.USERNAME);
verify(remoteService).existsSecurityUser(PutUserRequestDataTestFactory.USERNAME);
}
@SneakyThrows
@Test
void shouldDeleteSecurityUserIfExists() {
when(remoteService.existsSecurityUser(any())).thenReturn(true);
service.deleteSecurityUserIfExists(PutUserRequestDataTestFactory.USERNAME);
verify(remoteService).deleteSecurityUser(PutUserRequestDataTestFactory.USERNAME);
......@@ -259,19 +239,9 @@ class OzgCloudElasticsearchServiceTest {
@Nested
class TestDeleteSecurityRoleIfExists {
@SneakyThrows
@Test
void shouldCheckIfSecurityRoleExists() {
service.deleteSecurityRoleIfExists(PutRoleRequestDataTestFactory.NAME);
verify(remoteService).existsSecurityRole(PutRoleRequestDataTestFactory.NAME);
}
@SneakyThrows
@Test
void shouldDeleteSecurityRoleIfExists() {
when(remoteService.existsSecurityRole(any())).thenReturn(true);
service.deleteSecurityRoleIfExists(PutRoleRequestDataTestFactory.NAME);
verify(remoteService).deleteSecurityRole(PutRoleRequestDataTestFactory.NAME);
......@@ -284,19 +254,9 @@ class OzgCloudElasticsearchServiceTest {
private static final String INDEX_NAME = NAMESPACE;
@SneakyThrows
@Test
void shouldCheckIfIndexExists() {
service.deleteIndexIfExists(INDEX_NAME);
verify(remoteService).existsIndex(INDEX_NAME);
}
@SneakyThrows
@Test
void shouldDeleteSecurityRoleIfExists() {
when(remoteService.existsIndex(any())).thenReturn(true);
service.deleteIndexIfExists(INDEX_NAME);
verify(remoteService).deleteIndex(INDEX_NAME);
......
......@@ -55,22 +55,6 @@ class ElasticsearchRemoteServiceITCase {
void cleanup() {
deleteIndex();
}
@SneakyThrows
@Test
void shouldReturnTrue() {
var exists = service.existsIndex(INDEX_NAME);
assertThat(exists).isTrue();
}
}
@SneakyThrows
@Test
void shouldReturnFalseIfMissing() {
var exists = service.existsIndex(INDEX_NAME);
assertThat(exists).isFalse();
}
}
......@@ -86,7 +70,7 @@ class ElasticsearchRemoteServiceITCase {
@SneakyThrows
@Test
void shouldCreateIndex() {
service.createIndex(INDEX_NAME);
service.createOrUpdateIndex(INDEX_NAME);
assertThat(existsIndex()).isTrue();
}
......@@ -111,22 +95,6 @@ class ElasticsearchRemoteServiceITCase {
void cleanup() {
deleteIndex();
}
@SneakyThrows
@Test
void shouldReturnTrue() {
var exists = service.existsSecurityRole(PutRoleRequestDataTestFactory.NAME);
assertThat(exists).isTrue();
}
}
@SneakyThrows
@Test
void shouldReturnFalseIfMissing() {
var exists = service.existsSecurityRole(PutRoleRequestDataTestFactory.NAME);
assertThat(exists).isFalse();
}
}
......@@ -173,22 +141,6 @@ class ElasticsearchRemoteServiceITCase {
deleteIndex();
deleteSecurityRole();
}
@SneakyThrows
@Test
void shouldReturnTrue() {
var exists = service.existsSecurityUser(PutUserRequestDataTestFactory.USERNAME);
assertThat(exists).isTrue();
}
}
@SneakyThrows
@Test
void shouldReturnFalseIfMissing() {
var exists = service.existsSecurityUser(PutUserRequestDataTestFactory.USERNAME);
assertThat(exists).isFalse();
}
}
......@@ -196,6 +148,7 @@ class ElasticsearchRemoteServiceITCase {
private void deleteSecurityRole() {
client.security().deleteUser(builder -> builder.username(PutUserRequestDataTestFactory.USERNAME));
}
@SneakyThrows
private void deleteIndex() {
client.indices().delete(builder -> builder.index(INDEX_NAME));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment