/*
 * Copyright 2016 Crown Copyright
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package stroom.search.elastic;

import stroom.search.elastic.shared.ElasticConnectionConfig;
import stroom.search.elastic.shared.ElasticConnectionTestAction;
import stroom.task.server.AbstractTaskHandler;
import stroom.task.server.TaskHandlerBean;
import stroom.util.shared.SharedString;
import stroom.util.spring.StroomScope;

import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.MainResponse;
import org.springframework.context.annotation.Scope;

@TaskHandlerBean(task = ElasticConnectionTestAction.class)
@Scope(StroomScope.TASK)
public class ElasticConnectionTestHandler extends AbstractTaskHandler<ElasticConnectionTestAction, SharedString> {
    @Override
    public SharedString exec(final ElasticConnectionTestAction action) {
        try {
            final ElasticConnectionConfig connectionConfig = action.getElasticIndex().getConnectionConfig();
            final RestHighLevelClient elasticClient = new ElasticClientFactory().create(connectionConfig);

            MainResponse response = elasticClient.info(RequestOptions.DEFAULT);

            final StringBuilder sb = new StringBuilder();
            sb.append("Cluster URLs: ");
            sb.append(connectionConfig.getConnectionUrls());
            sb.append("\nCluster name: ");
            sb.append(response.getClusterName());
            sb.append("\nCluster UUID: ");
            sb.append(response.getClusterUuid());
            sb.append("\nNode name: ");
            sb.append(response.getNodeName());
            sb.append("\nVersion: ");
            sb.append(response.getVersion().getNumber());

            return SharedString.wrap(sb.toString());
        } catch (final Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

@@ -921,16 +921,68 @@
 Scope;%0A%0A
+import org.elasticsearch.ResourceNotFoundException;%0A
 import o
@@ -1125,16 +1125,131 @@
 sponse;%0A
+import org.elasticsearch.client.indices.GetIndexRequest;%0Aimport org.elasticsearch.client.indices.GetIndexResponse;%0A
 import o
@@ -2418,24 +2418,531 @@
 Number());%0A%0A
+            // Check whether the specified index exists%0A            final String indexName = action.getElasticIndex().getIndexName();%0A            final GetIndexRequest getIndexRequest = new GetIndexRequest(indexName);%0A            GetIndexResponse getIndexResponse = elasticClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);%0A            if (getIndexResponse.getIndices().length %3C 1) %7B%0A                throw new ResourceNotFoundException(%22Index '%22 + indexName + %22' was not found%22);%0A            %7D%0A%0A
             

Test ES index exists during connection test