blob: 5a6912fbe410a2bee4a518b3b2365785ee21a1bd [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 Millsdca79d72018-05-30 13:07:01 -050013 '$scope', '$window', 'APIUtils', 'dataService', '$route', '$q',
14 function($scope, $window, APIUtils, dataService, $route, $q) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070015 $scope.dataService = dataService;
16 $scope.network = {};
Gunnar Millsa45c3852018-05-30 16:18:45 -050017 $scope.old_interface = {};
Andrew Geisslerd27bb132018-05-24 11:07:27 -070018 $scope.interface = {};
19 $scope.networkDevice = false;
20 $scope.hostname = '';
Gunnar Millse9f5fe72018-05-04 13:43:10 -050021 $scope.defaultgateway = '';
Gunnar Mills7ddc7272018-04-12 16:12:03 -050022 $scope.set_network_error = '';
23 $scope.set_network_success = false;
24 $scope.selectedInterface = '';
Gunnar Millsd01504c2018-05-03 13:01:51 -050025 $scope.confirm_settings = false;
Iftekharul Islam2a489552017-11-02 13:23:08 -050026
Andrew Geisslerd27bb132018-05-24 11:07:27 -070027 $scope.selectInterface = function(interfaceId) {
28 $scope.interface = $scope.network.interfaces[interfaceId];
Gunnar Millsa45c3852018-05-30 16:18:45 -050029 // 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 Geisslerd27bb132018-05-24 11:07:27 -070031 $scope.selectedInterface = interfaceId;
32 $scope.networkDevice = false;
33 };
Gunnar Mills7ddc7272018-04-12 16:12:03 -050034 $scope.setNetworkSettings = function() {
Gunnar Millsd01504c2018-05-03 13:01:51 -050035 // Hides the confirm network settings modal
36 $scope.confirm_settings = false;
Gunnar Mills7ddc7272018-04-12 16:12:03 -050037 $scope.set_network_error = '';
38 $scope.set_network_success = false;
Gunnar Millsdca79d72018-05-30 13:07:01 -050039 var promises = [];
40
Gunnar Mills659651e2018-05-30 15:21:07 -050041 // 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 Mills309e06a2018-05-30 13:18:10 -050052
Gunnar Millsa45c3852018-05-30 16:18:45 -050053 // 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 Mills659651e2018-05-30 15:21:07 -050067 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 Millsdca79d72018-05-30 13:07:01 -050074
75 };
76
77 function setMACAddress() {
78 return APIUtils
Gunnar Mills7ddc7272018-04-12 16:12:03 -050079 .setMACAddress(
80 $scope.selectedInterface, $scope.interface.MACAddress)
81 .then(
Gunnar Millsdca79d72018-05-30 13:07:01 -050082 function(data) {},
Gunnar Mills7ddc7272018-04-12 16:12:03 -050083 function(error) {
Gunnar Millsdca79d72018-05-30 13:07:01 -050084 console.log(JSON.stringify(error));
Gunnar Mills7ddc7272018-04-12 16:12:03 -050085 $scope.set_network_error = 'MAC Address';
86 });
Gunnar Millsdca79d72018-05-30 13:07:01 -050087 }
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 Mills309e06a2018-05-30 13:18:10 -050098
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 Millsa45c3852018-05-30 16:18:45 -0500109 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 Mills9a0094d2018-05-02 21:50:56 -0500138 $scope.refresh = function() {
139 $route.reload();
140 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700141 APIUtils.getNetworkInfo().then(function(data) {
Gunnar Mills659651e2018-05-30 15:21:07 -0500142 dataService.setNetworkInfo(data);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700143 $scope.network = data.formatted_data;
144 $scope.hostname = data.hostname;
Gunnar Millse9f5fe72018-05-04 13:43:10 -0500145 $scope.defaultgateway = data.defaultgateway;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700146 if ($scope.network.interface_ids.length) {
147 $scope.selectedInterface = $scope.network.interface_ids[0];
148 $scope.interface =
149 $scope.network.interfaces[$scope.selectedInterface];
Gunnar Millsa45c3852018-05-30 16:18:45 -0500150 // 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 Geisslerd27bb132018-05-24 11:07:27 -0700153 }
154 });
155 }
156 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500157
158})(angular);