manage_alert_notifications_controller.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. * @method loadAlertNotifications
  161. */
  162. loadAlertNotifications: function () {
  163. var self = this;
  164. this.set('isLoaded', false);
  165. App.router.get('updateController').updateAlertGroups(function () {
  166. App.ajax.send({
  167. name: 'alerts.notifications',
  168. sender: self,
  169. success: 'getAlertNotificationsSuccessCallback',
  170. error: 'getAlertNotificationsErrorCallback'
  171. });
  172. });
  173. },
  174. /**
  175. * Success-callback for load alert notifications request
  176. * @param {object} json
  177. * @method getAlertNotificationsSuccessCallback
  178. */
  179. getAlertNotificationsSuccessCallback: function (json) {
  180. App.alertNotificationMapper.map(json);
  181. this.set('isLoaded', true);
  182. },
  183. /**
  184. * Error-callback for load alert notifications request
  185. * @method getAlertNotificationsErrorCallback
  186. */
  187. getAlertNotificationsErrorCallback: function () {
  188. this.set('isLoaded', true);
  189. },
  190. /**
  191. * Add Notification button handler
  192. * @method addAlertNotification
  193. */
  194. addAlertNotification: function () {
  195. var inputFields = this.get('inputFields');
  196. inputFields.set('global.disabled', false);
  197. Em.keys(inputFields).forEach(function (key) {
  198. inputFields.set(key + '.value', inputFields.get(key + '.defaultValue'));
  199. });
  200. this.showCreateEditPopup(false);
  201. },
  202. /**
  203. * Edit Notification button handler
  204. * @method editAlertNotification
  205. */
  206. editAlertNotification: function () {
  207. this.fillEditCreateInputs();
  208. this.showCreateEditPopup(true);
  209. },
  210. /**
  211. * Fill inputs of Create/Edit popup form
  212. * @param addCopyToName define whether add 'Copy of ' to name
  213. * @method fillEditCreateInputs
  214. */
  215. fillEditCreateInputs: function (addCopyToName) {
  216. var inputFields = this.get('inputFields');
  217. var selectedAlertNotification = this.get('selectedAlertNotification');
  218. inputFields.set('name.value', (addCopyToName ? 'Copy of ' : '') + selectedAlertNotification.get('name'));
  219. inputFields.set('groups.value', selectedAlertNotification.get('groups').toArray());
  220. inputFields.set('email.value', selectedAlertNotification.get('properties')['ambari.dispatch.recipients'] ?
  221. selectedAlertNotification.get('properties')['ambari.dispatch.recipients'].join(', ') : '');
  222. inputFields.set('SMTPServer.value', selectedAlertNotification.get('properties')['mail.smtp.host']);
  223. inputFields.set('SMTPPort.value', selectedAlertNotification.get('properties')['mail.smtp.port']);
  224. inputFields.set('emailFrom.value', selectedAlertNotification.get('properties')['mail.smtp.from']);
  225. inputFields.set('version.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.version']);
  226. inputFields.set('OIDs.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.oids.trap']);
  227. inputFields.set('community.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.community']);
  228. inputFields.set('port.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.port']);
  229. inputFields.set('severityFilter.value', selectedAlertNotification.get('alertStates'));
  230. inputFields.set('global.value', selectedAlertNotification.get('global'));
  231. // not allow to edit global field
  232. inputFields.set('global.disabled', true);
  233. inputFields.set('description.value', selectedAlertNotification.get('description'));
  234. inputFields.set('method.value', selectedAlertNotification.get('type'));
  235. inputFields.get('customProperties').clear();
  236. var properties = selectedAlertNotification.get('properties');
  237. var ignoredCustomProperties = this.get('ignoredCustomProperties');
  238. Em.keys(properties).forEach(function (k) {
  239. if (ignoredCustomProperties.contains(k)) return;
  240. inputFields.get('customProperties').pushObject({
  241. name: k,
  242. value: properties[k],
  243. defaultValue: properties[k]
  244. });
  245. });
  246. },
  247. /**
  248. * Show Edit or Create Notification popup
  249. * @param {boolean} isEdit true - edit, false - create
  250. * @returns {App.ModalPopup}
  251. * @method showCreateEditPopup
  252. */
  253. showCreateEditPopup: function (isEdit) {
  254. var self = this;
  255. var createEditPopup = App.ModalPopup.show({
  256. header: isEdit ? Em.I18n.t('alerts.actions.manage_alert_notifications_popup.editHeader') : Em.I18n.t('alerts.actions.manage_alert_notifications_popup.addHeader'),
  257. classNames: ['create-edit-alert-notification-popup'],
  258. bodyClass: Em.View.extend({
  259. controller: this,
  260. templateName: require('templates/main/alerts/create_alert_notification'),
  261. /**
  262. * @type {string}
  263. */
  264. tooltipForGlobalCheckbox: function () {
  265. return isEdit ? '' : Em.I18n.t('alerts.actions.manage_alert_notifications_popup.global.tooltip');
  266. }.property(),
  267. didInsertElement: function () {
  268. App.tooltip($('.checkbox-tooltip'));
  269. this.nameValidation();
  270. },
  271. isEmailMethodSelected: function () {
  272. return this.get('controller.inputFields.method.value') === 'EMAIL';
  273. }.property('controller.inputFields.method.value'),
  274. nameValidation: function () {
  275. this.set('parentView.hasErrors', !this.get('controller.inputFields.name.value').trim());
  276. }.observes('controller.inputFields.name.value'),
  277. groupsSelectView: Em.Select.extend({
  278. attributeBindings: ['disabled'],
  279. init: function () {
  280. this._super();
  281. this.set('parentView.groupSelect', this);
  282. }
  283. }),
  284. groupSelect: null,
  285. selectAllGroups: function () {
  286. if (!this.get('controller.inputFields.global.value')) {
  287. this.set('groupSelect.selection', this.get('groupSelect.content').slice());
  288. }
  289. },
  290. clearAllGroups: function () {
  291. if (!this.get('controller.inputFields.global.value')) {
  292. this.set('groupSelect.selection', []);
  293. }
  294. },
  295. severitySelectView: Em.Select.extend({
  296. init: function () {
  297. this._super();
  298. this.set('parentView.severitySelect', this);
  299. }
  300. }),
  301. severitySelect: null,
  302. selectAllSeverity: function () {
  303. this.set('severitySelect.selection', this.get('severitySelect.content').slice());
  304. },
  305. clearAllSeverity: function () {
  306. this.set('severitySelect.selection', []);
  307. }
  308. }),
  309. isSaving: false,
  310. hasErrors: false,
  311. primary: Em.I18n.t('common.save'),
  312. disablePrimary: function () {
  313. return this.get('isSaving') || this.get('hasErrors');
  314. }.property('isSaving', 'hasErrors'),
  315. onPrimary: function () {
  316. this.set('isSaving', true);
  317. var apiObject = self.formatNotificationAPIObject();
  318. if (isEdit) {
  319. self.updateAlertNotification(apiObject);
  320. } else {
  321. self.createAlertNotification(apiObject);
  322. }
  323. }
  324. });
  325. this.set('createEditPopup', createEditPopup);
  326. return createEditPopup;
  327. },
  328. /**
  329. * Create API-formatted object from data populate by user
  330. * @returns {Object}
  331. * @method formatNotificationAPIObject
  332. */
  333. formatNotificationAPIObject: function () {
  334. var inputFields = this.get('inputFields');
  335. var properties = {};
  336. if (inputFields.get('method.value') === 'EMAIL') {
  337. properties['ambari.dispatch.recipients'] = inputFields.get('email.value').replace(/\s/g, '').split(',');
  338. properties['mail.smtp.host'] = inputFields.get('SMTPServer.value');
  339. properties['mail.smtp.port'] = inputFields.get('SMTPPort.value');
  340. properties['mail.smtp.from'] = inputFields.get('emailFrom.value');
  341. } else {
  342. properties['ambari.dispatch.snmp.version'] = inputFields.get('version.value');
  343. properties['ambari.dispatch.snmp.oids.trap'] = inputFields.get('OIDs.value');
  344. properties['ambari.dispatch.snmp.community'] = inputFields.get('community.value');
  345. properties['ambari.dispatch.snmp.port'] = inputFields.get('port.value');
  346. }
  347. inputFields.get('customProperties').forEach(function (customProperty) {
  348. properties[customProperty.name] = customProperty.value;
  349. });
  350. var apiObject = {
  351. AlertTarget: {
  352. name: inputFields.get('name.value'),
  353. description: inputFields.get('description.value'),
  354. global: inputFields.get('global.value'),
  355. notification_type: inputFields.get('method.value'),
  356. alert_states: inputFields.get('severityFilter.value'),
  357. properties: properties
  358. }
  359. };
  360. if (!inputFields.get('global.value')) {
  361. apiObject.AlertTarget.groups = inputFields.get('groups.value').mapProperty('id');
  362. }
  363. return apiObject;
  364. },
  365. /**
  366. * Send request to server to create Alert Notification
  367. * @param {object} apiObject (@see formatNotificationAPIObject)
  368. * @returns {$.ajax}
  369. * @method createAlertNotification
  370. */
  371. createAlertNotification: function (apiObject) {
  372. return App.ajax.send({
  373. name: 'alerts.create_alert_notification',
  374. sender: this,
  375. data: {
  376. data: apiObject
  377. },
  378. success: 'createAlertNotificationSuccessCallback',
  379. error: 'saveErrorCallback'
  380. });
  381. },
  382. /**
  383. * Success callback for <code>createAlertNotification</code>
  384. * @method createAlertNotificationSuccessCallback
  385. */
  386. createAlertNotificationSuccessCallback: function () {
  387. this.loadAlertNotifications();
  388. var createEditPopup = this.get('createEditPopup');
  389. if (createEditPopup) {
  390. createEditPopup.hide();
  391. }
  392. },
  393. /**
  394. * Send request to server to update Alert Notification
  395. * @param {object} apiObject (@see formatNotificationAPIObject)
  396. * @returns {$.ajax}
  397. * @method updateAlertNotification
  398. */
  399. updateAlertNotification: function (apiObject) {
  400. return App.ajax.send({
  401. name: 'alerts.update_alert_notification',
  402. sender: this,
  403. data: {
  404. data: apiObject,
  405. id: this.get('selectedAlertNotification.id')
  406. },
  407. success: 'updateAlertNotificationSuccessCallback',
  408. error: 'saveErrorCallback'
  409. });
  410. },
  411. /**
  412. * Success callback for <code>updateAlertNotification</code>
  413. * @method updateAlertNotificationSuccessCallback
  414. */
  415. updateAlertNotificationSuccessCallback: function () {
  416. this.loadAlertNotifications();
  417. var createEditPopup = this.get('createEditPopup');
  418. if (createEditPopup) {
  419. createEditPopup.hide();
  420. }
  421. },
  422. /**
  423. * Error callback for <code>createAlertNotification</code> and <code>updateAlertNotification</code>
  424. * @method saveErrorCallback
  425. */
  426. saveErrorCallback: function () {
  427. this.set('createEditPopup.isSaving', false);
  428. },
  429. /**
  430. * Delete Notification button handler
  431. * @return {App.ModalPopup}
  432. * @method deleteAlertNotification
  433. */
  434. deleteAlertNotification: function () {
  435. var self = this;
  436. return App.showConfirmationPopup(function () {
  437. App.ajax.send({
  438. name: 'alerts.delete_alert_notification',
  439. sender: self,
  440. data: {
  441. id: self.get('selectedAlertNotification.id')
  442. },
  443. success: 'deleteAlertNotificationSuccessCallback'
  444. });
  445. }, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteBody').format(this.get('selectedAlertNotification.name')),
  446. null, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteHeader'), Em.I18n.t('common.delete'));
  447. },
  448. /**
  449. * Success callback for <code>deleteAlertNotification</code>
  450. * @method deleteAlertNotificationSuccessCallback
  451. */
  452. deleteAlertNotificationSuccessCallback: function () {
  453. this.loadAlertNotifications();
  454. var selectedAlertNotification = this.get('selectedAlertNotification');
  455. selectedAlertNotification.deleteRecord();
  456. this.set('selectedAlertNotification', null);
  457. },
  458. /**
  459. * Duplicate Notification button handler
  460. * @method duplicateAlertNotification
  461. */
  462. duplicateAlertNotification: function () {
  463. this.fillEditCreateInputs(true);
  464. this.showCreateEditPopup();
  465. },
  466. /**
  467. * Show popup with form for new custom property
  468. * @method addCustomPropertyHandler
  469. * @return {App.ModalPopup}
  470. */
  471. addCustomPropertyHandler: function () {
  472. var self = this;
  473. return App.ModalPopup.show({
  474. header: Em.I18n.t('alerts.notifications.addCustomPropertyPopup.header'),
  475. primary: Em.I18n.t('common.add'),
  476. bodyClass: Em.View.extend({
  477. /**
  478. * If some error with new custom property
  479. * @type {boolean}
  480. */
  481. isError: false,
  482. controller: this,
  483. /**
  484. * Error message for new custom property (invalid name, existed name etc)
  485. * @type {string}
  486. */
  487. errorMessage: '',
  488. /**
  489. * Check new custom property for errors with its name
  490. * @method errorHandler
  491. */
  492. errorsHandler: function () {
  493. var name = this.get('controller.newCustomProperty.name');
  494. var flag = validator.isValidConfigKey(name);
  495. if (flag) {
  496. if (this.get('controller.inputFields.customProperties').mapProperty('name').contains(name) ||
  497. this.get('controller.ignoredCustomProperties').contains(name)) {
  498. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.propertyExists'));
  499. flag = false;
  500. }
  501. }
  502. else {
  503. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.invalidPropertyName'));
  504. }
  505. this.set('isError', !flag);
  506. this.set('parentView.disablePrimary', !flag);
  507. }.observes('controller.newCustomProperty.name'),
  508. templateName: require('templates/main/alerts/add_custom_config_to_alert_notification_popup')
  509. }),
  510. disablePrimary: true,
  511. onPrimary: function () {
  512. self.addCustomProperty();
  513. self.set('newCustomProperty', {name: '', value: ''}); // cleanup
  514. this.hide();
  515. }
  516. });
  517. },
  518. /**
  519. * Add Custom Property to <code>selectedAlertNotification</code> (push it to <code>properties</code>-field)
  520. * @method addCustomProperty
  521. */
  522. addCustomProperty: function () {
  523. var newCustomProperty = this.get('newCustomProperty');
  524. this.get('inputFields.customProperties').pushObject({
  525. name: newCustomProperty.name,
  526. value: newCustomProperty.value,
  527. defaultValue: newCustomProperty.value
  528. });
  529. },
  530. /**
  531. * "Remove"-button click handler
  532. * Delete customProperty from <code>inputFields.customProperties</code>
  533. * @param {object} e
  534. * @method removeCustomProperty
  535. */
  536. removeCustomPropertyHandler: function (e) {
  537. var customProperties = this.get('inputFields.customProperties');
  538. this.set('inputFields.customProperties', customProperties.without(e.context));
  539. }
  540. });