blob: e2c2b225a44b9996cdf9bbae4d9f372bf86bd45b [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', [
Gunnar Mills854fbba2018-09-13 15:41:30 -050013 '$scope', '$window', 'APIUtils', '$route', '$q',
14 function($scope, $window, APIUtils, $route, $q) {
15 $scope.managers = [];
Gunnar Millsff64c542018-08-24 15:49:09 -050016 $scope.loading = true;
Gunnar Mills854fbba2018-09-13 15:41:30 -050017 $scope.error = false;
18 $scope.success = false;
Gunnar Mills35d18cb2018-10-05 11:24:14 -050019 $scope.managersToDelete = [];
Gunnar Millsff64c542018-08-24 15:49:09 -050020
21 var getSNMPManagers = APIUtils.getSNMPManagers().then(
22 function(data) {
Gunnar Mills854fbba2018-09-13 15:41:30 -050023 // 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,
Gunnar Mills35d18cb2018-10-05 11:24:14 -050029 updatePort: false,
Gunnar Mills87510842018-09-18 15:49:33 -050030 address: data.data[key].Address,
Gunnar Mills35d18cb2018-10-05 11:24:14 -050031 updateAddress: false
Gunnar Mills854fbba2018-09-13 15:41:30 -050032 })
33 }
Gunnar Millsff64c542018-08-24 15:49:09 -050034 },
35 function(error) {
36 console.log(JSON.stringify(error));
37 });
38
39 getSNMPManagers.finally(function() {
40 $scope.loading = false;
41 });
Gunnar Mills854fbba2018-09-13 15:41:30 -050042
43 $scope.addNewSNMPManager = function() {
44 $scope.managers.push({address: '', port: ''});
45 };
46
47 $scope.removeSNMPManager = function(index) {
48 // If the SNMP Manager has a path it exists on the backend and we
49 // need to make a call to remove it
50 if ($scope.managers[index].path) {
Gunnar Mills35d18cb2018-10-05 11:24:14 -050051 $scope.managersToDelete.push($scope.managers[index].path);
Gunnar Mills854fbba2018-09-13 15:41:30 -050052 }
53 $scope.managers.splice(index, 1);
54 };
55
56 $scope.refresh = function() {
57 $route.reload();
58 };
59
60 $scope.setSNMP = function() {
61 $scope.error = false;
62 $scope.success = false;
63 $scope.loading = true;
64 var promises = [];
65
66 // Interate in reverse so can splice
67 // https://stackoverflow.com/questions/9882284/looping-through-array-and-removing-items-without-breaking-for-loop
68 var i = $scope.managers.length;
69 while (i--) {
70 // Remove any SNMP Manager with an empty address and port
71 if (!$scope.managers[i].address && !$scope.managers[i].port) {
72 $scope.removeSNMPManager(i);
73 continue;
74 }
75
76 // Throw an error if only 1 of the fields is filled out
77 if (!$scope.managers[i].address || !$scope.managers[i].port) {
78 // TODO: Highlight the field that is empty
79 $scope.loading = false;
80 $scope.error = true;
81 return;
82 }
83
84 // If the manager does not have a 'path', it is a new manager
85 // and needs to be created
86 if (!$scope.managers[i].path) {
87 promises.push(addManager(
88 $scope.managers[i].address, $scope.managers[i].port));
89 } else {
Gunnar Mills35d18cb2018-10-05 11:24:14 -050090 if ($scope.managers[i].updateAddress) {
Gunnar Mills87510842018-09-18 15:49:33 -050091 promises.push(setManagerAddress(
92 $scope.managers[i].path, $scope.managers[i].address));
93 }
Gunnar Mills35d18cb2018-10-05 11:24:14 -050094 if ($scope.managers[i].updatePort) {
Gunnar Mills87510842018-09-18 15:49:33 -050095 promises.push(setManagerPort(
96 $scope.managers[i].path, $scope.managers[i].port));
97 }
Gunnar Mills854fbba2018-09-13 15:41:30 -050098 }
99 }
100
101 // Add delete promises last since we might be adding to
Gunnar Mills35d18cb2018-10-05 11:24:14 -0500102 // managersToDelete above
103 for (var i in $scope.managersToDelete) {
104 promises.push(deleteManager($scope.managersToDelete[i]));
Gunnar Mills854fbba2018-09-13 15:41:30 -0500105 }
106
107 $q.all(promises)
108 .then(
109 function() {
110 $scope.success = true;
111 },
112 function(errors) {
113 console.log(JSON.stringify(errors));
114 $scope.error = true;
115 })
116 .finally(function() {
117 $scope.loading = false;
118 });
119 };
120
121 function addManager(address, port) {
122 return APIUtils.addSNMPManager(address, port);
123 }
124
125 function deleteManager(path) {
126 return APIUtils.deleteObject(path);
127 }
128
129 function setManagerAddress(path, address) {
130 return APIUtils.setSNMPManagerAddress(path, address);
131 }
132
133 function setManagerPort(path, port) {
134 return APIUtils.setSNMPManagerPort(path, port);
135 }
Gunnar Millsff64c542018-08-24 15:49:09 -0500136 }
137 ]);
138})(angular);