manage_alert_notifications_controller.js 30 KB

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