manage_alert_notifications_controller.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. allGroups: Em.Object.create({
  55. value: '',
  56. defaultValue: 'custom',
  57. disabled: false,
  58. isAll: function () {
  59. return this.get('value') == 'all';
  60. }.property('value')
  61. }),
  62. method: {
  63. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.method'),
  64. value: '',
  65. defaultValue: ''
  66. },
  67. email: {
  68. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.email'),
  69. value: '',
  70. defaultValue: ''
  71. },
  72. SMTPServer: {
  73. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPServer'),
  74. value: '',
  75. defaultValue: ''
  76. },
  77. SMTPPort: {
  78. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPPort'),
  79. value: '',
  80. defaultValue: ''
  81. },
  82. SMTPUseAuthentication: Em.Object.create({
  83. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPUseAuthentication'),
  84. value: false,
  85. defaultValue: false,
  86. invertedValue: Em.computed.not('value')
  87. }),
  88. SMTPUsername: {
  89. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPUsername'),
  90. value: '',
  91. defaultValue: ''
  92. },
  93. SMTPPassword: {
  94. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPPassword'),
  95. value: '',
  96. defaultValue: ''
  97. },
  98. retypeSMTPPassword: {
  99. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.retypeSMTPPassword'),
  100. value: '',
  101. defaultValue: ''
  102. },
  103. SMTPSTARTTLS: {
  104. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPSTARTTLS'),
  105. value: false,
  106. defaultValue: false
  107. },
  108. emailFrom: {
  109. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.emailFrom'),
  110. value: '',
  111. defaultValue: ''
  112. },
  113. version: {
  114. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.version'),
  115. value: '',
  116. defaultValue: ''
  117. },
  118. OIDs: {
  119. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.OIDs'),
  120. value: '',
  121. defaultValue: ''
  122. },
  123. community: {
  124. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.community'),
  125. value: '',
  126. defaultValue: ''
  127. },
  128. port: {
  129. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.port'),
  130. value: '',
  131. defaultValue: ''
  132. },
  133. severityFilter: {
  134. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.severityFilter'),
  135. value: [],
  136. defaultValue: []
  137. },
  138. description: {
  139. label: Em.I18n.t('common.description'),
  140. value: '',
  141. defaultValue: ''
  142. },
  143. customProperties: Em.A([])
  144. }),
  145. /**
  146. * List of available Notification types
  147. * used in Type combobox
  148. * @type {Array}
  149. */
  150. methods: ['EMAIL', 'SNMP'],
  151. /**
  152. * List of available value for Severity Filter
  153. * used in Severity Filter combobox
  154. * @type {Array}
  155. */
  156. severities: ['OK', 'WARNING', 'CRITICAL', 'UNKNOWN'],
  157. /**
  158. * List of available SNMP versions
  159. * @type {Array}
  160. */
  161. SNMPVersions: ['SNMPv1', 'SNMPv2c', 'SNMPv3'],
  162. /**
  163. * List of all Alert Notifications
  164. * @type {App.AlertNotification[]}
  165. */
  166. alertNotifications: function () {
  167. return this.get('isLoaded') ? App.AlertNotification.find().toArray() : [];
  168. }.property('isLoaded'),
  169. /**
  170. * List of all Alert Groups
  171. * @type {App.AlertGroup[]}
  172. */
  173. allAlertGroups: function () {
  174. return this.get('isLoaded') ? App.AlertGroup.find().toArray() : [];
  175. }.property('isLoaded'),
  176. /**
  177. * Selected Alert Notification
  178. * @type {App.AlertNotification}
  179. */
  180. selectedAlertNotification: null,
  181. /**
  182. * Addable to <code>selectedAlertNotification.properties</code> custom property
  183. * @type {{name: string, value: string}}
  184. */
  185. newCustomProperty: {name: '', value: ''},
  186. /**
  187. * List custom property names that shouldn't be displayed on Edit page
  188. * @type {string[]}
  189. */
  190. ignoredCustomProperties: [
  191. 'ambari.dispatch.credential.password',
  192. 'ambari.dispatch.credential.username',
  193. 'ambari.dispatch.recipients',
  194. 'ambari.dispatch.snmp.community',
  195. 'ambari.dispatch.snmp.oids.trap',
  196. 'ambari.dispatch.snmp.port',
  197. 'ambari.dispatch.snmp.version',
  198. 'mail.smtp.auth',
  199. 'mail.smtp.from',
  200. 'mail.smtp.host',
  201. 'mail.smtp.port',
  202. 'mail.smtp.starttls.enable'
  203. ],
  204. /**
  205. * Load all Alert Notifications from server
  206. * @method loadAlertNotifications
  207. */
  208. loadAlertNotifications: function () {
  209. var self = this;
  210. this.set('isLoaded', false);
  211. App.router.get('updateController').updateAlertGroups(function () {
  212. App.ajax.send({
  213. name: 'alerts.notifications',
  214. sender: self,
  215. success: 'getAlertNotificationsSuccessCallback',
  216. error: 'getAlertNotificationsErrorCallback'
  217. });
  218. });
  219. },
  220. /**
  221. * Success-callback for load alert notifications request
  222. * @param {object} json
  223. * @method getAlertNotificationsSuccessCallback
  224. */
  225. getAlertNotificationsSuccessCallback: function (json) {
  226. App.alertNotificationMapper.map(json);
  227. this.set('isLoaded', true);
  228. },
  229. /**
  230. * Error-callback for load alert notifications request
  231. * @method getAlertNotificationsErrorCallback
  232. */
  233. getAlertNotificationsErrorCallback: function () {
  234. this.set('isLoaded', true);
  235. },
  236. /**
  237. * Add Notification button handler
  238. * @method addAlertNotification
  239. */
  240. addAlertNotification: function () {
  241. var inputFields = this.get('inputFields');
  242. inputFields.setProperties({
  243. 'global.disabled': false
  244. });
  245. Em.keys(inputFields).forEach(function (key) {
  246. inputFields.set(key + '.value', inputFields.get(key + '.defaultValue'));
  247. });
  248. inputFields.set('severityFilter.value', ['OK', 'WARNING', 'CRITICAL', 'UNKNOWN']);
  249. this.showCreateEditPopup(false);
  250. },
  251. /**
  252. * Edit Notification button handler
  253. * @method editAlertNotification
  254. */
  255. editAlertNotification: function () {
  256. this.fillEditCreateInputs();
  257. this.showCreateEditPopup(true);
  258. },
  259. /**
  260. * Fill inputs of Create/Edit popup form
  261. * @param addCopyToName define whether add 'Copy of ' to name
  262. * @method fillEditCreateInputs
  263. */
  264. fillEditCreateInputs: function (addCopyToName) {
  265. var inputFields = this.get('inputFields');
  266. var selectedAlertNotification = this.get('selectedAlertNotification');
  267. inputFields.set('name.value', (addCopyToName ? 'Copy of ' : '') + selectedAlertNotification.get('name'));
  268. inputFields.set('groups.value', selectedAlertNotification.get('groups').toArray());
  269. inputFields.set('email.value', selectedAlertNotification.get('properties')['ambari.dispatch.recipients'] ?
  270. selectedAlertNotification.get('properties')['ambari.dispatch.recipients'].join(', ') : '');
  271. inputFields.set('SMTPServer.value', selectedAlertNotification.get('properties')['mail.smtp.host']);
  272. inputFields.set('SMTPPort.value', selectedAlertNotification.get('properties')['mail.smtp.port']);
  273. inputFields.set('SMTPUseAuthentication.value', selectedAlertNotification.get('properties')['mail.smtp.auth']);
  274. inputFields.set('SMTPUsername.value', selectedAlertNotification.get('properties')['ambari.dispatch.credential.username']);
  275. inputFields.set('SMTPPassword.value', selectedAlertNotification.get('properties')['ambari.dispatch.credential.password']);
  276. inputFields.set('retypeSMTPPassword.value', selectedAlertNotification.get('properties')['ambari.dispatch.credential.password']);
  277. inputFields.set('SMTPSTARTTLS.value', selectedAlertNotification.get('properties')['mail.smtp.starttls.enable']);
  278. inputFields.set('emailFrom.value', selectedAlertNotification.get('properties')['mail.smtp.from']);
  279. inputFields.set('version.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.version']);
  280. inputFields.set('OIDs.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.oids.trap']);
  281. inputFields.set('community.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.community']);
  282. inputFields.set('port.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.port']);
  283. inputFields.set('severityFilter.value', selectedAlertNotification.get('alertStates'));
  284. inputFields.set('global.value', selectedAlertNotification.get('global'));
  285. inputFields.set('allGroups.value', selectedAlertNotification.get('global') ? 'all' : 'custom');
  286. // not allow to edit global field
  287. inputFields.set('global.disabled', true);
  288. inputFields.set('description.value', selectedAlertNotification.get('description'));
  289. inputFields.set('method.value', selectedAlertNotification.get('type'));
  290. inputFields.get('customProperties').clear();
  291. var properties = selectedAlertNotification.get('properties');
  292. var ignoredCustomProperties = this.get('ignoredCustomProperties');
  293. Em.keys(properties).forEach(function (k) {
  294. if (ignoredCustomProperties.contains(k)) return;
  295. inputFields.get('customProperties').pushObject({
  296. name: k,
  297. value: properties[k],
  298. defaultValue: properties[k]
  299. });
  300. });
  301. },
  302. /**
  303. * Show Edit or Create Notification popup
  304. * @param {boolean} isEdit true - edit, false - create
  305. * @returns {App.ModalPopup}
  306. * @method showCreateEditPopup
  307. */
  308. showCreateEditPopup: function (isEdit) {
  309. var self = this;
  310. var createEditPopup = App.ModalPopup.show({
  311. header: isEdit ? Em.I18n.t('alerts.actions.manage_alert_notifications_popup.editHeader') : Em.I18n.t('alerts.actions.manage_alert_notifications_popup.addHeader'),
  312. classNames: ['create-edit-alert-notification-popup'],
  313. marginBottom: 130,
  314. bodyClass: Em.View.extend({
  315. controller: this,
  316. templateName: require('templates/main/alerts/create_alert_notification'),
  317. didInsertElement: function () {
  318. App.tooltip($('.checkbox-tooltip'));
  319. this.nameValidation();
  320. this.emailToValidation();
  321. this.emailFromValidation();
  322. this.smtpPortValidation();
  323. this.portValidation();
  324. this.retypePasswordValidation();
  325. },
  326. isEmailMethodSelected: function () {
  327. return this.get('controller.inputFields.method.value') === 'EMAIL';
  328. }.property('controller.inputFields.method.value'),
  329. nameValidation: function () {
  330. var newName = this.get('controller.inputFields.name.value').trim();
  331. var errorMessage = '';
  332. // on editing, save current notification name
  333. if (newName && !this.get('currentName')) {
  334. this.set('currentName', newName);
  335. }
  336. if (isEdit) {
  337. // edit current alert notification
  338. if (!newName) {
  339. this.set('nameError', true);
  340. errorMessage = Em.I18n.t('alerts.actions.manage_alert_notifications_popup.error.name.empty');
  341. } else if (newName && newName != this.get('currentName') && self.get('alertNotifications').mapProperty('name').contains(newName)) {
  342. this.set('nameError', true);
  343. errorMessage = Em.I18n.t('alerts.actions.manage_alert_notifications_popup.error.name.existed');
  344. } else {
  345. this.set('nameError', false);
  346. }
  347. } else {
  348. // add new alert notification
  349. if (!newName) {
  350. this.set('nameError', true);
  351. errorMessage = Em.I18n.t('alerts.actions.manage_alert_notifications_popup.error.name.empty');
  352. } else if (newName && self.get('alertNotifications').mapProperty('name').contains(newName)) {
  353. this.set('nameError', true);
  354. errorMessage = Em.I18n.t('alerts.actions.manage_alert_notifications_popup.error.name.existed');
  355. } else {
  356. this.set('nameError', false);
  357. }
  358. }
  359. this.set('controller.inputFields.name.errorMsg', errorMessage);
  360. }.observes('controller.inputFields.name.value'),
  361. emailToValidation: function () {
  362. var emailTo = this.get('controller.inputFields.email.value');
  363. if (emailTo && !validator.isValidEmail(emailTo)) {
  364. this.set('emailToError', true);
  365. this.set('controller.inputFields.email.errorMsg', Em.I18n.t('alerts.notifications.error.email'));
  366. } else {
  367. this.set('emailToError', false);
  368. this.set('controller.inputFields.email.errorMsg', null);
  369. }
  370. }.observes('controller.inputFields.email.value'),
  371. emailFromValidation: function () {
  372. var emailFrom = this.get('controller.inputFields.emailFrom.value');
  373. if (emailFrom && !validator.isValidEmail(emailFrom)) {
  374. this.set('emailFromError', true);
  375. this.set('controller.inputFields.emailFrom.errorMsg', Em.I18n.t('alerts.notifications.error.email'));
  376. } else {
  377. this.set('emailFromError', false);
  378. this.set('controller.inputFields.emailFrom.errorMsg', null);
  379. }
  380. }.observes('controller.inputFields.emailFrom.value'),
  381. smtpPortValidation: function () {
  382. var value = this.get('controller.inputFields.SMTPPort.value');
  383. if (value && (!validator.isValidInt(value) || value < 0)) {
  384. this.set('smtpPortError', true);
  385. this.set('controller.inputFields.SMTPPort.errorMsg', Em.I18n.t('alerts.notifications.error.integer'));
  386. } else {
  387. this.set('smtpPortError', false);
  388. this.set('controller.inputFields.SMTPPort.errorMsg', null);
  389. }
  390. }.observes('controller.inputFields.SMTPPort.value'),
  391. portValidation: function () {
  392. var value = this.get('controller.inputFields.port.value');
  393. if (value && (!validator.isValidInt(value) || value < 0)) {
  394. this.set('portError', true);
  395. this.set('controller.inputFields.port.errorMsg', Em.I18n.t('alerts.notifications.error.integer'));
  396. } else {
  397. this.set('portError', false);
  398. this.set('controller.inputFields.port.errorMsg', null);
  399. }
  400. }.observes('controller.inputFields.port.value'),
  401. retypePasswordValidation: function () {
  402. var passwordValue = this.get('controller.inputFields.SMTPPassword.value');
  403. var retypePasswordValue = this.get('controller.inputFields.retypeSMTPPassword.value');
  404. if (passwordValue !== retypePasswordValue) {
  405. this.set('passwordError', true);
  406. this.set('controller.inputFields.retypeSMTPPassword.errorMsg', Em.I18n.t('alerts.notifications.error.retypePassword'));
  407. } else {
  408. this.set('passwordError', false);
  409. this.set('controller.inputFields.retypeSMTPPassword.errorMsg', null);
  410. }
  411. }.observes('controller.inputFields.retypeSMTPPassword.value', 'controller.inputFields.SMTPPassword.value'),
  412. setParentErrors: function () {
  413. var hasErrors = this.get('nameError') || this.get('emailToError') || this.get('emailFromError') ||
  414. this.get('smtpPortError') || this.get('portError') || this.get('passwordError');
  415. this.set('parentView.hasErrors', hasErrors);
  416. }.observes('nameError', 'emailToError', 'emailFromError', 'smtpPortError', 'portError', 'passwordError'),
  417. groupsSelectView: Em.Select.extend({
  418. attributeBindings: ['disabled'],
  419. init: function () {
  420. this._super();
  421. this.set('parentView.groupSelect', this);
  422. }
  423. }),
  424. groupSelect: null,
  425. /**
  426. * Select all alert-groups if <code>allGroups.value</code> is 'custom'
  427. * @method selectAllGroups
  428. */
  429. selectAllGroups: function () {
  430. if (this.get('controller.inputFields.allGroups.value') == 'custom') {
  431. this.set('groupSelect.selection', this.get('groupSelect.content').slice());
  432. }
  433. },
  434. /**
  435. * Deselect all alert-groups if <code>allGroups.value</code> is 'custom'
  436. * @method clearAllGroups
  437. */
  438. clearAllGroups: function () {
  439. if (this.get('controller.inputFields.allGroups.value') == 'custom') {
  440. this.set('groupSelect.selection', []);
  441. }
  442. },
  443. severitySelectView: Em.Select.extend({
  444. init: function () {
  445. this._super();
  446. this.set('parentView.severitySelect', this);
  447. }
  448. }),
  449. severitySelect: null,
  450. /**
  451. * Determines if all alert-groups are selected
  452. * @type {boolean}
  453. */
  454. allGroupsSelected: function () {
  455. return this.get('groupSelect.selection.length') === this.get('groupSelect.content.length');
  456. }.property('groupSelect.selection.length', 'groupSelect.content.length', 'groupSelect.disabled'),
  457. /**
  458. * Determines if no one alert-group is selected
  459. * @type {boolean}
  460. */
  461. noneGroupsSelected: function () {
  462. return this.get('groupSelect.selection.length') === 0;
  463. }.property('groupSelect.selection.length', 'groupSelect.content.length', 'groupSelect.disabled'),
  464. /**
  465. * Determines if all severities are selected
  466. * @type {boolean}
  467. */
  468. allSeveritySelected: function () {
  469. return this.get('severitySelect.selection.length') === this.get('severitySelect.content.length');
  470. }.property('severitySelect.selection.length', 'severitySelect.content.length'),
  471. /**
  472. * Determines if no one severity is selected
  473. * @type {boolean}
  474. */
  475. noneSeveritySelected: function () {
  476. return this.get('severitySelect.selection.length') === 0;
  477. }.property('severitySelect.selection.length', 'severitySelect.content.length'),
  478. /**
  479. * Select all severities
  480. * @method selectAllSeverity
  481. */
  482. selectAllSeverity: function () {
  483. this.set('severitySelect.selection', this.get('severitySelect.content').slice());
  484. },
  485. /**
  486. * Deselect all severities
  487. * @method clearAllSeverity
  488. */
  489. clearAllSeverity: function () {
  490. this.set('severitySelect.selection', []);
  491. }
  492. }),
  493. isSaving: false,
  494. hasErrors: false,
  495. primary: Em.I18n.t('common.save'),
  496. disablePrimary: function () {
  497. return this.get('isSaving') || this.get('hasErrors');
  498. }.property('isSaving', 'hasErrors'),
  499. onPrimary: function () {
  500. this.set('isSaving', true);
  501. var apiObject = self.formatNotificationAPIObject();
  502. if (isEdit) {
  503. self.updateAlertNotification(apiObject);
  504. } else {
  505. self.createAlertNotification(apiObject);
  506. }
  507. }
  508. });
  509. this.set('createEditPopup', createEditPopup);
  510. return createEditPopup;
  511. },
  512. /**
  513. * Create API-formatted object from data populate by user
  514. * @returns {Object}
  515. * @method formatNotificationAPIObject
  516. */
  517. formatNotificationAPIObject: function () {
  518. var inputFields = this.get('inputFields');
  519. var properties = {};
  520. if (inputFields.get('method.value') === 'EMAIL') {
  521. properties['ambari.dispatch.recipients'] = inputFields.get('email.value').replace(/\s/g, '').split(',');
  522. properties['mail.smtp.host'] = inputFields.get('SMTPServer.value');
  523. properties['mail.smtp.port'] = inputFields.get('SMTPPort.value');
  524. properties['mail.smtp.from'] = inputFields.get('emailFrom.value');
  525. properties['mail.smtp.auth'] = inputFields.get('SMTPUseAuthentication.value');
  526. if (inputFields.get('SMTPUseAuthentication.value')) {
  527. properties['ambari.dispatch.credential.username'] = inputFields.get('SMTPUsername.value');
  528. properties['ambari.dispatch.credential.password'] = inputFields.get('SMTPPassword.value');
  529. properties['mail.smtp.starttls.enable'] = inputFields.get('SMTPSTARTTLS.value');
  530. }
  531. } else {
  532. properties['ambari.dispatch.snmp.version'] = inputFields.get('version.value');
  533. properties['ambari.dispatch.snmp.oids.trap'] = inputFields.get('OIDs.value');
  534. properties['ambari.dispatch.snmp.community'] = inputFields.get('community.value');
  535. properties['ambari.dispatch.snmp.port'] = inputFields.get('port.value');
  536. }
  537. inputFields.get('customProperties').forEach(function (customProperty) {
  538. properties[customProperty.name] = customProperty.value;
  539. });
  540. var apiObject = {
  541. AlertTarget: {
  542. name: inputFields.get('name.value'),
  543. description: inputFields.get('description.value'),
  544. global: inputFields.get('allGroups.value') === 'all',
  545. notification_type: inputFields.get('method.value'),
  546. alert_states: inputFields.get('severityFilter.value'),
  547. properties: properties
  548. }
  549. };
  550. if (inputFields.get('allGroups.value') == 'custom') {
  551. apiObject.AlertTarget.groups = inputFields.get('groups.value').mapProperty('id');
  552. }
  553. return apiObject;
  554. },
  555. /**
  556. * Send request to server to create Alert Notification
  557. * @param {object} apiObject (@see formatNotificationAPIObject)
  558. * @returns {$.ajax}
  559. * @method createAlertNotification
  560. */
  561. createAlertNotification: function (apiObject) {
  562. return App.ajax.send({
  563. name: 'alerts.create_alert_notification',
  564. sender: this,
  565. data: {
  566. data: apiObject
  567. },
  568. success: 'createAlertNotificationSuccessCallback',
  569. error: 'saveErrorCallback'
  570. });
  571. },
  572. /**
  573. * Success callback for <code>createAlertNotification</code>
  574. * @method createAlertNotificationSuccessCallback
  575. */
  576. createAlertNotificationSuccessCallback: function () {
  577. this.loadAlertNotifications();
  578. var createEditPopup = this.get('createEditPopup');
  579. if (createEditPopup) {
  580. createEditPopup.hide();
  581. }
  582. },
  583. /**
  584. * Send request to server to update Alert Notification
  585. * @param {object} apiObject (@see formatNotificationAPIObject)
  586. * @returns {$.ajax}
  587. * @method updateAlertNotification
  588. */
  589. updateAlertNotification: function (apiObject) {
  590. return App.ajax.send({
  591. name: 'alerts.update_alert_notification',
  592. sender: this,
  593. data: {
  594. data: apiObject,
  595. id: this.get('selectedAlertNotification.id')
  596. },
  597. success: 'updateAlertNotificationSuccessCallback',
  598. error: 'saveErrorCallback'
  599. });
  600. },
  601. /**
  602. * Success callback for <code>updateAlertNotification</code>
  603. * @method updateAlertNotificationSuccessCallback
  604. */
  605. updateAlertNotificationSuccessCallback: function () {
  606. this.loadAlertNotifications();
  607. var createEditPopup = this.get('createEditPopup');
  608. if (createEditPopup) {
  609. createEditPopup.hide();
  610. }
  611. },
  612. /**
  613. * Error callback for <code>createAlertNotification</code> and <code>updateAlertNotification</code>
  614. * @method saveErrorCallback
  615. */
  616. saveErrorCallback: function () {
  617. this.set('createEditPopup.isSaving', false);
  618. },
  619. /**
  620. * Delete Notification button handler
  621. * @return {App.ModalPopup}
  622. * @method deleteAlertNotification
  623. */
  624. deleteAlertNotification: function () {
  625. var self = this;
  626. return App.showConfirmationPopup(function () {
  627. App.ajax.send({
  628. name: 'alerts.delete_alert_notification',
  629. sender: self,
  630. data: {
  631. id: self.get('selectedAlertNotification.id')
  632. },
  633. success: 'deleteAlertNotificationSuccessCallback'
  634. });
  635. }, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteBody').format(this.get('selectedAlertNotification.name')),
  636. null, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteHeader'), Em.I18n.t('common.delete'));
  637. },
  638. /**
  639. * Success callback for <code>deleteAlertNotification</code>
  640. * @method deleteAlertNotificationSuccessCallback
  641. */
  642. deleteAlertNotificationSuccessCallback: function () {
  643. this.loadAlertNotifications();
  644. var selectedAlertNotification = this.get('selectedAlertNotification');
  645. selectedAlertNotification.deleteRecord();
  646. this.set('selectedAlertNotification', null);
  647. },
  648. /**
  649. * Duplicate Notification button handler
  650. * @method duplicateAlertNotification
  651. */
  652. duplicateAlertNotification: function () {
  653. this.fillEditCreateInputs(true);
  654. this.showCreateEditPopup();
  655. },
  656. /**
  657. * Show popup with form for new custom property
  658. * @method addCustomPropertyHandler
  659. * @return {App.ModalPopup}
  660. */
  661. addCustomPropertyHandler: function () {
  662. var self = this;
  663. return App.ModalPopup.show({
  664. header: Em.I18n.t('alerts.notifications.addCustomPropertyPopup.header'),
  665. primary: Em.I18n.t('common.add'),
  666. bodyClass: Em.View.extend({
  667. /**
  668. * If some error with new custom property
  669. * @type {boolean}
  670. */
  671. isError: false,
  672. controller: this,
  673. /**
  674. * Error message for new custom property (invalid name, existed name etc)
  675. * @type {string}
  676. */
  677. errorMessage: '',
  678. /**
  679. * Check new custom property for errors with its name
  680. * @method errorHandler
  681. */
  682. errorsHandler: function () {
  683. var name = this.get('controller.newCustomProperty.name');
  684. var flag = validator.isValidConfigKey(name);
  685. if (flag) {
  686. if (this.get('controller.inputFields.customProperties').mapProperty('name').contains(name) ||
  687. this.get('controller.ignoredCustomProperties').contains(name)) {
  688. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.propertyExists'));
  689. flag = false;
  690. }
  691. }
  692. else {
  693. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.invalidPropertyName'));
  694. }
  695. this.set('isError', !flag);
  696. this.set('parentView.disablePrimary', !flag);
  697. }.observes('controller.newCustomProperty.name'),
  698. templateName: require('templates/main/alerts/add_custom_config_to_alert_notification_popup')
  699. }),
  700. disablePrimary: true,
  701. onPrimary: function () {
  702. self.addCustomProperty();
  703. self.set('newCustomProperty', {name: '', value: ''}); // cleanup
  704. this.hide();
  705. }
  706. });
  707. },
  708. /**
  709. * Add Custom Property to <code>selectedAlertNotification</code> (push it to <code>properties</code>-field)
  710. * @method addCustomProperty
  711. */
  712. addCustomProperty: function () {
  713. var newCustomProperty = this.get('newCustomProperty');
  714. this.get('inputFields.customProperties').pushObject({
  715. name: newCustomProperty.name,
  716. value: newCustomProperty.value,
  717. defaultValue: newCustomProperty.value
  718. });
  719. },
  720. /**
  721. * "Remove"-button click handler
  722. * Delete customProperty from <code>inputFields.customProperties</code>
  723. * @param {object} e
  724. * @method removeCustomProperty
  725. */
  726. removeCustomPropertyHandler: function (e) {
  727. var customProperties = this.get('inputFields.customProperties');
  728. this.set('inputFields.customProperties', customProperties.without(e.context));
  729. }
  730. });