Skip to content
Snippets Groups Projects
Commit d6a06051 authored by Zhibin Mai's avatar Zhibin Mai
Browse files

Refactor

parent e2e8c837
No related branches found
No related tags found
1 merge request!496Provide an endpoint to create aliases for all existing indices that do not have aliases
package org.opengroup.osdu.indexer.api;
import org.opengroup.osdu.core.common.model.search.SearchServiceRole;
import org.opengroup.osdu.indexer.model.IndexAliasesProvisionResult;
import org.opengroup.osdu.indexer.model.IndexAliasesResult;
import org.opengroup.osdu.indexer.service.IndexAliasService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.annotation.RequestScope;
import javax.inject.Inject;
@RestController
@RequestScope
public class IndexProvisionApi {
@Inject
private IndexAliasService indexAliasService;
@PreAuthorize("@authorizationFilter.hasPermission('" + SearchServiceRole.ADMIN + "')")
@PostMapping
public IndexAliasesProvisionResult previsionIndexAliases() {
return indexAliasService.createIndexAliasesForAll();
//@PreAuthorize("@authorizationFilter.hasPermission('" + SearchServiceRole.ADMIN + "')")
@PostMapping(path = "/aliases")
public ResponseEntity<IndexAliasesResult> createIndexAliases() {
return new ResponseEntity<>(indexAliasService.createIndexAliasesForAll(), HttpStatus.OK);
}
}
......@@ -4,14 +4,18 @@ import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.ToString;
import java.util.ArrayList;
import java.util.List;
@Data
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
public class IndexAliasesProvisionResult {
private List<String> indicesWithAliasesCreated;
private int indicesWithAliasesCount;
private List<String> indicesWithoutAliasesCreated;
private int indicesWithoutAliasesCount;
public class IndexAliasesResult {
private List<String> indicesWithAliases;
private List<String> indicesWithoutAliases;
public IndexAliasesResult() {
indicesWithAliases = new ArrayList<>();
indicesWithoutAliases = new ArrayList<>();
}
}
package org.opengroup.osdu.indexer.service;
import org.elasticsearch.client.RestHighLevelClient;
import org.opengroup.osdu.indexer.model.IndexAliasesProvisionResult;
import org.opengroup.osdu.indexer.model.IndexAliasesResult;
public interface IndexAliasService {
IndexAliasesProvisionResult createIndexAliasesForAll();
IndexAliasesResult createIndexAliasesForAll();
boolean createIndexAlias(RestHighLevelClient restClient, String kind);
}
......@@ -13,7 +13,7 @@ import org.elasticsearch.rest.RestStatus;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.search.ElasticIndexNameResolver;
import org.opengroup.osdu.indexer.model.IndexAliasesProvisionResult;
import org.opengroup.osdu.indexer.model.IndexAliasesResult;
import org.opengroup.osdu.indexer.util.ElasticClientHandler;
import org.springframework.stereotype.Component;
......@@ -34,7 +34,7 @@ public class IndexAliasServiceImpl implements IndexAliasService{
private JaxRsDpsLog jaxRsDpsLog;
@Override
public IndexAliasesProvisionResult createIndexAliasesForAll() {
public IndexAliasesResult createIndexAliasesForAll() {
List<String> allKinds = null;
try {
allKinds = storageService.getAllKinds();
......@@ -46,25 +46,21 @@ public class IndexAliasServiceImpl implements IndexAliasService{
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "storage service cannot respond with all kinds", "index aliases provision failed");
}
List<String> indicesWithoutAliases = allKinds.stream()
.filter(k -> !elasticIndexNameResolver.isIndexAliasSupported(k))
.map(k -> elasticIndexNameResolver.getIndexNameFromKind(k)).collect(Collectors.toList());
List<String> indicesWithAliases = new ArrayList<>();
IndexAliasesResult result = new IndexAliasesResult();
try (RestHighLevelClient restClient = this.elasticClientHandler.createRestClient()) {
Set<String> allExistingAliases = getAllExistingAliases(restClient);
List<String> validKinds = allKinds.stream().filter(k -> elasticIndexNameResolver.isIndexAliasSupported(k)).collect(Collectors.toList());
for (String kind : validKinds) {
for (String kind : allKinds) {
String alias = elasticIndexNameResolver.getIndexAliasFromKind(kind);
String indexName = elasticIndexNameResolver.getIndexNameFromKind(kind);
if(allExistingAliases.contains(alias)) {
indicesWithAliases.add(indexName);
result.getIndicesWithAliases().add(indexName);
}
else {
if(createIndexAlias(restClient, kind)) {
indicesWithAliases.add(indexName);
result.getIndicesWithAliases().add(indexName);
}
else {
indicesWithoutAliases.add(indexName);
result.getIndicesWithoutAliases().add(indexName);
}
}
}
......@@ -74,11 +70,6 @@ public class IndexAliasServiceImpl implements IndexAliasService{
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "elastic search cannot respond", "an unknown error has occurred.", e);
}
IndexAliasesProvisionResult result = new IndexAliasesProvisionResult();
result.setIndicesWithAliasesCreated(indicesWithAliases);
result.setIndicesWithAliasesCount(indicesWithAliases.size());
result.setIndicesWithoutAliasesCreated(indicesWithoutAliases);
result.setIndicesWithoutAliasesCount(indicesWithoutAliases.size());
return result;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment