forked from opensearch-project/common-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
common utils to support Microsoft teams in notifications (opensearch-…
…project#428) (opensearch-project#512) * Added common utils for microsoft teams * Added configType ,eventstatus,configDataProperties and msTeams files * Added ConfigType,EventStatus,ConfigDataProperties and MicrosoftTeams * fix build * fix build, add more test * change strings import * fix after core --------- (cherry picked from commit bae1bee) Signed-off-by: danielkyalo599 <[email protected]> Signed-off-by: dankyalo599 <[email protected]> Signed-off-by: zhichao-aws <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: danielkyalo599 <[email protected]> Signed-off-by: AWSHurneyt <[email protected]>
- Loading branch information
1 parent
9c05a48
commit ae9de53
Showing
14 changed files
with
417 additions
and
15 deletions.
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
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
99 changes: 99 additions & 0 deletions
99
src/main/kotlin/org/opensearch/commons/notifications/model/MicrosoftTeams.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,99 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.commons.notifications.model | ||
|
||
import org.opensearch.commons.notifications.NotificationConstants.URL_TAG | ||
import org.opensearch.commons.utils.logger | ||
import org.opensearch.commons.utils.validateUrl | ||
import org.opensearch.core.common.Strings | ||
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 | ||
|
||
/** | ||
* Data class representing MicrosoftTeams channel. | ||
*/ | ||
data class MicrosoftTeams( | ||
val url: String | ||
) : BaseConfigData { | ||
|
||
init { | ||
require(!Strings.isNullOrEmpty(url)) { "URL is null or empty" } | ||
validateUrl(url) | ||
} | ||
|
||
companion object { | ||
private val log by logger(MicrosoftTeams::class.java) | ||
|
||
/** | ||
* reader to create instance of class from writable. | ||
*/ | ||
val reader = Writeable.Reader { MicrosoftTeams(it) } | ||
|
||
/** | ||
* Parser to parse xContent | ||
*/ | ||
val xParser = XParser { parse(it) } | ||
|
||
/** | ||
* Creator used in REST communication. | ||
* @param parser XContentParser to deserialize data from. | ||
*/ | ||
@JvmStatic | ||
@Throws(IOException::class) | ||
fun parse(parser: XContentParser): MicrosoftTeams { | ||
var url: String? = null | ||
|
||
XContentParserUtils.ensureExpectedToken( | ||
XContentParser.Token.START_OBJECT, | ||
parser.currentToken(), | ||
parser | ||
) | ||
while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
val fieldName = parser.currentName() | ||
parser.nextToken() | ||
when (fieldName) { | ||
URL_TAG -> url = parser.text() | ||
else -> { | ||
parser.skipChildren() | ||
log.info("Unexpected field: $fieldName, while parsing MicrosoftTeams destination") | ||
} | ||
} | ||
} | ||
url ?: throw IllegalArgumentException("$URL_TAG field absent") | ||
return MicrosoftTeams(url) | ||
} | ||
} | ||
|
||
/** | ||
* Constructor used in transport action communication. | ||
* @param input StreamInput stream to deserialize data from. | ||
*/ | ||
constructor(input: StreamInput) : this( | ||
url = input.readString() | ||
) | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
override fun writeTo(output: StreamOutput) { | ||
output.writeString(url) | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder { | ||
builder!! | ||
return builder.startObject() | ||
.field(URL_TAG, url) | ||
.endObject() | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ import org.opensearch.commons.notifications.model.Email | |
import org.opensearch.commons.notifications.model.EmailGroup | ||
import org.opensearch.commons.notifications.model.EmailRecipient | ||
import org.opensearch.commons.notifications.model.MethodType | ||
import org.opensearch.commons.notifications.model.MicrosoftTeams | ||
import org.opensearch.commons.notifications.model.NotificationConfig | ||
import org.opensearch.commons.notifications.model.Slack | ||
import org.opensearch.commons.notifications.model.SmtpAccount | ||
|
@@ -57,6 +58,16 @@ internal class CreateNotificationConfigRequestTests { | |
isEnabled = true | ||
) | ||
} | ||
private fun createMicrosoftTeamsContentConfigObject(): NotificationConfig { | ||
val sampleMicrosoftTeams = MicrosoftTeams("https://domain.com/sample_microsoft_teams_url#1234567890") | ||
return NotificationConfig( | ||
"name", | ||
"description", | ||
ConfigType.MICROSOFT_TEAMS, | ||
configData = sampleMicrosoftTeams, | ||
isEnabled = true | ||
) | ||
} | ||
|
||
private fun createEmailGroupContentConfigObject(): NotificationConfig { | ||
val sampleEmailGroup = EmailGroup(listOf(EmailRecipient("[email protected]"))) | ||
|
@@ -114,6 +125,20 @@ internal class CreateNotificationConfigRequestTests { | |
assertNull(recreatedObject.validate()) | ||
assertEquals(configRequest.notificationConfig, recreatedObject.notificationConfig) | ||
} | ||
@Test | ||
fun `Create config serialize and deserialize transport object should be equal microsoft teams`() { | ||
val configRequest = CreateNotificationConfigRequest( | ||
createMicrosoftTeamsContentConfigObject() | ||
) | ||
val recreatedObject = | ||
recreateObject(configRequest) { | ||
CreateNotificationConfigRequest( | ||
it | ||
) | ||
} | ||
assertNull(recreatedObject.validate()) | ||
assertEquals(configRequest.notificationConfig, recreatedObject.notificationConfig) | ||
} | ||
|
||
@Test | ||
fun `Create config serialize and deserialize transport object should be equal slack`() { | ||
|
@@ -189,6 +214,15 @@ internal class CreateNotificationConfigRequestTests { | |
assertNull(recreatedObject.validate()) | ||
assertEquals(configRequest.notificationConfig, recreatedObject.notificationConfig) | ||
} | ||
@Test | ||
fun `Create config serialize and deserialize using json object should be equal microsoft teams`() { | ||
val configRequest = CreateNotificationConfigRequest( | ||
createMicrosoftTeamsContentConfigObject() | ||
) | ||
val jsonString = getJsonString(configRequest) | ||
val recreatedObject = createObjectFromJsonString(jsonString) { CreateNotificationConfigRequest.parse(it) } | ||
assertEquals(configRequest.notificationConfig, recreatedObject.notificationConfig) | ||
} | ||
|
||
@Test | ||
fun `Create config serialize and deserialize using json object should be equal`() { | ||
|
@@ -275,6 +309,32 @@ internal class CreateNotificationConfigRequestTests { | |
val recreatedObject = createObjectFromJsonString(jsonString) { CreateNotificationConfigRequest.parse(it) } | ||
assertEquals(config, recreatedObject.notificationConfig) | ||
} | ||
@Test | ||
fun `Create config should deserialize json object using parser microsoft teams`() { | ||
val sampleMicrosoftTeams = MicrosoftTeams("https://domain.com/sample_microsoft_teams_url#1234567890") | ||
val config = NotificationConfig( | ||
"name", | ||
"description", | ||
ConfigType.MICROSOFT_TEAMS, | ||
configData = sampleMicrosoftTeams, | ||
isEnabled = true | ||
) | ||
|
||
val jsonString = """ | ||
{ | ||
"config_id":"config_id1", | ||
"config":{ | ||
"name":"name", | ||
"description":"description", | ||
"config_type":"microsoft_teams", | ||
"is_enabled":true, | ||
"microsoft_teams":{"url":"https://domain.com/sample_microsoft_teams_url#1234567890"} | ||
} | ||
} | ||
""".trimIndent() | ||
val recreatedObject = createObjectFromJsonString(jsonString) { CreateNotificationConfigRequest.parse(it) } | ||
assertEquals(config, recreatedObject.notificationConfig) | ||
} | ||
|
||
@Test | ||
fun `Create config should deserialize json object using parser webhook`() { | ||
|
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
Oops, something went wrong.