|
| 1 | +import constants from "../../common/constants.mjs"; |
| 2 | +import app from "../../uptimerobot.app.mjs"; |
| 3 | +import utils from "../../common/utils.mjs"; |
| 4 | + |
| 5 | +export default { |
| 6 | + key: "uptimerobot-create-monitor", |
| 7 | + name: "Create Monitor", |
| 8 | + description: "Create a new monitor. [See the documentation](https://uptimerobot.com/api/).", |
| 9 | + version: "0.0.1", |
| 10 | + type: "action", |
| 11 | + props: { |
| 12 | + app, |
| 13 | + type: { |
| 14 | + type: "string", |
| 15 | + label: "Monitor Type", |
| 16 | + description: "The type of the monitor.", |
| 17 | + reloadProps: true, |
| 18 | + default: constants.MONITOR_TYPE.PING.value, |
| 19 | + options: Object.values(constants.MONITOR_TYPE), |
| 20 | + }, |
| 21 | + friendlyName: { |
| 22 | + description: "A friendly name for the monitor.", |
| 23 | + propDefinition: [ |
| 24 | + app, |
| 25 | + "friendlyName", |
| 26 | + ], |
| 27 | + }, |
| 28 | + url: { |
| 29 | + type: "string", |
| 30 | + label: "URL, IP Or Host", |
| 31 | + description: "The URL, IP address or Host to monitor.", |
| 32 | + }, |
| 33 | + interval: { |
| 34 | + type: "string", |
| 35 | + label: "Monitor Interval", |
| 36 | + description: "The interval for the monitoring check in seconds. It is recommended to use at least 1-minute checks [available in paid plans](https://app.uptimerobot.com/billing/pricing).", |
| 37 | + default: String(5 * 60), |
| 38 | + options: [ |
| 39 | + { |
| 40 | + label: "5 Minutes", |
| 41 | + value: String(5 * 60), |
| 42 | + }, |
| 43 | + { |
| 44 | + label: "30 Minutes", |
| 45 | + value: String(30 * 60), |
| 46 | + }, |
| 47 | + { |
| 48 | + label: "1 Hour", |
| 49 | + value: String(60 * 60), |
| 50 | + }, |
| 51 | + { |
| 52 | + label: "12 Hours", |
| 53 | + value: String(12 * 60 * 60), |
| 54 | + }, |
| 55 | + { |
| 56 | + label: "1 Day", |
| 57 | + value: String(24 * 60 * 60), |
| 58 | + }, |
| 59 | + ], |
| 60 | + }, |
| 61 | + alertContacts: { |
| 62 | + type: "string[]", |
| 63 | + label: "Alert Contacts", |
| 64 | + propDefinition: [ |
| 65 | + app, |
| 66 | + "alertContact", |
| 67 | + ], |
| 68 | + }, |
| 69 | + }, |
| 70 | + additionalProps() { |
| 71 | + const { |
| 72 | + type: monitorType, |
| 73 | + subType, |
| 74 | + } = this; |
| 75 | + |
| 76 | + const timeout = { |
| 77 | + type: "string", |
| 78 | + label: "Request Timeout", |
| 79 | + description: "The request timeout. The shorter the timeout the earlier we mark website as down.", |
| 80 | + default: "30", |
| 81 | + options: [ |
| 82 | + { |
| 83 | + label: "1 Second", |
| 84 | + value: "1", |
| 85 | + }, |
| 86 | + { |
| 87 | + label: "15 Seconds", |
| 88 | + value: "15", |
| 89 | + }, |
| 90 | + { |
| 91 | + label: "30 Seconds", |
| 92 | + value: "30", |
| 93 | + }, |
| 94 | + { |
| 95 | + label: "45 Seconds", |
| 96 | + value: "45", |
| 97 | + }, |
| 98 | + { |
| 99 | + label: "1 Minute", |
| 100 | + value: "60", |
| 101 | + }, |
| 102 | + ], |
| 103 | + }; |
| 104 | + |
| 105 | + const authProps = { |
| 106 | + httpAuthType: { |
| 107 | + type: "string", |
| 108 | + label: "HTTP Auth Type", |
| 109 | + description: "The HTTP auth type for the monitor.", |
| 110 | + optional: true, |
| 111 | + options: [ |
| 112 | + { |
| 113 | + label: "HTTP Basic", |
| 114 | + value: "1", |
| 115 | + }, |
| 116 | + { |
| 117 | + label: "Digest", |
| 118 | + value: "2", |
| 119 | + }, |
| 120 | + ], |
| 121 | + }, |
| 122 | + httpUsername: { |
| 123 | + type: "string", |
| 124 | + label: "HTTP Username", |
| 125 | + description: "The HTTP username for the monitor.", |
| 126 | + optional: true, |
| 127 | + }, |
| 128 | + httpPassword: { |
| 129 | + type: "string", |
| 130 | + label: "HTTP Password", |
| 131 | + description: "The HTTP password for the monitor.", |
| 132 | + optional: true, |
| 133 | + }, |
| 134 | + }; |
| 135 | + |
| 136 | + if (monitorType === constants.MONITOR_TYPE.PORT.value) { |
| 137 | + return { |
| 138 | + timeout, |
| 139 | + subType: { |
| 140 | + type: "string", |
| 141 | + label: "Port Type", |
| 142 | + description: "The type of the port.", |
| 143 | + options: Object.values(constants.PORT_TYPE), |
| 144 | + default: constants.PORT_TYPE.HTTP.value, |
| 145 | + reloadProps: true, |
| 146 | + }, |
| 147 | + ...(subType === constants.PORT_TYPE.CUSTOM.value && { |
| 148 | + port: { |
| 149 | + type: "string", |
| 150 | + label: "Port", |
| 151 | + description: "The port number to monitor.", |
| 152 | + }, |
| 153 | + }), |
| 154 | + }; |
| 155 | + } |
| 156 | + |
| 157 | + if (monitorType === constants.MONITOR_TYPE.KEYWORD.value) { |
| 158 | + return { |
| 159 | + keywordValue: { |
| 160 | + type: "string", |
| 161 | + label: "Keyword Value", |
| 162 | + description: "The keyword must be present in the response HTML source. You can use HTML markup, too. Eg. `apple` or `<span id=\"availability\">Out of stock</span>`.", |
| 163 | + }, |
| 164 | + keywordType: { |
| 165 | + type: "string", |
| 166 | + label: "Keyword Type", |
| 167 | + description: "The keyword type of the monitor.", |
| 168 | + default: "1", |
| 169 | + options: [ |
| 170 | + { |
| 171 | + label: "Start incident when keyword exists", |
| 172 | + value: "1", |
| 173 | + }, |
| 174 | + { |
| 175 | + label: "Start incident when keyword does not exist", |
| 176 | + value: "2", |
| 177 | + }, |
| 178 | + ], |
| 179 | + }, |
| 180 | + keywordCaseType: { |
| 181 | + type: "string", |
| 182 | + label: "Keyword Case Type", |
| 183 | + description: "The keyword case type of the monitor.", |
| 184 | + default: "1", |
| 185 | + options: [ |
| 186 | + { |
| 187 | + label: "Case Sensitive", |
| 188 | + value: "0", |
| 189 | + }, |
| 190 | + { |
| 191 | + label: "Case Insensitive", |
| 192 | + value: "1", |
| 193 | + }, |
| 194 | + ], |
| 195 | + }, |
| 196 | + ...authProps, |
| 197 | + }; |
| 198 | + } |
| 199 | + |
| 200 | + if (monitorType === constants.MONITOR_TYPE.HTTPS.value) { |
| 201 | + return { |
| 202 | + timeout, |
| 203 | + ...authProps, |
| 204 | + }; |
| 205 | + } |
| 206 | + |
| 207 | + return {}; |
| 208 | + }, |
| 209 | + methods: { |
| 210 | + formatAlertContacts(alertContacts, useDefaultThresholdAndRecurrence = true) { |
| 211 | + const threshold = 0; |
| 212 | + const recurrence = 0; |
| 213 | + return utils.parseArray(alertContacts) |
| 214 | + ?.map((value) => useDefaultThresholdAndRecurrence |
| 215 | + ? `${value}_${threshold}_${recurrence}` |
| 216 | + : value) |
| 217 | + .join("-"); |
| 218 | + }, |
| 219 | + createMonitor(args = {}) { |
| 220 | + return this.app.post({ |
| 221 | + path: "/newMonitor", |
| 222 | + ...args, |
| 223 | + }); |
| 224 | + }, |
| 225 | + }, |
| 226 | + async run({ $ }) { |
| 227 | + const { |
| 228 | + createMonitor, |
| 229 | + formatAlertContacts, |
| 230 | + type, |
| 231 | + friendlyName, |
| 232 | + url, |
| 233 | + interval, |
| 234 | + alertContacts, |
| 235 | + timeout, |
| 236 | + subType, |
| 237 | + port, |
| 238 | + keywordType, |
| 239 | + keywordValue, |
| 240 | + keywordCaseType, |
| 241 | + httpUsername, |
| 242 | + httpPassword, |
| 243 | + httpAuthType, |
| 244 | + } = this; |
| 245 | + |
| 246 | + const response = await createMonitor({ |
| 247 | + $, |
| 248 | + data: { |
| 249 | + friendly_name: friendlyName, |
| 250 | + url, |
| 251 | + type, |
| 252 | + interval, |
| 253 | + alert_contacts: formatAlertContacts(alertContacts), |
| 254 | + timeout, |
| 255 | + sub_type: subType, |
| 256 | + port, |
| 257 | + keyword_type: keywordType, |
| 258 | + keyword_value: keywordValue, |
| 259 | + keyword_case_type: keywordCaseType, |
| 260 | + http_username: httpUsername, |
| 261 | + http_password: httpPassword, |
| 262 | + http_auth_type: httpAuthType, |
| 263 | + }, |
| 264 | + }); |
| 265 | + |
| 266 | + $.export("$summary", `Successfully created monitor with ID \`${response.monitor.id}\`.`); |
| 267 | + return response; |
| 268 | + }, |
| 269 | +}; |
0 commit comments