123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648 |
- /**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var App = require('app');
- var validator = require('utils/validator');
- App.ManageAlertNotificationsController = Em.Controller.extend({
- name: 'manageAlertNotificationsController',
- /**
- * Are alert notifications loaded
- * @type {boolean}
- */
- isLoaded: false,
- /**
- * Create/Edit modal popup object
- * used to hide popup
- * @type {App.ModalPopup}
- */
- createEditPopup: null,
- /**
- * Map of edit inputs shown in Create/Edit Notification popup
- * @type {Object}
- */
- inputFields: Em.Object.create({
- name: {
- label: Em.I18n.t('common.name'),
- value: '',
- defaultValue: ''
- },
- groups: {
- label: Em.I18n.t('common.groups'),
- value: [],
- defaultValue: []
- },
- global: {
- label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.global'),
- value: false,
- defaultValue: false,
- disabled: false
- },
- method: {
- label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.method'),
- value: '',
- defaultValue: ''
- },
- email: {
- label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.email'),
- value: '',
- defaultValue: ''
- },
- SMTPServer: {
- label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPServer'),
- value: '',
- defaultValue: ''
- },
- SMTPPort: {
- label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPPort'),
- value: '',
- defaultValue: ''
- },
- SMTPUseAuthentication: Em.Object.create({
- label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPUseAuthentication'),
- value: false,
- defaultValue: false,
- inversedValue: function () {
- return !this.get('value');
- }.property('value')
- }),
- SMTPUsername: {
- label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPUsername'),
- value: '',
- defaultValue: ''
- },
- SMTPPassword: {
- label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPPassword'),
- value: '',
- defaultValue: ''
- },
- SMTPSTARTTLS: {
- label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPSTARTTLS'),
- value: false,
- defaultValue: false
- },
- emailFrom: {
- label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.emailFrom'),
- value: '',
- defaultValue: ''
- },
- version: {
- label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.version'),
- value: '',
- defaultValue: ''
- },
- OIDs: {
- label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.OIDs'),
- value: '',
- defaultValue: ''
- },
- community: {
- label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.community'),
- value: '',
- defaultValue: ''
- },
- port: {
- label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.port'),
- value: '',
- defaultValue: ''
- },
- severityFilter: {
- label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.severityFilter'),
- value: [],
- defaultValue: []
- },
- description: {
- label: Em.I18n.t('common.description'),
- value: '',
- defaultValue: ''
- },
- customProperties: Em.A([])
- }),
- /**
- * List of available Notification types
- * used in Type combobox
- * @type {Array}
- */
- methods: ['EMAIL', 'SNMP'],
- /**
- * List of available value for Severity Filter
- * used in Severity Filter combobox
- * @type {Array}
- */
- severities: ['OK', 'WARNING', 'CRITICAL', 'UNKNOWN'],
- /**
- * List of available SNMP versions
- * @type {Array}
- */
- SNMPVersions: ['SNMPv1', 'SNMPv2c', 'SNMPv3'],
- /**
- * List of all Alert Notifications
- * @type {App.AlertNotification[]}
- */
- alertNotifications: function () {
- return this.get('isLoaded') ? App.AlertNotification.find().toArray() : [];
- }.property('isLoaded'),
- /**
- * List of all Alert Groups
- * @type {App.AlertGroup[]}
- */
- allAlertGroups: function () {
- return this.get('isLoaded') ? App.AlertGroup.find().toArray() : [];
- }.property('isLoaded'),
- /**
- * Selected Alert Notification
- * @type {App.AlertNotification}
- */
- selectedAlertNotification: null,
- /**
- * Addable to <code>selectedAlertNotification.properties</code> custom property
- * @type {{name: string, value: string}}
- */
- newCustomProperty: {name: '', value: ''},
- /**
- * List custom property names that shouldn't be displayed on Edit page
- * @type {string[]}
- */
- ignoredCustomProperties: [
- 'ambari.dispatch.credential.password',
- 'ambari.dispatch.credential.username',
- 'ambari.dispatch.recipients',
- 'ambari.dispatch.snmp.community',
- 'ambari.dispatch.snmp.oids.trap',
- 'ambari.dispatch.snmp.port',
- 'ambari.dispatch.snmp.version',
- 'mail.smtp.auth',
- 'mail.smtp.from',
- 'mail.smtp.host',
- 'mail.smtp.port',
- 'mail.smtp.starttls.enable'
- ],
- /**
- * Load all Alert Notifications from server
- * @method loadAlertNotifications
- */
- loadAlertNotifications: function () {
- var self = this;
- this.set('isLoaded', false);
- App.router.get('updateController').updateAlertGroups(function () {
- App.ajax.send({
- name: 'alerts.notifications',
- sender: self,
- success: 'getAlertNotificationsSuccessCallback',
- error: 'getAlertNotificationsErrorCallback'
- });
- });
- },
- /**
- * Success-callback for load alert notifications request
- * @param {object} json
- * @method getAlertNotificationsSuccessCallback
- */
- getAlertNotificationsSuccessCallback: function (json) {
- App.alertNotificationMapper.map(json);
- this.set('isLoaded', true);
- },
- /**
- * Error-callback for load alert notifications request
- * @method getAlertNotificationsErrorCallback
- */
- getAlertNotificationsErrorCallback: function () {
- this.set('isLoaded', true);
- },
- /**
- * Add Notification button handler
- * @method addAlertNotification
- */
- addAlertNotification: function () {
- var inputFields = this.get('inputFields');
- inputFields.set('global.disabled', false);
- Em.keys(inputFields).forEach(function (key) {
- inputFields.set(key + '.value', inputFields.get(key + '.defaultValue'));
- });
- this.showCreateEditPopup(false);
- },
- /**
- * Edit Notification button handler
- * @method editAlertNotification
- */
- editAlertNotification: function () {
- this.fillEditCreateInputs();
- this.showCreateEditPopup(true);
- },
- /**
- * Fill inputs of Create/Edit popup form
- * @param addCopyToName define whether add 'Copy of ' to name
- * @method fillEditCreateInputs
- */
- fillEditCreateInputs: function (addCopyToName) {
- var inputFields = this.get('inputFields');
- var selectedAlertNotification = this.get('selectedAlertNotification');
- inputFields.set('name.value', (addCopyToName ? 'Copy of ' : '') + selectedAlertNotification.get('name'));
- inputFields.set('groups.value', selectedAlertNotification.get('groups').toArray());
- inputFields.set('email.value', selectedAlertNotification.get('properties')['ambari.dispatch.recipients'] ?
- selectedAlertNotification.get('properties')['ambari.dispatch.recipients'].join(', ') : '');
- inputFields.set('SMTPServer.value', selectedAlertNotification.get('properties')['mail.smtp.host']);
- inputFields.set('SMTPPort.value', selectedAlertNotification.get('properties')['mail.smtp.port']);
- inputFields.set('SMTPUseAuthentication.value', selectedAlertNotification.get('properties')['mail.smtp.auth']);
- inputFields.set('SMTPUsername.value', selectedAlertNotification.get('properties')['ambari.dispatch.credential.username']);
- inputFields.set('SMTPPassword.value', selectedAlertNotification.get('properties')['ambari.dispatch.credential.password']);
- inputFields.set('SMTPSTARTTLS.value', selectedAlertNotification.get('properties')['mail.smtp.starttls.enable']);
- inputFields.set('emailFrom.value', selectedAlertNotification.get('properties')['mail.smtp.from']);
- inputFields.set('version.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.version']);
- inputFields.set('OIDs.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.oids.trap']);
- inputFields.set('community.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.community']);
- inputFields.set('port.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.port']);
- inputFields.set('severityFilter.value', selectedAlertNotification.get('alertStates'));
- inputFields.set('global.value', selectedAlertNotification.get('global'));
- // not allow to edit global field
- inputFields.set('global.disabled', true);
- inputFields.set('description.value', selectedAlertNotification.get('description'));
- inputFields.set('method.value', selectedAlertNotification.get('type'));
- inputFields.get('customProperties').clear();
- var properties = selectedAlertNotification.get('properties');
- var ignoredCustomProperties = this.get('ignoredCustomProperties');
- Em.keys(properties).forEach(function (k) {
- if (ignoredCustomProperties.contains(k)) return;
- inputFields.get('customProperties').pushObject({
- name: k,
- value: properties[k],
- defaultValue: properties[k]
- });
- });
- },
- /**
- * Show Edit or Create Notification popup
- * @param {boolean} isEdit true - edit, false - create
- * @returns {App.ModalPopup}
- * @method showCreateEditPopup
- */
- showCreateEditPopup: function (isEdit) {
- var self = this;
- var createEditPopup = App.ModalPopup.show({
- header: isEdit ? Em.I18n.t('alerts.actions.manage_alert_notifications_popup.editHeader') : Em.I18n.t('alerts.actions.manage_alert_notifications_popup.addHeader'),
- classNames: ['create-edit-alert-notification-popup'],
- bodyClass: Em.View.extend({
- controller: this,
- templateName: require('templates/main/alerts/create_alert_notification'),
- /**
- * @type {string}
- */
- tooltipForGlobalCheckbox: function () {
- return isEdit ? '' : Em.I18n.t('alerts.actions.manage_alert_notifications_popup.global.tooltip');
- }.property(),
- didInsertElement: function () {
- App.tooltip($('.checkbox-tooltip'));
- this.nameValidation();
- },
- isEmailMethodSelected: function () {
- return this.get('controller.inputFields.method.value') === 'EMAIL';
- }.property('controller.inputFields.method.value'),
- nameValidation: function () {
- this.set('parentView.hasErrors', !this.get('controller.inputFields.name.value').trim());
- }.observes('controller.inputFields.name.value'),
- groupsSelectView: Em.Select.extend({
- attributeBindings: ['disabled'],
- init: function () {
- this._super();
- this.set('parentView.groupSelect', this);
- }
- }),
- groupSelect: null,
- selectAllGroups: function () {
- if (!this.get('controller.inputFields.global.value')) {
- this.set('groupSelect.selection', this.get('groupSelect.content').slice());
- }
- },
- clearAllGroups: function () {
- if (!this.get('controller.inputFields.global.value')) {
- this.set('groupSelect.selection', []);
- }
- },
- severitySelectView: Em.Select.extend({
- init: function () {
- this._super();
- this.set('parentView.severitySelect', this);
- }
- }),
- severitySelect: null,
- selectAllSeverity: function () {
- this.set('severitySelect.selection', this.get('severitySelect.content').slice());
- },
- clearAllSeverity: function () {
- this.set('severitySelect.selection', []);
- }
- }),
- isSaving: false,
- hasErrors: false,
- primary: Em.I18n.t('common.save'),
- disablePrimary: function () {
- return this.get('isSaving') || this.get('hasErrors');
- }.property('isSaving', 'hasErrors'),
- onPrimary: function () {
- this.set('isSaving', true);
- var apiObject = self.formatNotificationAPIObject();
- if (isEdit) {
- self.updateAlertNotification(apiObject);
- } else {
- self.createAlertNotification(apiObject);
- }
- }
- });
- this.set('createEditPopup', createEditPopup);
- return createEditPopup;
- },
- /**
- * Create API-formatted object from data populate by user
- * @returns {Object}
- * @method formatNotificationAPIObject
- */
- formatNotificationAPIObject: function () {
- var inputFields = this.get('inputFields');
- var properties = {};
- if (inputFields.get('method.value') === 'EMAIL') {
- properties['ambari.dispatch.recipients'] = inputFields.get('email.value').replace(/\s/g, '').split(',');
- properties['mail.smtp.host'] = inputFields.get('SMTPServer.value');
- properties['mail.smtp.port'] = inputFields.get('SMTPPort.value');
- properties['mail.smtp.from'] = inputFields.get('emailFrom.value');
- properties['mail.smtp.auth'] = inputFields.get('SMTPUseAuthentication.value');
- if (inputFields.get('SMTPUseAuthentication.value')) {
- properties['ambari.dispatch.credential.username'] = inputFields.get('SMTPUsername.value');
- properties['ambari.dispatch.credential.password'] = inputFields.get('SMTPPassword.value');
- properties['mail.smtp.starttls.enable'] = inputFields.get('SMTPSTARTTLS.value');
- }
- } else {
- properties['ambari.dispatch.snmp.version'] = inputFields.get('version.value');
- properties['ambari.dispatch.snmp.oids.trap'] = inputFields.get('OIDs.value');
- properties['ambari.dispatch.snmp.community'] = inputFields.get('community.value');
- properties['ambari.dispatch.snmp.port'] = inputFields.get('port.value');
- }
- inputFields.get('customProperties').forEach(function (customProperty) {
- properties[customProperty.name] = customProperty.value;
- });
- var apiObject = {
- AlertTarget: {
- name: inputFields.get('name.value'),
- description: inputFields.get('description.value'),
- global: inputFields.get('global.value'),
- notification_type: inputFields.get('method.value'),
- alert_states: inputFields.get('severityFilter.value'),
- properties: properties
- }
- };
- if (!inputFields.get('global.value')) {
- apiObject.AlertTarget.groups = inputFields.get('groups.value').mapProperty('id');
- }
- return apiObject;
- },
- /**
- * Send request to server to create Alert Notification
- * @param {object} apiObject (@see formatNotificationAPIObject)
- * @returns {$.ajax}
- * @method createAlertNotification
- */
- createAlertNotification: function (apiObject) {
- return App.ajax.send({
- name: 'alerts.create_alert_notification',
- sender: this,
- data: {
- data: apiObject
- },
- success: 'createAlertNotificationSuccessCallback',
- error: 'saveErrorCallback'
- });
- },
- /**
- * Success callback for <code>createAlertNotification</code>
- * @method createAlertNotificationSuccessCallback
- */
- createAlertNotificationSuccessCallback: function () {
- this.loadAlertNotifications();
- var createEditPopup = this.get('createEditPopup');
- if (createEditPopup) {
- createEditPopup.hide();
- }
- },
- /**
- * Send request to server to update Alert Notification
- * @param {object} apiObject (@see formatNotificationAPIObject)
- * @returns {$.ajax}
- * @method updateAlertNotification
- */
- updateAlertNotification: function (apiObject) {
- return App.ajax.send({
- name: 'alerts.update_alert_notification',
- sender: this,
- data: {
- data: apiObject,
- id: this.get('selectedAlertNotification.id')
- },
- success: 'updateAlertNotificationSuccessCallback',
- error: 'saveErrorCallback'
- });
- },
- /**
- * Success callback for <code>updateAlertNotification</code>
- * @method updateAlertNotificationSuccessCallback
- */
- updateAlertNotificationSuccessCallback: function () {
- this.loadAlertNotifications();
- var createEditPopup = this.get('createEditPopup');
- if (createEditPopup) {
- createEditPopup.hide();
- }
- },
- /**
- * Error callback for <code>createAlertNotification</code> and <code>updateAlertNotification</code>
- * @method saveErrorCallback
- */
- saveErrorCallback: function () {
- this.set('createEditPopup.isSaving', false);
- },
- /**
- * Delete Notification button handler
- * @return {App.ModalPopup}
- * @method deleteAlertNotification
- */
- deleteAlertNotification: function () {
- var self = this;
- return App.showConfirmationPopup(function () {
- App.ajax.send({
- name: 'alerts.delete_alert_notification',
- sender: self,
- data: {
- id: self.get('selectedAlertNotification.id')
- },
- success: 'deleteAlertNotificationSuccessCallback'
- });
- }, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteBody').format(this.get('selectedAlertNotification.name')),
- null, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteHeader'), Em.I18n.t('common.delete'));
- },
- /**
- * Success callback for <code>deleteAlertNotification</code>
- * @method deleteAlertNotificationSuccessCallback
- */
- deleteAlertNotificationSuccessCallback: function () {
- this.loadAlertNotifications();
- var selectedAlertNotification = this.get('selectedAlertNotification');
- selectedAlertNotification.deleteRecord();
- this.set('selectedAlertNotification', null);
- },
- /**
- * Duplicate Notification button handler
- * @method duplicateAlertNotification
- */
- duplicateAlertNotification: function () {
- this.fillEditCreateInputs(true);
- this.showCreateEditPopup();
- },
- /**
- * Show popup with form for new custom property
- * @method addCustomPropertyHandler
- * @return {App.ModalPopup}
- */
- addCustomPropertyHandler: function () {
- var self = this;
- return App.ModalPopup.show({
- header: Em.I18n.t('alerts.notifications.addCustomPropertyPopup.header'),
- primary: Em.I18n.t('common.add'),
- bodyClass: Em.View.extend({
- /**
- * If some error with new custom property
- * @type {boolean}
- */
- isError: false,
- controller: this,
- /**
- * Error message for new custom property (invalid name, existed name etc)
- * @type {string}
- */
- errorMessage: '',
- /**
- * Check new custom property for errors with its name
- * @method errorHandler
- */
- errorsHandler: function () {
- var name = this.get('controller.newCustomProperty.name');
- var flag = validator.isValidConfigKey(name);
- if (flag) {
- if (this.get('controller.inputFields.customProperties').mapProperty('name').contains(name) ||
- this.get('controller.ignoredCustomProperties').contains(name)) {
- this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.propertyExists'));
- flag = false;
- }
- }
- else {
- this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.invalidPropertyName'));
- }
- this.set('isError', !flag);
- this.set('parentView.disablePrimary', !flag);
- }.observes('controller.newCustomProperty.name'),
- templateName: require('templates/main/alerts/add_custom_config_to_alert_notification_popup')
- }),
- disablePrimary: true,
- onPrimary: function () {
- self.addCustomProperty();
- self.set('newCustomProperty', {name: '', value: ''}); // cleanup
- this.hide();
- }
- });
- },
- /**
- * Add Custom Property to <code>selectedAlertNotification</code> (push it to <code>properties</code>-field)
- * @method addCustomProperty
- */
- addCustomProperty: function () {
- var newCustomProperty = this.get('newCustomProperty');
- this.get('inputFields.customProperties').pushObject({
- name: newCustomProperty.name,
- value: newCustomProperty.value,
- defaultValue: newCustomProperty.value
- });
- },
- /**
- * "Remove"-button click handler
- * Delete customProperty from <code>inputFields.customProperties</code>
- * @param {object} e
- * @method removeCustomProperty
- */
- removeCustomPropertyHandler: function (e) {
- var customProperties = this.get('inputFields.customProperties');
- this.set('inputFields.customProperties', customProperties.without(e.context));
- }
- });
|