diff --git a/ozgcloud-elasticsearch-operator/src/main/java/de/ozgcloud/operator/OzgCloudElasticsearchService.java b/ozgcloud-elasticsearch-operator/src/main/java/de/ozgcloud/operator/OzgCloudElasticsearchService.java index d7207e433215e4bbdbf7a5254b0aa9d16dfbee09..f823b17c2e75ecaa65274391cc45910f431828d6 100644 --- a/ozgcloud-elasticsearch-operator/src/main/java/de/ozgcloud/operator/OzgCloudElasticsearchService.java +++ b/ozgcloud-elasticsearch-operator/src/main/java/de/ozgcloud/operator/OzgCloudElasticsearchService.java @@ -54,17 +54,11 @@ 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 { - LOG.debug("{}: Check elasticsearch role...", roleName); - if (!remoteService.existsSecurityRole(roleName)) { - remoteService.createSecurityRole(buildPutRoleRequestData(roleName)); - } + remoteService.createSecurityRole(buildPutRoleRequestData(roleName)); } PutRoleRequestData buildPutRoleRequestData(String roleName) { @@ -76,10 +70,7 @@ public class OzgCloudElasticsearchService { } public void createSecurityUserIfMissing(String namespace, String password) throws Exception { - LOG.debug("{}: Check elasticsearch user...", namespace); - if (!remoteService.existsSecurityUser(namespace)) { - remoteService.createSecurityUser(buildPutUserRequestData(namespace, password)); - } + remoteService.createSecurityUser(buildPutUserRequestData(namespace, password)); } PutUserRequestData buildPutUserRequestData(String namespace, String password) { @@ -87,24 +78,15 @@ public class OzgCloudElasticsearchService { } public void deleteSecurityUserIfExists(String userName) throws Exception { - LOG.debug("{}: Check delete elasticsearch user...", userName); - if (remoteService.existsSecurityUser(userName)) { - remoteService.deleteSecurityUser(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); - } + remoteService.deleteSecurityRole(roleName); } public void deleteIndexIfExists(String indexName) throws Exception { - LOG.debug("{}: Check delete elasticsearch index ...", indexName); - if (remoteService.existsIndex(indexName)) { - remoteService.deleteIndex(indexName); - } + remoteService.deleteIndex(indexName); } public void createCertificateIfMissing(String namespace) { diff --git a/ozgcloud-elasticsearch-operator/src/main/java/de/ozgcloud/operator/common/elasticsearch/ElasticsearchRemoteService.java b/ozgcloud-elasticsearch-operator/src/main/java/de/ozgcloud/operator/common/elasticsearch/ElasticsearchRemoteService.java index 12c4c5db85dff6deeb1d815386f13b54823d6b38..ace393085d14c37b8ca6691078da1b7d8e12ac77 100644 --- a/ozgcloud-elasticsearch-operator/src/main/java/de/ozgcloud/operator/common/elasticsearch/ElasticsearchRemoteService.java +++ b/ozgcloud-elasticsearch-operator/src/main/java/de/ozgcloud/operator/common/elasticsearch/ElasticsearchRemoteService.java @@ -1,11 +1,8 @@ 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,36 +18,21 @@ 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); } } - public boolean existsSecurityRole(String roleName) throws Exception { - return !client.security().getRole(builder -> builder.name(roleName)).result().isEmpty(); - } - public void createSecurityRole(PutRoleRequestData requestData) throws Exception { try { - LOG.info("{}: Create elasticsearch role ", requestData.getName()); + LOG.info("{}: Create or update elasticsearch role ", requestData.getName()); client.security().putRole(createPutRoleRequest(requestData)); - LOG.info("{}: Create elasticsearch role successful", requestData.getName()); + LOG.info("{}: Create or update elasticsearch role successful", requestData.getName()); } catch (Exception e) { throw new RuntimeException("Create elasticsearch role " + requestData.getName() + "failed.", e); } @@ -74,15 +56,11 @@ public class ElasticsearchRemoteService { return builder; } - public boolean existsSecurityUser(String userName) throws Exception { - return !client.security().getUser(builder -> builder.username(userName)).result().isEmpty(); - } - public void createSecurityUser(PutUserRequestData requestData) throws Exception { try { - LOG.info("{}: Create elasticsearch user", requestData.getUsername()); + LOG.info("{}: Create or update elasticsearch user", requestData.getUsername()); client.security().putUser(createPutUserRequest(requestData)); - LOG.info("{}: Create elasticsearch user successful", requestData.getUsername()); + LOG.info("{}: Create or update elasticsearch user successful", requestData.getUsername()); } catch (Exception e) { throw new RuntimeException("Create elasticsearch user " + requestData.getUsername() + "failed.", e); } @@ -102,9 +80,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 +90,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 +100,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); } diff --git a/ozgcloud-elasticsearch-operator/src/test/java/de/ozgcloud/operator/OzgCloudElasticsearchServiceTest.java b/ozgcloud-elasticsearch-operator/src/test/java/de/ozgcloud/operator/OzgCloudElasticsearchServiceTest.java index 8d0c3734879f08925a6dce3fa0fcc6ee97dc711d..f591a335d88d3a85b338592c9cd8064670c8d6a4 100644 --- a/ozgcloud-elasticsearch-operator/src/test/java/de/ozgcloud/operator/OzgCloudElasticsearchServiceTest.java +++ b/ozgcloud-elasticsearch-operator/src/test/java/de/ozgcloud/operator/OzgCloudElasticsearchServiceTest.java @@ -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); } } @@ -139,18 +129,9 @@ class OzgCloudElasticsearchServiceTest { private final PutRoleRequestData putRoleRequest = PutRoleRequestDataTestFactory.create(); - @SneakyThrows - @Test - void shouldCheckIfSecurityRoleExists() { - service.createSecurityRoleIfMissing(NAMESPACE); - - verify(remoteService).existsSecurityRole(NAMESPACE); - } - @SneakyThrows @Test void shouldCreateSecurityRoleIfMissing() { - when(remoteService.existsSecurityRole(any())).thenReturn(false); doReturn(putRoleRequest).when(service).buildPutRoleRequestData(any()); service.createSecurityRoleIfMissing(NAMESPACE); @@ -200,18 +181,9 @@ class OzgCloudElasticsearchServiceTest { private final PutUserRequestData putUserRequestData = PutUserRequestDataTestFactory.create(); - @SneakyThrows - @Test - void shouldCheckIfSecurityUserExists() { - service.createSecurityUserIfMissing(NAMESPACE, PutUserRequestDataTestFactory.PASSWORD); - - verify(remoteService).existsSecurityUser(NAMESPACE); - } - @SneakyThrows @Test void shouldCreateSecurityUserIfMissing() { - when(remoteService.existsSecurityUser(any())).thenReturn(false); doReturn(putUserRequestData).when(service).buildPutUserRequestData(any(), any()); service.createSecurityUserIfMissing(NAMESPACE, PutUserRequestDataTestFactory.PASSWORD); @@ -254,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); @@ -277,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); @@ -302,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); diff --git a/ozgcloud-elasticsearch-operator/src/test/java/de/ozgcloud/operator/common/elasticsearch/ElasticsearchRemoteServiceITCase.java b/ozgcloud-elasticsearch-operator/src/test/java/de/ozgcloud/operator/common/elasticsearch/ElasticsearchRemoteServiceITCase.java index 5ac49af8471222eff42202a3ec63a15a14cd579a..329e3fcb59e055bc7e568d3f69c4f3bb58a3658e 100644 --- a/ozgcloud-elasticsearch-operator/src/test/java/de/ozgcloud/operator/common/elasticsearch/ElasticsearchRemoteServiceITCase.java +++ b/ozgcloud-elasticsearch-operator/src/test/java/de/ozgcloud/operator/common/elasticsearch/ElasticsearchRemoteServiceITCase.java @@ -21,9 +21,9 @@ import de.ozgcloud.operator.PutUserRequestDataTestFactory; import lombok.SneakyThrows; class ElasticsearchRemoteServiceITCase { - + private static final String INDEX_NAME = "test_index"; - + private final ElasticsearchClient client = ElasticsearchTestClient.create(); private final ElasticsearchRemoteService service = new ElasticsearchRemoteService(client); @@ -31,7 +31,7 @@ class ElasticsearchRemoteServiceITCase { public static void startContainer() { ElasticsearchTestClient.ELASTICSEARCH_CONTAINER.start(); } - + @AfterAll public static void stopContainer() { ElasticsearchTestClient.ELASTICSEARCH_CONTAINER.stop(); @@ -40,311 +40,264 @@ class ElasticsearchRemoteServiceITCase { @DisplayName("Exists index") @Nested class TestExistsIndex { - + @DisplayName("on existing") @Nested class TestOnExisting { - + @SneakyThrows @BeforeEach private void initIndex() { createIndex(); } - + @AfterEach 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(); } } - + @DisplayName("Create index") @Nested class TestCreateIndex { - + @AfterEach void cleanup() { deleteIndex(); } - + @SneakyThrows @Test void shouldCreateIndex() { - service.createIndex(INDEX_NAME); - + service.createOrUpdateIndex(INDEX_NAME); + assertThat(existsIndex()).isTrue(); } } - + @DisplayName("Exists security role") @Nested class TestExistsSecurityRole { - + @DisplayName("on existing") @Nested class TestOnExisting { - + @SneakyThrows @BeforeEach private void initSecurityRole() { createIndex(); client.security().putRole(service.createPutRoleRequest(PutRoleRequestDataTestFactory.create())); } - + @AfterEach 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(); } } - + @DisplayName("Create security role") @Nested class TestCreateSecurityRole { - + @AfterEach void cleanup() { deleteSecurityRole(); } - + @SneakyThrows @Test void shouldCreateSecurityRole() { service.createSecurityRole(PutRoleRequestDataTestFactory.create()); - + assertThat(existsSecurityRole()).isTrue(); } - + @SneakyThrows private void deleteSecurityRole() { client.security().deleteRole(builder -> builder.name(PutRoleRequestDataTestFactory.NAME)); } } - + @DisplayName("Exists security user") @Nested class TestExistsSecurityUser { - + @DisplayName("on existing") @Nested class TestOnExisting { - + @SneakyThrows @BeforeEach private void initSecurityUser() { createIndex(); client.security().putUser(service.createPutUserRequest(PutUserRequestDataTestFactory.create())); } - + @AfterEach void cleanup() { 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(); } } - + @SneakyThrows private void deleteSecurityRole() { client.security().deleteUser(builder -> builder.username(PutUserRequestDataTestFactory.USERNAME)); } + @SneakyThrows private void deleteIndex() { client.indices().delete(builder -> builder.index(INDEX_NAME)); } - + @DisplayName("Create security user") @Nested class TestCreateSecurityUser { - + @AfterEach void cleanup() { deleteSecurityUser(); } - + @SneakyThrows @Test void shouldCreateSecurityUser() { service.createSecurityUser(PutUserRequestDataTestFactory.create()); - + assertThat(existsSecurityUser()).isTrue(); } - + @SneakyThrows private boolean existsSecurityUser() { return !client.security().getUser(builder -> builder.username(PutUserRequestDataTestFactory.USERNAME)).result().isEmpty(); } - + @SneakyThrows private void deleteSecurityUser() { client.security().deleteUser(builder -> builder.username(PutUserRequestDataTestFactory.USERNAME)); } } - + @DisplayName("Delete index") @Nested class TestDeleteIndex { - + @BeforeEach void init() { createIndex(); } - + @SneakyThrows @Test - void shouldDeleteIfExists() { + void shouldDeleteIfExists() { service.deleteIndex(INDEX_NAME); - + assertThat(existsIndex()).isFalse(); } } - + @DisplayName("Delete security role") @Nested class TestDeleteSecurityRole { - + @BeforeEach void init() { createIndex(); createSecurityRole(); } - + @AfterEach void cleanup() { deleteIndex(); } - + @SneakyThrows @Test void shouldDeleteIfExists() { assertThat(existsSecurityRole()).isTrue(); - + service.deleteSecurityRole(PutRoleRequestDataTestFactory.NAME); - + assertThat(existsSecurityRole()).isFalse(); } } - + @DisplayName("Delete security user") @Nested class TestDeleteSecurityUser { - + @BeforeEach void init() { createIndex(); createSecurityUser(); } - + @AfterEach void cleanup() { deleteIndex(); } - + @SneakyThrows @Test void shouldDeleteIfExists() { assertThat(existsSecurityUser()).isTrue(); - + service.deleteSecurityUser(PutUserRequestDataTestFactory.USERNAME); - + assertThat(existsSecurityUser()).isFalse(); } } - + @SneakyThrows private boolean existsIndex() { return client.indices().exists(ExistsRequest.of(builder -> builder.index(INDEX_NAME))).value(); } - + @SneakyThrows private void createIndex() { client.indices().create(builder -> builder.index(INDEX_NAME)); } - + @SneakyThrows private void createSecurityRole() { client.security().putRole(this::buildRequest); } - + private PutRoleRequest.Builder buildRequest(PutRoleRequest.Builder requestBuilder) { requestBuilder.name(PutRoleRequestDataTestFactory.NAME); requestBuilder.indices(this::buildIndicesPrivilegesRequest); return requestBuilder; } - + private IndicesPrivileges.Builder buildIndicesPrivilegesRequest(IndicesPrivileges.Builder builder) { builder.names(IndicesPrivilegesDataTestFactory.NAME); builder.privileges(IndicesPrivilegesDataTestFactory.PRIVILEGES); - + return builder; } - + @SneakyThrows private boolean existsSecurityRole() { return !client.security().getRole(builder -> builder.name(PutRoleRequestDataTestFactory.NAME)).result().isEmpty(); } - + @SneakyThrows private void createSecurityUser() { client.security().putUser(this::buildPutUserRequest); } - + private PutUserRequest.Builder buildPutUserRequest(PutUserRequest.Builder builder) { builder.username(PutUserRequestDataTestFactory.USERNAME); builder.roles(PutUserRequestDataTestFactory.ROLES); builder.password(PutUserRequestDataTestFactory.PASSWORD); - + return builder; } - + @SneakyThrows private boolean existsSecurityUser() { return !client.security().getUser(builder -> builder.username(PutUserRequestDataTestFactory.USERNAME)).result().isEmpty();