manage_alert_notifications_controller.js 32 KB

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