Skip to content

Commit

Permalink
Merge changes in the main branch to the 1.x branch. (opensearch-proje…
Browse files Browse the repository at this point in the history
…ct#42)

* Update Release Notes for GA (opensearch-project#36)

* Update Release Notes for GA

* Update Release Notes for GA include RC1 Changes as well.

Signed-off-by: Aditya Jindal <[email protected]>

* add method type in CustomWebhook data model (opensearch-project#39)

Signed-off-by: Zhongnan Su <[email protected]>

* Fix class loader issue for notifications response (opensearch-project#40)

* Fix class loader issue for notifications

Signed-off-by: Joshua Li <[email protected]>

* Fix formatting

Signed-off-by: Joshua Li <[email protected]>

* Refactor creation of action listener object

Signed-off-by: Joshua Li <[email protected]>

* Fix indentation

Signed-off-by: Joshua Li <[email protected]>

* Remove unused suppresses

Signed-off-by: Joshua Li <[email protected]>

* Add UT for notification API

Signed-off-by: Chen Dai <[email protected]>

* Add UT for notification API

Signed-off-by: Chen Dai <[email protected]>

* Add UT for send notification API

Signed-off-by: Chen Dai <[email protected]>

* Fix Github workflow failure

Signed-off-by: Chen Dai <[email protected]>

* Fix Github workflow failure

Signed-off-by: Chen Dai <[email protected]>

* Refactor UT code

Signed-off-by: Chen Dai <[email protected]>

Co-authored-by: Joshua Li <[email protected]>

Co-authored-by: Zhongnan Su <[email protected]>
Co-authored-by: Chen Dai <[email protected]>
Co-authored-by: Joshua Li <[email protected]>
Signed-off-by: AWSHurneyt <[email protected]>
  • Loading branch information
4 people authored and AWSHurneyt committed Feb 29, 2024
1 parent d05a50e commit 10ac92e
Show file tree
Hide file tree
Showing 7 changed files with 391 additions and 14 deletions.
29 changes: 29 additions & 0 deletions release-notes/opensearch-common-utils.release-notes-1.0.0.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Version 1.0.0.0 2021-07-01

Compatible with OpenSearch 1.0.0

### Enhancements

* Notification plugin interface and models ([#31](https://github.com/opensearch-project/common-utils/pull/31))

### Infrastructure

* Support for kotlin and JUnit5 with mockito ([#29](https://github.com/opensearch-project/common-utils/pull/29))
* Removing Kotlin Runtime library bundled into library ([#30](https://github.com/opensearch-project/common-utils/pull/30))
* Bump to version 1.0.0.0 #34 ([#34](https://github.com/opensearch-project/common-utils/pull/34))

### Documentation

* Update OpenSearch branch to 1.0 ([#28](https://github.com/opensearch-project/common-utils/pull/28))
* Cleanup READMEs. ([#32](https://github.com/opensearch-project/common-utils/pull/32))

### Maintainence

* Update issue template with multiple labels ([#18](https://github.com/opensearch-project/common-utils/pull/18))
* Rename namespaces from OpenDistro to OpenSearch ([#20](https://github.com/opensearch-project/common-utils/pull/20))
* Rename classes, variables, methods to incorporate OpenSearch ([#21](https://github.com/opensearch-project/common-utils/pull/21))
* Rename remaining identifiers to OpenSearch ([#23](https://github.com/opensearch-project/common-utils/pull/23))
* Version changed to rc1 #24 ([#24](https://github.com/opensearch-project/common-utils/pull/24))
* Rename consts as per changes in security plugin ([#25](https://github.com/opensearch-project/common-utils/pull/25))
* Move workflow tags to rc1 ([#26](https://github.com/opensearch-project/common-utils/pull/26))
* Add rc1 release notes ([#27](https://github.com/opensearch-project/common-utils/pull/27))
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
package org.opensearch.commons.notifications

import org.opensearch.action.ActionListener
import org.opensearch.action.ActionResponse
import org.opensearch.client.node.NodeClient
import org.opensearch.common.io.stream.Writeable
import org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT
import org.opensearch.commons.notifications.action.BaseResponse
import org.opensearch.commons.notifications.action.CreateNotificationConfigRequest
import org.opensearch.commons.notifications.action.CreateNotificationConfigResponse
import org.opensearch.commons.notifications.action.DeleteNotificationConfigRequest
Expand Down Expand Up @@ -56,6 +59,7 @@ import org.opensearch.commons.notifications.action.UpdateNotificationConfigRespo
import org.opensearch.commons.notifications.model.ChannelMessage
import org.opensearch.commons.notifications.model.EventSource
import org.opensearch.commons.utils.SecureClientWrapper
import org.opensearch.commons.utils.recreateObject

/**
* All the transport action plugin interfaces for the Notification plugin
Expand All @@ -76,7 +80,7 @@ object NotificationsPluginInterface {
client.execute(
CREATE_NOTIFICATION_CONFIG_ACTION_TYPE,
request,
listener
wrapActionListener(listener) { response -> recreateObject(response) { CreateNotificationConfigResponse(it) } }
)
}

Expand All @@ -94,7 +98,7 @@ object NotificationsPluginInterface {
client.execute(
UPDATE_NOTIFICATION_CONFIG_ACTION_TYPE,
request,
listener
wrapActionListener(listener) { response -> recreateObject(response) { UpdateNotificationConfigResponse(it) } }
)
}

Expand All @@ -112,7 +116,7 @@ object NotificationsPluginInterface {
client.execute(
DELETE_NOTIFICATION_CONFIG_ACTION_TYPE,
request,
listener
wrapActionListener(listener) { response -> recreateObject(response) { DeleteNotificationConfigResponse(it) } }
)
}

Expand All @@ -130,7 +134,7 @@ object NotificationsPluginInterface {
client.execute(
GET_NOTIFICATION_CONFIG_ACTION_TYPE,
request,
listener
wrapActionListener(listener) { response -> recreateObject(response) { GetNotificationConfigResponse(it) } }
)
}

Expand All @@ -148,7 +152,7 @@ object NotificationsPluginInterface {
client.execute(
GET_NOTIFICATION_EVENT_ACTION_TYPE,
request,
listener
wrapActionListener(listener) { response -> recreateObject(response) { GetNotificationEventResponse(it) } }
)
}

Expand All @@ -166,7 +170,7 @@ object NotificationsPluginInterface {
client.execute(
GET_PLUGIN_FEATURES_ACTION_TYPE,
request,
listener
wrapActionListener(listener) { response -> recreateObject(response) { GetPluginFeaturesResponse(it) } }
)
}

Expand All @@ -184,7 +188,7 @@ object NotificationsPluginInterface {
client.execute(
GET_FEATURE_CHANNEL_LIST_ACTION_TYPE,
request,
listener
wrapActionListener(listener) { response -> recreateObject(response) { GetFeatureChannelListResponse(it) } }
)
}

Expand All @@ -209,7 +213,30 @@ object NotificationsPluginInterface {
wrapper.execute(
SEND_NOTIFICATION_ACTION_TYPE,
SendNotificationRequest(eventSource, channelMessage, channelIds, threadContext),
listener
wrapActionListener(listener) { response -> recreateObject(response) { SendNotificationResponse(it) } }
)
}

/**
* Wrap action listener on concrete response class by a new created one on ActionResponse.
* This is required because the response may be loaded by different classloader across plugins.
* The onResponse(ActionResponse) avoids type cast exception and give a chance to recreate
* the response object.
*/
@Suppress("UNCHECKED_CAST")
private fun <Response : BaseResponse> wrapActionListener(
listener: ActionListener<Response>,
recreate: (Writeable) -> Response
): ActionListener<Response> {
return object : ActionListener<ActionResponse> {
override fun onResponse(response: ActionResponse) {
val recreated = response as? Response ?: recreate(response)
listener.onResponse(recreated)
}

override fun onFailure(exception: java.lang.Exception) {
listener.onFailure(exception)
}
} as ActionListener<Response>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.opensearch.commons.notifications.model

import org.opensearch.commons.utils.EnumParser

enum class HttpMethodType(val tag: String) {
POST("POST") {
override fun toString(): String {
return tag
}
},
PUT("PUT") {
override fun toString(): String {
return tag
}
},
PATCH("PATCH") {
override fun toString(): String {
return tag
}
};

companion object {
private val tagMap = values().associateBy { it.tag }

val enumParser = EnumParser { fromTagOrDefault(it) }

/**
* Get HttpMethodType from tag or POST if not found
* @param tag the tag
* @return MethodType corresponding to tag. POST if invalid tag.
*/
fun fromTagOrDefault(tag: String): HttpMethodType {
return tagMap[tag] ?: POST
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import org.opensearch.common.xcontent.XContentBuilder
import org.opensearch.common.xcontent.XContentParser
import org.opensearch.common.xcontent.XContentParserUtils
import org.opensearch.commons.notifications.NotificationConstants.HEADER_PARAMS_TAG
import org.opensearch.commons.notifications.NotificationConstants.METHOD_TAG
import org.opensearch.commons.notifications.NotificationConstants.URL_TAG
import org.opensearch.commons.utils.STRING_READER
import org.opensearch.commons.utils.STRING_WRITER
Expand All @@ -47,7 +48,8 @@ import java.io.IOException
*/
data class Webhook(
val url: String,
val headerParams: Map<String, String> = mapOf()
val headerParams: Map<String, String> = mapOf(),
val method: HttpMethodType = HttpMethodType.POST
) : BaseConfigData {

init {
Expand Down Expand Up @@ -77,6 +79,7 @@ data class Webhook(
fun parse(parser: XContentParser): Webhook {
var url: String? = null
var headerParams: Map<String, String> = mapOf()
var method = HttpMethodType.POST

XContentParserUtils.ensureExpectedToken(
XContentParser.Token.START_OBJECT,
Expand All @@ -89,14 +92,15 @@ data class Webhook(
when (fieldName) {
URL_TAG -> url = parser.text()
HEADER_PARAMS_TAG -> headerParams = parser.mapStrings()
METHOD_TAG -> method = HttpMethodType.fromTagOrDefault(parser.text())
else -> {
parser.skipChildren()
log.info("Unexpected field: $fieldName, while parsing Webhook destination")
}
}
}
url ?: throw IllegalArgumentException("$URL_TAG field absent")
return Webhook(url, headerParams)
return Webhook(url, headerParams, method)
}
}

Expand All @@ -108,6 +112,7 @@ data class Webhook(
return builder.startObject()
.field(URL_TAG, url)
.field(HEADER_PARAMS_TAG, headerParams)
.field(METHOD_TAG, method.tag)
.endObject()
}

Expand All @@ -117,7 +122,8 @@ data class Webhook(
*/
constructor(input: StreamInput) : this(
url = input.readString(),
headerParams = input.readMap(STRING_READER, STRING_READER)
headerParams = input.readMap(STRING_READER, STRING_READER),
method = input.readEnum(HttpMethodType::class.java)
)

/**
Expand All @@ -126,5 +132,6 @@ data class Webhook(
override fun writeTo(output: StreamOutput) {
output.writeString(url)
output.writeMap(headerParams, STRING_WRITER, STRING_WRITER)
output.writeEnum(method)
}
}
Loading

0 comments on commit 10ac92e

Please sign in to comment.