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