in alerting/src/main/kotlin/org/opensearch/alerting/model/destination/Destination.kt [133:205]
fun parse(
xcp: XContentParser,
id: String = NO_ID,
version: Long = NO_VERSION,
seqNo: Int = NO_SEQ_NO,
primaryTerm: Int = NO_PRIMARY_TERM
): Destination {
lateinit var name: String
var user: User? = null
lateinit var type: String
var slack: Slack? = null
var chime: Chime? = null
var customWebhook: CustomWebhook? = null
var email: Email? = null
var lastUpdateTime: Instant? = null
var schemaVersion = NO_SCHEMA_VERSION
ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp)
while (xcp.nextToken() != XContentParser.Token.END_OBJECT) {
val fieldName = xcp.currentName()
xcp.nextToken()
when (fieldName) {
NAME_FIELD -> name = xcp.text()
USER_FIELD -> user = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) null else User.parse(xcp)
TYPE_FIELD -> {
type = xcp.text()
val allowedTypes = DestinationType.values().map { it.value }
if (!allowedTypes.contains(type)) {
throw IllegalStateException("Type should be one of the $allowedTypes")
}
}
LAST_UPDATE_TIME_FIELD -> lastUpdateTime = xcp.instant()
CHIME -> {
chime = Chime.parse(xcp)
}
SLACK -> {
slack = Slack.parse(xcp)
}
CUSTOMWEBHOOK -> {
customWebhook = CustomWebhook.parse(xcp)
}
EMAIL -> {
email = Email.parse(xcp)
}
TEST_ACTION -> {
// This condition is for integ tests to avoid parsing
}
SCHEMA_VERSION -> {
schemaVersion = xcp.intValue()
}
else -> {
xcp.skipChildren()
}
}
}
return Destination(
id,
version,
schemaVersion,
seqNo,
primaryTerm,
DestinationType.valueOf(type.toUpperCase(Locale.ROOT)),
requireNotNull(name) { "Destination name is null" },
user,
lastUpdateTime ?: Instant.now(),
chime,
slack,
customWebhook,
email
)
}