Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GET SM policies return empty list when ism config index does not exist #1072

Merged
merged 5 commits into from
Jan 24, 2024
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.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 @@

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

Check warning on line 77 in src/main/kotlin/org/opensearch/indexmanagement/snapshotmanagement/api/transport/get/TransportGetSMPoliciesAction.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/indexmanagement/snapshotmanagement/api/transport/get/TransportGetSMPoliciesAction.kt#L76-L77

Added lines #L76 - L77 were not covered by tests
if (unwrappedException is IndexNotFoundException) {
// config index hasn't been initialized, catch this here and show empty result for policies
Pair(emptyList(), 0L)

Check warning on line 80 in src/main/kotlin/org/opensearch/indexmanagement/snapshotmanagement/api/transport/get/TransportGetSMPoliciesAction.kt

View check run for this annotation

Codecov / codecov/patch

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

Added line #L80 was not covered by tests
} 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
Loading