diff --git a/bootstrap-app/src/cmr/bootstrap/api/resharding.clj b/bootstrap-app/src/cmr/bootstrap/api/resharding.clj index df56fea062..7bdb60149b 100644 --- a/bootstrap-app/src/cmr/bootstrap/api/resharding.clj +++ b/bootstrap-app/src/cmr/bootstrap/api/resharding.clj @@ -5,7 +5,9 @@ [cmr.bootstrap.api.messages :as msg] [cmr.bootstrap.api.util :as api-util] [cmr.bootstrap.services.bootstrap-service :as service] - [cmr.common.services.errors :as errors])) + [cmr.common.services.errors :as errors] + [cmr.elastic-utils.es-index-helper :as es-helper] + [cmr.elastic-utils.search.es-index :as es-index])) (defn- validate-num-shards "Validates that the number of shards is a positive integer." @@ -27,11 +29,23 @@ (when (string/blank? es-cluster-name) (errors/throw-service-error :bad-request "Empty elastic cluster name is not allowed."))) +(defn- validate-index-exists + [context index es-cluster-name] + (let [conn (es-index/context->conn context es-cluster-name)] + (when-not (or (es-helper/exists? conn index) + (es-helper/alias-exists? conn index)) + (errors/throw-service-error + :not-found + (format "Index or alias [%s] does not exist in the Elasticsearch cluster [%s]" + index + es-cluster-name))))) + (defn start "Kicks off resharding of an index." [context index params] (let [es-cluster-name (:elastic_name params) _ (validate-es-cluster-name-not-blank es-cluster-name) + _ (validate-index-exists context index es-cluster-name) num-shards-str (:num_shards params) _ (validate-num-shards num-shards-str) dispatcher (api-util/get-dispatcher context params :migrate-index)] diff --git a/system-int-test/test/cmr/system_int_test/bootstrap/reshard_index_test.clj b/system-int-test/test/cmr/system_int_test/bootstrap/reshard_index_test.clj index 45d035747f..42830da5af 100644 --- a/system-int-test/test/cmr/system_int_test/bootstrap/reshard_index_test.clj +++ b/system-int-test/test/cmr/system_int_test/bootstrap/reshard_index_test.clj @@ -62,7 +62,7 @@ (bootstrap/start-reshard-index "1_small_collections" {:synchronous false :num-shards "abc" :elastic-name gran-elastic-name})))) (testing "index must exist" (is (= {:status 404 - :errors ["Index [1_non-existing-index] does not exist."]} + :errors [(format "Index or alias [%s] does not exist in the Elasticsearch cluster [%s]" "1_non-existing-index" gran-elastic-name)]} (bootstrap/start-reshard-index "1_non-existing-index" {:synchronous false :num-shards 1 :elastic-name gran-elastic-name})))) (testing "attempting to reshard an index that is already being resharded fails" (is (= {:status 200