manage_alert_notifications_controller.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. var validator = require('utils/validator');
  20. App.ManageAlertNotificationsController = Em.Controller.extend({
  21. name: 'manageAlertNotificationsController',
  22. /**
  23. * Are alert notifications loaded
  24. * @type {boolean}
  25. */
  26. isLoaded: false,
  27. /**
  28. * Create/Edit modal popup object
  29. * used to hide popup
  30. * @type {App.ModalPopup}
  31. */
  32. createEditPopup: null,
  33. /**
  34. * Map of edit inputs shown in Create/Edit Notification popup
  35. * @type {Object}
  36. */
  37. inputFields: Em.Object.create({
  38. name: {
  39. label: Em.I18n.t('common.name'),
  40. value: '',
  41. defaultValue: ''
  42. },
  43. groups: {
  44. label: Em.I18n.t('common.groups'),
  45. value: [],
  46. defaultValue: []
  47. },
  48. global: {
  49. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.global'),
  50. value: false,
  51. defaultValue: false,
  52. disabled: false
  53. },
  54. method: {
  55. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.method'),
  56. value: '',
  57. defaultValue: ''
  58. },
  59. email: {
  60. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.email'),
  61. value: '',
  62. defaultValue: ''
  63. },
  64. SMTPServer: {
  65. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPServer'),
  66. value: '',
  67. defaultValue: ''
  68. },
  69. SMTPPort: {
  70. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPPort'),
  71. value: '',
  72. defaultValue: ''
  73. },
  74. emailFrom: {
  75. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.emailFrom'),
  76. value: '',
  77. defaultValue: ''
  78. },
  79. version: {
  80. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.version'),
  81. value: '',
  82. defaultValue: ''
  83. },
  84. OIDs: {
  85. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.OIDs'),
  86. value: '',
  87. defaultValue: ''
  88. },
  89. community: {
  90. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.community'),
  91. value: '',
  92. defaultValue: ''
  93. },
  94. port: {
  95. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.port'),
  96. value: '',
  97. defaultValue: ''
  98. },
  99. severityFilter: {
  100. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.severityFilter'),
  101. value: [],
  102. defaultValue: []
  103. },
  104. description: {
  105. label: Em.I18n.t('common.description'),
  106. value: '',
  107. defaultValue: ''
  108. },
  109. customProperties: Em.A([])
  110. }),
  111. /**
  112. * List of available Notification types
  113. * used in Type combobox
  114. * @type {Array}
  115. */
  116. methods: ['EMAIL', 'SNMP'],
  117. /**
  118. * List of available value for Severity Filter
  119. * used in Severity Filter combobox
  120. * @type {Array}
  121. */
  122. severities: ['OK', 'WARNING', 'CRITICAL', 'UNKNOWN'],
  123. /**
  124. * List of available SNMP versions
  125. * @type {Array}
  126. */
  127. SNMPVersions: ['SNMPv1', 'SNMPv2c', 'SNMPv3'],
  128. /**
  129. * List of all Alert Notifications
  130. * @type {App.AlertNotification[]}
  131. */
  132. alertNotifications: function () {
  133. return this.get('isLoaded') ? App.AlertNotification.find().toArray() : [];
  134. }.property('isLoaded'),
  135. /**
  136. * List of all Alert Groups
  137. * @type {App.AlertGroup[]}
  138. */
  139. allAlertGroups: function () {
  140. return this.get('isLoaded') ? App.AlertGroup.find().toArray() : [];
  141. }.property('isLoaded'),
  142. /**
  143. * Selected Alert Notification
  144. * @type {App.AlertNotification}
  145. */
  146. selectedAlertNotification: null,
  147. /**
  148. * Addable to <code>selectedAlertNotification.properties</code> custom property
  149. * @type {{name: string, value: string}}
  150. */
  151. newCustomProperty: {name: '', value: ''},
  152. /**
  153. * List custom property names that shouldn't be displayed on Edit page
  154. * @type {string[]}
  155. */
  156. ignoredCustomProperties: ['ambari.dispatch.recipients', 'mail.smtp.host', 'mail.smtp.port', 'mail.smtp.from', 'ambari.dispatch.snmp.version',
  157. 'ambari.dispatch.snmp.oids.trap', 'ambari.dispatch.snmp.community', 'ambari.dispatch.snmp.port'],
  158. /**
  159. * Load all Alert Notifications from server
  160. * Don't do anything if controller not isLoaded
  161. * @returns {$.ajax|null}
  162. */
  163. loadAlertNotifications: function () {
  164. this.set('isLoaded', false);
  165. return App.ajax.send({
  166. name: 'alerts.notifications',
  167. sender: this,
  168. success: 'getAlertNotificationsSuccessCallback',
  169. error: 'getAlertNotificationsErrorCallback'
  170. });
  171. },
  172. /**
  173. * Success-callback for load alert notifications request
  174. * @param {object} json
  175. * @method getAlertNotificationsSuccessCallback
  176. */
  177. getAlertNotificationsSuccessCallback: function (json) {
  178. App.alertNotificationMapper.map(json);
  179. this.set('isLoaded', true);
  180. },
  181. /**
  182. * Error-callback for load alert notifications request
  183. * @method getAlertNotificationsErrorCallback
  184. */
  185. getAlertNotificationsErrorCallback: function () {
  186. this.set('isLoaded', true);
  187. },
  188. /**
  189. * Add Notification button handler
  190. * @method addAlertNotification
  191. */
  192. addAlertNotification: function () {
  193. var inputFields = this.get('inputFields');
  194. inputFields.set('global.disabled', false);
  195. Em.keys(inputFields).forEach(function (key) {
  196. inputFields.set(key + '.value', inputFields.get(key + '.defaultValue'));
  197. });
  198. this.showCreateEditPopup(false);
  199. },
  200. /**
  201. * Edit Notification button handler
  202. * @method editAlertNotification
  203. */
  204. editAlertNotification: function () {
  205. this.fillEditCreateInputs();
  206. this.showCreateEditPopup(true);
  207. },
  208. /**
  209. * Fill inputs of Create/Edit popup form
  210. * @param addCopyToName define whether add 'Copy of ' to name
  211. * @method fillEditCreateInputs
  212. */
  213. fillEditCreateInputs: function (addCopyToName) {
  214. var inputFields = this.get('inputFields');
  215. var selectedAlertNotification = this.get('selectedAlertNotification');
  216. inputFields.set('name.value', (addCopyToName ? 'Copy of ' : '') + selectedAlertNotification.get('name'));
  217. inputFields.set('groups.value', selectedAlertNotification.get('groups').toArray());
  218. inputFields.set('email.value', selectedAlertNotification.get('properties')['ambari.dispatch.recipients'] ?
  219. selectedAlertNotification.get('properties')['ambari.dispatch.recipients'].join(', ') : '');
  220. inputFields.set('SMTPServer.value', selectedAlertNotification.get('properties')['mail.smtp.host']);
  221. inputFields.set('SMTPPort.value', selectedAlertNotification.get('properties')['mail.smtp.port']);
  222. inputFields.set('emailFrom.value', selectedAlertNotification.get('properties')['mail.smtp.from']);
  223. inputFields.set('version.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.version']);
  224. inputFields.set('OIDs.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.oids.trap']);
  225. inputFields.set('community.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.community']);
  226. inputFields.set('port.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.port']);
  227. inputFields.set('severityFilter.value', selectedAlertNotification.get('alertStates'));
  228. inputFields.set('global.value', selectedAlertNotification.get('global'));
  229. // not allow to edit global field
  230. inputFields.set('global.disabled', true);
  231. inputFields.set('description.value', selectedAlertNotification.get('description'));
  232. inputFields.set('method.value', selectedAlertNotification.get('type'));
  233. inputFields.get('customProperties').clear();
  234. var properties = selectedAlertNotification.get('properties');
  235. var ignoredCustomProperties = this.get('ignoredCustomProperties');
  236. Em.keys(properties).forEach(function (k) {
  237. if (ignoredCustomProperties.contains(k)) return;
  238. inputFields.get('customProperties').pushObject({
  239. name: k,
  240. value: properties[k],
  241. defaultValue: properties[k]
  242. });
  243. });
  244. },
  245. /**
  246. * Show Edit or Create Notification popup
  247. * @param {boolean} isEdit true - edit, false - create
  248. * @returns {App.ModalPopup}
  249. * @method showCreateEditPopup
  250. */
  251. showCreateEditPopup: function (isEdit) {
  252. var self = this;
  253. var createEditPopup = App.ModalPopup.show({
  254. header: isEdit ? Em.I18n.t('alerts.actions.manage_alert_notifications_popup.editHeader') : Em.I18n.t('alerts.actions.manage_alert_notifications_popup.addHeader'),
  255. classNames: ['create-edit-alert-notification-popup'],
  256. bodyClass: Em.View.extend({
  257. controller: this,
  258. templateName: require('templates/main/alerts/create_alert_notification'),
  259. didInsertElement: function () {
  260. this.nameValidation();
  261. },
  262. isEmailMethodSelected: function () {
  263. return this.get('controller.inputFields.method.value') === 'EMAIL';
  264. }.property('controller.inputFields.method.value'),
  265. nameValidation: function () {
  266. this.set('parentView.hasErrors', !this.get('controller.inputFields.name.value').trim());
  267. }.observes('controller.inputFields.name.value'),
  268. groupsSelectView: Em.Select.extend({
  269. init: function () {
  270. this._super();
  271. this.set('parentView.groupSelect', this);
  272. }
  273. }),
  274. groupSelect: null,
  275. selectAllGroups: function () {
  276. this.set('groupSelect.selection', this.get('groupSelect.content').slice());
  277. },
  278. clearAllGroups: function () {
  279. this.set('groupSelect.selection', []);
  280. },
  281. severitySelectView: Em.Select.extend({
  282. init: function () {
  283. this._super();
  284. this.set('parentView.severitySelect', this);
  285. }
  286. }),
  287. severitySelect: null,
  288. selectAllSeverity: function () {
  289. this.set('severitySelect.selection', this.get('severitySelect.content').slice());
  290. },
  291. clearAllSeverity: function () {
  292. this.set('severitySelect.selection', []);
  293. }
  294. }),
  295. isSaving: false,
  296. hasErrors: false,
  297. primary: Em.I18n.t('common.save'),
  298. disablePrimary: function () {
  299. return this.get('isSaving') || this.get('hasErrors');
  300. }.property('isSaving', 'hasErrors'),
  301. onPrimary: function () {
  302. this.set('isSaving', true);
  303. var apiObject = self.formatNotificationAPIObject();
  304. if (isEdit) {
  305. self.updateAlertNotification(apiObject);
  306. } else {
  307. self.createAlertNotification(apiObject);
  308. }
  309. }
  310. });
  311. this.set('createEditPopup', createEditPopup);
  312. return createEditPopup;
  313. },
  314. /**
  315. * Create API-formatted object from data populate by user
  316. * @returns {Object}
  317. * @method formatNotificationAPIObject
  318. */
  319. formatNotificationAPIObject: function () {
  320. var inputFields = this.get('inputFields');
  321. var properties = {};
  322. var clusterId = App.Cluster.find().objectAt(0).get('id');
  323. if (inputFields.get('method.value') === 'EMAIL') {
  324. properties['ambari.dispatch.recipients'] = inputFields.get('email.value').replace(/\s/g, '').split(',');
  325. properties['mail.smtp.host'] = inputFields.get('SMTPServer.value');
  326. properties['mail.smtp.port'] = inputFields.get('SMTPPort.value');
  327. properties['mail.smtp.from'] = inputFields.get('emailFrom.value');
  328. } else {
  329. properties['ambari.dispatch.snmp.version'] = inputFields.get('version.value');
  330. properties['ambari.dispatch.snmp.oids.trap'] = inputFields.get('OIDs.value');
  331. properties['ambari.dispatch.snmp.community'] = inputFields.get('community.value');
  332. properties['ambari.dispatch.snmp.port'] = inputFields.get('port.value');
  333. }
  334. inputFields.get('customProperties').forEach(function (customProperty) {
  335. properties[customProperty.name] = customProperty.value;
  336. });
  337. return {
  338. AlertTarget: {
  339. name: inputFields.get('name.value'),
  340. description: inputFields.get('description.value'),
  341. global: inputFields.get('global.value'),
  342. groups: inputFields.get('groups.value').map(function (group) {
  343. return {
  344. name: group.get('name'),
  345. id: group.get('id'),
  346. default: group.get('default'),
  347. cluster_id: clusterId
  348. };
  349. }),
  350. notification_type: inputFields.get('method.value'),
  351. alert_states: inputFields.get('severityFilter.value'),
  352. properties: properties
  353. }
  354. };
  355. },
  356. /**
  357. * Send request to server to create Alert Notification
  358. * @param {object} apiObject (@see formatNotificationAPIObject)
  359. * @returns {$.ajax}
  360. * @method createAlertNotification
  361. */
  362. createAlertNotification: function (apiObject) {
  363. return App.ajax.send({
  364. name: 'alerts.create_alert_notification',
  365. sender: this,
  366. data: {
  367. data: apiObject
  368. },
  369. success: 'createAlertNotificationSuccessCallback',
  370. error: 'saveErrorCallback'
  371. });
  372. },
  373. /**
  374. * Success callback for <code>createAlertNotification</code>
  375. * @method createAlertNotificationSuccessCallback
  376. */
  377. createAlertNotificationSuccessCallback: function () {
  378. this.loadAlertNotifications();
  379. var createEditPopup = this.get('createEditPopup');
  380. if (createEditPopup) {
  381. createEditPopup.hide();
  382. }
  383. },
  384. /**
  385. * Send request to server to update Alert Notification
  386. * @param {object} apiObject (@see formatNotificationAPIObject)
  387. * @returns {$.ajax}
  388. * @method updateAlertNotification
  389. */
  390. updateAlertNotification: function (apiObject) {
  391. return App.ajax.send({
  392. name: 'alerts.update_alert_notification',
  393. sender: this,
  394. data: {
  395. data: apiObject,
  396. id: this.get('selectedAlertNotification.id')
  397. },
  398. success: 'updateAlertNotificationSuccessCallback',
  399. error: 'saveErrorCallback'
  400. });
  401. },
  402. /**
  403. * Success callback for <code>updateAlertNotification</code>
  404. * @method updateAlertNotificationSuccessCallback
  405. */
  406. updateAlertNotificationSuccessCallback: function () {
  407. this.loadAlertNotifications();
  408. var createEditPopup = this.get('createEditPopup');
  409. if (createEditPopup) {
  410. createEditPopup.hide();
  411. }
  412. },
  413. /**
  414. * Error callback for <code>createAlertNotification</code> and <code>updateAlertNotification</code>
  415. * @method saveErrorCallback
  416. */
  417. saveErrorCallback: function () {
  418. this.set('createEditPopup.isSaving', false);
  419. },
  420. /**
  421. * Delete Notification button handler
  422. * @return {App.ModalPopup}
  423. * @method deleteAlertNotification
  424. */
  425. deleteAlertNotification: function () {
  426. var self = this;
  427. return App.showConfirmationPopup(function () {
  428. App.ajax.send({
  429. name: 'alerts.delete_alert_notification',
  430. sender: self,
  431. data: {
  432. id: self.get('selectedAlertNotification.id')
  433. },
  434. success: 'deleteAlertNotificationSuccessCallback'
  435. });
  436. }, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteBody').format(this.get('selectedAlertNotification.name')),
  437. null, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteHeader'), Em.I18n.t('common.delete'));
  438. },
  439. /**
  440. * Success callback for <code>deleteAlertNotification</code>
  441. * @method deleteAlertNotificationSuccessCallback
  442. */
  443. deleteAlertNotificationSuccessCallback: function () {
  444. this.loadAlertNotifications();
  445. var selectedAlertNotification = this.get('selectedAlertNotification');
  446. selectedAlertNotification.deleteRecord();
  447. this.set('selectedAlertNotification', null);
  448. },
  449. /**
  450. * Duplicate Notification button handler
  451. * @method duplicateAlertNotification
  452. */
  453. duplicateAlertNotification: function () {
  454. this.fillEditCreateInputs(true);
  455. this.showCreateEditPopup();
  456. },
  457. /**
  458. * Show popup with form for new custom property
  459. * @method addCustomPropertyHandler
  460. * @return {App.ModalPopup}
  461. */
  462. addCustomPropertyHandler: function () {
  463. var self = this;
  464. return App.ModalPopup.show({
  465. header: Em.I18n.t('alerts.notifications.addCustomPropertyPopup.header'),
  466. primary: Em.I18n.t('common.add'),
  467. bodyClass: Em.View.extend({
  468. /**
  469. * If some error with new custom property
  470. * @type {boolean}
  471. */
  472. isError: false,
  473. controller: this,
  474. /**
  475. * Error message for new custom property (invalid name, existed name etc)
  476. * @type {string}
  477. */
  478. errorMessage: '',
  479. /**
  480. * Check new custom property for errors with its name
  481. * @method errorHandler
  482. */
  483. errorsHandler: function () {
  484. var name = this.get('controller.newCustomProperty.name');
  485. var flag = validator.isValidConfigKey(name);
  486. if (flag) {
  487. if (this.get('controller.inputFields.customProperties').mapProperty('name').contains(name) ||
  488. this.get('controller.ignoredCustomProperties').contains(name)) {
  489. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.propertyExists'));
  490. flag = false;
  491. }
  492. }
  493. else {
  494. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.invalidPropertyName'));
  495. }
  496. this.set('isError', !flag);
  497. this.set('parentView.disablePrimary', !flag);
  498. }.observes('controller.newCustomProperty.name'),
  499. templateName: require('templates/main/alerts/add_custom_config_to_alert_notification_popup')
  500. }),
  501. disablePrimary: true,
  502. onPrimary: function () {
  503. self.addCustomProperty();
  504. self.set('newCustomProperty', {name: '', value: ''}); // cleanup
  505. this.hide();
  506. }
  507. });
  508. },
  509. /**
  510. * Add Custom Property to <code>selectedAlertNotification</code> (push it to <code>properties</code>-field)
  511. * @method addCustomProperty
  512. */
  513. addCustomProperty: function () {
  514. var newCustomProperty = this.get('newCustomProperty');
  515. this.get('inputFields.customProperties').pushObject({
  516. name: newCustomProperty.name,
  517. value: newCustomProperty.value,
  518. defaultValue: newCustomProperty.value
  519. });
  520. },
  521. /**
  522. * "Remove"-button click handler
  523. * Delete customProperty from <code>inputFields.customProperties</code>
  524. * @param {object} e
  525. * @method removeCustomProperty
  526. */
  527. removeCustomPropertyHandler: function (e) {
  528. var customProperties = this.get('inputFields.customProperties');
  529. this.set('inputFields.customProperties', customProperties.without(e.context));
  530. }
  531. });