manage_alert_notifications_controller.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. bodyClass: Em.View.extend({
  256. controller: this,
  257. templateName: require('templates/main/alerts/create_alert_notification'),
  258. didInsertElement: function () {
  259. this.nameValidation();
  260. },
  261. isEmailMethodSelected: function () {
  262. return this.get('controller.inputFields.method.value') === 'EMAIL';
  263. }.property('controller.inputFields.method.value'),
  264. nameValidation: function () {
  265. this.set('parentView.hasErrors', !this.get('controller.inputFields.name.value').trim());
  266. }.observes('controller.inputFields.name.value'),
  267. groupsSelectView: Em.Select.extend({
  268. init: function () {
  269. this._super();
  270. this.set('parentView.groupSelect', this);
  271. }
  272. }),
  273. groupSelect: null,
  274. selectAllGroups: function () {
  275. this.set('groupSelect.selection', this.get('groupSelect.content').slice());
  276. },
  277. clearAllGroups: function () {
  278. this.set('groupSelect.selection', []);
  279. },
  280. severitySelectView: Em.Select.extend({
  281. init: function () {
  282. this._super();
  283. this.set('parentView.severitySelect', this);
  284. }
  285. }),
  286. severitySelect: null,
  287. selectAllSeverity: function () {
  288. this.set('severitySelect.selection', this.get('severitySelect.content').slice());
  289. },
  290. clearAllSeverity: function () {
  291. this.set('severitySelect.selection', []);
  292. }
  293. }),
  294. isSaving: false,
  295. hasErrors: false,
  296. primary: Em.I18n.t('common.save'),
  297. disablePrimary: function () {
  298. return this.get('isSaving') || this.get('hasErrors');
  299. }.property('isSaving', 'hasErrors'),
  300. onPrimary: function () {
  301. this.set('isSaving', true);
  302. var apiObject = self.formatNotificationAPIObject();
  303. if (isEdit) {
  304. self.updateAlertNotification(apiObject);
  305. } else {
  306. self.createAlertNotification(apiObject);
  307. }
  308. }
  309. });
  310. this.set('createEditPopup', createEditPopup);
  311. return createEditPopup;
  312. },
  313. /**
  314. * Create API-formatted object from data populate by user
  315. * @returns {Object}
  316. * @method formatNotificationAPIObject
  317. */
  318. formatNotificationAPIObject: function () {
  319. var inputFields = this.get('inputFields');
  320. var properties = {};
  321. var clusterId = App.Cluster.find().objectAt(0).get('id');
  322. if (inputFields.get('method.value') === 'EMAIL') {
  323. properties['ambari.dispatch.recipients'] = inputFields.get('email.value').replace(/\s/g, '').split(',');
  324. properties['mail.smtp.host'] = inputFields.get('SMTPServer.value');
  325. properties['mail.smtp.port'] = inputFields.get('SMTPPort.value');
  326. properties['mail.smtp.from'] = inputFields.get('emailFrom.value');
  327. } else {
  328. properties['ambari.dispatch.snmp.version'] = inputFields.get('version.value');
  329. properties['ambari.dispatch.snmp.oids.trap'] = inputFields.get('OIDs.value');
  330. properties['ambari.dispatch.snmp.community'] = inputFields.get('community.value');
  331. properties['ambari.dispatch.snmp.port'] = inputFields.get('port.value');
  332. }
  333. inputFields.get('customProperties').forEach(function (customProperty) {
  334. properties[customProperty.name] = customProperty.value;
  335. });
  336. return {
  337. AlertTarget: {
  338. name: inputFields.get('name.value'),
  339. description: inputFields.get('description.value'),
  340. global: inputFields.get('global.value'),
  341. groups: inputFields.get('groups.value').map(function (group) {
  342. return {
  343. name: group.get('name'),
  344. id: group.get('id'),
  345. default: group.get('default'),
  346. cluster_id: clusterId
  347. };
  348. }),
  349. notification_type: inputFields.get('method.value'),
  350. alert_states: inputFields.get('severityFilter.value'),
  351. properties: properties
  352. }
  353. };
  354. },
  355. /**
  356. * Send request to server to create Alert Notification
  357. * @param {object} apiObject (@see formatNotificationAPIObject)
  358. * @returns {$.ajax}
  359. * @method createAlertNotification
  360. */
  361. createAlertNotification: function (apiObject) {
  362. return App.ajax.send({
  363. name: 'alerts.create_alert_notification',
  364. sender: this,
  365. data: {
  366. data: apiObject
  367. },
  368. success: 'createAlertNotificationSuccessCallback',
  369. error: 'saveErrorCallback'
  370. });
  371. },
  372. /**
  373. * Success callback for <code>createAlertNotification</code>
  374. * @method createAlertNotificationSuccessCallback
  375. */
  376. createAlertNotificationSuccessCallback: function () {
  377. this.loadAlertNotifications();
  378. var createEditPopup = this.get('createEditPopup');
  379. if (createEditPopup) {
  380. createEditPopup.hide();
  381. }
  382. },
  383. /**
  384. * Send request to server to update Alert Notification
  385. * @param {object} apiObject (@see formatNotificationAPIObject)
  386. * @returns {$.ajax}
  387. * @method updateAlertNotification
  388. */
  389. updateAlertNotification: function (apiObject) {
  390. return App.ajax.send({
  391. name: 'alerts.update_alert_notification',
  392. sender: this,
  393. data: {
  394. data: apiObject,
  395. id: this.get('selectedAlertNotification.id')
  396. },
  397. success: 'updateAlertNotificationSuccessCallback',
  398. error: 'saveErrorCallback'
  399. });
  400. },
  401. /**
  402. * Success callback for <code>updateAlertNotification</code>
  403. * @method updateAlertNotificationSuccessCallback
  404. */
  405. updateAlertNotificationSuccessCallback: function () {
  406. this.loadAlertNotifications();
  407. var createEditPopup = this.get('createEditPopup');
  408. if (createEditPopup) {
  409. createEditPopup.hide();
  410. }
  411. },
  412. /**
  413. * Error callback for <code>createAlertNotification</code> and <code>updateAlertNotification</code>
  414. * @method saveErrorCallback
  415. */
  416. saveErrorCallback: function () {
  417. this.set('createEditPopup.isSaving', false);
  418. },
  419. /**
  420. * Delete Notification button handler
  421. * @return {App.ModalPopup}
  422. * @method deleteAlertNotification
  423. */
  424. deleteAlertNotification: function () {
  425. var self = this;
  426. return App.showConfirmationPopup(function () {
  427. App.ajax.send({
  428. name: 'alerts.delete_alert_notification',
  429. sender: self,
  430. data: {
  431. id: self.get('selectedAlertNotification.id')
  432. },
  433. success: 'deleteAlertNotificationSuccessCallback'
  434. });
  435. }, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteBody').format(this.get('selectedAlertNotification.name')),
  436. null, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteHeader'), Em.I18n.t('common.delete'));
  437. },
  438. /**
  439. * Success callback for <code>deleteAlertNotification</code>
  440. * @method deleteAlertNotificationSuccessCallback
  441. */
  442. deleteAlertNotificationSuccessCallback: function () {
  443. this.loadAlertNotifications();
  444. var selectedAlertNotification = this.get('selectedAlertNotification');
  445. selectedAlertNotification.deleteRecord();
  446. this.set('selectedAlertNotification', null);
  447. },
  448. /**
  449. * Duplicate Notification button handler
  450. * @method duplicateAlertNotification
  451. */
  452. duplicateAlertNotification: function () {
  453. this.fillEditCreateInputs(true);
  454. this.showCreateEditPopup();
  455. },
  456. /**
  457. * Show popup with form for new custom property
  458. * @method addCustomPropertyHandler
  459. * @return {App.ModalPopup}
  460. */
  461. addCustomPropertyHandler: function () {
  462. var self = this;
  463. return App.ModalPopup.show({
  464. header: Em.I18n.t('alerts.notifications.addCustomPropertyPopup.header'),
  465. primary: Em.I18n.t('common.add'),
  466. bodyClass: Em.View.extend({
  467. /**
  468. * If some error with new custom property
  469. * @type {boolean}
  470. */
  471. isError: false,
  472. controller: this,
  473. /**
  474. * Error message for new custom property (invalid name, existed name etc)
  475. * @type {string}
  476. */
  477. errorMessage: '',
  478. /**
  479. * Check new custom property for errors with its name
  480. * @method errorHandler
  481. */
  482. errorsHandler: function () {
  483. var name = this.get('controller.newCustomProperty.name');
  484. var flag = validator.isValidConfigKey(name);
  485. if (flag) {
  486. if (this.get('controller.inputFields.customProperties').mapProperty('name').contains(name) ||
  487. this.get('controller.ignoredCustomProperties').contains(name)) {
  488. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.propertyExists'));
  489. flag = false;
  490. }
  491. }
  492. else {
  493. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.invalidPropertyName'));
  494. }
  495. this.set('isError', !flag);
  496. this.set('parentView.disablePrimary', !flag);
  497. }.observes('controller.newCustomProperty.name'),
  498. templateName: require('templates/main/alerts/add_custom_config_to_alert_notification_popup')
  499. }),
  500. disablePrimary: true,
  501. onPrimary: function () {
  502. self.addCustomProperty();
  503. self.set('newCustomProperty', {name: '', value: ''}); // cleanup
  504. this.hide();
  505. }
  506. });
  507. },
  508. /**
  509. * Add Custom Property to <code>selectedAlertNotification</code> (push it to <code>properties</code>-field)
  510. * @method addCustomProperty
  511. */
  512. addCustomProperty: function () {
  513. var newCustomProperty = this.get('newCustomProperty');
  514. this.get('inputFields.customProperties').pushObject({
  515. name: newCustomProperty.name,
  516. value: newCustomProperty.value,
  517. defaultValue: newCustomProperty.value
  518. });
  519. },
  520. /**
  521. * "Remove"-button click handler
  522. * Delete customProperty from <code>inputFields.customProperties</code>
  523. * @param {object} e
  524. * @method removeCustomProperty
  525. */
  526. removeCustomPropertyHandler: function (e) {
  527. var customProperties = this.get('inputFields.customProperties');
  528. this.set('inputFields.customProperties', customProperties.without(e.context));
  529. }
  530. });