Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Controller for network |
| 3 | * |
| 4 | * @module app/configuration |
| 5 | * @exports networkController |
| 6 | * @name networkController |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 9 | window.angular && (function(angular) { |
| 10 | 'use strict'; |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 11 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 12 | angular.module('app.configuration').controller('networkController', [ |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 13 | '$scope', '$window', 'APIUtils', 'dataService', '$route', '$q', |
| 14 | function($scope, $window, APIUtils, dataService, $route, $q) { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 15 | $scope.dataService = dataService; |
| 16 | $scope.network = {}; |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame^] | 17 | $scope.old_interface = {}; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 18 | $scope.interface = {}; |
| 19 | $scope.networkDevice = false; |
| 20 | $scope.hostname = ''; |
Gunnar Mills | e9f5fe7 | 2018-05-04 13:43:10 -0500 | [diff] [blame] | 21 | $scope.defaultgateway = ''; |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 22 | $scope.set_network_error = ''; |
| 23 | $scope.set_network_success = false; |
| 24 | $scope.selectedInterface = ''; |
Gunnar Mills | d01504c | 2018-05-03 13:01:51 -0500 | [diff] [blame] | 25 | $scope.confirm_settings = false; |
Iftekharul Islam | 2a48955 | 2017-11-02 13:23:08 -0500 | [diff] [blame] | 26 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 27 | $scope.selectInterface = function(interfaceId) { |
| 28 | $scope.interface = $scope.network.interfaces[interfaceId]; |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame^] | 29 | // Copy the interface so we know later if changes were made to the page |
| 30 | $scope.old_interface = JSON.parse(JSON.stringify($scope.interface)); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 31 | $scope.selectedInterface = interfaceId; |
| 32 | $scope.networkDevice = false; |
| 33 | }; |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 34 | $scope.setNetworkSettings = function() { |
Gunnar Mills | d01504c | 2018-05-03 13:01:51 -0500 | [diff] [blame] | 35 | // Hides the confirm network settings modal |
| 36 | $scope.confirm_settings = false; |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 37 | $scope.set_network_error = ''; |
| 38 | $scope.set_network_success = false; |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 39 | var promises = []; |
| 40 | |
Gunnar Mills | 659651e | 2018-05-30 15:21:07 -0500 | [diff] [blame] | 41 | // MAC Address are case-insensitive |
| 42 | if ($scope.interface.MACAddress.toLowerCase() != |
| 43 | dataService.mac_address.toLowerCase()) { |
| 44 | promises.push(setMACAddress()); |
| 45 | } |
| 46 | if ($scope.defaultgateway != dataService.defaultgateway) { |
| 47 | promises.push(setDefaultGateway()); |
| 48 | } |
| 49 | if ($scope.hostname != dataService.hostname) { |
| 50 | promises.push(setHostname()); |
| 51 | } |
Gunnar Mills | 309e06a | 2018-05-30 13:18:10 -0500 | [diff] [blame] | 52 | |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame^] | 53 | // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways |
| 54 | if (!$scope.interface.DHCPEnabled) { |
| 55 | for (var i in $scope.interface.ipv4.values) { |
| 56 | if ($scope.interface.ipv4.values[i].Address != |
| 57 | $scope.old_interface.ipv4.values[i].Address || |
| 58 | $scope.interface.ipv4.values[i].PrefixLength != |
| 59 | $scope.old_interface.ipv4.values[i].PrefixLength || |
| 60 | $scope.interface.ipv4.values[i].Gateway != |
| 61 | $scope.old_interface.ipv4.values[i].Gateway) { |
| 62 | promises.push(setIPV4(i)); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
Gunnar Mills | 659651e | 2018-05-30 15:21:07 -0500 | [diff] [blame] | 67 | if (promises.length) { |
| 68 | $q.all(promises).finally(function() { |
| 69 | if (!$scope.set_network_error) { |
| 70 | $scope.set_network_success = true; |
| 71 | } |
| 72 | }); |
| 73 | } |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 74 | |
| 75 | }; |
| 76 | |
| 77 | function setMACAddress() { |
| 78 | return APIUtils |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 79 | .setMACAddress( |
| 80 | $scope.selectedInterface, $scope.interface.MACAddress) |
| 81 | .then( |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 82 | function(data) {}, |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 83 | function(error) { |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 84 | console.log(JSON.stringify(error)); |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 85 | $scope.set_network_error = 'MAC Address'; |
| 86 | }); |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | function setDefaultGateway() { |
| 90 | return APIUtils.setDefaultGateway($scope.defaultgateway) |
| 91 | .then( |
| 92 | function(data) {}, |
| 93 | function(error) { |
| 94 | console.log(JSON.stringify(error)); |
| 95 | $scope.set_network_error = 'Default Gateway'; |
| 96 | }); |
| 97 | } |
Gunnar Mills | 309e06a | 2018-05-30 13:18:10 -0500 | [diff] [blame] | 98 | |
| 99 | function setHostname() { |
| 100 | return APIUtils.setHostname($scope.hostname) |
| 101 | .then( |
| 102 | function(data) {}, |
| 103 | function(error) { |
| 104 | console.log(JSON.stringify(error)); |
| 105 | $scope.set_network_error = 'Hostname'; |
| 106 | }); |
| 107 | } |
| 108 | |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame^] | 109 | function setIPV4(index) { |
| 110 | // The correct way to edit an IPV4 interface is to remove it and then |
| 111 | // add a new one |
| 112 | return APIUtils |
| 113 | .deleteIPV4( |
| 114 | $scope.selectedInterface, $scope.interface.ipv4.ids[index]) |
| 115 | .then( |
| 116 | function(data) { |
| 117 | return APIUtils |
| 118 | .addIPV4( |
| 119 | $scope.selectedInterface, |
| 120 | $scope.interface.ipv4.values[index].Address, |
| 121 | $scope.interface.ipv4.values[index].PrefixLength, |
| 122 | $scope.interface.ipv4.values[index].Gateway) |
| 123 | .then( |
| 124 | function(data) {}, |
| 125 | function(error) { |
| 126 | console.log(JSON.stringify(error)); |
| 127 | $scope.set_network_error = |
| 128 | $scope.interface.ipv4.values[index].Address; |
| 129 | }); |
| 130 | }, |
| 131 | function(error) { |
| 132 | console.log(JSON.stringify(error)); |
| 133 | $scope.set_network_error = |
| 134 | $scope.interface.ipv4.values[index].Address; |
| 135 | }); |
| 136 | } |
| 137 | |
Gunnar Mills | 9a0094d | 2018-05-02 21:50:56 -0500 | [diff] [blame] | 138 | $scope.refresh = function() { |
| 139 | $route.reload(); |
| 140 | }; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 141 | APIUtils.getNetworkInfo().then(function(data) { |
Gunnar Mills | 659651e | 2018-05-30 15:21:07 -0500 | [diff] [blame] | 142 | dataService.setNetworkInfo(data); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 143 | $scope.network = data.formatted_data; |
| 144 | $scope.hostname = data.hostname; |
Gunnar Mills | e9f5fe7 | 2018-05-04 13:43:10 -0500 | [diff] [blame] | 145 | $scope.defaultgateway = data.defaultgateway; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 146 | if ($scope.network.interface_ids.length) { |
| 147 | $scope.selectedInterface = $scope.network.interface_ids[0]; |
| 148 | $scope.interface = |
| 149 | $scope.network.interfaces[$scope.selectedInterface]; |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame^] | 150 | // Copy the interface so we know later if changes were made to the |
| 151 | // page |
| 152 | $scope.old_interface = JSON.parse(JSON.stringify($scope.interface)); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 153 | } |
| 154 | }); |
| 155 | } |
| 156 | ]); |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 157 | |
| 158 | })(angular); |