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
No related branches found
No related tags found
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,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) {
......
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);
......
......@@ -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,105 +40,73 @@ 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() {
......@@ -146,70 +114,55 @@ class ElasticsearchRemoteServiceITCase {
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() {
......@@ -217,134 +170,134 @@ class ElasticsearchRemoteServiceITCase {
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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment