Skip to content

Commit

Permalink
GET SM policies return empty list when ism config index does not exist (
Browse files Browse the repository at this point in the history
#1072) (#1078)

(cherry picked from commit 67292ca)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: bowenlan-amzn <[email protected]>
  • Loading branch information
3 people authored Jan 25, 2024
1 parent d221483 commit 1162fd3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,12 +70,18 @@ class TransportGetSMPoliciesAction @Inject constructor(

private suspend fun getAllPolicies(searchParams: SearchParams, user: User?): Pair<List<SMPolicy>, 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, Any?>>
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`() {
Expand Down

0 comments on commit 1162fd3

Please sign in to comment.