manage_alert_notifications_controller.js 30 KB

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