-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Email recipients made nested for backward compatibility with Alerting (…
- Loading branch information
Showing
11 changed files
with
361 additions
and
141 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
119 changes: 119 additions & 0 deletions
119
src/main/kotlin/org/opensearch/commons/notifications/model/EmailRecipient.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,119 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
* | ||
*/ | ||
package org.opensearch.commons.notifications.model | ||
|
||
import org.opensearch.common.io.stream.StreamInput | ||
import org.opensearch.common.io.stream.StreamOutput | ||
import org.opensearch.common.io.stream.Writeable | ||
import org.opensearch.common.xcontent.ToXContent | ||
import org.opensearch.common.xcontent.XContentBuilder | ||
import org.opensearch.common.xcontent.XContentParser | ||
import org.opensearch.common.xcontent.XContentParserUtils | ||
import org.opensearch.commons.notifications.NotificationConstants.RECIPIENT_TAG | ||
import org.opensearch.commons.utils.logger | ||
import org.opensearch.commons.utils.validateEmail | ||
import java.io.IOException | ||
|
||
/** | ||
* Data class representing Email recipient. | ||
*/ | ||
data class EmailRecipient( | ||
val recipient: String | ||
) : BaseConfigData { | ||
|
||
init { | ||
validateEmail(recipient) | ||
} | ||
|
||
companion object { | ||
private val log by logger(EmailRecipient::class.java) | ||
|
||
/** | ||
* reader to create instance of class from writable. | ||
*/ | ||
val reader = Writeable.Reader { EmailRecipient(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): EmailRecipient { | ||
var recipient: 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) { | ||
RECIPIENT_TAG -> recipient = parser.text() | ||
else -> { | ||
parser.skipChildren() | ||
log.info("Unexpected field: $fieldName, while parsing EmailRecipient") | ||
} | ||
} | ||
} | ||
recipient ?: throw IllegalArgumentException("$RECIPIENT_TAG field absent") | ||
return EmailRecipient(recipient) | ||
} | ||
} | ||
|
||
/** | ||
* Constructor used in transport action communication. | ||
* @param input StreamInput stream to deserialize data from. | ||
*/ | ||
constructor(input: StreamInput) : this( | ||
recipient = input.readString() | ||
) | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
override fun writeTo(output: StreamOutput) { | ||
output.writeString(recipient) | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder { | ||
builder!! | ||
return builder.startObject() | ||
.field(RECIPIENT_TAG, recipient) | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ import org.opensearch.commons.notifications.model.Chime | |
import org.opensearch.commons.notifications.model.ConfigType | ||
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.NotificationConfig | ||
import org.opensearch.commons.notifications.model.Slack | ||
|
@@ -84,7 +85,7 @@ internal class CreateNotificationConfigRequestTests { | |
} | ||
|
||
private fun createEmailGroupContentConfigObject(): NotificationConfig { | ||
val sampleEmailGroup = EmailGroup(listOf("[email protected]")) | ||
val sampleEmailGroup = EmailGroup(listOf(EmailRecipient("[email protected]"))) | ||
return NotificationConfig( | ||
"name", | ||
"description", | ||
|
@@ -98,7 +99,7 @@ internal class CreateNotificationConfigRequestTests { | |
private fun createEmailContentConfigObject(): NotificationConfig { | ||
val sampleEmail = Email( | ||
emailAccountID = "[email protected]", | ||
recipients = listOf("[email protected]"), | ||
recipients = listOf(EmailRecipient("[email protected]")), | ||
emailGroupIds = listOf("[email protected]") | ||
) | ||
return NotificationConfig( | ||
|
@@ -365,7 +366,7 @@ internal class CreateNotificationConfigRequestTests { | |
|
||
@Test | ||
fun `Create config should deserialize json object using parser Email Group`() { | ||
val sampleEmailGroup = EmailGroup(listOf("[email protected]")) | ||
val sampleEmailGroup = EmailGroup(listOf(EmailRecipient("[email protected]"))) | ||
val config = NotificationConfig( | ||
"name", | ||
"description", | ||
|
@@ -384,7 +385,7 @@ internal class CreateNotificationConfigRequestTests { | |
"config_type":"email_group", | ||
"feature_list":["index_management"], | ||
"is_enabled":true, | ||
"email_group":{"recipient_list":["[email protected]"]} | ||
"email_group":{"recipient_list":[{"recipient":"[email protected]"}]} | ||
} | ||
} | ||
""".trimIndent() | ||
|
@@ -396,7 +397,7 @@ internal class CreateNotificationConfigRequestTests { | |
fun `Update config should deserialize json object using parser Email`() { | ||
val sampleEmail = Email( | ||
emailAccountID = "[email protected]", | ||
recipients = listOf("[email protected]"), | ||
recipients = listOf(EmailRecipient("[email protected]")), | ||
emailGroupIds = listOf("[email protected]") | ||
) | ||
val config = NotificationConfig( | ||
|
@@ -417,8 +418,11 @@ internal class CreateNotificationConfigRequestTests { | |
"config_type":"email", | ||
"feature_list":["index_management"], | ||
"is_enabled":true, | ||
"email":{"email_account_id":"[email protected]","recipient_list":["[email protected]"], | ||
"email_group_id_list":["[email protected]"] } | ||
"email":{ | ||
"email_account_id":"[email protected]", | ||
"recipient_list":[{"recipient":"[email protected]"}], | ||
"email_group_id_list":["[email protected]"] | ||
} | ||
} | ||
} | ||
""".trimIndent() | ||
|
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 |
---|---|---|
|
@@ -36,6 +36,7 @@ import org.opensearch.commons.notifications.model.Chime | |
import org.opensearch.commons.notifications.model.ConfigType | ||
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.NotificationConfig | ||
import org.opensearch.commons.notifications.model.Slack | ||
|
@@ -84,7 +85,7 @@ internal class UpdateNotificationConfigRequestTests { | |
} | ||
|
||
private fun createEmailGroupContentConfigObject(): NotificationConfig { | ||
val sampleEmailGroup = EmailGroup(listOf("[email protected]")) | ||
val sampleEmailGroup = EmailGroup(listOf(EmailRecipient("[email protected]"))) | ||
return NotificationConfig( | ||
"name", | ||
"description", | ||
|
@@ -98,7 +99,7 @@ internal class UpdateNotificationConfigRequestTests { | |
private fun createEmailContentConfigObject(): NotificationConfig { | ||
val sampleEmail = Email( | ||
emailAccountID = "[email protected]", | ||
recipients = listOf("[email protected]"), | ||
recipients = listOf(EmailRecipient("[email protected]")), | ||
emailGroupIds = listOf("[email protected]") | ||
) | ||
return NotificationConfig( | ||
|
@@ -334,7 +335,7 @@ internal class UpdateNotificationConfigRequestTests { | |
|
||
@Test | ||
fun `Update config should deserialize json object using parser Email Group`() { | ||
val sampleEmailGroup = EmailGroup(listOf("[email protected]")) | ||
val sampleEmailGroup = EmailGroup(listOf(EmailRecipient("[email protected]"))) | ||
val config = NotificationConfig( | ||
"name", | ||
"description", | ||
|
@@ -353,7 +354,7 @@ internal class UpdateNotificationConfigRequestTests { | |
"config_type":"email_group", | ||
"feature_list":["index_management"], | ||
"is_enabled":true, | ||
"email_group":{"recipient_list":["[email protected]"]} | ||
"email_group":{"recipient_list":[{"recipient":"[email protected]"}]} | ||
} | ||
} | ||
""".trimIndent() | ||
|
@@ -366,7 +367,7 @@ internal class UpdateNotificationConfigRequestTests { | |
fun `Update config should deserialize json object using parser Email`() { | ||
val sampleEmail = Email( | ||
emailAccountID = "[email protected]", | ||
recipients = listOf("[email protected]"), | ||
recipients = listOf(EmailRecipient("[email protected]")), | ||
emailGroupIds = listOf("[email protected]") | ||
) | ||
val config = NotificationConfig( | ||
|
@@ -387,8 +388,11 @@ internal class UpdateNotificationConfigRequestTests { | |
"config_type":"email", | ||
"feature_list":["index_management"], | ||
"is_enabled":true, | ||
"email":{"email_account_id":"[email protected]","recipient_list":["[email protected]"], | ||
"email_group_id_list":["[email protected]"] } | ||
"email":{ | ||
"email_account_id":"[email protected]", | ||
"recipient_list":[{"recipient":"[email protected]"}], | ||
"email_group_id_list":["[email protected]"] | ||
} | ||
} | ||
} | ||
""".trimIndent() | ||
|
Oops, something went wrong.