This repository was archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathCheckMkTask.java
More file actions
executable file
·144 lines (120 loc) · 5.88 KB
/
Copy pathCheckMkTask.java
File metadata and controls
executable file
·144 lines (120 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.tw.go.task.check_mk;
import com.google.gson.GsonBuilder;
import com.thoughtworks.go.plugin.api.GoPluginIdentifier;
import com.thoughtworks.go.plugin.api.annotation.Extension;
import com.thoughtworks.go.plugin.api.logging.Logger;
import com.thoughtworks.go.plugin.api.request.GoPluginApiRequest;
import com.thoughtworks.go.plugin.api.response.DefaultGoApiResponse;
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import com.tw.go.plugin.common.*;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@Extension
public class CheckMkTask extends BaseGoPlugin {
public static final String CHECK_MK_SERVER = "Server";
public static final String HOSTNAME = "Hostname";
public static final String HOST_IP = "HostIp";
public static final String FOLDER_PATH = "FolderPath";
public static final String USERNAME = "Username";
public static final String PASSWORD = "Password";
public static final String ACTION = "SelectedAction";
private Logger logger = Logger.getLoggerFor(CheckMkTask.class);
@Override
protected GoPluginApiResponse handleTaskView(GoPluginApiRequest request) {
int responseCode = DefaultGoApiResponse.SUCCESS_RESPONSE_CODE;
HashMap view = new HashMap();
view.put("displayValue", "Monitoring - Check MK");
try {
String checkMkTemplate = IOUtils.toString(getClass().getResourceAsStream("/views/checkMk.template.html"), "UTF-8");
view.put("template", checkMkTemplate);
} catch (Exception e) {
responseCode = DefaultGoApiResponse.INTERNAL_ERROR;
String errorMessage = "Failed to find template: " + e.getMessage();
view.put("exception", errorMessage);
logger.error(errorMessage, e);
}
return createResponse(responseCode, view);
}
@Override
protected GoPluginApiResponse handleTaskExecution(GoPluginApiRequest request) throws Exception {
Map executionRequest = (Map) new GsonBuilder().create().fromJson(request.requestBody(), Object.class);
Map<String, Map> config = (Map) executionRequest.get("config");
Context context = new Context((Map) executionRequest.get("context"));
try {
CheckMkTaskExecutor checkMkTaskExecutor = TaskExecutorFactory.Create(MaskingJobConsoleLogger.getConsoleLogger(),
context, config);
Result result = checkMkTaskExecutor.execute();
return createResponse(result.responseCode(), result.toMap());
} catch (JobNotSupportedException | IOException e) {
return success(new Result(false, e.getMessage()));
}
}
@Override
protected GoPluginApiResponse handleValidation(GoPluginApiRequest request) {
HashMap validationResult = new HashMap();
int responseCode = DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE;
Map configMap = (Map) new GsonBuilder().create().fromJson(request.requestBody(), Object.class);
HashMap errorMap = new HashMap();
/* if (!configMap.containsKey(URL_PROPERTY) || ((Map) configMap.get(URL_PROPERTY)).get("value") == null || ((String) ((Map) configMap.get(URL_PROPERTY)).get("value")).trim().isEmpty()) {
errorMap.put(URL_PROPERTY, "URL cannot be empty");
}
validationResult.put("errors", errorMap);
*/
return createResponse(responseCode, validationResult);
}
// return json description to tell go.cd which config properties need to be stored
// sample and schema can be found at http://www.go.cd/documentation/developer/writing_go_plugins/task/version_1_0/configuration.html
@Override
protected GoPluginApiResponse handleGetConfigRequest(GoPluginApiRequest request) {
HashMap config = new HashMap();
HashMap server = new HashMap();
server.put("display-order", "0");
server.put("display-name", "Server");
server.put("required", true);
config.put(CHECK_MK_SERVER, server);
HashMap username = new HashMap();
username.put("display-order", "1");
username.put("display-name", "Username");
username.put("required", true);
config.put(USERNAME, username);
HashMap password = new HashMap();
password.put("display-order", "2");
password.put("display-name", "Password");
password.put("required", true);
config.put(PASSWORD, password);
HashMap action = new HashMap();
action.put("display-order", "3");
action.put("display-name", "Selected Action");
action.put("required", true);
config.put(ACTION, action);
HashMap hostname = new HashMap();
hostname.put("display-order", "4");
hostname.put("display-name", "Host name");
hostname.put("required", false);
config.put(HOSTNAME, hostname);
HashMap hostIp = new HashMap();
hostIp.put("display-order", "5");
hostIp.put("display-name", "Host Ip");
hostIp.put("required", false);
config.put(HOST_IP, hostIp);
HashMap folderPath = new HashMap();
folderPath.put("display-order", "6");
folderPath.put("display-name", "Folder path");
folderPath.put("required", false);
config.put(FOLDER_PATH, folderPath);
return createResponse(DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE, config);
}
private GoPluginApiResponse createResponse(int responseCode, Map body) {
final DefaultGoPluginApiResponse response = new DefaultGoPluginApiResponse(responseCode);
response.setResponseBody(new GsonBuilder().serializeNulls().create().toJson(body));
return response;
}
@Override
public GoPluginIdentifier pluginIdentifier() {
return new GoPluginIdentifier("task", Arrays.asList("1.0"));
}
}