From b0da831fef590d03f7f31c0cad1e31639f68130d Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Tue, 13 Feb 2024 11:12:43 +0100
Subject: [PATCH] revert make create index idempotent

---
 .../OzgCloudElasticsearchService.java         | 10 +++++--
 .../ElasticsearchRemoteService.java           | 28 ++++++++++++++-----
 .../OzgCloudElasticsearchServiceTest.java     | 24 ++++++++++++++--
 .../ElasticsearchRemoteServiceITCase.java     | 22 +++++++++++++--
 4 files changed, 70 insertions(+), 14 deletions(-)

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 3a91533..57b6bd7 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,7 +54,10 @@ public class OzgCloudElasticsearchService {
 	}
 
 	public void createIndexIfMissing(String name) throws Exception {
-		remoteService.createOrUpdateIndex(name);
+		LOG.debug("{}: Check elasticsearch index...", name);
+		if (!remoteService.existsIndex(name)) {
+			remoteService.createIndex(name);
+		}
 	}
 
 	public void createSecurityRoleIfMissing(String roleName) throws Exception {
@@ -86,7 +89,10 @@ public class OzgCloudElasticsearchService {
 	}
 
 	public void deleteIndexIfExists(String indexName) throws Exception {
-		remoteService.deleteIndex(indexName);
+		LOG.debug("{}: Check delete elasticsearch index ...", indexName);
+		if (remoteService.existsIndex(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 e401f95..2fbf452 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,8 +1,11 @@
 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;
@@ -18,13 +21,24 @@ public class ElasticsearchRemoteService {
 
 	private final ElasticsearchClient client;
 
-	public void createOrUpdateIndex(String indexName) throws Exception {
+	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 {
 		try {
 			LOG.info("{}: Create or update elasticsearch index", indexName);
 			client.indices().create(builder -> builder.index(indexName));
 			LOG.info("{}: Create or update elasticsearch index successful", indexName);
 		} catch (Exception e) {
-			throw new RuntimeException("Create elasticsearch index " + indexName + "failed.", e);
+			throw new RuntimeException("Create elasticsearch index " + indexName + " failed.", e);
 		}
 	}
 
@@ -38,7 +52,7 @@ public class ElasticsearchRemoteService {
 			client.security().putRole(createPutRoleRequest(requestData));
 			LOG.info("{}: Create or update elasticsearch role successful", requestData.getName());
 		} catch (Exception e) {
-			throw new RuntimeException("Create elasticsearch role " + requestData.getName() + "failed.", e);
+			throw new RuntimeException("Create elasticsearch role " + requestData.getName() + " failed.", e);
 		}
 	}
 
@@ -70,7 +84,7 @@ public class ElasticsearchRemoteService {
 			client.security().putUser(createPutUserRequest(requestData));
 			LOG.info("{}: Create or update elasticsearch user successful", requestData.getUsername());
 		} catch (Exception e) {
-			throw new RuntimeException("Create elasticsearch user " + requestData.getUsername() + "failed.", e);
+			throw new RuntimeException("Create elasticsearch user " + requestData.getUsername() + " failed.", e);
 		}
 	}
 
@@ -92,7 +106,7 @@ public class ElasticsearchRemoteService {
 			client.indices().delete(builder -> builder.index(indexName));
 			LOG.info("{}: Delete elasticsearch index if exists successful", indexName);
 		} catch (Exception e) {
-			throw new RuntimeException("Delete elasticsearch index " + indexName + "failed.", e);
+			throw new RuntimeException("Delete elasticsearch index " + indexName + " failed.", e);
 		}
 	}
 
@@ -102,7 +116,7 @@ public class ElasticsearchRemoteService {
 			client.security().deleteRole(builder -> builder.name(roleName));
 			LOG.info("{}: Delete elasticsearch role if exists successful", roleName);
 		} catch (Exception e) {
-			throw new RuntimeException("Delete elasticsearch role " + roleName + "failed.", e);
+			throw new RuntimeException("Delete elasticsearch role " + roleName + " failed.", e);
 		}
 	}
 
@@ -112,7 +126,7 @@ public class ElasticsearchRemoteService {
 			client.security().deleteUser(builder -> builder.username(userName));
 			LOG.info("{}: Delete elasticsearch user if exists successful", userName);
 		} catch (Exception e) {
-			throw new RuntimeException("Delete elasticsearch user " + userName + "failed.", e);
+			throw new RuntimeException("Delete elasticsearch user " + userName + " failed.", e);
 		}
 	}
 }
\ No newline at end of file
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 5175eed..387a3bc 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,12 +114,22 @@ 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).createOrUpdateIndex(NAMESPACE);
+			verify(remoteService).createIndex(NAMESPACE);
 		}
 	}
 
@@ -256,7 +266,17 @@ class OzgCloudElasticsearchServiceTest {
 
 		@SneakyThrows
 		@Test
-		void shouldDeleteSecurityRoleIfExists() {
+		void shouldCheckIfIndexExists() {
+			service.deleteIndexIfExists(INDEX_NAME);
+
+			verify(remoteService).existsIndex(INDEX_NAME);
+		}
+
+		@SneakyThrows
+		@Test
+		void shouldDeleteIndexIfExists() {
+			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 2c850da..8d77416 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
@@ -55,6 +55,22 @@ 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();
 		}
 	}
 
@@ -70,7 +86,7 @@ class ElasticsearchRemoteServiceITCase {
 		@SneakyThrows
 		@Test
 		void shouldCreateIndex() {
-			service.createOrUpdateIndex(INDEX_NAME);
+			service.createIndex(INDEX_NAME);
 
 			assertThat(existsIndex()).isTrue();
 		}
@@ -111,7 +127,7 @@ class ElasticsearchRemoteServiceITCase {
 		@Test
 		void shouldCreateSecurityRole() {
 			service.createOrUpdateSecurityRole(PutRoleRequestDataTestFactory.create());
-			
+
 			assertThat(existsSecurityRole()).isTrue();
 		}
 
@@ -167,7 +183,7 @@ class ElasticsearchRemoteServiceITCase {
 		@Test
 		void shouldCreateSecurityUser() {
 			service.createOrUpdateSecurityUser(PutUserRequestDataTestFactory.create());
-			
+
 			assertThat(existsSecurityUser()).isTrue();
 		}
 
-- 
GitLab