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