-
Notifications
You must be signed in to change notification settings - Fork 105
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
API to enable/disable a monitor #1771
Open
vikhy-aws
wants to merge
6
commits into
opensearch-project:main
Choose a base branch
from
vikhy-aws:feat-1058
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+357
−1
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
58de5a9
feat: add an api endpoint to toggle the state of a monitor
vikhy-aws 314dc06
feat: add an api to toggle monitor's enabled state
vikhy-aws a40061d
feat: add tests for the toggle monitor api
vikhy-aws 14148f6
fix: change endpoint to match the code style for other APIs
vikhy-aws d712404
fix: change endpoint to match the code style for other APIs
vikhy-aws 98c0f98
fix: add additional tests for including query params and update accor…
vikhy-aws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
103 changes: 103 additions & 0 deletions
103
alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestToggleMonitorAction.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,103 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.alerting.resthandler | ||
|
||
import org.apache.logging.log4j.LogManager | ||
import org.opensearch.alerting.AlertingPlugin | ||
import org.opensearch.client.node.NodeClient | ||
import org.opensearch.common.xcontent.XContentType | ||
import org.opensearch.commons.alerting.action.AlertingActions.TOGGLE_MONITOR_ACTION_TYPE | ||
import org.opensearch.commons.alerting.action.ToggleMonitorRequest | ||
import org.opensearch.commons.alerting.action.ToggleMonitorResponse | ||
import org.opensearch.commons.alerting.model.Monitor | ||
import org.opensearch.commons.alerting.util.AlertingException | ||
import org.opensearch.core.action.ActionListener | ||
import org.opensearch.core.rest.RestStatus | ||
import org.opensearch.core.xcontent.ToXContent | ||
import org.opensearch.core.xcontent.XContentBuilder | ||
import org.opensearch.rest.BaseRestHandler | ||
import org.opensearch.rest.BytesRestResponse | ||
import org.opensearch.rest.RestHandler | ||
import org.opensearch.rest.RestHandler.ReplacedRoute | ||
import org.opensearch.rest.RestHandler.Route | ||
import org.opensearch.rest.RestRequest | ||
import org.opensearch.rest.RestRequest.Method.PUT | ||
import java.io.IOException | ||
|
||
private val log = LogManager.getLogger(RestToggleMonitorAction::class.java) | ||
|
||
class RestToggleMonitorAction : BaseRestHandler() { | ||
override fun getName(): String { | ||
return "toggle_monitor_action" | ||
} | ||
|
||
override fun routes(): List<Route> { | ||
return listOf() | ||
} | ||
|
||
override fun replacedRoutes(): MutableList<RestHandler.ReplacedRoute> { | ||
return mutableListOf( | ||
ReplacedRoute( | ||
PUT, | ||
"${AlertingPlugin.MONITOR_BASE_URI}/{monitorID}/_enable", | ||
PUT, | ||
"${AlertingPlugin.LEGACY_OPENDISTRO_MONITOR_BASE_URI}/{monitorID}/_enable" | ||
), | ||
ReplacedRoute( | ||
PUT, | ||
"${AlertingPlugin.MONITOR_BASE_URI}/{monitorID}/_disable", | ||
PUT, | ||
"${AlertingPlugin.LEGACY_OPENDISTRO_MONITOR_BASE_URI}/{monitorID}/_disable" | ||
) | ||
) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { | ||
val monitorId = request.param("monitorID", Monitor.NO_ID) | ||
if (request.method() == PUT && Monitor.NO_ID == monitorId) { | ||
vikhy-aws marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw AlertingException.wrap(IllegalArgumentException("Missing monitor ID")) | ||
vikhy-aws marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Check if the request is being made to enable the monitor | ||
val enabled = request.path().endsWith("/_enable") | ||
vikhy-aws marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
log.debug("{} {}/{}/{}", request.method(), AlertingPlugin.MONITOR_BASE_URI, monitorId, if (enabled) "enable" else "disable") | ||
|
||
return RestChannelConsumer { channel -> | ||
val toggleMonitorRequest = ToggleMonitorRequest( | ||
monitorId = monitorId, | ||
enabled = enabled, | ||
seqNo = -1, // Updated in the transport layer | ||
primaryTerm = -1, // Updated in the transport layer | ||
method = request.method() | ||
) | ||
|
||
client.execute( | ||
TOGGLE_MONITOR_ACTION_TYPE, | ||
toggleMonitorRequest, | ||
object : ActionListener<ToggleMonitorResponse> { | ||
override fun onResponse(response: ToggleMonitorResponse) { | ||
channel.sendResponse( | ||
BytesRestResponse( | ||
RestStatus.OK, | ||
response.toXContent( | ||
XContentBuilder.builder( | ||
XContentType.JSON.xContent() | ||
), | ||
ToXContent.EMPTY_PARAMS | ||
) | ||
) | ||
) | ||
} | ||
override fun onFailure(e: Exception) { | ||
channel.sendResponse(BytesRestResponse(channel, e)) | ||
} | ||
} | ||
) | ||
} | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportToggleMonitorAction.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,128 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.alerting.transport | ||
|
||
import org.opensearch.OpenSearchStatusException | ||
import org.opensearch.action.support.ActionFilters | ||
import org.opensearch.action.support.HandledTransportAction | ||
import org.opensearch.action.support.WriteRequest | ||
import org.opensearch.client.node.NodeClient | ||
import org.opensearch.common.inject.Inject | ||
import org.opensearch.commons.alerting.action.AlertingActions | ||
import org.opensearch.commons.alerting.action.AlertingActions.GET_MONITOR_ACTION_TYPE | ||
import org.opensearch.commons.alerting.action.AlertingActions.INDEX_MONITOR_ACTION_TYPE | ||
import org.opensearch.commons.alerting.action.GetMonitorRequest | ||
import org.opensearch.commons.alerting.action.GetMonitorResponse | ||
import org.opensearch.commons.alerting.action.IndexMonitorRequest | ||
import org.opensearch.commons.alerting.action.IndexMonitorResponse | ||
import org.opensearch.commons.alerting.action.ToggleMonitorRequest | ||
import org.opensearch.commons.alerting.action.ToggleMonitorResponse | ||
import org.opensearch.core.action.ActionListener | ||
import org.opensearch.core.common.io.stream.NamedWriteableRegistry | ||
import org.opensearch.core.rest.RestStatus | ||
import org.opensearch.tasks.Task | ||
import org.opensearch.transport.TransportService | ||
import java.time.Instant | ||
|
||
class TransportToggleMonitorAction @Inject constructor( | ||
transportService: TransportService, | ||
val client: NodeClient, | ||
val namedWriteableRegistry: NamedWriteableRegistry, | ||
actionFilters: ActionFilters | ||
) : HandledTransportAction<ToggleMonitorRequest, ToggleMonitorResponse>( | ||
AlertingActions.TOGGLE_MONITOR_ACTION_NAME, | ||
transportService, | ||
actionFilters, | ||
::ToggleMonitorRequest | ||
) { | ||
|
||
override fun doExecute( | ||
task: Task, | ||
request: ToggleMonitorRequest, | ||
actionListener: ActionListener<ToggleMonitorResponse> | ||
) { | ||
val monitorId = request.monitorId | ||
val enabled = request.enabled | ||
|
||
val getMonitorRequest = GetMonitorRequest( | ||
monitorId = monitorId, | ||
version = -3L, | ||
method = request.method, | ||
srcContext = null | ||
) | ||
|
||
client.execute( | ||
GET_MONITOR_ACTION_TYPE, | ||
getMonitorRequest, | ||
object : ActionListener<GetMonitorResponse> { | ||
override fun onResponse(getMonitorResponse: GetMonitorResponse) { | ||
try { | ||
if (getMonitorResponse.monitor == null) { | ||
actionListener.onFailure( | ||
OpenSearchStatusException("Monitor $monitorId not found", RestStatus.NOT_FOUND) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small observation. As a best practice, we should end error messages with periods ( |
||
) | ||
return | ||
} | ||
|
||
if (getMonitorResponse.monitor!!.enabled == enabled) { | ||
actionListener.onFailure( | ||
OpenSearchStatusException( | ||
"Monitor $monitorId is already ${if (enabled) "enabled" else "disabled"}", | ||
RestStatus.BAD_REQUEST | ||
) | ||
) | ||
return | ||
} | ||
|
||
// Create a copy of the monitor with the updated state | ||
val updatedMonitor = getMonitorResponse.monitor!!.copy( | ||
enabled = enabled, | ||
enabledTime = if (enabled) Instant.now() else null | ||
) | ||
|
||
// Call indexMonitor API to update the monitor | ||
val indexMonitorRequest = IndexMonitorRequest( | ||
monitorId = monitorId, | ||
seqNo = getMonitorResponse.seqNo, | ||
primaryTerm = getMonitorResponse.primaryTerm, | ||
refreshPolicy = WriteRequest.RefreshPolicy.IMMEDIATE, | ||
method = request.method, | ||
monitor = updatedMonitor, | ||
) | ||
|
||
client.execute( | ||
INDEX_MONITOR_ACTION_TYPE, | ||
indexMonitorRequest, | ||
object : ActionListener<IndexMonitorResponse> { | ||
override fun onResponse(indexMonitorResponse: IndexMonitorResponse) { | ||
actionListener.onResponse( | ||
ToggleMonitorResponse( | ||
id = monitorId, | ||
version = indexMonitorResponse.version, | ||
seqNo = indexMonitorResponse.seqNo, | ||
primaryTerm = indexMonitorResponse.primaryTerm, | ||
monitor = updatedMonitor | ||
) | ||
) | ||
} | ||
|
||
override fun onFailure(e: Exception) { | ||
actionListener.onFailure(e) | ||
} | ||
} | ||
) | ||
} catch (e: Exception) { | ||
actionListener.onFailure(e) | ||
} | ||
} | ||
|
||
override fun onFailure(e: Exception) { | ||
actionListener.onFailure(e) | ||
} | ||
} | ||
) | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What security level perms are we giving to these APIs, we need to make changes in Security plugin for that, check this -> https://github.com/opensearch-project/security/blob/a1ccbd56621609d1ae0b0f7b4780b968ed06024b/config/roles.yml#L32