blob: 5708ec855241bf8372409cafa2f0461c201646c8 [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 = {};
17 $scope.interface = {};
18 $scope.networkDevice = false;
19 $scope.hostname = '';
Gunnar Millse9f5fe72018-05-04 13:43:10 -050020 $scope.defaultgateway = '';
Gunnar Mills7ddc7272018-04-12 16:12:03 -050021 $scope.set_network_error = '';
22 $scope.set_network_success = false;
23 $scope.selectedInterface = '';
Gunnar Millsd01504c2018-05-03 13:01:51 -050024 $scope.confirm_settings = false;
Iftekharul Islam2a489552017-11-02 13:23:08 -050025
Andrew Geisslerd27bb132018-05-24 11:07:27 -070026 $scope.selectInterface = function(interfaceId) {
27 $scope.interface = $scope.network.interfaces[interfaceId];
28 $scope.selectedInterface = interfaceId;
29 $scope.networkDevice = false;
30 };
Gunnar Mills7ddc7272018-04-12 16:12:03 -050031 $scope.setNetworkSettings = function() {
Gunnar Millsd01504c2018-05-03 13:01:51 -050032 // Hides the confirm network settings modal
33 $scope.confirm_settings = false;
Gunnar Mills7ddc7272018-04-12 16:12:03 -050034 $scope.set_network_error = '';
35 $scope.set_network_success = false;
Gunnar Millsdca79d72018-05-30 13:07:01 -050036 var promises = [];
37
Gunnar Mills659651e2018-05-30 15:21:07 -050038 // MAC Address are case-insensitive
39 if ($scope.interface.MACAddress.toLowerCase() !=
40 dataService.mac_address.toLowerCase()) {
41 promises.push(setMACAddress());
42 }
43 if ($scope.defaultgateway != dataService.defaultgateway) {
44 promises.push(setDefaultGateway());
45 }
46 if ($scope.hostname != dataService.hostname) {
47 promises.push(setHostname());
48 }
Gunnar Mills309e06a2018-05-30 13:18:10 -050049
Gunnar Mills659651e2018-05-30 15:21:07 -050050 if (promises.length) {
51 $q.all(promises).finally(function() {
52 if (!$scope.set_network_error) {
53 $scope.set_network_success = true;
54 }
55 });
56 }
Gunnar Millsdca79d72018-05-30 13:07:01 -050057
58 };
59
60 function setMACAddress() {
61 return APIUtils
Gunnar Mills7ddc7272018-04-12 16:12:03 -050062 .setMACAddress(
63 $scope.selectedInterface, $scope.interface.MACAddress)
64 .then(
Gunnar Millsdca79d72018-05-30 13:07:01 -050065 function(data) {},
Gunnar Mills7ddc7272018-04-12 16:12:03 -050066 function(error) {
Gunnar Millsdca79d72018-05-30 13:07:01 -050067 console.log(JSON.stringify(error));
Gunnar Mills7ddc7272018-04-12 16:12:03 -050068 $scope.set_network_error = 'MAC Address';
69 });
Gunnar Millsdca79d72018-05-30 13:07:01 -050070 }
71
72 function setDefaultGateway() {
73 return APIUtils.setDefaultGateway($scope.defaultgateway)
74 .then(
75 function(data) {},
76 function(error) {
77 console.log(JSON.stringify(error));
78 $scope.set_network_error = 'Default Gateway';
79 });
80 }
Gunnar Mills309e06a2018-05-30 13:18:10 -050081
82 function setHostname() {
83 return APIUtils.setHostname($scope.hostname)
84 .then(
85 function(data) {},
86 function(error) {
87 console.log(JSON.stringify(error));
88 $scope.set_network_error = 'Hostname';
89 });
90 }
91
Gunnar Mills9a0094d2018-05-02 21:50:56 -050092 $scope.refresh = function() {
93 $route.reload();
94 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -070095 APIUtils.getNetworkInfo().then(function(data) {
Gunnar Mills659651e2018-05-30 15:21:07 -050096 dataService.setNetworkInfo(data);
Andrew Geisslerd27bb132018-05-24 11:07:27 -070097 $scope.network = data.formatted_data;
98 $scope.hostname = data.hostname;
Gunnar Millse9f5fe72018-05-04 13:43:10 -050099 $scope.defaultgateway = data.defaultgateway;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700100 if ($scope.network.interface_ids.length) {
101 $scope.selectedInterface = $scope.network.interface_ids[0];
102 $scope.interface =
103 $scope.network.interfaces[$scope.selectedInterface];
104 }
105 });
106 }
107 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500108
109})(angular);