manage_alert_notifications_controller.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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: function () {
  367. return this.get('controller.inputFields.method.value') === 'EMAIL';
  368. }.property('controller.inputFields.method.value'),
  369. methodObserver: function () {
  370. var currentMethod = this.get('controller.inputFields.method.value'),
  371. validationMap = self.get('validationMap');
  372. self.get('methods').forEach(function (method) {
  373. var validations = validationMap[method];
  374. if (method == currentMethod) {
  375. validations.mapProperty('validator').forEach(function (key) {
  376. this.get(key).call(this);
  377. }, this);
  378. } else {
  379. validations.mapProperty('errorKey').forEach(function (key) {
  380. this.set(key, false);
  381. }, this);
  382. }
  383. }, this);
  384. }.observes('controller.inputFields.method.value'),
  385. nameValidation: function () {
  386. var newName = this.get('controller.inputFields.name.value').trim();
  387. var errorMessage = '';
  388. // on editing, save current notification name
  389. if (newName && !this.get('currentName')) {
  390. this.set('currentName', newName);
  391. }
  392. if (isEdit) {
  393. // edit current alert notification
  394. if (!newName) {
  395. this.set('nameError', true);
  396. errorMessage = Em.I18n.t('alerts.actions.manage_alert_notifications_popup.error.name.empty');
  397. } else if (newName && newName != this.get('currentName') && self.get('alertNotifications').mapProperty('name').contains(newName)) {
  398. this.set('nameError', true);
  399. errorMessage = Em.I18n.t('alerts.actions.manage_alert_notifications_popup.error.name.existed');
  400. } else {
  401. this.set('nameError', false);
  402. }
  403. } else {
  404. // add new alert notification
  405. if (!newName) {
  406. this.set('nameError', true);
  407. errorMessage = Em.I18n.t('alerts.actions.manage_alert_notifications_popup.error.name.empty');
  408. } else if (newName && self.get('alertNotifications').mapProperty('name').contains(newName)) {
  409. this.set('nameError', true);
  410. errorMessage = Em.I18n.t('alerts.actions.manage_alert_notifications_popup.error.name.existed');
  411. } else {
  412. this.set('nameError', false);
  413. }
  414. }
  415. this.set('controller.inputFields.name.errorMsg', errorMessage);
  416. }.observes('controller.inputFields.name.value'),
  417. emailToValidation: function () {
  418. var emailToError = false;
  419. if (this.get('isEmailMethodSelected')) {
  420. var inputValues = this.get('controller.inputFields.email.value').trim().split(',');
  421. emailToError = inputValues.some(function(emailTo) {
  422. return emailTo && !validator.isValidEmail(emailTo.trim());
  423. })
  424. }
  425. this.set('emailToError', emailToError);
  426. this.set('controller.inputFields.email.errorMsg', emailToError ? Em.I18n.t('alerts.notifications.error.email') : null);
  427. }.observes('controller.inputFields.email.value'),
  428. emailFromValidation: function () {
  429. var emailFrom = this.get('controller.inputFields.emailFrom.value');
  430. if (emailFrom && !validator.isValidEmail(emailFrom)) {
  431. this.set('emailFromError', true);
  432. this.set('controller.inputFields.emailFrom.errorMsg', Em.I18n.t('alerts.notifications.error.email'));
  433. } else {
  434. this.set('emailFromError', false);
  435. this.set('controller.inputFields.emailFrom.errorMsg', null);
  436. }
  437. }.observes('controller.inputFields.emailFrom.value'),
  438. smtpPortValidation: function () {
  439. var value = this.get('controller.inputFields.SMTPPort.value');
  440. if (value && (!validator.isValidInt(value) || value < 0)) {
  441. this.set('smtpPortError', true);
  442. this.set('controller.inputFields.SMTPPort.errorMsg', Em.I18n.t('alerts.notifications.error.integer'));
  443. } else {
  444. this.set('smtpPortError', false);
  445. this.set('controller.inputFields.SMTPPort.errorMsg', null);
  446. }
  447. }.observes('controller.inputFields.SMTPPort.value'),
  448. hostsValidation: function() {
  449. var inputValue = this.get('controller.inputFields.host.value').trim(),
  450. hostError = false;;
  451. if (!this.get('isEmailMethodSelected')) {
  452. var array = inputValue.split(',');
  453. hostError = array.some(function(hostname) {
  454. return hostname && !validator.isHostname(hostname.trim());
  455. });
  456. hostError = hostError || inputValue==='';
  457. }
  458. this.set('hostError', hostError);
  459. this.set('controller.inputFields.host.errorMsg', hostError ? Em.I18n.t('alerts.notifications.error.host') : null);
  460. }.observes('controller.inputFields.host.value'),
  461. portValidation: function () {
  462. var value = this.get('controller.inputFields.port.value');
  463. if (value && (!validator.isValidInt(value) || value < 0)) {
  464. this.set('portError', true);
  465. this.set('controller.inputFields.port.errorMsg', Em.I18n.t('alerts.notifications.error.integer'));
  466. } else {
  467. this.set('portError', false);
  468. this.set('controller.inputFields.port.errorMsg', null);
  469. }
  470. }.observes('controller.inputFields.port.value'),
  471. retypePasswordValidation: function () {
  472. var passwordValue = this.get('controller.inputFields.SMTPPassword.value');
  473. var retypePasswordValue = this.get('controller.inputFields.retypeSMTPPassword.value');
  474. if (passwordValue !== retypePasswordValue) {
  475. this.set('passwordError', true);
  476. this.set('controller.inputFields.retypeSMTPPassword.errorMsg', Em.I18n.t('alerts.notifications.error.retypePassword'));
  477. } else {
  478. this.set('passwordError', false);
  479. this.set('controller.inputFields.retypeSMTPPassword.errorMsg', null);
  480. }
  481. }.observes('controller.inputFields.retypeSMTPPassword.value', 'controller.inputFields.SMTPPassword.value'),
  482. setParentErrors: function () {
  483. var hasErrors = this.get('nameError') || this.get('emailToError') || this.get('emailFromError') ||
  484. this.get('smtpPortError') || this.get('hostError') || this.get('portError') || this.get('passwordError');
  485. this.set('parentView.hasErrors', hasErrors);
  486. }.observes('nameError', 'emailToError', 'emailFromError', 'smtpPortError', 'hostError', 'portError', 'passwordError'),
  487. groupsSelectView: Em.Select.extend({
  488. attributeBindings: ['disabled'],
  489. init: function () {
  490. this._super();
  491. this.set('parentView.groupSelect', this);
  492. }
  493. }),
  494. groupSelect: null,
  495. /**
  496. * Select all alert-groups if <code>allGroups.value</code> is 'custom'
  497. * @method selectAllGroups
  498. */
  499. selectAllGroups: function () {
  500. if (this.get('controller.inputFields.allGroups.value') == 'custom') {
  501. this.set('groupSelect.selection', this.get('groupSelect.content').slice());
  502. }
  503. },
  504. /**
  505. * Deselect all alert-groups if <code>allGroups.value</code> is 'custom'
  506. * @method clearAllGroups
  507. */
  508. clearAllGroups: function () {
  509. if (this.get('controller.inputFields.allGroups.value') == 'custom') {
  510. this.set('groupSelect.selection', []);
  511. }
  512. },
  513. severitySelectView: Em.Select.extend({
  514. init: function () {
  515. this._super();
  516. this.set('parentView.severitySelect', this);
  517. }
  518. }),
  519. severitySelect: null,
  520. /**
  521. * Determines if all alert-groups are selected
  522. * @type {boolean}
  523. */
  524. allGroupsSelected: Em.computed.equalProperties('groupSelect.selection.length', 'groupSelect.content.length'),
  525. /**
  526. * Determines if no one alert-group is selected
  527. * @type {boolean}
  528. */
  529. noneGroupsSelected: Em.computed.empty('groupSelect.selection'),
  530. /**
  531. * Determines if all severities are selected
  532. * @type {boolean}
  533. */
  534. allSeveritySelected: Em.computed.equalProperties('severitySelect.selection.length', 'severitySelect.content.length'),
  535. /**
  536. * Determines if no one severity is selected
  537. * @type {boolean}
  538. */
  539. noneSeveritySelected: Em.computed.empty('severitySelect.selection'),
  540. /**
  541. * Select all severities
  542. * @method selectAllSeverity
  543. */
  544. selectAllSeverity: function () {
  545. this.set('severitySelect.selection', this.get('severitySelect.content').slice());
  546. },
  547. /**
  548. * Deselect all severities
  549. * @method clearAllSeverity
  550. */
  551. clearAllSeverity: function () {
  552. this.set('severitySelect.selection', []);
  553. }
  554. }),
  555. isSaving: false,
  556. hasErrors: false,
  557. primary: Em.I18n.t('common.save'),
  558. disablePrimary: function () {
  559. return this.get('isSaving') || this.get('hasErrors');
  560. }.property('isSaving', 'hasErrors'),
  561. onPrimary: function () {
  562. this.set('isSaving', true);
  563. var apiObject = self.formatNotificationAPIObject();
  564. if (isEdit) {
  565. self.updateAlertNotification(apiObject);
  566. } else {
  567. self.createAlertNotification(apiObject);
  568. }
  569. }
  570. });
  571. this.set('createEditPopup', createEditPopup);
  572. return createEditPopup;
  573. },
  574. /**
  575. * Create API-formatted object from data populate by user
  576. * @returns {Object}
  577. * @method formatNotificationAPIObject
  578. */
  579. formatNotificationAPIObject: function () {
  580. var inputFields = this.get('inputFields');
  581. var properties = {};
  582. if (inputFields.get('method.value') === 'EMAIL') {
  583. properties['ambari.dispatch.recipients'] = inputFields.get('email.value').replace(/\s/g, '').split(',');
  584. properties['mail.smtp.host'] = inputFields.get('SMTPServer.value');
  585. properties['mail.smtp.port'] = inputFields.get('SMTPPort.value');
  586. properties['mail.smtp.from'] = inputFields.get('emailFrom.value');
  587. properties['mail.smtp.auth'] = inputFields.get('SMTPUseAuthentication.value');
  588. if (inputFields.get('SMTPUseAuthentication.value')) {
  589. properties['ambari.dispatch.credential.username'] = inputFields.get('SMTPUsername.value');
  590. properties['ambari.dispatch.credential.password'] = inputFields.get('SMTPPassword.value');
  591. properties['mail.smtp.starttls.enable'] = inputFields.get('SMTPSTARTTLS.value');
  592. }
  593. } else {
  594. properties['ambari.dispatch.snmp.version'] = inputFields.get('version.value');
  595. properties['ambari.dispatch.snmp.oids.trap'] = inputFields.get('OIDs.value');
  596. properties['ambari.dispatch.snmp.oids.subject'] = inputFields.get('OIDs.value');
  597. properties['ambari.dispatch.snmp.oids.body'] = inputFields.get('OIDs.value');
  598. properties['ambari.dispatch.snmp.community'] = inputFields.get('community.value');
  599. properties['ambari.dispatch.recipients'] = inputFields.get('host.value').replace(/\s/g, '').split(',');
  600. properties['ambari.dispatch.snmp.port'] = inputFields.get('port.value');
  601. }
  602. inputFields.get('customProperties').forEach(function (customProperty) {
  603. properties[customProperty.name] = customProperty.value;
  604. });
  605. var apiObject = {
  606. AlertTarget: {
  607. name: inputFields.get('name.value'),
  608. description: inputFields.get('description.value'),
  609. global: inputFields.get('allGroups.value') === 'all',
  610. notification_type: inputFields.get('method.value'),
  611. alert_states: inputFields.get('severityFilter.value'),
  612. properties: properties
  613. }
  614. };
  615. if (inputFields.get('allGroups.value') == 'custom') {
  616. apiObject.AlertTarget.groups = inputFields.get('groups.value').mapProperty('id');
  617. }
  618. return apiObject;
  619. },
  620. /**
  621. * Send request to server to create Alert Notification
  622. * @param {object} apiObject (@see formatNotificationAPIObject)
  623. * @returns {$.ajax}
  624. * @method createAlertNotification
  625. */
  626. createAlertNotification: function (apiObject) {
  627. return App.ajax.send({
  628. name: 'alerts.create_alert_notification',
  629. sender: this,
  630. data: {
  631. data: apiObject
  632. },
  633. success: 'createAlertNotificationSuccessCallback',
  634. error: 'saveErrorCallback'
  635. });
  636. },
  637. /**
  638. * Success callback for <code>createAlertNotification</code>
  639. * @method createAlertNotificationSuccessCallback
  640. */
  641. createAlertNotificationSuccessCallback: function () {
  642. this.loadAlertNotifications();
  643. var createEditPopup = this.get('createEditPopup');
  644. if (createEditPopup) {
  645. createEditPopup.hide();
  646. }
  647. },
  648. /**
  649. * Send request to server to update Alert Notification
  650. * @param {object} apiObject (@see formatNotificationAPIObject)
  651. * @returns {$.ajax}
  652. * @method updateAlertNotification
  653. */
  654. updateAlertNotification: function (apiObject) {
  655. return App.ajax.send({
  656. name: 'alerts.update_alert_notification',
  657. sender: this,
  658. data: {
  659. data: apiObject,
  660. id: this.get('selectedAlertNotification.id')
  661. },
  662. success: 'updateAlertNotificationSuccessCallback',
  663. error: 'saveErrorCallback'
  664. });
  665. },
  666. /**
  667. * Success callback for <code>updateAlertNotification</code>
  668. * @method updateAlertNotificationSuccessCallback
  669. */
  670. updateAlertNotificationSuccessCallback: function () {
  671. this.loadAlertNotifications();
  672. var createEditPopup = this.get('createEditPopup');
  673. if (createEditPopup) {
  674. createEditPopup.hide();
  675. }
  676. },
  677. /**
  678. * Error callback for <code>createAlertNotification</code> and <code>updateAlertNotification</code>
  679. * @method saveErrorCallback
  680. */
  681. saveErrorCallback: function () {
  682. this.set('createEditPopup.isSaving', false);
  683. },
  684. /**
  685. * Delete Notification button handler
  686. * @return {App.ModalPopup}
  687. * @method deleteAlertNotification
  688. */
  689. deleteAlertNotification: function () {
  690. var self = this;
  691. return App.showConfirmationPopup(function () {
  692. App.ajax.send({
  693. name: 'alerts.delete_alert_notification',
  694. sender: self,
  695. data: {
  696. id: self.get('selectedAlertNotification.id')
  697. },
  698. success: 'deleteAlertNotificationSuccessCallback'
  699. });
  700. }, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteBody').format(this.get('selectedAlertNotification.name')),
  701. null, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteHeader'), Em.I18n.t('common.delete'));
  702. },
  703. /**
  704. * Success callback for <code>deleteAlertNotification</code>
  705. * @method deleteAlertNotificationSuccessCallback
  706. */
  707. deleteAlertNotificationSuccessCallback: function () {
  708. this.loadAlertNotifications();
  709. var selectedAlertNotification = this.get('selectedAlertNotification');
  710. selectedAlertNotification.deleteRecord();
  711. this.set('selectedAlertNotification', null);
  712. },
  713. /**
  714. * Duplicate Notification button handler
  715. * @method duplicateAlertNotification
  716. */
  717. duplicateAlertNotification: function () {
  718. this.fillEditCreateInputs(true);
  719. this.showCreateEditPopup();
  720. },
  721. /**
  722. * Show popup with form for new custom property
  723. * @method addCustomPropertyHandler
  724. * @return {App.ModalPopup}
  725. */
  726. addCustomPropertyHandler: function () {
  727. var self = this;
  728. return App.ModalPopup.show({
  729. header: Em.I18n.t('alerts.notifications.addCustomPropertyPopup.header'),
  730. primary: Em.I18n.t('common.add'),
  731. bodyClass: Em.View.extend({
  732. /**
  733. * If some error with new custom property
  734. * @type {boolean}
  735. */
  736. isError: false,
  737. controller: this,
  738. /**
  739. * Error message for new custom property (invalid name, existed name etc)
  740. * @type {string}
  741. */
  742. errorMessage: '',
  743. /**
  744. * Check new custom property for errors with its name
  745. * @method errorHandler
  746. */
  747. errorsHandler: function () {
  748. var name = this.get('controller.newCustomProperty.name');
  749. var flag = validator.isValidConfigKey(name);
  750. if (flag) {
  751. if (this.get('controller.inputFields.customProperties').mapProperty('name').contains(name) ||
  752. this.get('controller.ignoredCustomProperties').contains(name)) {
  753. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.propertyExists'));
  754. flag = false;
  755. }
  756. }
  757. else {
  758. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.invalidPropertyName'));
  759. }
  760. this.set('isError', !flag);
  761. this.set('parentView.disablePrimary', !flag);
  762. }.observes('controller.newCustomProperty.name'),
  763. templateName: require('templates/main/alerts/add_custom_config_to_alert_notification_popup')
  764. }),
  765. disablePrimary: true,
  766. onPrimary: function () {
  767. self.addCustomProperty();
  768. self.set('newCustomProperty', {name: '', value: ''}); // cleanup
  769. this.hide();
  770. }
  771. });
  772. },
  773. /**
  774. * Add Custom Property to <code>selectedAlertNotification</code> (push it to <code>properties</code>-field)
  775. * @method addCustomProperty
  776. */
  777. addCustomProperty: function () {
  778. var newCustomProperty = this.get('newCustomProperty');
  779. this.get('inputFields.customProperties').pushObject({
  780. name: newCustomProperty.name,
  781. value: newCustomProperty.value,
  782. defaultValue: newCustomProperty.value
  783. });
  784. },
  785. /**
  786. * "Remove"-button click handler
  787. * Delete customProperty from <code>inputFields.customProperties</code>
  788. * @param {object} e
  789. * @method removeCustomProperty
  790. */
  791. removeCustomPropertyHandler: function (e) {
  792. var customProperties = this.get('inputFields.customProperties');
  793. this.set('inputFields.customProperties', customProperties.without(e.context));
  794. }
  795. });