From 1162fd3ce272282ec1e6ad12809a23e1f58ce635 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Fri, 26 Jan 2024 07:26:23 +0800 Subject: [PATCH] GET SM policies return empty list when ism config index does not exist (#1072) (#1078) (cherry picked from commit 67292ca3326602f6914ca6282e3b7f77e7556d03) Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] Co-authored-by: bowenlan-amzn --- .../get/TransportGetSMPoliciesAction.kt | 17 ++++++++++++----- .../resthandler/RestGetSnapshotManagementIT.kt | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/org/opensearch/indexmanagement/snapshotmanagement/api/transport/get/TransportGetSMPoliciesAction.kt b/src/main/kotlin/org/opensearch/indexmanagement/snapshotmanagement/api/transport/get/TransportGetSMPoliciesAction.kt index a93daf398..bcc927a09 100644 --- a/src/main/kotlin/org/opensearch/indexmanagement/snapshotmanagement/api/transport/get/TransportGetSMPoliciesAction.kt +++ b/src/main/kotlin/org/opensearch/indexmanagement/snapshotmanagement/api/transport/get/TransportGetSMPoliciesAction.kt @@ -17,6 +17,7 @@ import org.opensearch.common.settings.Settings import org.opensearch.common.util.concurrent.ThreadContext import org.opensearch.commons.authuser.User import org.opensearch.index.IndexNotFoundException +import org.opensearch.ExceptionsHelper import org.opensearch.index.query.BoolQueryBuilder import org.opensearch.index.query.ExistsQueryBuilder import org.opensearch.index.query.Operator @@ -69,12 +70,18 @@ class TransportGetSMPoliciesAction @Inject constructor( private suspend fun getAllPolicies(searchParams: SearchParams, user: User?): Pair, Long> { val searchRequest = getAllPoliciesRequest(searchParams, user) - val searchResponse: SearchResponse = try { - client.suspendUntil { search(searchRequest, it) } - } catch (e: IndexNotFoundException) { - throw OpenSearchStatusException("Snapshot management config index not found", RestStatus.NOT_FOUND) + return try { + val searchResponse = client.suspendUntil { search(searchRequest, it) } + parseGetAllPoliciesResponse(searchResponse) + } catch (e: Exception) { + val unwrappedException = ExceptionsHelper.unwrapCause(e) as Exception + if (unwrappedException is IndexNotFoundException) { + // config index hasn't been initialized, catch this here and show empty result for policies + Pair(emptyList(), 0L) + } else { + throw unwrappedException + } } - return parseGetAllPoliciesResponse(searchResponse) } private fun getAllPoliciesRequest(searchParams: SearchParams, user: User?): SearchRequest { diff --git a/src/test/kotlin/org/opensearch/indexmanagement/snapshotmanagement/resthandler/RestGetSnapshotManagementIT.kt b/src/test/kotlin/org/opensearch/indexmanagement/snapshotmanagement/resthandler/RestGetSnapshotManagementIT.kt index 0e509f394..4565b8e44 100644 --- a/src/test/kotlin/org/opensearch/indexmanagement/snapshotmanagement/resthandler/RestGetSnapshotManagementIT.kt +++ b/src/test/kotlin/org/opensearch/indexmanagement/snapshotmanagement/resthandler/RestGetSnapshotManagementIT.kt @@ -80,6 +80,20 @@ class RestGetSnapshotManagementIT : SnapshotManagementRestTestCase() { } } + @Throws(Exception::class) + @Suppress("UNCHECKED_CAST") + fun `test getting all snapshot management policies when config index doesn't exist`() { + val response = client().makeRequest( + "GET", IndexManagementPlugin.SM_POLICIES_URI, null, + BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json") + ) + val map = response.asMap() + val totalPolicies = map["total_policies"] as Int + val responsePolicies = map["policies"] as List> + assertTrue("Total policies is 0", totalPolicies == 0) + assertTrue("Response list of policies is empty", responsePolicies.isEmpty()) + } + @Throws(Exception::class) @Suppress("UNCHECKED_CAST") fun `test getting all snapshot management policies with search params`() {