fun parse()

in alerting/src/main/kotlin/org/opensearch/alerting/model/action/Action.kt [123:179]


        fun parse(xcp: XContentParser): Action {
            var id = UUIDs.base64UUID() // assign a default action id if one is not specified
            lateinit var name: String
            lateinit var destinationId: String
            var subjectTemplate: Script? = null // subject template could be null for some destinations
            lateinit var messageTemplate: Script
            var throttleEnabled = false
            var throttle: Throttle? = null
            var actionExecutionPolicy: ActionExecutionPolicy? = null

            XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp)
            while (xcp.nextToken() != XContentParser.Token.END_OBJECT) {
                val fieldName = xcp.currentName()
                xcp.nextToken()
                when (fieldName) {
                    ID_FIELD -> id = xcp.text()
                    NAME_FIELD -> name = xcp.textOrNull()
                    DESTINATION_ID_FIELD -> destinationId = xcp.textOrNull()
                    SUBJECT_TEMPLATE_FIELD -> {
                        subjectTemplate = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) null else
                            Script.parse(xcp, Script.DEFAULT_TEMPLATE_LANG)
                    }
                    MESSAGE_TEMPLATE_FIELD -> messageTemplate = Script.parse(xcp, Script.DEFAULT_TEMPLATE_LANG)
                    THROTTLE_FIELD -> {
                        throttle = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) null else Throttle.parse(xcp)
                    }
                    THROTTLE_ENABLED_FIELD -> {
                        throttleEnabled = xcp.booleanValue()
                    }
                    ACTION_EXECUTION_POLICY_FIELD -> {
                        actionExecutionPolicy = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) {
                            null
                        } else {
                            ActionExecutionPolicy.parse(xcp)
                        }
                    }
                    else -> {
                        throw IllegalStateException("Unexpected field: $fieldName, while parsing action")
                    }
                }
            }

            if (throttleEnabled) {
                requireNotNull(throttle, { "Action throttle enabled but not set throttle value" })
            }

            return Action(
                requireNotNull(name) { "Action name is null" },
                requireNotNull(destinationId) { "Destination id is null" },
                subjectTemplate,
                requireNotNull(messageTemplate) { "Action message template is null" },
                throttleEnabled,
                throttle,
                id = requireNotNull(id),
                actionExecutionPolicy = actionExecutionPolicy
            )
        }