Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds custom action parsing #273

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactors custom action parsing
Signed-off-by: Robert Downs <downsrob@amazon.com>
downsrob committed Feb 24, 2022
commit e6dc30ec75d8d5e815ab5a857d1bd9c82d2a1e84
Original file line number Diff line number Diff line change
@@ -27,6 +27,8 @@ abstract class Action(
builder.startObject()
configTimeout?.toXContent(builder, params)
configRetry?.toXContent(builder, params)
// Include a "custom" object wrapper for custom actions to allow extensions to put arbitrary action configs in the config
// index. The EXCLUDE_CUSTOM_FIELD_PARAM is used to not include this wrapper in api responses
if (customAction && !params.paramAsBoolean(EXCLUDE_CUSTOM_FIELD_PARAM, false)) builder.startObject(CUSTOM_ACTION_FIELD)
bowenlan-amzn marked this conversation as resolved.
Show resolved Hide resolved
populateAction(builder, params)
if (customAction && !params.paramAsBoolean(EXCLUDE_CUSTOM_FIELD_PARAM, false)) builder.endObject()
Original file line number Diff line number Diff line change
@@ -80,27 +80,16 @@ class ISMActionsParser private constructor() {
ActionTimeout.TIMEOUT_FIELD -> timeout = ActionTimeout.parse(xcp)
ActionRetry.RETRY_FIELD -> retry = ActionRetry.parse(xcp)
Action.CUSTOM_ACTION_FIELD -> {
bowenlan-amzn marked this conversation as resolved.
Show resolved Hide resolved
xcp.nextToken()
// The "custom" wrapper allows extensions to create arbitrary actions without updating the config mappings
// We consume the full custom wrapper and parse the action in this step
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.nextToken(), xcp)
val customActionType = xcp.currentName()
xcp.nextToken()
val customParser = parsers.firstOrNull { it.getActionType() == customActionType }
if (customParser != null) {
action = customParser.fromXContent(xcp, totalActions)
action.customAction = customParser.customAction
} else {
throw IllegalArgumentException("Invalid field: [$customActionType] found in custom Actions.")
}
XContentParserUtils.ensureExpectedToken(XContentParser.Token.END_OBJECT, xcp.currentToken(), xcp)
xcp.nextToken()
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, xcp.nextToken(), xcp)
action = parseAction(xcp, totalActions, customActionType)
XContentParserUtils.ensureExpectedToken(XContentParser.Token.END_OBJECT, xcp.nextToken(), xcp)
}
else -> {
val parser = parsers.firstOrNull { it.getActionType() == type }
if (parser != null) {
action = parser.fromXContent(xcp, totalActions)
action.customAction = parser.customAction
} else {
throw IllegalArgumentException("Invalid field: [$type] found in Actions.")
}
action = parseAction(xcp, totalActions, type)
}
}
}
@@ -113,6 +102,19 @@ class ISMActionsParser private constructor() {
return action
}

private fun parseAction(xcp: XContentParser, totalActions: Int, type: String): Action {
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp)
val action: Action?
val parser = parsers.firstOrNull { it.getActionType() == type }
if (parser != null) {
action = parser.fromXContent(xcp, totalActions)
action.customAction = parser.customAction
} else {
throw IllegalArgumentException("Invalid field: [$type] found in Actions.")
}
return action
}

companion object {
val instance: ISMActionsParser by lazy { HOLDER.instance }
fun getDuplicateActionTypesMessage(actionType: String) = "Multiple action parsers attempted to register the same action type [$actionType]"