Watcher: Add proxy support to pagerduty action (elastic/elasticsearch#3542)

This is the last action that needs additional support for proxies.

You can set a proxy in the JSON like this:

```
"actions" : {
  "notify-pagerduty" : {
    "pagerduty" : {
      "description" : "Main system down, please check!",
      "proxy" : { "host" : "localhost", "port" : 8080 }
    }
  }
}
```

Closes elastic/elasticsearch#3372

Original commit: elastic/x-pack-elasticsearch@b99969fd6b
This commit is contained in:
Alexander Reelsen 2016-10-05 10:10:02 +02:00 committed by GitHub
parent 7ffebef2cd
commit 53103e988f
7 changed files with 122 additions and 69 deletions

View File

@ -14,6 +14,7 @@ import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.xpack.common.http.HttpMethod; import org.elasticsearch.xpack.common.http.HttpMethod;
import org.elasticsearch.xpack.common.http.HttpProxy;
import org.elasticsearch.xpack.common.http.HttpRequest; import org.elasticsearch.xpack.common.http.HttpRequest;
import org.elasticsearch.xpack.common.http.Scheme; import org.elasticsearch.xpack.common.http.Scheme;
import org.elasticsearch.xpack.common.text.TextTemplate; import org.elasticsearch.xpack.common.text.TextTemplate;
@ -41,6 +42,7 @@ public class IncidentEvent implements ToXContent {
static final String PATH = "/generic/2010-04-15/create_event.json"; static final String PATH = "/generic/2010-04-15/create_event.json";
final String description; final String description;
@Nullable final HttpProxy proxy;
@Nullable final String incidentKey; @Nullable final String incidentKey;
@Nullable final String client; @Nullable final String client;
@Nullable final String clientUrl; @Nullable final String clientUrl;
@ -51,16 +53,17 @@ public class IncidentEvent implements ToXContent {
public IncidentEvent(String description, @Nullable String eventType, @Nullable String incidentKey, @Nullable String client, public IncidentEvent(String description, @Nullable String eventType, @Nullable String incidentKey, @Nullable String client,
@Nullable String clientUrl, @Nullable String account, boolean attachPayload, @Nullable String clientUrl, @Nullable String account, boolean attachPayload,
@Nullable IncidentEventContext[] contexts) { @Nullable IncidentEventContext[] contexts, @Nullable HttpProxy proxy) {
this.description = description; this.description = description;
if (description == null) { if (description == null) {
throw new IllegalStateException("could not create pagerduty event. missing required [" + throw new IllegalStateException("could not create pagerduty event. missing required [" +
XField.DESCRIPTION.getPreferredName() + "] setting"); Fields.DESCRIPTION.getPreferredName() + "] setting");
} }
this.incidentKey = incidentKey; this.incidentKey = incidentKey;
this.client = client; this.client = client;
this.clientUrl = clientUrl; this.clientUrl = clientUrl;
this.account = account; this.account = account;
this.proxy = proxy;
this.attachPayload = attachPayload; this.attachPayload = attachPayload;
this.contexts = contexts; this.contexts = contexts;
this.eventType = Strings.hasLength(eventType) ? eventType : "trigger"; this.eventType = Strings.hasLength(eventType) ? eventType : "trigger";
@ -79,12 +82,13 @@ public class IncidentEvent implements ToXContent {
Objects.equals(attachPayload, template.attachPayload) && Objects.equals(attachPayload, template.attachPayload) &&
Objects.equals(eventType, template.eventType) && Objects.equals(eventType, template.eventType) &&
Objects.equals(account, template.account) && Objects.equals(account, template.account) &&
Objects.equals(proxy, template.proxy) &&
Arrays.equals(contexts, template.contexts); Arrays.equals(contexts, template.contexts);
} }
@Override @Override
public int hashCode() { public int hashCode() {
int result = Objects.hash(description, incidentKey, client, clientUrl, account, attachPayload, eventType); int result = Objects.hash(description, incidentKey, client, clientUrl, account, attachPayload, eventType, proxy);
result = 31 * result + Arrays.hashCode(contexts); result = 31 * result + Arrays.hashCode(contexts);
return result; return result;
} }
@ -94,29 +98,30 @@ public class IncidentEvent implements ToXContent {
.method(HttpMethod.POST) .method(HttpMethod.POST)
.scheme(Scheme.HTTPS) .scheme(Scheme.HTTPS)
.path(PATH) .path(PATH)
.proxy(proxy)
.jsonBody(new ToXContent() { .jsonBody(new ToXContent() {
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field(XField.SERVICE_KEY.getPreferredName(), serviceKey); builder.field(Fields.SERVICE_KEY.getPreferredName(), serviceKey);
builder.field(XField.EVENT_TYPE.getPreferredName(), eventType); builder.field(Fields.EVENT_TYPE.getPreferredName(), eventType);
builder.field(XField.DESCRIPTION.getPreferredName(), description); builder.field(Fields.DESCRIPTION.getPreferredName(), description);
if (incidentKey != null) { if (incidentKey != null) {
builder.field(XField.INCIDENT_KEY.getPreferredName(), incidentKey); builder.field(Fields.INCIDENT_KEY.getPreferredName(), incidentKey);
} }
if (client != null) { if (client != null) {
builder.field(XField.CLIENT.getPreferredName(), client); builder.field(Fields.CLIENT.getPreferredName(), client);
} }
if (clientUrl != null) { if (clientUrl != null) {
builder.field(XField.CLIENT_URL.getPreferredName(), clientUrl); builder.field(Fields.CLIENT_URL.getPreferredName(), clientUrl);
} }
if (attachPayload) { if (attachPayload) {
builder.startObject(XField.DETAILS.getPreferredName()); builder.startObject(Fields.DETAILS.getPreferredName());
builder.field(XField.PAYLOAD.getPreferredName()); builder.field(Fields.PAYLOAD.getPreferredName());
payload.toXContent(builder, params); payload.toXContent(builder, params);
builder.endObject(); builder.endObject();
} }
if (contexts != null && contexts.length > 0) { if (contexts != null && contexts.length > 0) {
builder.startArray(IncidentEvent.XField.CONTEXT.getPreferredName()); builder.startArray(Fields.CONTEXT.getPreferredName());
for (IncidentEventContext context : contexts) { for (IncidentEventContext context : contexts) {
context.toXContent(builder, params); context.toXContent(builder, params);
} }
@ -131,22 +136,25 @@ public class IncidentEvent implements ToXContent {
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject(); builder.startObject();
builder.field(XField.DESCRIPTION.getPreferredName(), description); builder.field(Fields.DESCRIPTION.getPreferredName(), description);
if (incidentKey != null) { if (incidentKey != null) {
builder.field(XField.INCIDENT_KEY.getPreferredName(), incidentKey); builder.field(Fields.INCIDENT_KEY.getPreferredName(), incidentKey);
} }
if (client != null) { if (client != null) {
builder.field(XField.CLIENT.getPreferredName(), client); builder.field(Fields.CLIENT.getPreferredName(), client);
} }
if (clientUrl != null) { if (clientUrl != null) {
builder.field(XField.CLIENT_URL.getPreferredName(), clientUrl); builder.field(Fields.CLIENT_URL.getPreferredName(), clientUrl);
} }
if (account != null) { if (account != null) {
builder.field(XField.ACCOUNT.getPreferredName(), account); builder.field(Fields.ACCOUNT.getPreferredName(), account);
} }
builder.field(XField.ATTACH_PAYLOAD.getPreferredName(), attachPayload); if (proxy != null) {
proxy.toXContent(builder, params);
}
builder.field(Fields.ATTACH_PAYLOAD.getPreferredName(), attachPayload);
if (contexts != null) { if (contexts != null) {
builder.startArray(XField.CONTEXT.getPreferredName()); builder.startArray(Fields.CONTEXT.getPreferredName());
for (IncidentEventContext context : contexts) { for (IncidentEventContext context : contexts) {
context.toXContent(builder, params); context.toXContent(builder, params);
} }
@ -172,9 +180,11 @@ public class IncidentEvent implements ToXContent {
public final String account; public final String account;
final Boolean attachPayload; final Boolean attachPayload;
final IncidentEventContext.Template[] contexts; final IncidentEventContext.Template[] contexts;
final HttpProxy proxy;
public Template(TextTemplate description, TextTemplate eventType, TextTemplate incidentKey, TextTemplate client, public Template(TextTemplate description, TextTemplate eventType, TextTemplate incidentKey, TextTemplate client,
TextTemplate clientUrl, String account, Boolean attachPayload, IncidentEventContext.Template[] contexts) { TextTemplate clientUrl, String account, Boolean attachPayload, IncidentEventContext.Template[] contexts,
HttpProxy proxy) {
this.description = description; this.description = description;
this.eventType = eventType; this.eventType = eventType;
this.incidentKey = incidentKey; this.incidentKey = incidentKey;
@ -183,6 +193,7 @@ public class IncidentEvent implements ToXContent {
this.account = account; this.account = account;
this.attachPayload = attachPayload; this.attachPayload = attachPayload;
this.contexts = contexts; this.contexts = contexts;
this.proxy = proxy;
} }
@Override @Override
@ -198,12 +209,13 @@ public class IncidentEvent implements ToXContent {
Objects.equals(eventType, template.eventType) && Objects.equals(eventType, template.eventType) &&
Objects.equals(attachPayload, template.attachPayload) && Objects.equals(attachPayload, template.attachPayload) &&
Objects.equals(account, template.account) && Objects.equals(account, template.account) &&
Objects.equals(proxy, template.proxy) &&
Arrays.equals(contexts, template.contexts); Arrays.equals(contexts, template.contexts);
} }
@Override @Override
public int hashCode() { public int hashCode() {
int result = Objects.hash(description, eventType, incidentKey, client, clientUrl, attachPayload, account); int result = Objects.hash(description, eventType, incidentKey, client, clientUrl, attachPayload, account, proxy);
result = 31 * result + Arrays.hashCode(contexts); result = 31 * result + Arrays.hashCode(contexts);
return result; return result;
} }
@ -224,33 +236,36 @@ public class IncidentEvent implements ToXContent {
contexts[i] = this.contexts[i].render(engine, model, defaults); contexts[i] = this.contexts[i].render(engine, model, defaults);
} }
} }
return new IncidentEvent(description, eventType, incidentKey, client, clientUrl, account, attachPayload, contexts); return new IncidentEvent(description, eventType, incidentKey, client, clientUrl, account, attachPayload, contexts, proxy);
} }
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject(); builder.startObject();
builder.field(XField.DESCRIPTION.getPreferredName(), description, params); builder.field(Fields.DESCRIPTION.getPreferredName(), description, params);
if (incidentKey != null) { if (incidentKey != null) {
builder.field(XField.INCIDENT_KEY.getPreferredName(), incidentKey, params); builder.field(Fields.INCIDENT_KEY.getPreferredName(), incidentKey, params);
} }
if (client != null) { if (client != null) {
builder.field(XField.CLIENT.getPreferredName(), client, params); builder.field(Fields.CLIENT.getPreferredName(), client, params);
} }
if (clientUrl != null) { if (clientUrl != null) {
builder.field(XField.CLIENT_URL.getPreferredName(), clientUrl, params); builder.field(Fields.CLIENT_URL.getPreferredName(), clientUrl, params);
} }
if (eventType != null) { if (eventType != null) {
builder.field(XField.EVENT_TYPE.getPreferredName(), eventType, params); builder.field(Fields.EVENT_TYPE.getPreferredName(), eventType, params);
} }
if (attachPayload != null) { if (attachPayload != null) {
builder.field(XField.ATTACH_PAYLOAD.getPreferredName(), attachPayload); builder.field(Fields.ATTACH_PAYLOAD.getPreferredName(), attachPayload);
} }
if (account != null) { if (account != null) {
builder.field(XField.ACCOUNT.getPreferredName(), account); builder.field(Fields.ACCOUNT.getPreferredName(), account);
}
if (proxy != null) {
proxy.toXContent(builder, params);
} }
if (contexts != null) { if (contexts != null) {
builder.startArray(XField.CONTEXT.getPreferredName()); builder.startArray(Fields.CONTEXT.getPreferredName());
for (IncidentEventContext.Template context : contexts) { for (IncidentEventContext.Template context : contexts) {
context.toXContent(builder, params); context.toXContent(builder, params);
} }
@ -266,6 +281,7 @@ public class IncidentEvent implements ToXContent {
TextTemplate clientUrl = null; TextTemplate clientUrl = null;
TextTemplate eventType = null; TextTemplate eventType = null;
String account = null; String account = null;
HttpProxy proxy = null;
Boolean attachPayload = null; Boolean attachPayload = null;
IncidentEventContext.Template[] contexts = null; IncidentEventContext.Template[] contexts = null;
@ -274,56 +290,58 @@ public class IncidentEvent implements ToXContent {
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName(); currentFieldName = parser.currentName();
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, XField.INCIDENT_KEY)) { } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Fields.INCIDENT_KEY)) {
try { try {
incidentKey = TextTemplate.parse(parser); incidentKey = TextTemplate.parse(parser);
} catch (ElasticsearchParseException e) { } catch (ElasticsearchParseException e) {
throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]", throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]",
XField.INCIDENT_KEY.getPreferredName()); Fields.INCIDENT_KEY.getPreferredName());
} }
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, XField.DESCRIPTION)) { } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Fields.DESCRIPTION)) {
try { try {
description = TextTemplate.parse(parser); description = TextTemplate.parse(parser);
} catch (ElasticsearchParseException e) { } catch (ElasticsearchParseException e) {
throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]", throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]",
XField.DESCRIPTION.getPreferredName()); Fields.DESCRIPTION.getPreferredName());
} }
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, XField.CLIENT)) { } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Fields.CLIENT)) {
try { try {
client = TextTemplate.parse(parser); client = TextTemplate.parse(parser);
} catch (ElasticsearchParseException e) { } catch (ElasticsearchParseException e) {
throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]", throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]",
XField.CLIENT.getPreferredName()); Fields.CLIENT.getPreferredName());
} }
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, XField.CLIENT_URL)) { } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Fields.CLIENT_URL)) {
try { try {
clientUrl = TextTemplate.parse(parser); clientUrl = TextTemplate.parse(parser);
} catch (ElasticsearchParseException e) { } catch (ElasticsearchParseException e) {
throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]", throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]",
XField.CLIENT_URL.getPreferredName()); Fields.CLIENT_URL.getPreferredName());
} }
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, XField.ACCOUNT)) { } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Fields.ACCOUNT)) {
try { try {
account = parser.text(); account = parser.text();
} catch (ElasticsearchParseException e) { } catch (ElasticsearchParseException e) {
throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]", throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]",
XField.CLIENT_URL.getPreferredName()); Fields.CLIENT_URL.getPreferredName());
} }
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, XField.EVENT_TYPE)) { } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Fields.PROXY)) {
proxy = HttpProxy.parse(parser);
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Fields.EVENT_TYPE)) {
try { try {
eventType = TextTemplate.parse(parser); eventType = TextTemplate.parse(parser);
} catch (ElasticsearchParseException e) { } catch (ElasticsearchParseException e) {
throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]", throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]",
XField.EVENT_TYPE.getPreferredName()); Fields.EVENT_TYPE.getPreferredName());
} }
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, XField.ATTACH_PAYLOAD)) { } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Fields.ATTACH_PAYLOAD)) {
if (token == XContentParser.Token.VALUE_BOOLEAN) { if (token == XContentParser.Token.VALUE_BOOLEAN) {
attachPayload = parser.booleanValue(); attachPayload = parser.booleanValue();
} else { } else {
throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}], " + throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}], " +
"expected a boolean value but found [{}] instead", XField.ATTACH_PAYLOAD.getPreferredName(), token); "expected a boolean value but found [{}] instead", Fields.ATTACH_PAYLOAD.getPreferredName(), token);
} }
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, XField.CONTEXT)) { } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Fields.CONTEXT)) {
if (token == XContentParser.Token.START_ARRAY) { if (token == XContentParser.Token.START_ARRAY) {
List<IncidentEventContext.Template> list = new ArrayList<>(); List<IncidentEventContext.Template> list = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
@ -331,7 +349,7 @@ public class IncidentEvent implements ToXContent {
list.add(IncidentEventContext.Template.parse(parser)); list.add(IncidentEventContext.Template.parse(parser));
} catch (ElasticsearchParseException e) { } catch (ElasticsearchParseException e) {
throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field " + throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field " +
"[{}]", XField.CONTEXT.getPreferredName()); "[{}]", Fields.CONTEXT.getPreferredName());
} }
} }
contexts = list.toArray(new IncidentEventContext.Template[list.size()]); contexts = list.toArray(new IncidentEventContext.Template[list.size()]);
@ -341,7 +359,7 @@ public class IncidentEvent implements ToXContent {
currentFieldName); currentFieldName);
} }
} }
return new Template(description, eventType, incidentKey, client, clientUrl, account, attachPayload, contexts); return new Template(description, eventType, incidentKey, client, clientUrl, account, attachPayload, contexts, proxy);
} }
public static class Builder { public static class Builder {
@ -352,6 +370,7 @@ public class IncidentEvent implements ToXContent {
TextTemplate clientUrl; TextTemplate clientUrl;
TextTemplate eventType; TextTemplate eventType;
String account; String account;
HttpProxy proxy;
Boolean attachPayload; Boolean attachPayload;
List<IncidentEventContext.Template> contexts = new ArrayList<>(); List<IncidentEventContext.Template> contexts = new ArrayList<>();
@ -389,6 +408,11 @@ public class IncidentEvent implements ToXContent {
return this; return this;
} }
public Builder setProxy(HttpProxy proxy) {
this.proxy = proxy;
return this;
}
public Builder addContext(IncidentEventContext.Template context) { public Builder addContext(IncidentEventContext.Template context) {
this.contexts.add(context); this.contexts.add(context);
return this; return this;
@ -397,17 +421,18 @@ public class IncidentEvent implements ToXContent {
public Template build() { public Template build() {
IncidentEventContext.Template[] contexts = this.contexts.isEmpty() ? null : IncidentEventContext.Template[] contexts = this.contexts.isEmpty() ? null :
this.contexts.toArray(new IncidentEventContext.Template[this.contexts.size()]); this.contexts.toArray(new IncidentEventContext.Template[this.contexts.size()]);
return new Template(description, eventType, incidentKey, client, clientUrl, account, attachPayload, contexts); return new Template(description, eventType, incidentKey, client, clientUrl, account, attachPayload, contexts, proxy);
} }
} }
} }
interface XField { interface Fields {
ParseField TYPE = new ParseField("type"); ParseField TYPE = new ParseField("type");
ParseField EVENT_TYPE = new ParseField("event_type"); ParseField EVENT_TYPE = new ParseField("event_type");
ParseField ACCOUNT = new ParseField("account"); ParseField ACCOUNT = new ParseField("account");
ParseField PROXY = new ParseField("proxy");
ParseField DESCRIPTION = new ParseField("description"); ParseField DESCRIPTION = new ParseField("description");
ParseField INCIDENT_KEY = new ParseField("incident_key"); ParseField INCIDENT_KEY = new ParseField("incident_key");
ParseField CLIENT = new ParseField("client"); ParseField CLIENT = new ParseField("client");

View File

@ -24,12 +24,12 @@ public class IncidentEventDefaults {
final Context.ImageDefaults image; final Context.ImageDefaults image;
public IncidentEventDefaults(Settings accountSettings) { public IncidentEventDefaults(Settings accountSettings) {
description = accountSettings.get(IncidentEvent.XField.DESCRIPTION.getPreferredName(), null); description = accountSettings.get(IncidentEvent.Fields.DESCRIPTION.getPreferredName(), null);
incidentKey = accountSettings.get(IncidentEvent.XField.INCIDENT_KEY.getPreferredName(), null); incidentKey = accountSettings.get(IncidentEvent.Fields.INCIDENT_KEY.getPreferredName(), null);
client = accountSettings.get(IncidentEvent.XField.CLIENT.getPreferredName(), null); client = accountSettings.get(IncidentEvent.Fields.CLIENT.getPreferredName(), null);
clientUrl = accountSettings.get(IncidentEvent.XField.CLIENT_URL.getPreferredName(), null); clientUrl = accountSettings.get(IncidentEvent.Fields.CLIENT_URL.getPreferredName(), null);
eventType = accountSettings.get(IncidentEvent.XField.EVENT_TYPE.getPreferredName(), null); eventType = accountSettings.get(IncidentEvent.Fields.EVENT_TYPE.getPreferredName(), null);
attachPayload = accountSettings.getAsBoolean(IncidentEvent.XField.ATTACH_PAYLOAD.getPreferredName(), false); attachPayload = accountSettings.getAsBoolean(IncidentEvent.Fields.ATTACH_PAYLOAD.getPreferredName(), false);
link = new Context.LinkDefaults(accountSettings.getAsSettings("link")); link = new Context.LinkDefaults(accountSettings.getAsSettings("link"));
image = new Context.ImageDefaults(accountSettings.getAsSettings("image")); image = new Context.ImageDefaults(accountSettings.getAsSettings("image"));

View File

@ -19,9 +19,6 @@ import org.elasticsearch.xpack.watcher.watch.Payload;
import java.util.Map; import java.util.Map;
/**
*
*/
public class ExecutablePagerDutyAction extends ExecutableAction<PagerDutyAction> { public class ExecutablePagerDutyAction extends ExecutableAction<PagerDutyAction> {
private final TextTemplateEngine templateEngine; private final TextTemplateEngine templateEngine;

View File

@ -16,9 +16,6 @@ import org.elasticsearch.xpack.notification.pagerduty.SentEvent;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
/**
*
*/
public class PagerDutyAction implements Action { public class PagerDutyAction implements Action {
public static final String TYPE = "pagerduty"; public static final String TYPE = "pagerduty";

View File

@ -9,15 +9,22 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.common.http.HttpClient; import org.elasticsearch.xpack.common.http.HttpClient;
import org.elasticsearch.xpack.common.http.HttpProxy;
import org.elasticsearch.xpack.common.http.HttpRequest;
import org.elasticsearch.xpack.common.http.HttpResponse;
import org.elasticsearch.xpack.notification.slack.message.SlackMessageDefaultsTests; import org.elasticsearch.xpack.notification.slack.message.SlackMessageDefaultsTests;
import org.elasticsearch.xpack.watcher.watch.Payload;
import org.junit.Before; import org.junit.Before;
import org.mockito.ArgumentCaptor;
import java.util.Map; import java.util.Map;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isOneOf; import static org.hamcrest.Matchers.isOneOf;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/** /**
* *
@ -123,6 +130,23 @@ public class PagerDutyAccountsTests extends ESTestCase {
} catch (SettingsException e) {} } catch (SettingsException e) {}
} }
public void testProxy() throws Exception {
Settings.Builder builder = Settings.builder().put("default_account", "account1");
addAccountSettings("account1", builder);
PagerDutyAccounts accounts = new PagerDutyAccounts(builder.build(), httpClient, logger);
PagerDutyAccount account = accounts.account("account1");
ArgumentCaptor<HttpRequest> argumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
when(httpClient.execute(argumentCaptor.capture())).thenReturn(new HttpResponse(200));
HttpProxy proxy = new HttpProxy("localhost", 8080);
IncidentEvent event = new IncidentEvent("foo", null, null, null, null, account.getName(), true, null, proxy);
account.send(event, Payload.EMPTY);
HttpRequest request = argumentCaptor.getValue();
assertThat(request.proxy(), is(proxy));
}
private void addAccountSettings(String name, Settings.Builder builder) { private void addAccountSettings(String name, Settings.Builder builder) {
builder.put("account." + name + ".service_api_key", randomAsciiOfLength(50)); builder.put("account." + name + ".service_api_key", randomAsciiOfLength(50));
Settings defaults = SlackMessageDefaultsTests.randomSettings(); Settings defaults = SlackMessageDefaultsTests.randomSettings();

View File

@ -14,19 +14,20 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.common.http.HttpProxy;
import org.elasticsearch.xpack.common.http.HttpRequest;
import org.elasticsearch.xpack.common.http.HttpResponse;
import org.elasticsearch.xpack.common.text.TextTemplate;
import org.elasticsearch.xpack.common.text.TextTemplateEngine; import org.elasticsearch.xpack.common.text.TextTemplateEngine;
import org.elasticsearch.xpack.watcher.actions.Action;
import org.elasticsearch.xpack.notification.pagerduty.IncidentEvent; import org.elasticsearch.xpack.notification.pagerduty.IncidentEvent;
import org.elasticsearch.xpack.notification.pagerduty.IncidentEventContext; import org.elasticsearch.xpack.notification.pagerduty.IncidentEventContext;
import org.elasticsearch.xpack.notification.pagerduty.IncidentEventDefaults; import org.elasticsearch.xpack.notification.pagerduty.IncidentEventDefaults;
import org.elasticsearch.xpack.notification.pagerduty.PagerDutyAccount; import org.elasticsearch.xpack.notification.pagerduty.PagerDutyAccount;
import org.elasticsearch.xpack.notification.pagerduty.PagerDutyService; import org.elasticsearch.xpack.notification.pagerduty.PagerDutyService;
import org.elasticsearch.xpack.notification.pagerduty.SentEvent; import org.elasticsearch.xpack.notification.pagerduty.SentEvent;
import org.elasticsearch.xpack.watcher.actions.Action;
import org.elasticsearch.xpack.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.watcher.execution.WatchExecutionContext;
import org.elasticsearch.xpack.watcher.execution.Wid; import org.elasticsearch.xpack.watcher.execution.Wid;
import org.elasticsearch.xpack.common.http.HttpRequest;
import org.elasticsearch.xpack.common.http.HttpResponse;
import org.elasticsearch.xpack.common.text.TextTemplate;
import org.elasticsearch.xpack.watcher.watch.Payload; import org.elasticsearch.xpack.watcher.watch.Payload;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.DateTimeZone; import org.joda.time.DateTimeZone;
@ -102,8 +103,8 @@ public class PagerDutyActionTests extends ESTestCase {
when(templateEngine.render(description, expectedModel)).thenReturn(description.getTemplate()); when(templateEngine.render(description, expectedModel)).thenReturn(description.getTemplate());
IncidentEvent event = new IncidentEvent(description.getTemplate(), null, wid.watchId(), null, null, accountName, IncidentEvent event = new IncidentEvent(description.getTemplate(), null, wid.watchId(), null, null, accountName, attachPayload,
attachPayload, null); null, null);
PagerDutyAccount account = mock(PagerDutyAccount.class); PagerDutyAccount account = mock(PagerDutyAccount.class);
when(account.getDefaults()).thenReturn(new IncidentEventDefaults(Settings.EMPTY)); when(account.getDefaults()).thenReturn(new IncidentEventDefaults(Settings.EMPTY));
HttpResponse response = mock(HttpResponse.class); HttpResponse response = mock(HttpResponse.class);
@ -163,6 +164,12 @@ public class PagerDutyActionTests extends ESTestCase {
builder.field("attach_payload", attachPayload.booleanValue()); builder.field("attach_payload", attachPayload.booleanValue());
} }
HttpProxy proxy = null;
if (randomBoolean()) {
proxy = new HttpProxy("localhost", 8080);
proxy.toXContent(builder, ToXContent.EMPTY_PARAMS);
}
IncidentEventContext.Template[] contexts = null; IncidentEventContext.Template[] contexts = null;
if (randomBoolean()) { if (randomBoolean()) {
contexts = new IncidentEventContext.Template[] { contexts = new IncidentEventContext.Template[] {
@ -186,7 +193,7 @@ public class PagerDutyActionTests extends ESTestCase {
assertThat(action.event, notNullValue()); assertThat(action.event, notNullValue());
assertThat(action.event, instanceOf(IncidentEvent.Template.class)); assertThat(action.event, instanceOf(IncidentEvent.Template.class));
assertThat(action.event, is(new IncidentEvent.Template(description, eventType, incidentKey, client, clientUrl, accountName, assertThat(action.event, is(new IncidentEvent.Template(description, eventType, incidentKey, client, clientUrl, accountName,
attachPayload, contexts))); attachPayload, contexts, proxy)));
} }
public void testParserSelfGenerated() throws Exception { public void testParserSelfGenerated() throws Exception {
@ -217,6 +224,9 @@ public class PagerDutyActionTests extends ESTestCase {
if (randomBoolean()) { if (randomBoolean()) {
event.setAccount(randomAsciiOfLength(50)).build(); event.setAccount(randomAsciiOfLength(50)).build();
} }
if (randomBoolean()) {
event.setProxy(new HttpProxy("localhost", 8080));
}
PagerDutyAction action = pagerDutyAction(event).build(); PagerDutyAction action = pagerDutyAction(event).build();
XContentBuilder jsonBuilder = jsonBuilder(); XContentBuilder jsonBuilder = jsonBuilder();

View File

@ -60,7 +60,7 @@ public class PagerDutyServiceTests extends AbstractWatcherIntegrationTestCase {
true, new IncidentEventContext[] { true, new IncidentEventContext[] {
IncidentEventContext.link("_href", "_text"), IncidentEventContext.link("_href", "_text"),
IncidentEventContext.image("_src", "_href", "_alt") IncidentEventContext.image("_src", "_href", "_alt")
}); }, null);
Payload payload = new Payload.Simple("_key", "_val"); Payload payload = new Payload.Simple("_key", "_val");