1ff64c54aSGunnar Mills/** 2ff64c54aSGunnar Mills * Controller for SNMP 3ff64c54aSGunnar Mills * 4ff64c54aSGunnar Mills * @module app/configuration 5ff64c54aSGunnar Mills * @exports snmpController 6ff64c54aSGunnar Mills * @name snmpController 7ff64c54aSGunnar Mills */ 8ff64c54aSGunnar Mills 9ff64c54aSGunnar Millswindow.angular && (function(angular) { 10ff64c54aSGunnar Mills 'use strict'; 11ff64c54aSGunnar Mills 12ff64c54aSGunnar Mills angular.module('app.configuration').controller('snmpController', [ 13*27ce84d2Sbeccabroek '$scope', '$window', 'APIUtils', '$route', '$q', 'toastService', 14*27ce84d2Sbeccabroek function($scope, $window, APIUtils, $route, $q, toastService) { 15854fbba1SGunnar Mills $scope.managers = []; 16ff64c54aSGunnar Mills $scope.loading = true; 1735d18cbcSGunnar Mills $scope.managersToDelete = []; 18ff64c54aSGunnar Mills 19ff64c54aSGunnar Mills var getSNMPManagers = APIUtils.getSNMPManagers().then( 20ff64c54aSGunnar Mills function(data) { 21854fbba1SGunnar Mills // Convert to array of objects from an object of objects, easier 22854fbba1SGunnar Mills // to manipulate (e.g. add/remove). Convert key to a path property. 23854fbba1SGunnar Mills for (var key in data.data) { 24854fbba1SGunnar Mills $scope.managers.push({ 25854fbba1SGunnar Mills path: key, 26854fbba1SGunnar Mills port: data.data[key].Port, 2735d18cbcSGunnar Mills updatePort: false, 288751084cSGunnar Mills address: data.data[key].Address, 2935d18cbcSGunnar Mills updateAddress: false 30854fbba1SGunnar Mills }) 31854fbba1SGunnar Mills } 32ff64c54aSGunnar Mills }, 33ff64c54aSGunnar Mills function(error) { 34*27ce84d2Sbeccabroek toastService.error('Unable to load SNMP settings.'); 35ff64c54aSGunnar Mills console.log(JSON.stringify(error)); 36ff64c54aSGunnar Mills }); 37ff64c54aSGunnar Mills 38ff64c54aSGunnar Mills getSNMPManagers.finally(function() { 39ff64c54aSGunnar Mills $scope.loading = false; 40ff64c54aSGunnar Mills }); 41854fbba1SGunnar Mills 42854fbba1SGunnar Mills $scope.addNewSNMPManager = function() { 43854fbba1SGunnar Mills $scope.managers.push({address: '', port: ''}); 44854fbba1SGunnar Mills }; 45854fbba1SGunnar Mills 46854fbba1SGunnar Mills $scope.removeSNMPManager = function(index) { 47854fbba1SGunnar Mills // If the SNMP Manager has a path it exists on the backend and we 48854fbba1SGunnar Mills // need to make a call to remove it 49854fbba1SGunnar Mills if ($scope.managers[index].path) { 5035d18cbcSGunnar Mills $scope.managersToDelete.push($scope.managers[index].path); 51854fbba1SGunnar Mills } 52854fbba1SGunnar Mills $scope.managers.splice(index, 1); 53854fbba1SGunnar Mills }; 54854fbba1SGunnar Mills 55854fbba1SGunnar Mills $scope.refresh = function() { 56854fbba1SGunnar Mills $route.reload(); 57854fbba1SGunnar Mills }; 58854fbba1SGunnar Mills 59854fbba1SGunnar Mills $scope.setSNMP = function() { 60854fbba1SGunnar Mills $scope.loading = true; 61854fbba1SGunnar Mills var promises = []; 62854fbba1SGunnar Mills 63854fbba1SGunnar Mills // Interate in reverse so can splice 64854fbba1SGunnar Mills // https://stackoverflow.com/questions/9882284/looping-through-array-and-removing-items-without-breaking-for-loop 65854fbba1SGunnar Mills var i = $scope.managers.length; 66854fbba1SGunnar Mills while (i--) { 67854fbba1SGunnar Mills // Remove any SNMP Manager with an empty address and port 68854fbba1SGunnar Mills if (!$scope.managers[i].address && !$scope.managers[i].port) { 69854fbba1SGunnar Mills $scope.removeSNMPManager(i); 70854fbba1SGunnar Mills continue; 71854fbba1SGunnar Mills } 72854fbba1SGunnar Mills 73854fbba1SGunnar Mills // Throw an error if only 1 of the fields is filled out 74854fbba1SGunnar Mills if (!$scope.managers[i].address || !$scope.managers[i].port) { 75854fbba1SGunnar Mills // TODO: Highlight the field that is empty 76854fbba1SGunnar Mills $scope.loading = false; 77*27ce84d2Sbeccabroek toastService.error('All fields are required.'); 78854fbba1SGunnar Mills return; 79854fbba1SGunnar Mills } 80854fbba1SGunnar Mills 81854fbba1SGunnar Mills // If the manager does not have a 'path', it is a new manager 82854fbba1SGunnar Mills // and needs to be created 83854fbba1SGunnar Mills if (!$scope.managers[i].path) { 84854fbba1SGunnar Mills promises.push(addManager( 85854fbba1SGunnar Mills $scope.managers[i].address, $scope.managers[i].port)); 86854fbba1SGunnar Mills } else { 8735d18cbcSGunnar Mills if ($scope.managers[i].updateAddress) { 88854fbba1SGunnar Mills promises.push(setManagerAddress( 89854fbba1SGunnar Mills $scope.managers[i].path, $scope.managers[i].address)); 908751084cSGunnar Mills } 9135d18cbcSGunnar Mills if ($scope.managers[i].updatePort) { 92854fbba1SGunnar Mills promises.push(setManagerPort( 93854fbba1SGunnar Mills $scope.managers[i].path, $scope.managers[i].port)); 94854fbba1SGunnar Mills } 95854fbba1SGunnar Mills } 968751084cSGunnar Mills } 97854fbba1SGunnar Mills 98854fbba1SGunnar Mills // Add delete promises last since we might be adding to 9935d18cbcSGunnar Mills // managersToDelete above 10035d18cbcSGunnar Mills for (var i in $scope.managersToDelete) { 10135d18cbcSGunnar Mills promises.push(deleteManager($scope.managersToDelete[i])); 102854fbba1SGunnar Mills } 103854fbba1SGunnar Mills 104854fbba1SGunnar Mills $q.all(promises) 105854fbba1SGunnar Mills .then( 106854fbba1SGunnar Mills function() { 107*27ce84d2Sbeccabroek toastService.success('SNMP Managers set.'); 108854fbba1SGunnar Mills }, 109854fbba1SGunnar Mills function(errors) { 110*27ce84d2Sbeccabroek toastService.error('Unable to set SNMP Managers.'); 111854fbba1SGunnar Mills console.log(JSON.stringify(errors)); 112854fbba1SGunnar Mills }) 113854fbba1SGunnar Mills .finally(function() { 114854fbba1SGunnar Mills $scope.loading = false; 115854fbba1SGunnar Mills }); 116854fbba1SGunnar Mills }; 117854fbba1SGunnar Mills 118854fbba1SGunnar Mills function addManager(address, port) { 119854fbba1SGunnar Mills return APIUtils.addSNMPManager(address, port); 120854fbba1SGunnar Mills } 121854fbba1SGunnar Mills 122854fbba1SGunnar Mills function deleteManager(path) { 123854fbba1SGunnar Mills return APIUtils.deleteObject(path); 124854fbba1SGunnar Mills } 125854fbba1SGunnar Mills 126854fbba1SGunnar Mills function setManagerAddress(path, address) { 127854fbba1SGunnar Mills return APIUtils.setSNMPManagerAddress(path, address); 128854fbba1SGunnar Mills } 129854fbba1SGunnar Mills 130854fbba1SGunnar Mills function setManagerPort(path, port) { 131854fbba1SGunnar Mills return APIUtils.setSNMPManagerPort(path, port); 132854fbba1SGunnar Mills } 133ff64c54aSGunnar Mills } 134ff64c54aSGunnar Mills ]); 135ff64c54aSGunnar Mills})(angular); 136