Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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