Skip to content

Commit 76da35e

Browse files
GET SM policies return empty list when ism config index does not exist (#1072)
(cherry picked from commit 67292ca) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 8cea155 commit 76da35e

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/main/kotlin/org/opensearch/indexmanagement/snapshotmanagement/api/transport/get/TransportGetSMPoliciesAction.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.opensearch.common.settings.Settings
1717
import org.opensearch.common.util.concurrent.ThreadContext
1818
import org.opensearch.commons.authuser.User
1919
import org.opensearch.index.IndexNotFoundException
20+
import org.opensearch.ExceptionsHelper
2021
import org.opensearch.index.query.BoolQueryBuilder
2122
import org.opensearch.index.query.ExistsQueryBuilder
2223
import org.opensearch.index.query.Operator
@@ -69,12 +70,18 @@ class TransportGetSMPoliciesAction @Inject constructor(
6970

7071
private suspend fun getAllPolicies(searchParams: SearchParams, user: User?): Pair<List<SMPolicy>, Long> {
7172
val searchRequest = getAllPoliciesRequest(searchParams, user)
72-
val searchResponse: SearchResponse = try {
73-
client.suspendUntil { search(searchRequest, it) }
74-
} catch (e: IndexNotFoundException) {
75-
throw OpenSearchStatusException("Snapshot management config index not found", RestStatus.NOT_FOUND)
73+
return try {
74+
val searchResponse = client.suspendUntil { search(searchRequest, it) }
75+
parseGetAllPoliciesResponse(searchResponse)
76+
} catch (e: Exception) {
77+
val unwrappedException = ExceptionsHelper.unwrapCause(e) as Exception
78+
if (unwrappedException is IndexNotFoundException) {
79+
// config index hasn't been initialized, catch this here and show empty result for policies
80+
Pair(emptyList(), 0L)
81+
} else {
82+
throw unwrappedException
83+
}
7684
}
77-
return parseGetAllPoliciesResponse(searchResponse)
7885
}
7986

8087
private fun getAllPoliciesRequest(searchParams: SearchParams, user: User?): SearchRequest {

src/test/kotlin/org/opensearch/indexmanagement/snapshotmanagement/resthandler/RestGetSnapshotManagementIT.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ class RestGetSnapshotManagementIT : SnapshotManagementRestTestCase() {
8080
}
8181
}
8282

83+
@Throws(Exception::class)
84+
@Suppress("UNCHECKED_CAST")
85+
fun `test getting all snapshot management policies when config index doesn't exist`() {
86+
val response = client().makeRequest(
87+
"GET", IndexManagementPlugin.SM_POLICIES_URI, null,
88+
BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json")
89+
)
90+
val map = response.asMap()
91+
val totalPolicies = map["total_policies"] as Int
92+
val responsePolicies = map["policies"] as List<Map<String, Any?>>
93+
assertTrue("Total policies is 0", totalPolicies == 0)
94+
assertTrue("Response list of policies is empty", responsePolicies.isEmpty())
95+
}
96+
8397
@Throws(Exception::class)
8498
@Suppress("UNCHECKED_CAST")
8599
fun `test getting all snapshot management policies with search params`() {

0 commit comments

Comments
 (0)