-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: bowenlan-amzn <[email protected]>
- Loading branch information
1 parent
ab87a61
commit 69c5466
Showing
27 changed files
with
2,335 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/org/opensearch/indexmanagement/snapshotmanagement/SMUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.indexmanagement.snapshotmanagement | ||
|
||
import org.apache.logging.log4j.LogManager | ||
|
||
private val log = LogManager.getLogger("Snapshot Management Helper") | ||
|
||
fun getSMDocId(policyName: String) = "$policyName-sm" | ||
fun getSMMetadataDocId(policyName: String) = "$policyName-sm-metadata" |
114 changes: 114 additions & 0 deletions
114
.../org/opensearch/indexmanagement/snapshotmanagement/api/resthandler/RestSMPolicyHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.indexmanagement.snapshotmanagement.api.resthandler | ||
|
||
import org.apache.logging.log4j.LogManager | ||
import org.opensearch.client.node.NodeClient | ||
import org.opensearch.indexmanagement.IndexManagementPlugin.Companion.SM_BASE_URI | ||
import org.opensearch.indexmanagement.snapshotmanagement.api.transport.SMActions | ||
import org.opensearch.indexmanagement.snapshotmanagement.api.transport.SMActions.DELETE_SM_ACTION_TYPE | ||
import org.opensearch.indexmanagement.snapshotmanagement.api.transport.SMActions.GET_SM_ACTION_TYPE | ||
import org.opensearch.indexmanagement.snapshotmanagement.api.transport.delete.DeleteSMRequest | ||
import org.opensearch.indexmanagement.snapshotmanagement.api.transport.get.GetSMRequest | ||
import org.opensearch.indexmanagement.snapshotmanagement.api.transport.index.IndexSMRequest | ||
import org.opensearch.indexmanagement.snapshotmanagement.model.SMPolicy | ||
import org.opensearch.rest.BaseRestHandler | ||
import org.opensearch.rest.BytesRestResponse | ||
import org.opensearch.rest.RestHandler.Route | ||
import org.opensearch.rest.RestRequest | ||
import org.opensearch.rest.RestRequest.Method.POST | ||
import org.opensearch.rest.RestRequest.Method.PUT | ||
import org.opensearch.rest.RestRequest.Method.GET | ||
import org.opensearch.rest.RestRequest.Method.DELETE | ||
import org.opensearch.rest.RestStatus | ||
import org.opensearch.rest.action.RestToXContentListener | ||
|
||
class RestSMPolicyHandler : BaseRestHandler() { | ||
|
||
private val log = LogManager.getLogger(javaClass) | ||
|
||
override fun getName(): String { | ||
return "snapshot_management_policy_rest_handler" | ||
} | ||
|
||
override fun routes(): List<Route> { | ||
return listOf( | ||
Route(POST, "$SM_BASE_URI/{policyName}"), | ||
Route(PUT, "$SM_BASE_URI/{policyName}"), | ||
Route(GET, "$SM_BASE_URI/{policyName}"), | ||
Route(DELETE, "$SM_BASE_URI/{policyName}"), | ||
) | ||
} | ||
|
||
override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { | ||
return when (request.method()) { | ||
POST -> indexRequest(request, client, true) | ||
PUT -> indexRequest(request, client, false) | ||
GET -> getRequest(request, client) | ||
DELETE -> deleteRequest(request, client) | ||
else -> RestChannelConsumer { | ||
it.sendResponse( | ||
BytesRestResponse( | ||
RestStatus.METHOD_NOT_ALLOWED, | ||
"${request.method()} is not allowed" | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun getRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { | ||
val policyName = request.param("policyName", "") | ||
if (policyName == "") { | ||
throw IllegalArgumentException("Missing policy name") | ||
} | ||
|
||
return RestChannelConsumer { | ||
client.execute( | ||
GET_SM_ACTION_TYPE, | ||
GetSMRequest(policyName), | ||
RestToXContentListener(it) | ||
) | ||
} | ||
} | ||
|
||
private fun indexRequest(request: RestRequest, client: NodeClient, create: Boolean): RestChannelConsumer { | ||
val policyName = request.param("policyName", "") | ||
if (policyName == "") { | ||
throw IllegalArgumentException("Missing policy name") | ||
} | ||
// TODO validate policy name validateGeneratedSnapshotName | ||
|
||
log.info("sm receive request ${request.requiredContent().utf8ToString()}") | ||
|
||
val xcp = request.contentParser() | ||
val policy = SMPolicy.parse(xcp, policyName = policyName) | ||
log.info("sm parsed $policy") | ||
|
||
return RestChannelConsumer { | ||
client.execute( | ||
SMActions.INDEX_SM_ACTION_TYPE, | ||
IndexSMRequest(policy, create), | ||
RestToXContentListener(it) | ||
) | ||
} | ||
} | ||
|
||
private fun deleteRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { | ||
val policyName = request.param("policyName", "") | ||
if (policyName == "") { | ||
throw IllegalArgumentException("Missing policy name") | ||
} | ||
|
||
return RestChannelConsumer { | ||
client.execute( | ||
DELETE_SM_ACTION_TYPE, | ||
DeleteSMRequest(policyName), | ||
RestToXContentListener(it) | ||
) | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...in/org/opensearch/indexmanagement/snapshotmanagement/api/transport/BaseTransportAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.indexmanagement.snapshotmanagement.api.transport | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import org.apache.logging.log4j.LogManager | ||
import org.opensearch.OpenSearchStatusException | ||
import org.opensearch.action.ActionListener | ||
import org.opensearch.action.ActionRequest | ||
import org.opensearch.action.ActionResponse | ||
import org.opensearch.action.support.ActionFilters | ||
import org.opensearch.action.support.HandledTransportAction | ||
import org.opensearch.client.Client | ||
import org.opensearch.common.io.stream.Writeable | ||
import org.opensearch.common.util.concurrent.ThreadContext.StoredContext | ||
import org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT | ||
import org.opensearch.commons.authuser.User | ||
import org.opensearch.commons.utils.logger | ||
import org.opensearch.rest.RestStatus | ||
import org.opensearch.tasks.Task | ||
import org.opensearch.transport.TransportService | ||
|
||
abstract class BaseTransportAction<Request : ActionRequest, Response : ActionResponse>( | ||
name: String, | ||
transportService: TransportService, | ||
val client: Client, | ||
actionFilters: ActionFilters, | ||
requestReader: Writeable.Reader<Request>, | ||
) : HandledTransportAction<Request, Response>( | ||
name, transportService, actionFilters, requestReader | ||
) { | ||
|
||
private val log = LogManager.getLogger(javaClass) | ||
private val coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.IO) | ||
|
||
override fun doExecute( | ||
task: Task, | ||
request: Request, | ||
listener: ActionListener<Response> | ||
) { | ||
val userStr: String? = | ||
client.threadPool().threadContext.getTransient<String>(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT) | ||
log.info("User and roles string from thread context: $userStr") | ||
val user: User? = User.parse(userStr) | ||
coroutineScope.launch { | ||
try { | ||
client.threadPool().threadContext.stashContext().use { threadContext -> | ||
listener.onResponse(executeRequest(request, user, threadContext)) | ||
} | ||
} catch (ex: Exception) { | ||
log.error("Uncaught exception:", ex) | ||
listener.onFailure( | ||
OpenSearchStatusException( | ||
ex.message, RestStatus.INTERNAL_SERVER_ERROR | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
// TODO could we get userStr from threadContext? | ||
abstract suspend fun executeRequest(request: Request, user: User?, threadContext: StoredContext): Response | ||
} |
Oops, something went wrong.