manage_alert_notifications_controller.js 31 KB

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