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*854fbba1SGunnar Mills '$scope', '$window', 'APIUtils', '$route', '$q', 14*854fbba1SGunnar Mills function($scope, $window, APIUtils, $route, $q) { 15*854fbba1SGunnar Mills $scope.managers = []; 16ff64c54aSGunnar Mills $scope.loading = true; 17*854fbba1SGunnar Mills $scope.error = false; 18*854fbba1SGunnar Mills $scope.success = false; 19*854fbba1SGunnar Mills $scope.managers_to_delete = []; 20ff64c54aSGunnar Mills 21ff64c54aSGunnar Mills var getSNMPManagers = APIUtils.getSNMPManagers().then( 22ff64c54aSGunnar Mills function(data) { 23*854fbba1SGunnar Mills // Convert to array of objects from an object of objects, easier 24*854fbba1SGunnar Mills // to manipulate (e.g. add/remove). Convert key to a path property. 25*854fbba1SGunnar Mills for (var key in data.data) { 26*854fbba1SGunnar Mills $scope.managers.push({ 27*854fbba1SGunnar Mills path: key, 28*854fbba1SGunnar Mills port: data.data[key].Port, 29*854fbba1SGunnar Mills address: data.data[key].Address 30*854fbba1SGunnar Mills }) 31*854fbba1SGunnar Mills } 32ff64c54aSGunnar Mills }, 33ff64c54aSGunnar Mills function(error) { 34ff64c54aSGunnar Mills console.log(JSON.stringify(error)); 35ff64c54aSGunnar Mills }); 36ff64c54aSGunnar Mills 37ff64c54aSGunnar Mills getSNMPManagers.finally(function() { 38ff64c54aSGunnar Mills $scope.loading = false; 39ff64c54aSGunnar Mills }); 40*854fbba1SGunnar Mills 41*854fbba1SGunnar Mills $scope.addNewSNMPManager = function() { 42*854fbba1SGunnar Mills $scope.managers.push({address: '', port: ''}); 43*854fbba1SGunnar Mills }; 44*854fbba1SGunnar Mills 45*854fbba1SGunnar Mills $scope.removeSNMPManager = function(index) { 46*854fbba1SGunnar Mills // If the SNMP Manager has a path it exists on the backend and we 47*854fbba1SGunnar Mills // need to make a call to remove it 48*854fbba1SGunnar Mills if ($scope.managers[index].path) { 49*854fbba1SGunnar Mills $scope.managers_to_delete.push($scope.managers[index].path); 50*854fbba1SGunnar Mills } 51*854fbba1SGunnar Mills $scope.managers.splice(index, 1); 52*854fbba1SGunnar Mills }; 53*854fbba1SGunnar Mills 54*854fbba1SGunnar Mills $scope.refresh = function() { 55*854fbba1SGunnar Mills $route.reload(); 56*854fbba1SGunnar Mills }; 57*854fbba1SGunnar Mills 58*854fbba1SGunnar Mills $scope.setSNMP = function() { 59*854fbba1SGunnar Mills $scope.error = false; 60*854fbba1SGunnar Mills $scope.success = false; 61*854fbba1SGunnar Mills $scope.loading = true; 62*854fbba1SGunnar Mills var promises = []; 63*854fbba1SGunnar Mills 64*854fbba1SGunnar Mills // Interate in reverse so can splice 65*854fbba1SGunnar Mills // https://stackoverflow.com/questions/9882284/looping-through-array-and-removing-items-without-breaking-for-loop 66*854fbba1SGunnar Mills var i = $scope.managers.length; 67*854fbba1SGunnar Mills while (i--) { 68*854fbba1SGunnar Mills // Remove any SNMP Manager with an empty address and port 69*854fbba1SGunnar Mills if (!$scope.managers[i].address && !$scope.managers[i].port) { 70*854fbba1SGunnar Mills $scope.removeSNMPManager(i); 71*854fbba1SGunnar Mills continue; 72*854fbba1SGunnar Mills } 73*854fbba1SGunnar Mills 74*854fbba1SGunnar Mills // Throw an error if only 1 of the fields is filled out 75*854fbba1SGunnar Mills if (!$scope.managers[i].address || !$scope.managers[i].port) { 76*854fbba1SGunnar Mills // TODO: Highlight the field that is empty 77*854fbba1SGunnar Mills $scope.loading = false; 78*854fbba1SGunnar Mills $scope.error = true; 79*854fbba1SGunnar Mills return; 80*854fbba1SGunnar Mills } 81*854fbba1SGunnar Mills 82*854fbba1SGunnar Mills // If the manager does not have a 'path', it is a new manager 83*854fbba1SGunnar Mills // and needs to be created 84*854fbba1SGunnar Mills if (!$scope.managers[i].path) { 85*854fbba1SGunnar Mills promises.push(addManager( 86*854fbba1SGunnar Mills $scope.managers[i].address, $scope.managers[i].port)); 87*854fbba1SGunnar Mills } else { 88*854fbba1SGunnar Mills // TODO: Check if we actually need to update the existing managers 89*854fbba1SGunnar Mills promises.push(setManagerAddress( 90*854fbba1SGunnar Mills $scope.managers[i].path, $scope.managers[i].address)); 91*854fbba1SGunnar Mills promises.push(setManagerPort( 92*854fbba1SGunnar Mills $scope.managers[i].path, $scope.managers[i].port)); 93*854fbba1SGunnar Mills } 94*854fbba1SGunnar Mills } 95*854fbba1SGunnar Mills 96*854fbba1SGunnar Mills // Add delete promises last since we might be adding to 97*854fbba1SGunnar Mills // managers_to_delete above 98*854fbba1SGunnar Mills for (var i in $scope.managers_to_delete) { 99*854fbba1SGunnar Mills promises.push(deleteManager($scope.managers_to_delete[i])); 100*854fbba1SGunnar Mills } 101*854fbba1SGunnar Mills 102*854fbba1SGunnar Mills $q.all(promises) 103*854fbba1SGunnar Mills .then( 104*854fbba1SGunnar Mills function() { 105*854fbba1SGunnar Mills $scope.success = true; 106*854fbba1SGunnar Mills }, 107*854fbba1SGunnar Mills function(errors) { 108*854fbba1SGunnar Mills console.log(JSON.stringify(errors)); 109*854fbba1SGunnar Mills $scope.error = true; 110*854fbba1SGunnar Mills }) 111*854fbba1SGunnar Mills .finally(function() { 112*854fbba1SGunnar Mills $scope.loading = false; 113*854fbba1SGunnar Mills }); 114*854fbba1SGunnar Mills }; 115*854fbba1SGunnar Mills 116*854fbba1SGunnar Mills function addManager(address, port) { 117*854fbba1SGunnar Mills return APIUtils.addSNMPManager(address, port); 118*854fbba1SGunnar Mills } 119*854fbba1SGunnar Mills 120*854fbba1SGunnar Mills function deleteManager(path) { 121*854fbba1SGunnar Mills return APIUtils.deleteObject(path); 122*854fbba1SGunnar Mills } 123*854fbba1SGunnar Mills 124*854fbba1SGunnar Mills function setManagerAddress(path, address) { 125*854fbba1SGunnar Mills return APIUtils.setSNMPManagerAddress(path, address); 126*854fbba1SGunnar Mills } 127*854fbba1SGunnar Mills 128*854fbba1SGunnar Mills function setManagerPort(path, port) { 129*854fbba1SGunnar Mills return APIUtils.setSNMPManagerPort(path, port); 130*854fbba1SGunnar Mills } 131ff64c54aSGunnar Mills } 132ff64c54aSGunnar Mills ]); 133ff64c54aSGunnar Mills})(angular); 134