blob: 8a92ef47ee6604e45f83b3b3249f899af1d601f0 [file] [log] [blame]
Iftekharul Islamcd789502017-04-19 14:37:55 -05001/**
2 * Controller for network
3 *
4 * @module app/configuration
5 * @exports networkController
6 * @name networkController
Iftekharul Islamcd789502017-04-19 14:37:55 -05007 */
8
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07009window.angular && (function(angular) {
10 'use strict';
Iftekharul Islamcd789502017-04-19 14:37:55 -050011
Andrew Geisslerd27bb132018-05-24 11:07:27 -070012 angular.module('app.configuration').controller('networkController', [
Gunnar Mills0af165b2018-06-01 16:24:56 -050013 '$scope', '$window', 'APIUtils', 'dataService', '$timeout', '$route', '$q',
beccabroek9a3b5422019-01-15 14:45:12 -060014 'ngToast',
15 function(
16 $scope, $window, APIUtils, dataService, $timeout, $route, $q, ngToast) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070017 $scope.dataService = dataService;
18 $scope.network = {};
beccabroek067a1cd2018-10-05 10:25:21 -050019 $scope.oldInterface = {};
Andrew Geisslerd27bb132018-05-24 11:07:27 -070020 $scope.interface = {};
21 $scope.networkDevice = false;
22 $scope.hostname = '';
beccabroek067a1cd2018-10-05 10:25:21 -050023 $scope.defaultGateway = '';
Gunnar Mills7ddc7272018-04-12 16:12:03 -050024 $scope.selectedInterface = '';
beccabroek067a1cd2018-10-05 10:25:21 -050025 $scope.confirmSettings = false;
Gunnar Mills84981f02018-05-31 15:19:01 -050026 $scope.loading = false;
beccabroek067a1cd2018-10-05 10:25:21 -050027 $scope.ipv4sToDelete = [];
Iftekharul Islam2a489552017-11-02 13:23:08 -050028
Gunnar Millsa3f75322018-07-10 17:06:02 -050029 loadNetworkInfo();
30
Andrew Geisslerd27bb132018-05-24 11:07:27 -070031 $scope.selectInterface = function(interfaceId) {
32 $scope.interface = $scope.network.interfaces[interfaceId];
Gunnar Millsa45c3852018-05-30 16:18:45 -050033 // Copy the interface so we know later if changes were made to the page
beccabroek067a1cd2018-10-05 10:25:21 -050034 $scope.oldInterface = JSON.parse(JSON.stringify($scope.interface));
Andrew Geisslerd27bb132018-05-24 11:07:27 -070035 $scope.selectedInterface = interfaceId;
36 $scope.networkDevice = false;
37 };
Gunnar Millsbc3ab722018-07-10 15:11:01 -050038
39 $scope.addDNSField = function() {
40 $scope.interface.Nameservers.push('');
41 };
42
beccabroekcff61502018-09-13 14:39:23 -050043 $scope.removeDNSField = function(index) {
44 $scope.interface.Nameservers.splice(index, 1);
45 };
46
beccabroek1a0e7d02018-09-24 15:03:40 -050047 $scope.addIpv4Field = function() {
48 $scope.interface.ipv4.values.push(
49 {Address: '', PrefixLength: '', Gateway: ''});
50 };
51
beccabroek971ac1a2018-09-24 13:14:05 -050052 $scope.removeIpv4Address = function(index) {
53 // Check if the IPV4 being removed has an id. This indicates that it is
54 // an existing address and needs to be removed in the back end.
55 if ($scope.interface.ipv4.values[index].id) {
beccabroek067a1cd2018-10-05 10:25:21 -050056 $scope.ipv4sToDelete.push($scope.interface.ipv4.values[index]);
beccabroek971ac1a2018-09-24 13:14:05 -050057 }
58 $scope.interface.ipv4.values.splice(index, 1);
59 };
60
Gunnar Mills7ddc7272018-04-12 16:12:03 -050061 $scope.setNetworkSettings = function() {
Gunnar Millsd01504c2018-05-03 13:01:51 -050062 // Hides the confirm network settings modal
beccabroek067a1cd2018-10-05 10:25:21 -050063 $scope.confirmSettings = false;
Gunnar Mills84981f02018-05-31 15:19:01 -050064 $scope.loading = true;
Gunnar Millsdca79d72018-05-30 13:07:01 -050065 var promises = [];
66
Gunnar Mills659651e2018-05-30 15:21:07 -050067 // MAC Address are case-insensitive
68 if ($scope.interface.MACAddress.toLowerCase() !=
69 dataService.mac_address.toLowerCase()) {
70 promises.push(setMACAddress());
71 }
beccabroek067a1cd2018-10-05 10:25:21 -050072 if ($scope.defaultGateway != dataService.defaultgateway) {
Gunnar Mills659651e2018-05-30 15:21:07 -050073 promises.push(setDefaultGateway());
74 }
75 if ($scope.hostname != dataService.hostname) {
76 promises.push(setHostname());
77 }
beccabroek067a1cd2018-10-05 10:25:21 -050078 if ($scope.interface.DHCPEnabled != $scope.oldInterface.DHCPEnabled) {
Gunnar Millscb2c3062018-05-31 13:13:30 -050079 promises.push(setDHCPEnabled());
80 }
Gunnar Mills309e06a2018-05-30 13:18:10 -050081
Gunnar Mills82658292018-06-06 16:51:32 -050082 // Remove any empty strings from the array. Important because we add an
83 // empty string to the end so the user can add a new DNS server, if the
84 // user doesn't fill out the field, we don't want to add.
85 $scope.interface.Nameservers =
86 $scope.interface.Nameservers.filter(Boolean);
Gunnar Mills06467822018-06-06 15:43:18 -050087 // toString() is a cheap way to compare 2 string arrays
88 if ($scope.interface.Nameservers.toString() !=
beccabroek067a1cd2018-10-05 10:25:21 -050089 $scope.oldInterface.Nameservers.toString()) {
Gunnar Mills06467822018-06-06 15:43:18 -050090 promises.push(setNameservers());
91 }
92
Gunnar Millsa45c3852018-05-30 16:18:45 -050093 // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways
94 if (!$scope.interface.DHCPEnabled) {
beccabroek971ac1a2018-09-24 13:14:05 -050095 // Delete existing IPV4 addresses that were removed
96 promises.push(removeIPV4s());
beccabroek1a0e7d02018-09-24 15:03:40 -050097 // Update any changed IPV4 addresses and add new
Gunnar Millsa45c3852018-05-30 16:18:45 -050098 for (var i in $scope.interface.ipv4.values) {
Gunnar Mills65491142018-06-04 14:23:33 -050099 if (!APIUtils.validIPV4IP(
100 $scope.interface.ipv4.values[i].Address)) {
beccabroek9a3b5422019-01-15 14:45:12 -0600101 ngToast.danger(
102 $scope.interface.ipv4.values[i].Address +
103 ' invalid IP parameter');
Gunnar Mills65491142018-06-04 14:23:33 -0500104 $scope.loading = false;
105 return;
106 }
107 if (!APIUtils.validIPV4IP(
108 $scope.interface.ipv4.values[i].Gateway)) {
beccabroek9a3b5422019-01-15 14:45:12 -0600109 ngToast.danger(
110 $scope.interface.ipv4.values[i].Address +
111 ' invalid gateway parameter');
Gunnar Mills65491142018-06-04 14:23:33 -0500112 $scope.loading = false;
113 return;
114 }
115 // The netmask prefix length will be undefined if outside range
116 if (!$scope.interface.ipv4.values[i].PrefixLength) {
beccabroek9a3b5422019-01-15 14:45:12 -0600117 ngToast.danger(
118 $scope.interface.ipv4.values[i].Address +
119 ' invalid Prefix Length parameter');
Gunnar Mills65491142018-06-04 14:23:33 -0500120 $scope.loading = false;
121 return;
122 }
beccabroek067a1cd2018-10-05 10:25:21 -0500123 if ($scope.interface.ipv4.values[i].updateAddress ||
124 $scope.interface.ipv4.values[i].updateGateway ||
125 $scope.interface.ipv4.values[i].updatePrefix) {
beccabroek1a0e7d02018-09-24 15:03:40 -0500126 // If IPV4 has an id it means it already exists in the back end,
127 // and in order to update it is required to remove previous IPV4
128 // address and add new one. See openbmc/openbmc/issues/2163.
129 // TODO: update to use PUT once issue 2163 is resolved.
130 if ($scope.interface.ipv4.values[i].id) {
131 promises.push(updateIPV4(i));
132 } else {
133 promises.push(addIPV4(i));
134 }
Gunnar Millsa45c3852018-05-30 16:18:45 -0500135 }
136 }
137 }
138
Gunnar Mills0af165b2018-06-01 16:24:56 -0500139 if (promises.length) {
beccabroek9a3b5422019-01-15 14:45:12 -0600140 $q.all(promises).then(
141 function(response) {
142 // Since an IPV4 interface (e.g. IP address, gateway, or
143 // netmask) edit is a delete then an add and the GUI can't
144 // calculate the interface id (e.g. 5c083707) beforehand and it
145 // is not returned by the REST call, openbmc#3227, reload the
146 // page after an edit, which makes a 2nd REST call. Do this for
147 // all network changes due to the possibility of a set network
148 // failing even though it returned success, openbmc#1641, and to
149 // update dataService and oldInterface to know which data has
150 // changed if the user continues to edit network settings.
151 // TODO: The reload is not ideal. Revisit this.
152 $timeout(function() {
153 loadNetworkInfo();
154 $scope.loading = false;
155 ngToast.success('Network settings saved');
156 }, 4000);
157 },
158 function(error) {
159 $scope.loading = false;
160 ngToast.danger('Network settings could not be saved');
161 })
Gunnar Mills0af165b2018-06-01 16:24:56 -0500162 } else {
Gunnar Mills84981f02018-05-31 15:19:01 -0500163 $scope.loading = false;
Gunnar Mills0af165b2018-06-01 16:24:56 -0500164 }
Gunnar Millsdca79d72018-05-30 13:07:01 -0500165 };
166
167 function setMACAddress() {
168 return APIUtils
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500169 .setMACAddress(
170 $scope.selectedInterface, $scope.interface.MACAddress)
171 .then(
Gunnar Millsdca79d72018-05-30 13:07:01 -0500172 function(data) {},
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500173 function(error) {
Gunnar Millsdca79d72018-05-30 13:07:01 -0500174 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600175 return $q.reject();
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500176 });
Gunnar Millsdca79d72018-05-30 13:07:01 -0500177 }
178
179 function setDefaultGateway() {
beccabroek067a1cd2018-10-05 10:25:21 -0500180 return APIUtils.setDefaultGateway($scope.defaultGateway)
Gunnar Millsdca79d72018-05-30 13:07:01 -0500181 .then(
182 function(data) {},
183 function(error) {
184 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600185 return $q.reject();
Gunnar Millsdca79d72018-05-30 13:07:01 -0500186 });
187 }
Gunnar Mills309e06a2018-05-30 13:18:10 -0500188
189 function setHostname() {
190 return APIUtils.setHostname($scope.hostname)
191 .then(
192 function(data) {},
193 function(error) {
194 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600195 return $q.reject();
Gunnar Mills309e06a2018-05-30 13:18:10 -0500196 });
197 }
198
Gunnar Millscb2c3062018-05-31 13:13:30 -0500199 function setDHCPEnabled() {
200 return APIUtils
201 .setDHCPEnabled(
202 $scope.selectedInterface, $scope.interface.DHCPEnabled)
203 .then(
204 function(data) {},
205 function(error) {
206 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600207 return $q.reject();
Gunnar Millscb2c3062018-05-31 13:13:30 -0500208 });
209 }
210
Gunnar Mills06467822018-06-06 15:43:18 -0500211 function setNameservers() {
Gunnar Mills82658292018-06-06 16:51:32 -0500212 // Nameservers does not allow an empty array, since we remove all empty
Gunnar Mills9863d122018-07-11 13:36:43 -0500213 // strings above, could have an empty array. TODO: openbmc/openbmc#3240
Gunnar Mills82658292018-06-06 16:51:32 -0500214 if ($scope.interface.Nameservers.length == 0) {
215 $scope.interface.Nameservers.push('');
216 }
Gunnar Mills06467822018-06-06 15:43:18 -0500217 return APIUtils
218 .setNameservers(
219 $scope.selectedInterface, $scope.interface.Nameservers)
220 .then(
221 function(data) {},
222 function(error) {
223 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600224 return $q.reject();
Gunnar Mills06467822018-06-06 15:43:18 -0500225 });
226 }
227
beccabroek971ac1a2018-09-24 13:14:05 -0500228 function removeIPV4s() {
beccabroek067a1cd2018-10-05 10:25:21 -0500229 return $scope.ipv4sToDelete.map(function(ipv4) {
beccabroek971ac1a2018-09-24 13:14:05 -0500230 return APIUtils.deleteIPV4($scope.selectedInterface, ipv4.id)
231 .then(
232 function(data) {},
233 function(error) {
234 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600235 return $q.reject();
beccabroek971ac1a2018-09-24 13:14:05 -0500236 })
237 });
238 }
239
beccabroek1a0e7d02018-09-24 15:03:40 -0500240 function addIPV4(index) {
241 return APIUtils
242 .addIPV4(
243 $scope.selectedInterface,
244 $scope.interface.ipv4.values[index].Address,
245 $scope.interface.ipv4.values[index].PrefixLength,
246 $scope.interface.ipv4.values[index].Gateway)
247 .then(
248 function(data) {},
249 function(error) {
250 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600251 return $q.reject();
beccabroek1a0e7d02018-09-24 15:03:40 -0500252 })
253 }
254
255 function updateIPV4(index) {
Gunnar Millsa45c3852018-05-30 16:18:45 -0500256 // The correct way to edit an IPV4 interface is to remove it and then
257 // add a new one
258 return APIUtils
259 .deleteIPV4(
beccabroek1a0e7d02018-09-24 15:03:40 -0500260 $scope.selectedInterface,
261 $scope.interface.ipv4.values[index].id)
Gunnar Millsa45c3852018-05-30 16:18:45 -0500262 .then(
263 function(data) {
264 return APIUtils
265 .addIPV4(
266 $scope.selectedInterface,
267 $scope.interface.ipv4.values[index].Address,
268 $scope.interface.ipv4.values[index].PrefixLength,
269 $scope.interface.ipv4.values[index].Gateway)
270 .then(
271 function(data) {},
272 function(error) {
273 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600274 return $q.reject();
Gunnar Millsa45c3852018-05-30 16:18:45 -0500275 });
276 },
277 function(error) {
278 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600279 return $q.reject();
Gunnar Millsa45c3852018-05-30 16:18:45 -0500280 });
281 }
282
Gunnar Mills9a0094d2018-05-02 21:50:56 -0500283 $scope.refresh = function() {
Gunnar Millsa3f75322018-07-10 17:06:02 -0500284 loadNetworkInfo();
Gunnar Mills9a0094d2018-05-02 21:50:56 -0500285 };
Gunnar Millsa3f75322018-07-10 17:06:02 -0500286
287 function loadNetworkInfo() {
288 APIUtils.getNetworkInfo().then(function(data) {
289 dataService.setNetworkInfo(data);
290 $scope.network = data.formatted_data;
291 $scope.hostname = data.hostname;
beccabroek067a1cd2018-10-05 10:25:21 -0500292 $scope.defaultGateway = data.defaultgateway;
Gunnar Millsa3f75322018-07-10 17:06:02 -0500293 if ($scope.network.interface_ids.length) {
Rebecca Shawffdef962018-07-19 13:37:37 -0500294 // Use the first network interface if the user hasn't chosen one
Gunnar Millsa3f75322018-07-10 17:06:02 -0500295 if (!$scope.selectedInterface ||
296 !$scope.network.interfaces[$scope.selectedInterface]) {
297 $scope.selectedInterface = $scope.network.interface_ids[0];
298 }
299 $scope.interface =
300 $scope.network.interfaces[$scope.selectedInterface];
301 // Copy the interface so we know later if changes were made to the
302 // page
beccabroek067a1cd2018-10-05 10:25:21 -0500303 $scope.oldInterface = JSON.parse(JSON.stringify($scope.interface));
Gunnar Millsa3f75322018-07-10 17:06:02 -0500304 }
beccabroek1a0e7d02018-09-24 15:03:40 -0500305 // Add id values and update flags to corresponding IPV4 objects
beccabroek971ac1a2018-09-24 13:14:05 -0500306 for (var i = 0; i < $scope.interface.ipv4.values.length; i++) {
307 $scope.interface.ipv4.values[i].id = $scope.interface.ipv4.ids[i];
beccabroek067a1cd2018-10-05 10:25:21 -0500308 $scope.interface.ipv4.values[i].updateAddress = false;
309 $scope.interface.ipv4.values[i].updateGateway = false;
310 $scope.interface.ipv4.values[i].updatePrefix = false;
beccabroek971ac1a2018-09-24 13:14:05 -0500311 }
Gunnar Millsa3f75322018-07-10 17:06:02 -0500312 });
313 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700314 }
315 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500316})(angular);