xref: /openbmc/phosphor-webui/app/configuration/controllers/snmp-controller.js (revision 3cd2c65b57002ac2324c361a0c53f6b1865b70c6)
1ff64c54aSGunnar Mills/**
2ff64c54aSGunnar Mills * Controller for SNMP
3ff64c54aSGunnar Mills *
4ff64c54aSGunnar Mills * @module app/configuration
5ff64c54aSGunnar Mills * @exports snmpController
6ff64c54aSGunnar Mills * @name snmpController
7ff64c54aSGunnar Mills */
8ff64c54aSGunnar Mills
9ff64c54aSGunnar Millswindow.angular && (function(angular) {
10ff64c54aSGunnar Mills  'use strict';
11ff64c54aSGunnar Mills
12ff64c54aSGunnar Mills  angular.module('app.configuration').controller('snmpController', [
1327ce84d2Sbeccabroek    '$scope', '$window', 'APIUtils', '$route', '$q', 'toastService',
1427ce84d2Sbeccabroek    function($scope, $window, APIUtils, $route, $q, toastService) {
15854fbba1SGunnar Mills      $scope.managers = [];
16ff64c54aSGunnar Mills      $scope.loading = true;
1735d18cbcSGunnar Mills      $scope.managersToDelete = [];
18ff64c54aSGunnar Mills
19ff64c54aSGunnar Mills      var getSNMPManagers = APIUtils.getSNMPManagers().then(
20ff64c54aSGunnar Mills          function(data) {
21854fbba1SGunnar Mills            // Convert to array of objects from an object of objects, easier
22854fbba1SGunnar Mills            // to manipulate (e.g. add/remove). Convert key to a path property.
23854fbba1SGunnar Mills            for (var key in data.data) {
24854fbba1SGunnar Mills              $scope.managers.push({
25854fbba1SGunnar Mills                path: key,
26854fbba1SGunnar Mills                port: data.data[key].Port,
2735d18cbcSGunnar Mills                updatePort: false,
288751084cSGunnar Mills                address: data.data[key].Address,
2935d18cbcSGunnar Mills                updateAddress: false
30854fbba1SGunnar Mills              })
31854fbba1SGunnar Mills            }
32ff64c54aSGunnar Mills          },
33ff64c54aSGunnar Mills          function(error) {
3427ce84d2Sbeccabroek            toastService.error('Unable to load SNMP settings.');
35ff64c54aSGunnar Mills            console.log(JSON.stringify(error));
36ff64c54aSGunnar Mills          });
37ff64c54aSGunnar Mills
38ff64c54aSGunnar Mills      getSNMPManagers.finally(function() {
39ff64c54aSGunnar Mills        $scope.loading = false;
40ff64c54aSGunnar Mills      });
41854fbba1SGunnar Mills
42854fbba1SGunnar Mills      $scope.addNewSNMPManager = function() {
43854fbba1SGunnar Mills        $scope.managers.push({address: '', port: ''});
44854fbba1SGunnar Mills      };
45854fbba1SGunnar Mills
46854fbba1SGunnar Mills      $scope.removeSNMPManager = function(index) {
47854fbba1SGunnar Mills        // If the SNMP Manager has a path it exists on the backend and we
48854fbba1SGunnar Mills        // need to make a call to remove it
49854fbba1SGunnar Mills        if ($scope.managers[index].path) {
5035d18cbcSGunnar Mills          $scope.managersToDelete.push($scope.managers[index].path);
51854fbba1SGunnar Mills        }
52854fbba1SGunnar Mills        $scope.managers.splice(index, 1);
53854fbba1SGunnar Mills      };
54854fbba1SGunnar Mills
55854fbba1SGunnar Mills      $scope.refresh = function() {
56854fbba1SGunnar Mills        $route.reload();
57854fbba1SGunnar Mills      };
58854fbba1SGunnar Mills
59854fbba1SGunnar Mills      $scope.setSNMP = function() {
60854fbba1SGunnar Mills        $scope.loading = true;
61854fbba1SGunnar Mills        var promises = [];
62854fbba1SGunnar Mills
63ddda4decSbeccabroek        // Validate that no field are empty and port is valid. Port value is
64ddda4decSbeccabroek        // undefined if it is an invalid number.
65*3cd2c65bSDerick Montague        for (let i in $scope.managers) {
66ddda4decSbeccabroek          if (!$scope.managers[i].address || !$scope.managers[i].port) {
67ddda4decSbeccabroek            $scope.loading = false;
68ddda4decSbeccabroek            toastService.error('Cannot save. Please resolve errors on page.');
69ddda4decSbeccabroek            return;
70ddda4decSbeccabroek          }
71ddda4decSbeccabroek        }
72ddda4decSbeccabroek        // Iterate in reverse so can splice
73854fbba1SGunnar Mills        // https://stackoverflow.com/questions/9882284/looping-through-array-and-removing-items-without-breaking-for-loop
74*3cd2c65bSDerick Montague        let managersLength = $scope.managers.length;
75*3cd2c65bSDerick Montague        while (managersLength--) {
76854fbba1SGunnar Mills          // If the manager does not have a 'path', it is a new manager
77854fbba1SGunnar Mills          // and needs to be created
78*3cd2c65bSDerick Montague          if (!$scope.managers[managersLength].path) {
79854fbba1SGunnar Mills            promises.push(addManager(
80*3cd2c65bSDerick Montague                $scope.managers[managersLength].address,
81*3cd2c65bSDerick Montague                $scope.managers[managersLength].port));
82854fbba1SGunnar Mills          } else {
83*3cd2c65bSDerick Montague            if ($scope.managers[managersLength].updateAddress) {
84854fbba1SGunnar Mills              promises.push(setManagerAddress(
85*3cd2c65bSDerick Montague                  $scope.managers[managersLength].path,
86*3cd2c65bSDerick Montague                  $scope.managers[managersLength].address));
878751084cSGunnar Mills            }
88*3cd2c65bSDerick Montague            if ($scope.managers[managersLength].updatePort) {
89854fbba1SGunnar Mills              promises.push(setManagerPort(
90*3cd2c65bSDerick Montague                  $scope.managers[managersLength].path,
91*3cd2c65bSDerick Montague                  $scope.managers[managersLength].port));
92854fbba1SGunnar Mills            }
93854fbba1SGunnar Mills          }
948751084cSGunnar Mills        }
95854fbba1SGunnar Mills
96854fbba1SGunnar Mills        // Add delete promises last since we might be adding to
9735d18cbcSGunnar Mills        // managersToDelete above
98*3cd2c65bSDerick Montague        for (let i in $scope.managersToDelete) {
9935d18cbcSGunnar Mills          promises.push(deleteManager($scope.managersToDelete[i]));
100854fbba1SGunnar Mills        }
101854fbba1SGunnar Mills
102854fbba1SGunnar Mills        $q.all(promises)
103854fbba1SGunnar Mills            .then(
104854fbba1SGunnar Mills                function() {
105*3cd2c65bSDerick Montague                  $scope.refresh();
106ddda4decSbeccabroek                  toastService.success('SNMP settings have been saved.');
107854fbba1SGunnar Mills                },
108854fbba1SGunnar Mills                function(errors) {
10927ce84d2Sbeccabroek                  toastService.error('Unable to set SNMP Managers.');
110854fbba1SGunnar Mills                  console.log(JSON.stringify(errors));
111854fbba1SGunnar Mills                })
112854fbba1SGunnar Mills            .finally(function() {
113854fbba1SGunnar Mills              $scope.loading = false;
114854fbba1SGunnar Mills            });
115854fbba1SGunnar Mills      };
116854fbba1SGunnar Mills
117854fbba1SGunnar Mills      function addManager(address, port) {
118854fbba1SGunnar Mills        return APIUtils.addSNMPManager(address, port);
119854fbba1SGunnar Mills      }
120854fbba1SGunnar Mills
121854fbba1SGunnar Mills      function deleteManager(path) {
122854fbba1SGunnar Mills        return APIUtils.deleteObject(path);
123854fbba1SGunnar Mills      }
124854fbba1SGunnar Mills
125854fbba1SGunnar Mills      function setManagerAddress(path, address) {
126854fbba1SGunnar Mills        return APIUtils.setSNMPManagerAddress(path, address);
127854fbba1SGunnar Mills      }
128854fbba1SGunnar Mills
129854fbba1SGunnar Mills      function setManagerPort(path, port) {
130854fbba1SGunnar Mills        return APIUtils.setSNMPManagerPort(path, port);
131854fbba1SGunnar Mills      }
132ff64c54aSGunnar Mills    }
133ff64c54aSGunnar Mills  ]);
134ff64c54aSGunnar Mills})(angular);
135