1cd789508SIftekharul Islam/** 2cd789508SIftekharul Islam * Controller for network 3cd789508SIftekharul Islam * 4cd789508SIftekharul Islam * @module app/configuration 5cd789508SIftekharul Islam * @exports networkController 6cd789508SIftekharul Islam * @name networkController 7cd789508SIftekharul Islam */ 8cd789508SIftekharul Islam 9cd789508SIftekharul Islamwindow.angular && (function(angular) { 10cd789508SIftekharul Islam 'use strict'; 11cd789508SIftekharul Islam 12d27bb135SAndrew Geissler angular.module('app.configuration').controller('networkController', [ 13dca79d73SGunnar Mills '$scope', '$window', 'APIUtils', 'dataService', '$route', '$q', 14dca79d73SGunnar Mills function($scope, $window, APIUtils, dataService, $route, $q) { 15cd789508SIftekharul Islam $scope.dataService = dataService; 162a489554SIftekharul Islam $scope.network = {}; 17a45c3852SGunnar Mills $scope.old_interface = {}; 182a489554SIftekharul Islam $scope.interface = {}; 192a489554SIftekharul Islam $scope.networkDevice = false; 20ba5e3f34SAndrew Geissler $scope.hostname = ''; 21e9f5fe77SGunnar Mills $scope.defaultgateway = ''; 227ddc7274SGunnar Mills $scope.set_network_error = ''; 237ddc7274SGunnar Mills $scope.set_network_success = false; 247ddc7274SGunnar Mills $scope.selectedInterface = ''; 25d01504cfSGunnar Mills $scope.confirm_settings = false; 262a489554SIftekharul Islam 272a489554SIftekharul Islam $scope.selectInterface = function(interfaceId) { 282a489554SIftekharul Islam $scope.interface = $scope.network.interfaces[interfaceId]; 29a45c3852SGunnar Mills // Copy the interface so we know later if changes were made to the page 30a45c3852SGunnar Mills $scope.old_interface = JSON.parse(JSON.stringify($scope.interface)); 312a489554SIftekharul Islam $scope.selectedInterface = interfaceId; 322a489554SIftekharul Islam $scope.networkDevice = false; 33ba5e3f34SAndrew Geissler }; 347ddc7274SGunnar Mills $scope.setNetworkSettings = function() { 35d01504cfSGunnar Mills // Hides the confirm network settings modal 36d01504cfSGunnar Mills $scope.confirm_settings = false; 377ddc7274SGunnar Mills $scope.set_network_error = ''; 387ddc7274SGunnar Mills $scope.set_network_success = false; 39dca79d73SGunnar Mills var promises = []; 40dca79d73SGunnar Mills 41659651e8SGunnar Mills // MAC Address are case-insensitive 42659651e8SGunnar Mills if ($scope.interface.MACAddress.toLowerCase() != 43659651e8SGunnar Mills dataService.mac_address.toLowerCase()) { 44dca79d73SGunnar Mills promises.push(setMACAddress()); 45659651e8SGunnar Mills } 46659651e8SGunnar Mills if ($scope.defaultgateway != dataService.defaultgateway) { 47dca79d73SGunnar Mills promises.push(setDefaultGateway()); 48659651e8SGunnar Mills } 49659651e8SGunnar Mills if ($scope.hostname != dataService.hostname) { 50309e06abSGunnar Mills promises.push(setHostname()); 51659651e8SGunnar Mills } 52*cb2c3060SGunnar Mills if ($scope.interface.DHCPEnabled != $scope.old_interface.DHCPEnabled) { 53*cb2c3060SGunnar Mills promises.push(setDHCPEnabled()); 54*cb2c3060SGunnar Mills } 55309e06abSGunnar Mills 56a45c3852SGunnar Mills // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways 57a45c3852SGunnar Mills if (!$scope.interface.DHCPEnabled) { 58a45c3852SGunnar Mills for (var i in $scope.interface.ipv4.values) { 59a45c3852SGunnar Mills if ($scope.interface.ipv4.values[i].Address != 60a45c3852SGunnar Mills $scope.old_interface.ipv4.values[i].Address || 61a45c3852SGunnar Mills $scope.interface.ipv4.values[i].PrefixLength != 62a45c3852SGunnar Mills $scope.old_interface.ipv4.values[i].PrefixLength || 63a45c3852SGunnar Mills $scope.interface.ipv4.values[i].Gateway != 64a45c3852SGunnar Mills $scope.old_interface.ipv4.values[i].Gateway) { 65a45c3852SGunnar Mills promises.push(setIPV4(i)); 66a45c3852SGunnar Mills } 67a45c3852SGunnar Mills } 68a45c3852SGunnar Mills } 69a45c3852SGunnar Mills 70659651e8SGunnar Mills if (promises.length) { 71dca79d73SGunnar Mills $q.all(promises).finally(function() { 72dca79d73SGunnar Mills if (!$scope.set_network_error) { 73dca79d73SGunnar Mills $scope.set_network_success = true; 74dca79d73SGunnar Mills } 75dca79d73SGunnar Mills }); 76659651e8SGunnar Mills } 77dca79d73SGunnar Mills 78dca79d73SGunnar Mills }; 79dca79d73SGunnar Mills 80dca79d73SGunnar Mills function setMACAddress() { 81dca79d73SGunnar Mills return APIUtils 827ddc7274SGunnar Mills .setMACAddress( 837ddc7274SGunnar Mills $scope.selectedInterface, $scope.interface.MACAddress) 847ddc7274SGunnar Mills .then( 85dca79d73SGunnar Mills function(data) {}, 867ddc7274SGunnar Mills function(error) { 87dca79d73SGunnar Mills console.log(JSON.stringify(error)); 887ddc7274SGunnar Mills $scope.set_network_error = 'MAC Address'; 897ddc7274SGunnar Mills }); 90dca79d73SGunnar Mills } 91dca79d73SGunnar Mills 92dca79d73SGunnar Mills function setDefaultGateway() { 93dca79d73SGunnar Mills return APIUtils.setDefaultGateway($scope.defaultgateway) 94dca79d73SGunnar Mills .then( 95dca79d73SGunnar Mills function(data) {}, 96dca79d73SGunnar Mills function(error) { 97dca79d73SGunnar Mills console.log(JSON.stringify(error)); 98dca79d73SGunnar Mills $scope.set_network_error = 'Default Gateway'; 99dca79d73SGunnar Mills }); 100dca79d73SGunnar Mills } 101309e06abSGunnar Mills 102309e06abSGunnar Mills function setHostname() { 103309e06abSGunnar Mills return APIUtils.setHostname($scope.hostname) 104309e06abSGunnar Mills .then( 105309e06abSGunnar Mills function(data) {}, 106309e06abSGunnar Mills function(error) { 107309e06abSGunnar Mills console.log(JSON.stringify(error)); 108309e06abSGunnar Mills $scope.set_network_error = 'Hostname'; 109309e06abSGunnar Mills }); 110309e06abSGunnar Mills } 111309e06abSGunnar Mills 112*cb2c3060SGunnar Mills function setDHCPEnabled() { 113*cb2c3060SGunnar Mills return APIUtils 114*cb2c3060SGunnar Mills .setDHCPEnabled( 115*cb2c3060SGunnar Mills $scope.selectedInterface, $scope.interface.DHCPEnabled) 116*cb2c3060SGunnar Mills .then( 117*cb2c3060SGunnar Mills function(data) {}, 118*cb2c3060SGunnar Mills function(error) { 119*cb2c3060SGunnar Mills console.log(JSON.stringify(error)); 120*cb2c3060SGunnar Mills $scope.set_network_error = 'DHCP'; 121*cb2c3060SGunnar Mills }); 122*cb2c3060SGunnar Mills } 123*cb2c3060SGunnar Mills 124a45c3852SGunnar Mills function setIPV4(index) { 125a45c3852SGunnar Mills // The correct way to edit an IPV4 interface is to remove it and then 126a45c3852SGunnar Mills // add a new one 127a45c3852SGunnar Mills return APIUtils 128a45c3852SGunnar Mills .deleteIPV4( 129a45c3852SGunnar Mills $scope.selectedInterface, $scope.interface.ipv4.ids[index]) 130a45c3852SGunnar Mills .then( 131a45c3852SGunnar Mills function(data) { 132a45c3852SGunnar Mills return APIUtils 133a45c3852SGunnar Mills .addIPV4( 134a45c3852SGunnar Mills $scope.selectedInterface, 135a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Address, 136a45c3852SGunnar Mills $scope.interface.ipv4.values[index].PrefixLength, 137a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Gateway) 138a45c3852SGunnar Mills .then( 139a45c3852SGunnar Mills function(data) {}, 140a45c3852SGunnar Mills function(error) { 141a45c3852SGunnar Mills console.log(JSON.stringify(error)); 142a45c3852SGunnar Mills $scope.set_network_error = 143a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Address; 144a45c3852SGunnar Mills }); 145a45c3852SGunnar Mills }, 146a45c3852SGunnar Mills function(error) { 147a45c3852SGunnar Mills console.log(JSON.stringify(error)); 148a45c3852SGunnar Mills $scope.set_network_error = 149a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Address; 150a45c3852SGunnar Mills }); 151a45c3852SGunnar Mills } 152a45c3852SGunnar Mills 1539a0094dcSGunnar Mills $scope.refresh = function() { 1549a0094dcSGunnar Mills $route.reload(); 1559a0094dcSGunnar Mills }; 1562a489554SIftekharul Islam APIUtils.getNetworkInfo().then(function(data) { 157659651e8SGunnar Mills dataService.setNetworkInfo(data); 1582a489554SIftekharul Islam $scope.network = data.formatted_data; 1592a489554SIftekharul Islam $scope.hostname = data.hostname; 160e9f5fe77SGunnar Mills $scope.defaultgateway = data.defaultgateway; 1612a489554SIftekharul Islam if ($scope.network.interface_ids.length) { 1622a489554SIftekharul Islam $scope.selectedInterface = $scope.network.interface_ids[0]; 163d27bb135SAndrew Geissler $scope.interface = 164d27bb135SAndrew Geissler $scope.network.interfaces[$scope.selectedInterface]; 165a45c3852SGunnar Mills // Copy the interface so we know later if changes were made to the 166a45c3852SGunnar Mills // page 167a45c3852SGunnar Mills $scope.old_interface = JSON.parse(JSON.stringify($scope.interface)); 1682a489554SIftekharul Islam } 1692a489554SIftekharul Islam }); 170cd789508SIftekharul Islam } 171ba5e3f34SAndrew Geissler ]); 172cd789508SIftekharul Islam 173cd789508SIftekharul Islam})(angular); 174