|
| 1 | +/** |
| 2 | + * Copyright 2022 IBM All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +const AlertsV1 = require('../../../dist/cis/alertsv1/v1'); |
| 19 | +const WebhooksV1 = require('../../../dist/cis/webhooksv1/v1'); |
| 20 | +const { IamAuthenticator } = require('ibm-cloud-sdk-core'); |
| 21 | +const authHelper = require('../../resources/auth-helper.js'); |
| 22 | + |
| 23 | +const timeout = 120000; // two minutes |
| 24 | + |
| 25 | +// Location of our config file. |
| 26 | +const configFile = 'cis.env'; |
| 27 | + |
| 28 | +// Use authHelper to skip tests if our configFile is not available. |
| 29 | +const describe = authHelper.prepareTests(configFile); |
| 30 | + |
| 31 | +// Retrieve the config file as an object. |
| 32 | +// We do this because we're going to directly use the |
| 33 | +// config properties, rather than let the SDK do it for us. |
| 34 | +const config = authHelper.loadConfig(); |
| 35 | + |
| 36 | +describe('AlertsV1', () => { |
| 37 | + jest.setTimeout(timeout); |
| 38 | + |
| 39 | + // Initialize the service client. |
| 40 | + const options = { |
| 41 | + authenticator: new IamAuthenticator({ |
| 42 | + apikey: config.CIS_SERVICES_APIKEY, |
| 43 | + url: config.CIS_SERVICES_AUTH_URL, |
| 44 | + }), |
| 45 | + crn: config.CIS_SERVICES_CRN, |
| 46 | + serviceUrl: config.CIS_SERVICES_URL, |
| 47 | + version: config.CIS_SERVICES_API_VERSION, |
| 48 | + zoneIdentifier: config.CIS_SERVICES_ZONE_ID, |
| 49 | + }; |
| 50 | + |
| 51 | + let webhooksV1; |
| 52 | + let webhooks; |
| 53 | + let webhookIdentifier; |
| 54 | + let alertsV1; |
| 55 | + let alerts; |
| 56 | + let alertIdentifier; |
| 57 | + |
| 58 | + test('should successfully complete initialization', done => { |
| 59 | + // Initialize the service client. |
| 60 | + alertsV1 = AlertsV1.newInstance(options); |
| 61 | + expect(webhooksV1).not.toBeNull(); |
| 62 | + webhooksV1 = WebhooksV1.newInstance(options); |
| 63 | + expect(alertsV1).not.toBeNull(); |
| 64 | + done(); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('clear all Alert-policies', () => { |
| 68 | + test('should successfully clear all Alert-policies', async done => { |
| 69 | + try { |
| 70 | + const response = await alertsV1.getAlertPolicies(); |
| 71 | + expect(response).toBeDefined(); |
| 72 | + expect(response.status).toEqual(200); |
| 73 | + |
| 74 | + const { result } = response || {}; |
| 75 | + expect(result).toBeDefined(); |
| 76 | + expect(result.result).toBeDefined(); |
| 77 | + if (result.result == null) { |
| 78 | + return done(); |
| 79 | + } |
| 80 | + |
| 81 | + for (let i = 0; i < result.result.length; i++) { |
| 82 | + const alertsId = result.result[i].id; |
| 83 | + const params1 = { |
| 84 | + policyId: alertsId, |
| 85 | + }; |
| 86 | + const response1 = alertsV1.deleteAlertPolicy(params1); |
| 87 | + expect(response1).toBeDefined(); |
| 88 | + // expect(response1.status).toEqual(200); |
| 89 | + // const { result1 } = response1 || {}; |
| 90 | + // expect(result1).toBeDefined(); |
| 91 | + } |
| 92 | + if (result && result.result) { |
| 93 | + expect(result.result.length).toBeGreaterThanOrEqual(0); |
| 94 | + expect(result.result).toBeDefined(); |
| 95 | + } |
| 96 | + done(); |
| 97 | + } catch (err) { |
| 98 | + done(err); |
| 99 | + } |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('Create the webhooks', () => { |
| 104 | + test('should successfully created webhooks', async done => { |
| 105 | + try { |
| 106 | + const name = 'My Slack Alert Webhook'; |
| 107 | + const url = 'https://hooks.slack.com/services/Ds3fdBFbV/456464Gdd'; |
| 108 | + const secret = options.authenticator.apikey; |
| 109 | + const params = { |
| 110 | + name: name, |
| 111 | + url: url, |
| 112 | + secret: secret, |
| 113 | + }; |
| 114 | + const response = await webhooksV1.createAlertWebhook(params); |
| 115 | + expect(response).toBeDefined(); |
| 116 | + expect(response.status).toEqual(201); |
| 117 | + |
| 118 | + const { result } = response || {}; |
| 119 | + |
| 120 | + expect(result).toBeDefined(); |
| 121 | + expect(result.result).toBeDefined(); |
| 122 | + |
| 123 | + webhookIdentifier = result.result.id; |
| 124 | + expect(webhookIdentifier).toBeDefined(); |
| 125 | + |
| 126 | + if (result && result.result) { |
| 127 | + webhooks = result.result; |
| 128 | + expect(webhooks).toBeDefined(); |
| 129 | + expect(result.success).toBeTruthy(); |
| 130 | + expect(webhooks.id).toBeDefined(); |
| 131 | + } |
| 132 | + done(); |
| 133 | + } catch (err) { |
| 134 | + done(err); |
| 135 | + } |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + describe('Create the Alert-policies', () => { |
| 140 | + test('should successfully create Alert-policies', async done => { |
| 141 | + try { |
| 142 | + const emailItemModel = { |
| 143 | + |
| 144 | + }; |
| 145 | + |
| 146 | + const webhooksItemModel = { |
| 147 | + id: webhookIdentifier, |
| 148 | + }; |
| 149 | + |
| 150 | + const mechanismsModel = { |
| 151 | + email: [emailItemModel], |
| 152 | + webhooks: [webhooksItemModel], |
| 153 | + }; |
| 154 | + const name = 'My Alert Policy'; |
| 155 | + const enabled = true; |
| 156 | + const alertType = 'g6_pool_toggle_alert'; |
| 157 | + const mechanisms = mechanismsModel; |
| 158 | + const description = 'A description for my alert policy'; |
| 159 | + const filters = { |
| 160 | + enabled: ['false', 'true'], |
| 161 | + pool_id: ['6e67c08e3bae7eb398101d08def8a68a', 'df2d9d70fcb194ea60d2e58397cb35a6'], |
| 162 | + }; |
| 163 | + const params = { |
| 164 | + name: name, |
| 165 | + enabled: enabled, |
| 166 | + alertType: alertType, |
| 167 | + mechanisms: mechanisms, |
| 168 | + description: description, |
| 169 | + filters: filters, |
| 170 | + }; |
| 171 | + |
| 172 | + const response = await alertsV1.createAlertPolicy(params); |
| 173 | + expect(response).toBeDefined(); |
| 174 | + expect(response.status).toEqual(201); |
| 175 | + |
| 176 | + const { result } = response || {}; |
| 177 | + |
| 178 | + expect(result).toBeDefined(); |
| 179 | + expect(result.result).toBeDefined(); |
| 180 | + |
| 181 | + alertIdentifier = result.result.id; |
| 182 | + expect(alertIdentifier).toBeDefined(); |
| 183 | + |
| 184 | + if (result && result.result) { |
| 185 | + alerts = result.result; |
| 186 | + expect(alerts).toBeDefined(); |
| 187 | + expect(result.success).toBeTruthy(); |
| 188 | + expect(alerts.id).toBeDefined(); |
| 189 | + } |
| 190 | + done(); |
| 191 | + } catch (err) { |
| 192 | + done(err); |
| 193 | + } |
| 194 | + }); |
| 195 | + }); |
| 196 | + |
| 197 | + describe('List the Alert-policies', () => { |
| 198 | + test('should successfully List Alert-policies', async done => { |
| 199 | + try { |
| 200 | + const response = await alertsV1.getAlertPolicies(); |
| 201 | + expect(response).toBeDefined(); |
| 202 | + expect(response.status).toEqual(200); |
| 203 | + |
| 204 | + const { result } = response || {}; |
| 205 | + |
| 206 | + expect(result).toBeDefined(); |
| 207 | + |
| 208 | + if (result && result.result) { |
| 209 | + expect(result.result.length).toBeGreaterThanOrEqual(1); |
| 210 | + expect(result.result).toBeDefined(); |
| 211 | + } |
| 212 | + done(); |
| 213 | + } catch (err) { |
| 214 | + done(err); |
| 215 | + } |
| 216 | + }); |
| 217 | + }); |
| 218 | + |
| 219 | + describe('Update the Alert-policies', () => { |
| 220 | + test('should successfully Updated Alert-policies', async done => { |
| 221 | + try { |
| 222 | + const emailItemModel = { |
| 223 | + |
| 224 | + }; |
| 225 | + |
| 226 | + const webhooksItemModel = { |
| 227 | + id: webhookIdentifier, |
| 228 | + }; |
| 229 | + |
| 230 | + const mechanismsModel = { |
| 231 | + email: [emailItemModel], |
| 232 | + webhooks: [webhooksItemModel], |
| 233 | + }; |
| 234 | + const policyId = alertIdentifier; |
| 235 | + const name = 'My Alert Policy'; |
| 236 | + const enabled = true; |
| 237 | + const alertType = 'g6_pool_toggle_alert'; |
| 238 | + const mechanisms = mechanismsModel; |
| 239 | + const description = 'A description for my alert policy'; |
| 240 | + const filters = { |
| 241 | + enabled: ['true', 'false'], |
| 242 | + pool_id: ['6e67c08e3bae7eb398101d08def8a68a', 'df2d9d70fcb194ea60d2e58397cb35a6'], |
| 243 | + }; |
| 244 | + const conditions = { |
| 245 | + conditions: { |
| 246 | + and: [ |
| 247 | + { |
| 248 | + or: [ |
| 249 | + { |
| 250 | + '==': [ |
| 251 | + { |
| 252 | + var: 'pool_id', |
| 253 | + }, |
| 254 | + '6e67c08e3bae7eb398101d08def8a68a', |
| 255 | + ], |
| 256 | + }, |
| 257 | + { |
| 258 | + '==': [ |
| 259 | + { |
| 260 | + var: 'pool_id', |
| 261 | + }, |
| 262 | + 'df2d9d70fcb194ea60d2e58397cb35a6', |
| 263 | + ], |
| 264 | + }, |
| 265 | + ], |
| 266 | + }, |
| 267 | + { |
| 268 | + or: [ |
| 269 | + { |
| 270 | + '==': [ |
| 271 | + { |
| 272 | + var: 'enabled', |
| 273 | + }, |
| 274 | + 'false', |
| 275 | + ], |
| 276 | + }, |
| 277 | + { |
| 278 | + '==': [ |
| 279 | + { |
| 280 | + var: 'enabled', |
| 281 | + }, |
| 282 | + 'true', |
| 283 | + ], |
| 284 | + }, |
| 285 | + ], |
| 286 | + }, |
| 287 | + ], |
| 288 | + }, |
| 289 | + }; |
| 290 | + const params = { |
| 291 | + policyId: policyId, |
| 292 | + name: name, |
| 293 | + enabled: enabled, |
| 294 | + alertType: alertType, |
| 295 | + mechanisms: mechanisms, |
| 296 | + description: description, |
| 297 | + filters: filters, |
| 298 | + conditions: conditions, |
| 299 | + }; |
| 300 | + |
| 301 | + const response = await alertsV1.updateAlertPolicy(params); |
| 302 | + expect(response).toBeDefined(); |
| 303 | + expect(response.status).toEqual(200); |
| 304 | + |
| 305 | + const { result } = response || {}; |
| 306 | + |
| 307 | + expect(result).toBeDefined(); |
| 308 | + expect(result.result).toBeDefined(); |
| 309 | + alertIdentifier = result.result.id; |
| 310 | + expect(alertIdentifier).toBeDefined(); |
| 311 | + |
| 312 | + if (result && result.result) { |
| 313 | + alerts = result.result; |
| 314 | + expect(alerts).toBeDefined(); |
| 315 | + expect(result.success).toBeTruthy(); |
| 316 | + expect(alerts.id).toBeDefined(); |
| 317 | + } |
| 318 | + done(); |
| 319 | + } catch (err) { |
| 320 | + done(err); |
| 321 | + } |
| 322 | + }); |
| 323 | + }); |
| 324 | + |
| 325 | + describe('get the Alert-policies by ID', () => { |
| 326 | + test('should successfully get Alert-policy', async done => { |
| 327 | + try { |
| 328 | + const params = { |
| 329 | + policyId: alertIdentifier, |
| 330 | + }; |
| 331 | + const response = await alertsV1.getAlertPolicy(params); |
| 332 | + expect(response).toBeDefined(); |
| 333 | + expect(response.status).toEqual(200); |
| 334 | + |
| 335 | + const { result } = response || {}; |
| 336 | + |
| 337 | + expect(result).toBeDefined(); |
| 338 | + if (result && result.result) { |
| 339 | + expect(result.result).toBeDefined(); |
| 340 | + } |
| 341 | + done(); |
| 342 | + } catch (err) { |
| 343 | + done(err); |
| 344 | + } |
| 345 | + }); |
| 346 | + }); |
| 347 | + |
| 348 | + describe('delete the Alert-policies by ID', () => { |
| 349 | + test('should successfully delete Alert-policies', async done => { |
| 350 | + try { |
| 351 | + const params = { |
| 352 | + policyId: alertIdentifier, |
| 353 | + }; |
| 354 | + const response = await alertsV1.deleteAlertPolicy(params); |
| 355 | + expect(response).toBeDefined(); |
| 356 | + expect(response.status).toEqual(200); |
| 357 | + |
| 358 | + const { result } = response || {}; |
| 359 | + |
| 360 | + expect(result).toBeDefined(); |
| 361 | + |
| 362 | + if (result && result.result) { |
| 363 | + expect(result.result).toBeDefined(); |
| 364 | + } |
| 365 | + done(); |
| 366 | + } catch (err) { |
| 367 | + done(err); |
| 368 | + } |
| 369 | + }); |
| 370 | + }); |
| 371 | + |
| 372 | + describe('delete the Webhooks by ID', () => { |
| 373 | + test('should successfully delete webhooks', async done => { |
| 374 | + try { |
| 375 | + const params = { |
| 376 | + webhookId: webhookIdentifier, |
| 377 | + }; |
| 378 | + const response = await webhooksV1.deleteAlertWebhook(params); |
| 379 | + expect(response).toBeDefined(); |
| 380 | + expect(response.status).toEqual(200); |
| 381 | + |
| 382 | + const { result } = response || {}; |
| 383 | + |
| 384 | + expect(result).toBeDefined(); |
| 385 | + |
| 386 | + if (result && result.result) { |
| 387 | + expect(result.result).toBeDefined(); |
| 388 | + } |
| 389 | + done(); |
| 390 | + } catch (err) { |
| 391 | + done(err); |
| 392 | + } |
| 393 | + }); |
| 394 | + }); |
| 395 | +}); |
0 commit comments