Commit 764bf720 by pahli Committed by Johannes Edmeier

Add Let´s Chat notifications

Adds an new Notifier implementation for Let's Chat.
parent 4f80a489
......@@ -241,3 +241,41 @@ account and configure it appropriately.
| `+++"*#{application.name}* (#{application.id}) is *#{to.status}*"+++`
|
|===
[letschat-notifications]
==== Let´s Chat notifications ====
To enable Let´s Chat notifications you need to add the host url and add the API token and username from Let´s Chat
.Let´s Chat notifications configuration options
|===
| Property name |Description |Default value
| spring.boot.admin.notify.letschat.enabled
| Enable let´s Chat notifications
| `true`
| spring.boot.admin.notify.letschat.ignore-changes
| Comma-delimited list of status changes to be ignored. Format: "<from-status>:<to-status>". Wildcards allowed.
| `"UNKNOWN:UP"`
| spring.boot.admin.notify.letschat.url
| The let´s Chat Host URL to send notifications
|
| spring.boot.admin.notify.letschat.room
| the room where to send the messages
|
| spring.boot.admin.notify.letschat.token
| the token to access the let´s Chat API
|
| spring.boot.admin.notify.letschat.username
| The username for which the token was created
| `Spring Boot Admin`
| spring.boot.admin.notify.letschat.message
| Message to use in the event. SpEL-expressions are supported
| `+++"*#{application.name}* (#{application.id}) is *#{to.status}*"+++`
|
|===
......@@ -40,6 +40,7 @@ import de.codecentric.boot.admin.notify.Notifier;
import de.codecentric.boot.admin.notify.NotifierListener;
import de.codecentric.boot.admin.notify.PagerdutyNotifier;
import de.codecentric.boot.admin.notify.SlackNotifier;
import de.codecentric.boot.admin.notify.LetsChatNotifier;
import de.codecentric.boot.admin.notify.filter.FilteringNotifier;
import de.codecentric.boot.admin.notify.filter.web.NotificationFilterController;
import de.codecentric.boot.admin.web.PrefixHandlerMapping;
......@@ -161,4 +162,16 @@ public class NotifierConfiguration {
}
}
@Configuration
@ConditionalOnProperty(prefix = "spring.boot.admin.notify.letschat", name = "url")
@AutoConfigureBefore({ NotifierListenerConfiguration.class,
CompositeNotifierConfiguration.class })
public static class LetsChatNotifierConfiguration {
@Bean
@ConditionalOnMissingBean
@ConfigurationProperties("spring.boot.admin.notify.letschat")
public LetsChatNotifier letsChatNotifier() {
return new LetsChatNotifier();
}
}
}
/*
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.notify;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.Base64Utils;
import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.event.ClientApplicationEvent;
/**
* Notifier submitting events to let´s Chat.
*
* @author Rico Pahlisch
*/
public class LetsChatNotifier extends AbstractStatusChangeNotifier {
private static final String DEFAULT_MESSAGE = "*#{application.name}* (#{application.id}) is *#{to.status}*";
private final SpelExpressionParser parser = new SpelExpressionParser();
private RestTemplate restTemplate = new RestTemplate();
/**
* Host URL for Let´s Chat
*/
private URI url;
/**
* Name of the room
*/
private String room;
/**
* Token for the Let´s chat API
*/
private String token;
/**
* username which sends notification
*/
private String username = "Spring Boot Admin";
/**
* Message template. SpEL template using event as root
*/
private Expression message;
public LetsChatNotifier() {
this.message = parser.parseExpression(DEFAULT_MESSAGE, ParserContext.TEMPLATE_EXPRESSION);
}
@Override
protected void doNotify(ClientApplicationEvent event) throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// Let's Chat requiers the token as basic username, the password can be an arbitrary string.
String auth = Base64Utils.encodeToString(String.format("%s:%s", token, username).getBytes());
headers.add(HttpHeaders.AUTHORIZATION, String.format("Basic %s", auth));
restTemplate.exchange(createUrl(), HttpMethod.POST, new HttpEntity<>(createMessage(event), headers), Void.class);
}
private URI createUrl() {
return URI.create(String.format("%s/rooms/%s/messages", url, room));
}
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void setUrl(URI url) {
this.url = url;
}
public void setUsername(String username) {
this.username = username;
}
public void setRoom(String room) {
this.room = room;
}
public void setToken(String token) {
this.token = token;
}
public void setMessage(String message) {
this.message = parser.parseExpression(message, ParserContext.TEMPLATE_EXPRESSION);
}
protected Object createMessage(ClientApplicationEvent event) {
Map<String, String> messageJson = new HashMap<>();
messageJson.put("text", getText(event));
return messageJson;
}
protected String getText(ClientApplicationEvent event) {
return message.getValue(event, String.class);
}
}
package de.codecentric.boot.admin.notify;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.Base64Utils;
import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.model.StatusInfo;
public class LetsChatNotifierTest {
private static final String room = "text_room";
private static final String token = "text_token";
private static final String user = "api_user";
private static final String appName = "App";
private static final String id = "-id-";
private static final String host = "http://localhost";
private LetsChatNotifier notifier;
private RestTemplate restTemplate;
@Before
public void setUp() {
restTemplate = mock(RestTemplate.class);
notifier = new LetsChatNotifier();
notifier.setUsername(user);
notifier.setUrl(URI.create(host));
notifier.setRoom(room);
notifier.setToken(token);
notifier.setRestTemplate(restTemplate);
}
@Test
public void test_onApplicationEvent_resolve() {
StatusInfo infoDown = StatusInfo.ofDown();
StatusInfo infoUp = StatusInfo.ofUp();
notifier.notify(getEvent(infoDown, infoUp));
HttpEntity<?> expected = expectedMessage(standardMessage(infoUp.getStatus(), appName, id));
verify(restTemplate).exchange(eq(URI.create(String.format("%s/rooms/%s/messages", host, room))), eq(HttpMethod.POST), eq(expected), eq(Void.class));
}
@Test
public void test_onApplicationEvent_resolve_with_custom_message() {
StatusInfo infoDown = StatusInfo.ofDown();
StatusInfo infoUp = StatusInfo.ofUp();
notifier.setMessage("TEST");
notifier.notify(getEvent(infoDown, infoUp));
HttpEntity<?> expected = expectedMessage("TEST");
verify(restTemplate).exchange(eq(URI.create(String.format("%s/rooms/%s/messages", host, room))), eq(HttpMethod.POST), eq(expected), eq(Void.class));
}
private ClientApplicationStatusChangedEvent getEvent(StatusInfo infoDown, StatusInfo infoUp) {
return new ClientApplicationStatusChangedEvent(
Application.create(appName).withId(id).withHealthUrl("http://health").build(),
infoDown, infoUp);
}
private HttpEntity<?> expectedMessage(String message) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
String auth = Base64Utils.encodeToString(String.format("%s:%s", token, user).getBytes());
httpHeaders.add(HttpHeaders.AUTHORIZATION, String.format("Basic %s", auth));
Map<String, Object> messageJson = new HashMap<>();
messageJson.put("text", message);
return new HttpEntity<>(messageJson, httpHeaders);
}
private String standardMessage(String status, String appName, String id) {
return "*" + appName + "* (" + id + ") is *" + status + "*";
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment