Use `ParseField` when parsing actions
Use parse fields when parsing and building XContent in Actions. Original commit: elastic/x-pack-elasticsearch@db48702b76
This commit is contained in:
parent
3147927e20
commit
31a3907bed
|
@ -11,6 +11,7 @@ import org.elasticsearch.alerts.actions.ActionException;
|
|||
import org.elasticsearch.alerts.support.StringTemplateUtils;
|
||||
import org.elasticsearch.cluster.settings.DynamicSettings;
|
||||
import org.elasticsearch.cluster.settings.Validator;
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.component.AbstractComponent;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.inject.internal.Nullable;
|
||||
|
@ -145,7 +146,7 @@ public class EmailAction extends Action<EmailAction.Result> implements NodeSetti
|
|||
Transport.send(email);
|
||||
|
||||
return new Result(true, fromAddressToUse, emailAddresses, subject, message );
|
||||
} catch (Throwable e){
|
||||
} catch (Throwable e) {
|
||||
throw new ActionException("failed to send mail for alert [" + alert.name() + "]", e);
|
||||
}
|
||||
|
||||
|
@ -154,7 +155,7 @@ public class EmailAction extends Action<EmailAction.Result> implements NodeSetti
|
|||
@Override
|
||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
builder.startObject();
|
||||
builder.field("addresses");
|
||||
builder.field(Parser.ADDRESSES_FIELD.getPreferredName());
|
||||
builder.startArray();
|
||||
for (Address emailAddress : emailAddresses){
|
||||
builder.value(emailAddress.toString());
|
||||
|
@ -162,15 +163,15 @@ public class EmailAction extends Action<EmailAction.Result> implements NodeSetti
|
|||
builder.endArray();
|
||||
|
||||
if (subjectTemplate != null) {
|
||||
StringTemplateUtils.writeTemplate("subject_template", subjectTemplate, builder, params);
|
||||
StringTemplateUtils.writeTemplate(Parser.SUBJECT_TEMPLATE_FIELD.getPreferredName(), subjectTemplate, builder, params);
|
||||
}
|
||||
|
||||
if (messageTemplate != null) {
|
||||
StringTemplateUtils.writeTemplate("message_template", messageTemplate, builder, params);
|
||||
StringTemplateUtils.writeTemplate(Parser.MESSAGE_TEMPLATE_FIELD.getPreferredName(), messageTemplate, builder, params);
|
||||
}
|
||||
|
||||
if (fromAddress != null) {
|
||||
builder.field("from", fromAddress);
|
||||
builder.field(Parser.FROM_FIELD.getPreferredName(), fromAddress);
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
|
@ -185,10 +186,14 @@ public class EmailAction extends Action<EmailAction.Result> implements NodeSetti
|
|||
|
||||
public static class Parser extends AbstractComponent implements Action.Parser<EmailAction> {
|
||||
|
||||
public static final ParseField FROM_FIELD = new ParseField("from");
|
||||
public static final ParseField ADDRESSES_FIELD = new ParseField("addresses");
|
||||
public static final ParseField MESSAGE_TEMPLATE_FIELD = new ParseField("message_template");
|
||||
public static final ParseField SUBJECT_TEMPLATE_FIELD = new ParseField("subject_template");
|
||||
|
||||
private final NodeSettingsService nodeSettingsService;
|
||||
private final StringTemplateUtils templateUtils;
|
||||
|
||||
|
||||
@Inject
|
||||
public Parser(Settings settings, DynamicSettings dynamicSettings, NodeSettingsService nodeSettingsService, StringTemplateUtils templateUtils) {
|
||||
super(settings);
|
||||
|
@ -221,32 +226,26 @@ public class EmailAction extends Action<EmailAction.Result> implements NodeSetti
|
|||
if (token == XContentParser.Token.FIELD_NAME) {
|
||||
currentFieldName = parser.currentName();
|
||||
} else if (token.isValue()) {
|
||||
switch (currentFieldName) {
|
||||
case "subject_template":
|
||||
subjectTemplate = StringTemplateUtils.readTemplate(parser);
|
||||
break;
|
||||
case "message_template":
|
||||
messageTemplate = StringTemplateUtils.readTemplate(parser);
|
||||
break;
|
||||
case "from":
|
||||
fromAddress = parser.text();
|
||||
break;
|
||||
default:
|
||||
throw new ActionException("could not parse email action. unexpected field [" + currentFieldName + "]");
|
||||
if (SUBJECT_TEMPLATE_FIELD.match(currentFieldName)) {
|
||||
subjectTemplate = StringTemplateUtils.readTemplate(parser);
|
||||
} else if (MESSAGE_TEMPLATE_FIELD.match(currentFieldName)) {
|
||||
messageTemplate = StringTemplateUtils.readTemplate(parser);
|
||||
} else if (FROM_FIELD.match(currentFieldName)) {
|
||||
fromAddress = parser.text();
|
||||
} else {
|
||||
throw new ActionException("could not parse email action. unexpected field [" + currentFieldName + "]");
|
||||
}
|
||||
} else if (token == XContentParser.Token.START_ARRAY) {
|
||||
switch (currentFieldName) {
|
||||
case "addresses":
|
||||
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
|
||||
try {
|
||||
addresses.add(InternetAddress.parse(parser.text())[0]);
|
||||
} catch (AddressException ae) {
|
||||
throw new ActionException("could not parse email action. unable to parse [" + parser.text() + "] as an email address", ae);
|
||||
}
|
||||
if (ADDRESSES_FIELD.match(currentFieldName)) {
|
||||
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
|
||||
try {
|
||||
addresses.add(InternetAddress.parse(parser.text())[0]);
|
||||
} catch (AddressException ae) {
|
||||
throw new ActionException("could not parse email action. unable to parse [" + parser.text() + "] as an email address", ae);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new ActionException("could not parse email action. unexpected field [" + currentFieldName + "]");
|
||||
}
|
||||
} else {
|
||||
throw new ActionException("could not parse email action. unexpected field [" + currentFieldName + "]");
|
||||
}
|
||||
} else {
|
||||
throw new ActionException("could not parse email action. unexpected token [" + token + "]");
|
||||
|
|
|
@ -11,6 +11,7 @@ import org.elasticsearch.alerts.Alert;
|
|||
import org.elasticsearch.alerts.actions.Action;
|
||||
import org.elasticsearch.alerts.actions.ActionException;
|
||||
import org.elasticsearch.alerts.support.init.proxy.ClientProxy;
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.component.AbstractComponent;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.logging.ESLogger;
|
||||
|
@ -69,14 +70,17 @@ public class IndexAction extends Action<IndexAction.Result> {
|
|||
@Override
|
||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
builder.startObject();
|
||||
builder.field("index", index);
|
||||
builder.field("type", type);
|
||||
builder.field(Parser.INDEX_FIELD.getPreferredName(), index);
|
||||
builder.field(Parser.TYPE_FIELD.getPreferredName(), type);
|
||||
builder.endObject();
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static class Parser extends AbstractComponent implements Action.Parser<IndexAction> {
|
||||
|
||||
public static final ParseField INDEX_FIELD = new ParseField("index");
|
||||
public static final ParseField TYPE_FIELD = new ParseField("type");
|
||||
|
||||
private final ClientProxy client;
|
||||
|
||||
@Inject
|
||||
|
@ -101,15 +105,12 @@ public class IndexAction extends Action<IndexAction.Result> {
|
|||
if (token == XContentParser.Token.FIELD_NAME) {
|
||||
currentFieldName = parser.currentName();
|
||||
} else if (token.isValue()) {
|
||||
switch (currentFieldName) {
|
||||
case "index":
|
||||
index = parser.text();
|
||||
break;
|
||||
case "type":
|
||||
type = parser.text();
|
||||
break;
|
||||
default:
|
||||
throw new ActionException("could not parse index action. unexpected field [" + currentFieldName + "]");
|
||||
if (INDEX_FIELD.match(currentFieldName)) {
|
||||
index = parser.text();
|
||||
} else if (TYPE_FIELD.match(currentFieldName)) {
|
||||
type = parser.text();
|
||||
} else {
|
||||
throw new ActionException("could not parse index action. unexpected field [" + currentFieldName + "]");
|
||||
}
|
||||
} else {
|
||||
throw new ActionException("could not parse index action. unexpected token [" + token + "]");
|
||||
|
|
|
@ -10,6 +10,7 @@ import org.elasticsearch.alerts.actions.Action;
|
|||
import org.elasticsearch.alerts.actions.ActionException;
|
||||
import org.elasticsearch.alerts.support.StringTemplateUtils;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.base.Charsets;
|
||||
import org.elasticsearch.common.component.AbstractComponent;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
|
@ -99,9 +100,9 @@ public class WebhookAction extends Action<WebhookAction.Result> {
|
|||
@Override
|
||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
builder.startObject();
|
||||
builder.field("method", method.getName());
|
||||
StringTemplateUtils.writeTemplate("body_template", bodyTemplate, builder, params);
|
||||
StringTemplateUtils.writeTemplate("url_template", urlTemplate, builder, params);
|
||||
builder.field(Parser.METHOD_FIELD.getPreferredName(), method.getName());
|
||||
StringTemplateUtils.writeTemplate(Parser.BODY_TEMPLATE_FIELD.getPreferredName(), bodyTemplate, builder, params);
|
||||
StringTemplateUtils.writeTemplate(Parser.URL_TEMPLATE_FIELD.getPreferredName(), urlTemplate, builder, params);
|
||||
builder.endObject();
|
||||
return builder;
|
||||
}
|
||||
|
@ -146,6 +147,10 @@ public class WebhookAction extends Action<WebhookAction.Result> {
|
|||
|
||||
public static class Parser extends AbstractComponent implements Action.Parser<WebhookAction> {
|
||||
|
||||
public static final ParseField METHOD_FIELD = new ParseField("method");
|
||||
public static final ParseField URL_TEMPLATE_FIELD = new ParseField("url_template");
|
||||
public static final ParseField BODY_TEMPLATE_FIELD = new ParseField("body_template");
|
||||
|
||||
private final StringTemplateUtils templateUtils;
|
||||
|
||||
@Inject
|
||||
|
@ -172,22 +177,18 @@ public class WebhookAction extends Action<WebhookAction.Result> {
|
|||
if (token == XContentParser.Token.FIELD_NAME) {
|
||||
currentFieldName = parser.currentName();
|
||||
} else if (token.isValue()) {
|
||||
switch (currentFieldName) {
|
||||
case "method":
|
||||
method = HttpMethod.valueOf(parser.text());
|
||||
if (method != HttpMethod.POST && method != HttpMethod.GET && method != HttpMethod.PUT) {
|
||||
throw new ActionException("could not parse webhook action. unsupported http method ["
|
||||
+ method.getName() + "]");
|
||||
}
|
||||
break;
|
||||
case "url_template":
|
||||
urlTemplate = StringTemplateUtils.readTemplate(parser);
|
||||
break;
|
||||
case "body_template":
|
||||
bodyTemplate = StringTemplateUtils.readTemplate(parser);
|
||||
break;
|
||||
default:
|
||||
throw new ActionException("could not parse webhook action. unexpected field [" + currentFieldName + "]");
|
||||
if (METHOD_FIELD.match(currentFieldName)) {
|
||||
method = HttpMethod.valueOf(parser.text());
|
||||
if (method != HttpMethod.POST && method != HttpMethod.GET && method != HttpMethod.PUT) {
|
||||
throw new ActionException("could not parse webhook action. unsupported http method ["
|
||||
+ method.getName() + "]");
|
||||
}
|
||||
} else if (URL_TEMPLATE_FIELD.match(currentFieldName)) {
|
||||
urlTemplate = StringTemplateUtils.readTemplate(parser);
|
||||
} else if (BODY_TEMPLATE_FIELD.match(currentFieldName)) {
|
||||
bodyTemplate = StringTemplateUtils.readTemplate(parser);
|
||||
} else {
|
||||
throw new ActionException("could not parse webhook action. unexpected field [" + currentFieldName + "]");
|
||||
}
|
||||
} else {
|
||||
throw new ActionException("could not parse webhook action. unexpected token [" + token + "]");
|
||||
|
|
Loading…
Reference in New Issue