xref: /openbmc/phosphor-webui/app/configuration/controllers/snmp-controller.js (revision 27ce84d247a41288228713342d4e7745d8806eba)
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', [
13     '$scope', '$window', 'APIUtils', '$route', '$q', 'toastService',
14     function($scope, $window, APIUtils, $route, $q, toastService) {
15       $scope.managers = [];
16       $scope.loading = true;
17       $scope.managersToDelete = [];
18 
19       var getSNMPManagers = APIUtils.getSNMPManagers().then(
20           function(data) {
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,
27                 updatePort: false,
28                 address: data.data[key].Address,
29                 updateAddress: false
30               })
31             }
32           },
33           function(error) {
34             toastService.error('Unable to load SNMP settings.');
35             console.log(JSON.stringify(error));
36           });
37 
38       getSNMPManagers.finally(function() {
39         $scope.loading = false;
40       });
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) {
50           $scope.managersToDelete.push($scope.managers[index].path);
51         }
52         $scope.managers.splice(index, 1);
53       };
54 
55       $scope.refresh = function() {
56         $route.reload();
57       };
58 
59       $scope.setSNMP = function() {
60         $scope.loading = true;
61         var promises = [];
62 
63         // Interate in reverse so can splice
64         // https://stackoverflow.com/questions/9882284/looping-through-array-and-removing-items-without-breaking-for-loop
65         var i = $scope.managers.length;
66         while (i--) {
67           // Remove any SNMP Manager with an empty address and port
68           if (!$scope.managers[i].address && !$scope.managers[i].port) {
69             $scope.removeSNMPManager(i);
70             continue;
71           }
72 
73           // Throw an error if only 1 of the fields is filled out
74           if (!$scope.managers[i].address || !$scope.managers[i].port) {
75             // TODO: Highlight the field that is empty
76             $scope.loading = false;
77             toastService.error('All fields are required.');
78             return;
79           }
80 
81           // If the manager does not have a 'path', it is a new manager
82           // and needs to be created
83           if (!$scope.managers[i].path) {
84             promises.push(addManager(
85                 $scope.managers[i].address, $scope.managers[i].port));
86           } else {
87             if ($scope.managers[i].updateAddress) {
88               promises.push(setManagerAddress(
89                   $scope.managers[i].path, $scope.managers[i].address));
90             }
91             if ($scope.managers[i].updatePort) {
92               promises.push(setManagerPort(
93                   $scope.managers[i].path, $scope.managers[i].port));
94             }
95           }
96         }
97 
98         // Add delete promises last since we might be adding to
99         // managersToDelete above
100         for (var i in $scope.managersToDelete) {
101           promises.push(deleteManager($scope.managersToDelete[i]));
102         }
103 
104         $q.all(promises)
105             .then(
106                 function() {
107                   toastService.success('SNMP Managers set.');
108                 },
109                 function(errors) {
110                   toastService.error('Unable to set SNMP Managers.');
111                   console.log(JSON.stringify(errors));
112                 })
113             .finally(function() {
114               $scope.loading = false;
115             });
116       };
117 
118       function addManager(address, port) {
119         return APIUtils.addSNMPManager(address, port);
120       }
121 
122       function deleteManager(path) {
123         return APIUtils.deleteObject(path);
124       }
125 
126       function setManagerAddress(path, address) {
127         return APIUtils.setSNMPManagerAddress(path, address);
128       }
129 
130       function setManagerPort(path, port) {
131         return APIUtils.setSNMPManagerPort(path, port);
132       }
133     }
134   ]);
135 })(angular);
136