felix.wang
4 years ago
committed by
GitHub
12 changed files with 681 additions and 0 deletions
@ -0,0 +1,82 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<!-- |
||||
~ Licensed to the Apache Software Foundation (ASF) under one or more |
||||
~ contributor license agreements. See the NOTICE file distributed with |
||||
~ this work for additional information regarding copyright ownership. |
||||
~ The ASF licenses this file to You 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. |
||||
--> |
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<parent> |
||||
<artifactId>dolphinscheduler-alert-plugin</artifactId> |
||||
<groupId>org.apache.dolphinscheduler</groupId> |
||||
<version>1.3.4-SNAPSHOT</version> |
||||
</parent> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
|
||||
<groupId>org.apache.dolphinscheduler</groupId> |
||||
<artifactId>dolphinscheduler-alert-feishu</artifactId> |
||||
<packaging>dolphinscheduler-plugin</packaging> |
||||
|
||||
<dependencies> |
||||
|
||||
<dependency> |
||||
<groupId>org.apache.dolphinscheduler</groupId> |
||||
<artifactId>dolphinscheduler-spi</artifactId> |
||||
<scope>provided</scope> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.apache.httpcomponents</groupId> |
||||
<artifactId>httpclient</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>com.google.guava</groupId> |
||||
<artifactId>guava</artifactId> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>ch.qos.logback</groupId> |
||||
<artifactId>logback-classic</artifactId> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>org.slf4j</groupId> |
||||
<artifactId>slf4j-api</artifactId> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>com.fasterxml.jackson.core</groupId> |
||||
<artifactId>jackson-annotations</artifactId> |
||||
<scope>provided</scope> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>junit</groupId> |
||||
<artifactId>junit</artifactId> |
||||
<scope>test</scope> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>org.mockito</groupId> |
||||
<artifactId>mockito-core</artifactId> |
||||
<type>jar</type> |
||||
<scope>test</scope> |
||||
</dependency> |
||||
</dependencies> |
||||
|
||||
<build> |
||||
<finalName>dolphinscheduler-alert-feishu-${project.version}</finalName> |
||||
</build> |
||||
|
||||
</project> |
@ -0,0 +1,37 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.plugin.alert.feishu; |
||||
|
||||
import org.apache.dolphinscheduler.spi.alert.AlertChannel; |
||||
import org.apache.dolphinscheduler.spi.alert.AlertData; |
||||
import org.apache.dolphinscheduler.spi.alert.AlertInfo; |
||||
import org.apache.dolphinscheduler.spi.alert.AlertResult; |
||||
import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer; |
||||
|
||||
import java.util.Map; |
||||
|
||||
public class FeiShuAlertChannel implements AlertChannel { |
||||
@Override |
||||
public AlertResult process(AlertInfo alertInfo) { |
||||
|
||||
AlertData alertData = alertInfo.getAlertData(); |
||||
String alertParams = alertInfo.getAlertParams(); |
||||
Map<String, String> paramsMap = PluginParamsTransfer.getPluginParamsMap(alertParams); |
||||
return new FeiShuSender(paramsMap).sendFeiShuMsg(alertData); |
||||
} |
||||
} |
@ -0,0 +1,82 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.plugin.alert.feishu; |
||||
|
||||
import org.apache.dolphinscheduler.spi.alert.AlertChannel; |
||||
import org.apache.dolphinscheduler.spi.alert.AlertChannelFactory; |
||||
import org.apache.dolphinscheduler.spi.params.InputParam; |
||||
import org.apache.dolphinscheduler.spi.params.PasswordParam; |
||||
import org.apache.dolphinscheduler.spi.params.RadioParam; |
||||
import org.apache.dolphinscheduler.spi.params.base.ParamsOptions; |
||||
import org.apache.dolphinscheduler.spi.params.base.PluginParams; |
||||
import org.apache.dolphinscheduler.spi.params.base.Validate; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
public class FeiShuAlertChannelFactory implements AlertChannelFactory { |
||||
@Override |
||||
public String getName() { |
||||
return "Feishu"; |
||||
} |
||||
|
||||
@Override |
||||
public List<PluginParams> getParams() { |
||||
InputParam webHookParam = InputParam.newBuilder(FeiShuParamsConstants.NAME_WEB_HOOK, FeiShuParamsConstants.WEB_HOOK) |
||||
.addValidate(Validate.newBuilder() |
||||
.setRequired(true) |
||||
.build()) |
||||
.build(); |
||||
RadioParam isEnableProxy = |
||||
RadioParam.newBuilder(FeiShuParamsConstants.NAME_FEI_SHU_PROXY_ENABLE, FeiShuParamsConstants.NAME_FEI_SHU_PROXY_ENABLE) |
||||
.addParamsOptions(new ParamsOptions("YES", true, false)) |
||||
.addParamsOptions(new ParamsOptions("NO", false, false)) |
||||
.setValue(true) |
||||
.addValidate(Validate.newBuilder() |
||||
.setRequired(false) |
||||
.build()) |
||||
.build(); |
||||
InputParam proxyParam = |
||||
InputParam.newBuilder(FeiShuParamsConstants.NAME_FEI_SHU_PROXY, FeiShuParamsConstants.FEI_SHU_PROXY) |
||||
.addValidate(Validate.newBuilder() |
||||
.setRequired(false).build()) |
||||
.build(); |
||||
|
||||
InputParam portParam = InputParam.newBuilder(FeiShuParamsConstants.NAME_FEI_SHU_PORT, FeiShuParamsConstants.FEI_SHU_PORT) |
||||
.addValidate(Validate.newBuilder() |
||||
.setRequired(false).build()) |
||||
.build(); |
||||
|
||||
InputParam userParam = |
||||
InputParam.newBuilder(FeiShuParamsConstants.NAME_FEI_SHU_USER, FeiShuParamsConstants.FEI_SHU_USER) |
||||
.addValidate(Validate.newBuilder() |
||||
.setRequired(false).build()) |
||||
.build(); |
||||
PasswordParam passwordParam = PasswordParam.newBuilder(FeiShuParamsConstants.NAME_FEI_SHU_PASSWORD, FeiShuParamsConstants.FEI_SHU_PASSWORD) |
||||
.setPlaceholder("if enable use authentication, you need input password") |
||||
.build(); |
||||
|
||||
return Arrays.asList(webHookParam, isEnableProxy, proxyParam, portParam, userParam, passwordParam); |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public AlertChannel create() { |
||||
return new FeiShuAlertChannel(); |
||||
} |
||||
} |
@ -0,0 +1,30 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.plugin.alert.feishu; |
||||
|
||||
import org.apache.dolphinscheduler.spi.DolphinSchedulerPlugin; |
||||
import org.apache.dolphinscheduler.spi.alert.AlertChannelFactory; |
||||
|
||||
import com.google.common.collect.ImmutableList; |
||||
|
||||
public class FeiShuAlertPlugin implements DolphinSchedulerPlugin { |
||||
@Override |
||||
public Iterable<AlertChannelFactory> getAlertChannelFactorys() { |
||||
return ImmutableList.of(new FeiShuAlertChannelFactory()); |
||||
} |
||||
} |
@ -0,0 +1,49 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.plugin.alert.feishu; |
||||
|
||||
public class FeiShuParamsConstants { |
||||
|
||||
private FeiShuParamsConstants() { |
||||
throw new IllegalStateException("Utility class"); |
||||
} |
||||
|
||||
static final String WEB_HOOK = "webhook"; |
||||
|
||||
static final String NAME_WEB_HOOK = "webHook"; |
||||
|
||||
public static final String FEI_SHU_PROXY_ENABLE = "isEnableProxy"; |
||||
|
||||
static final String NAME_FEI_SHU_PROXY_ENABLE = "isEnableProxy"; |
||||
|
||||
static final String FEI_SHU_PROXY = "proxy"; |
||||
|
||||
static final String NAME_FEI_SHU_PROXY = "proxy"; |
||||
|
||||
static final String FEI_SHU_PORT = "port"; |
||||
|
||||
static final String NAME_FEI_SHU_PORT = "port"; |
||||
|
||||
static final String FEI_SHU_USER = "user"; |
||||
|
||||
static final String NAME_FEI_SHU_USER = "user"; |
||||
|
||||
static final String FEI_SHU_PASSWORD = "password"; |
||||
|
||||
static final String NAME_FEI_SHU_PASSWORD = "password"; |
||||
} |
@ -0,0 +1,223 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.plugin.alert.feishu; |
||||
|
||||
import org.apache.dolphinscheduler.spi.alert.AlertData; |
||||
import org.apache.dolphinscheduler.spi.alert.AlertResult; |
||||
import org.apache.dolphinscheduler.spi.utils.JSONUtils; |
||||
|
||||
import org.apache.commons.codec.binary.StringUtils; |
||||
import org.apache.http.HttpEntity; |
||||
import org.apache.http.HttpHost; |
||||
import org.apache.http.HttpStatus; |
||||
import org.apache.http.client.config.RequestConfig; |
||||
import org.apache.http.client.methods.CloseableHttpResponse; |
||||
import org.apache.http.client.methods.HttpPost; |
||||
import org.apache.http.impl.client.CloseableHttpClient; |
||||
import org.apache.http.util.EntityUtils; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.HashMap; |
||||
import java.util.Iterator; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Map.Entry; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
|
||||
public class FeiShuSender { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(FeiShuSender.class); |
||||
|
||||
private String url; |
||||
|
||||
private Boolean enableProxy; |
||||
|
||||
private String proxy; |
||||
|
||||
private Integer port; |
||||
|
||||
private String user; |
||||
|
||||
private String password; |
||||
|
||||
FeiShuSender(Map<String, String> config) { |
||||
url = config.get(FeiShuParamsConstants.NAME_WEB_HOOK); |
||||
enableProxy = Boolean.valueOf(config.get(FeiShuParamsConstants.NAME_FEI_SHU_PROXY_ENABLE)); |
||||
if (Boolean.TRUE.equals(enableProxy)) { |
||||
port = Integer.parseInt(config.get(FeiShuParamsConstants.NAME_FEI_SHU_PORT)); |
||||
proxy = config.get(FeiShuParamsConstants.NAME_FEI_SHU_PROXY); |
||||
user = config.get(FeiShuParamsConstants.NAME_FEI_SHU_USER); |
||||
password = config.get(FeiShuParamsConstants.NAME_FEI_SHU_PASSWORD); |
||||
} |
||||
|
||||
} |
||||
|
||||
private static RequestConfig getProxyConfig(String proxy, int port) { |
||||
HttpHost httpProxy = new HttpHost(proxy, port); |
||||
return RequestConfig.custom().setProxy(httpProxy).build(); |
||||
} |
||||
|
||||
private static String textToJsonString(AlertData alertData) { |
||||
|
||||
Map<String, Object> items = new HashMap<>(2); |
||||
items.put("msg_type", "text"); |
||||
Map<String, String> textContent = new HashMap<>(); |
||||
byte[] byt = StringUtils.getBytesUtf8(formatContent(alertData)); |
||||
String txt = StringUtils.newStringUtf8(byt); |
||||
textContent.put("text", txt); |
||||
items.put("content", textContent); |
||||
return JSONUtils.toJsonString(items); |
||||
} |
||||
|
||||
private static AlertResult checkSendFeiShuSendMsgResult(String result) { |
||||
AlertResult alertResult = new AlertResult(); |
||||
alertResult.setStatus("false"); |
||||
|
||||
if (org.apache.dolphinscheduler.spi.utils.StringUtils.isBlank(result)) { |
||||
alertResult.setMessage("send fei shu msg error"); |
||||
logger.info("send fei shu msg error,fei shu server resp is null"); |
||||
return alertResult; |
||||
} |
||||
FeiShuSendMsgResponse sendMsgResponse = JSONUtils.parseObject(result, FeiShuSendMsgResponse.class); |
||||
|
||||
if (null == sendMsgResponse) { |
||||
alertResult.setMessage("send fei shu msg fail"); |
||||
logger.info("send fei shu msg error,resp error"); |
||||
return alertResult; |
||||
} |
||||
if (sendMsgResponse.statusCode == 0) { |
||||
alertResult.setStatus("true"); |
||||
alertResult.setMessage("send fei shu msg success"); |
||||
return alertResult; |
||||
} |
||||
alertResult.setMessage(String.format("alert send fei shu msg error : %s", sendMsgResponse.getStatusMessage())); |
||||
logger.info("alert send fei shu msg error : {} ,Extra : {} ", sendMsgResponse.getStatusMessage(), sendMsgResponse.getExtra()); |
||||
return alertResult; |
||||
} |
||||
|
||||
public static String formatContent(AlertData alertData) { |
||||
if (alertData.getContent() != null) { |
||||
List<Map> list; |
||||
try { |
||||
list = JSONUtils.toList(alertData.getContent(), Map.class); |
||||
} catch (Exception e) { |
||||
logger.error("json format exception", e); |
||||
return null; |
||||
} |
||||
|
||||
StringBuilder contents = new StringBuilder(100); |
||||
contents.append(String.format("`%s`%n", alertData.getTitle())); |
||||
for (Map map : list) { |
||||
Iterator<Entry<String, Object>> entries = map.entrySet().iterator(); |
||||
while (entries.hasNext()) { |
||||
Entry<String, Object> entry = entries.next(); |
||||
String key = entry.getKey(); |
||||
String value = entry.getValue().toString(); |
||||
contents.append(key + ":" + value); |
||||
contents.append("\n"); |
||||
} |
||||
} |
||||
return contents.toString(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
public AlertResult sendFeiShuMsg(AlertData alertData) { |
||||
AlertResult alertResult; |
||||
try { |
||||
String resp = sendMsg(alertData); |
||||
return checkSendFeiShuSendMsgResult(resp); |
||||
} catch (Exception e) { |
||||
logger.info("send fei shu alert msg exception : {}", e.getMessage()); |
||||
alertResult = new AlertResult(); |
||||
alertResult.setStatus("false"); |
||||
alertResult.setMessage("send fei shu alert fail."); |
||||
} |
||||
return alertResult; |
||||
} |
||||
|
||||
private String sendMsg(AlertData alertData) throws IOException { |
||||
|
||||
String msgToJson = textToJsonString(alertData); |
||||
|
||||
HttpPost httpPost = HttpRequestUtil.constructHttpPost(url, msgToJson); |
||||
|
||||
CloseableHttpClient httpClient; |
||||
|
||||
httpClient = HttpRequestUtil.getHttpClient(enableProxy, proxy, port, user, password); |
||||
|
||||
try { |
||||
CloseableHttpResponse response = httpClient.execute(httpPost); |
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode(); |
||||
if (statusCode != HttpStatus.SC_OK) { |
||||
logger.error("send feishu message error, return http status code: " + statusCode); |
||||
} |
||||
String resp; |
||||
try { |
||||
HttpEntity entity = response.getEntity(); |
||||
resp = EntityUtils.toString(entity, "utf-8"); |
||||
EntityUtils.consume(entity); |
||||
} finally { |
||||
response.close(); |
||||
} |
||||
logger.info("Ding Talk send title :{} ,content :{}, resp: {}", alertData.getTitle(), alertData.getContent(), resp); |
||||
return resp; |
||||
} finally { |
||||
httpClient.close(); |
||||
} |
||||
} |
||||
|
||||
public static class FeiShuSendMsgResponse { |
||||
@JsonProperty("Extra") |
||||
private String extra; |
||||
@JsonProperty("StatusCode") |
||||
private Integer statusCode; |
||||
@JsonProperty("StatusMessage") |
||||
private String statusMessage; |
||||
|
||||
public String getExtra() { |
||||
return extra; |
||||
} |
||||
|
||||
public void setExtra(String extra) { |
||||
this.extra = extra; |
||||
} |
||||
|
||||
public Integer getStatusCode() { |
||||
return statusCode; |
||||
} |
||||
|
||||
public void setStatusCode(Integer statusCode) { |
||||
this.statusCode = statusCode; |
||||
} |
||||
|
||||
public String getStatusMessage() { |
||||
return statusMessage; |
||||
} |
||||
|
||||
public void setStatusMessage(String statusMessage) { |
||||
this.statusMessage = statusMessage; |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,50 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.plugin.alert.feishu; |
||||
|
||||
import org.apache.http.HttpHost; |
||||
import org.apache.http.auth.AuthScope; |
||||
import org.apache.http.auth.UsernamePasswordCredentials; |
||||
import org.apache.http.client.CredentialsProvider; |
||||
import org.apache.http.client.methods.HttpPost; |
||||
import org.apache.http.entity.ContentType; |
||||
import org.apache.http.entity.StringEntity; |
||||
import org.apache.http.impl.client.BasicCredentialsProvider; |
||||
import org.apache.http.impl.client.CloseableHttpClient; |
||||
import org.apache.http.impl.client.HttpClients; |
||||
|
||||
public class HttpRequestUtil { |
||||
|
||||
public static CloseableHttpClient getHttpClient(boolean enableProxy, String proxy, Integer port, String user, String password) { |
||||
if (enableProxy) { |
||||
HttpHost httpProxy = new HttpHost(proxy, port); |
||||
CredentialsProvider provider = new BasicCredentialsProvider(); |
||||
provider.setCredentials(new AuthScope(httpProxy), new UsernamePasswordCredentials(user, password)); |
||||
return HttpClients.custom().setDefaultCredentialsProvider(provider).build(); |
||||
} else { |
||||
return HttpClients.createDefault(); |
||||
} |
||||
} |
||||
|
||||
public static HttpPost constructHttpPost(String url, String msg) { |
||||
HttpPost post = new HttpPost(url); |
||||
StringEntity entity = new StringEntity(msg, ContentType.APPLICATION_JSON); |
||||
post.setEntity(entity); |
||||
return post; |
||||
} |
||||
} |
@ -0,0 +1,45 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.plugin.alert.feishu; |
||||
|
||||
import org.apache.dolphinscheduler.spi.alert.AlertChannel; |
||||
import org.apache.dolphinscheduler.spi.params.base.PluginParams; |
||||
import org.apache.dolphinscheduler.spi.utils.JSONUtils; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
|
||||
public class FeiShuAlertChannelFactoryTest { |
||||
|
||||
@Test |
||||
public void testGetParams() { |
||||
FeiShuAlertChannelFactory feiShuAlertChannelFactory = new FeiShuAlertChannelFactory(); |
||||
List<PluginParams> params = feiShuAlertChannelFactory.getParams(); |
||||
JSONUtils.toJsonString(params); |
||||
Assert.assertEquals(6, params.size()); |
||||
} |
||||
|
||||
@Test |
||||
public void testCreate() { |
||||
FeiShuAlertChannelFactory feiShuAlertChannelFactory = new FeiShuAlertChannelFactory(); |
||||
AlertChannel alertChannel = feiShuAlertChannelFactory.create(); |
||||
Assert.assertNotNull(alertChannel); |
||||
} |
||||
} |
@ -0,0 +1,75 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.plugin.alert.feishu; |
||||
|
||||
import org.apache.dolphinscheduler.spi.alert.AlertData; |
||||
import org.apache.dolphinscheduler.spi.alert.AlertResult; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
|
||||
public class FeiShuSenderTest { |
||||
|
||||
|
||||
private static Map<String, String> feiShuConfig = new HashMap<>(); |
||||
|
||||
@Before |
||||
public void initFeiShuConfig() { |
||||
feiShuConfig.put(FeiShuParamsConstants.WEB_HOOK, "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxx"); |
||||
} |
||||
|
||||
@Test |
||||
public void testSend() { |
||||
AlertData alertData = new AlertData(); |
||||
alertData.setTitle("feishu test title"); |
||||
alertData.setContent("feishu test content"); |
||||
FeiShuSender feiShuSender = new FeiShuSender(feiShuConfig); |
||||
AlertResult alertResult = feiShuSender.sendFeiShuMsg(alertData); |
||||
Assert.assertEquals("false", alertResult.getStatus()); |
||||
} |
||||
|
||||
@Test |
||||
public void testFormatContent() { |
||||
String alertMsg = "[\n" |
||||
+ " {\n" |
||||
+ " \"owner\": \"dolphinscheduler\",\n" |
||||
+ " \"processEndTime\": \"2021-01-29 19:01:11\",\n" |
||||
+ " \"processHost\": \"10.81.129.4:5678\",\n" |
||||
+ " \"processId\": 2926,\n" |
||||
+ " \"processName\": \"3-20210129190038108\",\n" |
||||
+ " \"processStartTime\": \"2021-01-29 19:00:38\",\n" |
||||
+ " \"processState\": \"SUCCESS\",\n" |
||||
+ " \"processType\": \"START_PROCESS\",\n" |
||||
+ " \"projectId\": 2,\n" |
||||
+ " \"projectName\": \"testdelproject\",\n" |
||||
+ " \"recovery\": \"NO\",\n" |
||||
+ " \"retryTimes\": 0,\n" |
||||
+ " \"runTimes\": 1,\n" |
||||
+ " \"taskId\": 0\n" |
||||
+ " }\n" |
||||
+ "]"; |
||||
AlertData alertData = new AlertData(); |
||||
alertData.setTitle(""); |
||||
alertData.setContent(alertMsg); |
||||
Assert.assertNotNull(FeiShuSender.formatContent(alertData)); |
||||
} |
||||
} |
Loading…
Reference in new issue