generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 108
Add Target object for external data source support on Monitor and Alert models. #916
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
Merged
riysaxen-amzn
merged 1 commit into
opensearch-project:main
from
manaswini1920:feature/workspace-datasource-fields
Mar 25, 2026
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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
77 changes: 77 additions & 0 deletions
77
src/main/kotlin/org/opensearch/commons/alerting/model/Target.kt
This file contains hidden or 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,77 @@ | ||
| package org.opensearch.commons.alerting.model | ||
|
|
||
| import org.opensearch.core.common.io.stream.StreamInput | ||
| import org.opensearch.core.common.io.stream.StreamOutput | ||
| import org.opensearch.core.common.io.stream.Writeable | ||
| import org.opensearch.core.xcontent.ToXContent | ||
| import org.opensearch.core.xcontent.XContentBuilder | ||
| import org.opensearch.core.xcontent.XContentParser | ||
| import org.opensearch.core.xcontent.XContentParserUtils | ||
| import java.io.IOException | ||
|
|
||
| /** | ||
| * Describes where a monitor query executes. | ||
| * | ||
| * @property type the target type — LOCAL by default. Extensible for additional target types. | ||
| * @property endpoint the URL of the remote target. Required when type is not LOCAL. | ||
| */ | ||
| data class Target( | ||
| val type: String = LOCAL, | ||
| val endpoint: String = "" | ||
| ) : Writeable, ToXContent { | ||
|
|
||
| init { | ||
| require(type.isNotBlank()) { "target type cannot be empty" } | ||
| if (type != LOCAL) { | ||
| require(endpoint.isNotBlank()) { "endpoint is required when target type is not LOCAL" } | ||
| } | ||
| } | ||
|
|
||
| @Throws(IOException::class) | ||
| constructor(sin: StreamInput) : this( | ||
| type = sin.readString(), | ||
| endpoint = sin.readString() | ||
| ) | ||
|
|
||
| override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { | ||
| builder.startObject() | ||
| .field(TYPE_FIELD, type) | ||
| .field(ENDPOINT_FIELD, endpoint) | ||
| return builder.endObject() | ||
| } | ||
|
|
||
| @Throws(IOException::class) | ||
| override fun writeTo(out: StreamOutput) { | ||
| out.writeString(type) | ||
| out.writeString(endpoint) | ||
| } | ||
|
|
||
| companion object { | ||
| const val TYPE_FIELD = "type" | ||
| const val ENDPOINT_FIELD = "endpoint" | ||
| const val LOCAL = "local" | ||
| val DEFAULT = Target() | ||
|
|
||
| @JvmStatic | ||
| @Throws(IOException::class) | ||
| fun parse(xcp: XContentParser): Target { | ||
| var type = LOCAL | ||
| var endpoint = "" | ||
|
|
||
| XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp) | ||
| while (xcp.nextToken() != XContentParser.Token.END_OBJECT) { | ||
| val fieldName = xcp.currentName() | ||
| xcp.nextToken() | ||
| when (fieldName) { | ||
| TYPE_FIELD -> type = xcp.text() | ||
| ENDPOINT_FIELD -> endpoint = xcp.text() | ||
| } | ||
| } | ||
| return Target(type, endpoint) | ||
| } | ||
|
|
||
| @JvmStatic | ||
| @Throws(IOException::class) | ||
| fun readFrom(sin: StreamInput): Target = Target(sin) | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
src/test/kotlin/org/opensearch/commons/alerting/model/TargetTests.kt
This file contains hidden or 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,64 @@ | ||
| package org.opensearch.commons.alerting.model | ||
|
|
||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.Assertions.assertThrows | ||
| import org.junit.jupiter.api.Test | ||
| import org.opensearch.common.io.stream.BytesStreamOutput | ||
| import org.opensearch.common.xcontent.XContentFactory | ||
| import org.opensearch.common.xcontent.XContentType | ||
| import org.opensearch.core.common.io.stream.StreamInput | ||
| import org.opensearch.core.xcontent.ToXContent | ||
|
|
||
| class TargetTests { | ||
|
|
||
| @Test | ||
| fun `test default Target`() { | ||
| val target = Target() | ||
| assertEquals(Target.LOCAL, target.type) | ||
| assertEquals("", target.endpoint) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test Target with custom type and endpoint`() { | ||
| val target = Target("custom_type", "https://example.com") | ||
| assertEquals("custom_type", target.type) | ||
| assertEquals("https://example.com", target.endpoint) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test Target with empty type requires endpoint`() { | ||
| assertThrows(IllegalArgumentException::class.java) { | ||
| Target("", "") | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `test Target requires endpoint for non-LOCAL type`() { | ||
| assertThrows(IllegalArgumentException::class.java) { | ||
| Target("custom_type", "") | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `test Target stream serialization roundtrip`() { | ||
| val target = Target("custom_type", "https://example.com") | ||
| val out = BytesStreamOutput() | ||
| target.writeTo(out) | ||
| val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) | ||
| val deserialized = Target(sin) | ||
| assertEquals(target, deserialized) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test Target XContent roundtrip`() { | ||
| val target = Target("custom_type", "https://example.com") | ||
| val builder = XContentFactory.jsonBuilder() | ||
| target.toXContent(builder, ToXContent.EMPTY_PARAMS) | ||
| val json = builder.toString() | ||
|
|
||
| val parser = XContentType.JSON.xContent().createParser(null, null, json) | ||
| parser.nextToken() | ||
| val parsed = Target.parse(parser) | ||
| assertEquals(target, parsed) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.