1/**
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', [
13    '$scope', '$window', 'APIUtils', '$route', '$q',
14    function($scope, $window, APIUtils, $route, $q) {
15      $scope.managers = [];
16      $scope.loading = true;
17      $scope.error = false;
18      $scope.success = false;
19      $scope.managersToDelete = [];
20
21      var getSNMPManagers = APIUtils.getSNMPManagers().then(
22          function(data) {
23            // 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,
29                updatePort: false,
30                address: data.data[key].Address,
31                updateAddress: false
32              })
33            }
34          },
35          function(error) {
36            console.log(JSON.stringify(error));
37          });
38
39      getSNMPManagers.finally(function() {
40        $scope.loading = false;
41      });
42
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) {
51          $scope.managersToDelete.push($scope.managers[index].path);
52        }
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 {
90            if ($scope.managers[i].updateAddress) {
91              promises.push(setManagerAddress(
92                  $scope.managers[i].path, $scope.managers[i].address));
93            }
94            if ($scope.managers[i].updatePort) {
95              promises.push(setManagerPort(
96                  $scope.managers[i].path, $scope.managers[i].port));
97            }
98          }
99        }
100
101        // Add delete promises last since we might be adding to
102        // managersToDelete above
103        for (var i in $scope.managersToDelete) {
104          promises.push(deleteManager($scope.managersToDelete[i]));
105        }
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      }
136    }
137  ]);
138})(angular);
139