blob: 67e210f37a01c4f0c8be09cb5b4e8e994439d876 [file] [log] [blame]
Gunnar Millsff64c542018-08-24 15:49:09 -05001/**
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', [
beccabroek27ce84d2019-02-05 15:43:17 -060013 '$scope', '$window', 'APIUtils', '$route', '$q', 'toastService',
14 function($scope, $window, APIUtils, $route, $q, toastService) {
Gunnar Mills854fbba2018-09-13 15:41:30 -050015 $scope.managers = [];
Gunnar Millsff64c542018-08-24 15:49:09 -050016 $scope.loading = true;
Gunnar Mills35d18cb2018-10-05 11:24:14 -050017 $scope.managersToDelete = [];
Gunnar Millsff64c542018-08-24 15:49:09 -050018
19 var getSNMPManagers = APIUtils.getSNMPManagers().then(
20 function(data) {
Gunnar Mills854fbba2018-09-13 15:41:30 -050021 // 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 Mills35d18cb2018-10-05 11:24:14 -050027 updatePort: false,
Gunnar Mills87510842018-09-18 15:49:33 -050028 address: data.data[key].Address,
Gunnar Mills35d18cb2018-10-05 11:24:14 -050029 updateAddress: false
Gunnar Mills854fbba2018-09-13 15:41:30 -050030 })
31 }
Gunnar Millsff64c542018-08-24 15:49:09 -050032 },
33 function(error) {
beccabroek27ce84d2019-02-05 15:43:17 -060034 toastService.error('Unable to load SNMP settings.');
Gunnar Millsff64c542018-08-24 15:49:09 -050035 console.log(JSON.stringify(error));
36 });
37
38 getSNMPManagers.finally(function() {
39 $scope.loading = false;
40 });
Gunnar Mills854fbba2018-09-13 15:41:30 -050041
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 Mills35d18cb2018-10-05 11:24:14 -050050 $scope.managersToDelete.push($scope.managers[index].path);
Gunnar Mills854fbba2018-09-13 15:41:30 -050051 }
52 $scope.managers.splice(index, 1);
53 };
54
55 $scope.refresh = function() {
56 $route.reload();
57 };
58
59 $scope.setSNMP = function() {
Gunnar Mills854fbba2018-09-13 15:41:30 -050060 $scope.loading = true;
61 var promises = [];
62
beccabroekddda4de2019-01-28 15:17:44 -060063 // Validate that no field are empty and port is valid. Port value is
64 // undefined if it is an invalid number.
65 for (var i in $scope.managers) {
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 Mills854fbba2018-09-13 15:41:30 -050073 // https://stackoverflow.com/questions/9882284/looping-through-array-and-removing-items-without-breaking-for-loop
74 var i = $scope.managers.length;
75 while (i--) {
Gunnar Mills854fbba2018-09-13 15:41:30 -050076 // If the manager does not have a 'path', it is a new manager
77 // and needs to be created
78 if (!$scope.managers[i].path) {
79 promises.push(addManager(
80 $scope.managers[i].address, $scope.managers[i].port));
81 } else {
Gunnar Mills35d18cb2018-10-05 11:24:14 -050082 if ($scope.managers[i].updateAddress) {
Gunnar Mills87510842018-09-18 15:49:33 -050083 promises.push(setManagerAddress(
84 $scope.managers[i].path, $scope.managers[i].address));
85 }
Gunnar Mills35d18cb2018-10-05 11:24:14 -050086 if ($scope.managers[i].updatePort) {
Gunnar Mills87510842018-09-18 15:49:33 -050087 promises.push(setManagerPort(
88 $scope.managers[i].path, $scope.managers[i].port));
89 }
Gunnar Mills854fbba2018-09-13 15:41:30 -050090 }
91 }
92
93 // Add delete promises last since we might be adding to
Gunnar Mills35d18cb2018-10-05 11:24:14 -050094 // managersToDelete above
95 for (var i in $scope.managersToDelete) {
96 promises.push(deleteManager($scope.managersToDelete[i]));
Gunnar Mills854fbba2018-09-13 15:41:30 -050097 }
98
99 $q.all(promises)
100 .then(
101 function() {
beccabroekddda4de2019-01-28 15:17:44 -0600102 toastService.success('SNMP settings have been saved.');
Gunnar Mills854fbba2018-09-13 15:41:30 -0500103 },
104 function(errors) {
beccabroek27ce84d2019-02-05 15:43:17 -0600105 toastService.error('Unable to set SNMP Managers.');
Gunnar Mills854fbba2018-09-13 15:41:30 -0500106 console.log(JSON.stringify(errors));
Gunnar Mills854fbba2018-09-13 15:41:30 -0500107 })
108 .finally(function() {
109 $scope.loading = false;
110 });
111 };
112
113 function addManager(address, port) {
114 return APIUtils.addSNMPManager(address, port);
115 }
116
117 function deleteManager(path) {
118 return APIUtils.deleteObject(path);
119 }
120
121 function setManagerAddress(path, address) {
122 return APIUtils.setSNMPManagerAddress(path, address);
123 }
124
125 function setManagerPort(path, port) {
126 return APIUtils.setSNMPManagerPort(path, port);
127 }
Gunnar Millsff64c542018-08-24 15:49:09 -0500128 }
129 ]);
130})(angular);