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