Keep the selected interface

If given 2 network interfaces: eth0 and eth0_60 (a VLAN interface),
and eth0_60 is selected a "cancel" of the form would result in eth0
being selected. Fixed this by moving getNetworkInfo() to a function
and calling it on a cancel. Also, call this new function after a
successful network set (a temporary fix).

Tested: The above case keeps eth0_60 selected.
Change-Id: I136ca1030cbbb053ca3b96241197c56488204dfd
Signed-off-by: Gunnar Mills <gmills@us.ibm.com>
diff --git a/app/configuration/controllers/network-controller.js b/app/configuration/controllers/network-controller.js
index b0daefd..d528e7a 100644
--- a/app/configuration/controllers/network-controller.js
+++ b/app/configuration/controllers/network-controller.js
@@ -25,6 +25,8 @@
       $scope.confirm_settings = false;
       $scope.loading = false;
 
+      loadNetworkInfo();
+
       $scope.selectInterface = function(interfaceId) {
         $scope.interface = $scope.network.interfaces[interfaceId];
         // Copy the interface so we know later if changes were made to the page
@@ -121,7 +123,7 @@
               // settings.
               // TODO: The reload is not ideal. Revisit this.
               $timeout(function() {
-                $route.reload();
+                loadNetworkInfo();
               }, 4000);
             }
           });
@@ -222,22 +224,29 @@
       }
 
       $scope.refresh = function() {
-        $route.reload();
+        loadNetworkInfo();
       };
-      APIUtils.getNetworkInfo().then(function(data) {
-        dataService.setNetworkInfo(data);
-        $scope.network = data.formatted_data;
-        $scope.hostname = data.hostname;
-        $scope.defaultgateway = data.defaultgateway;
-        if ($scope.network.interface_ids.length) {
-          $scope.selectedInterface = $scope.network.interface_ids[0];
-          $scope.interface =
-              $scope.network.interfaces[$scope.selectedInterface];
-          // Copy the interface so we know later if changes were made to the
-          // page
-          $scope.old_interface = JSON.parse(JSON.stringify($scope.interface));
-        }
-      });
+
+      function loadNetworkInfo() {
+        APIUtils.getNetworkInfo().then(function(data) {
+          dataService.setNetworkInfo(data);
+          $scope.network = data.formatted_data;
+          $scope.hostname = data.hostname;
+          $scope.defaultgateway = data.defaultgateway;
+          if ($scope.network.interface_ids.length) {
+            // Use the first network interface if the user hasn't choosen one
+            if (!$scope.selectedInterface ||
+                !$scope.network.interfaces[$scope.selectedInterface]) {
+              $scope.selectedInterface = $scope.network.interface_ids[0];
+            }
+            $scope.interface =
+                $scope.network.interfaces[$scope.selectedInterface];
+            // Copy the interface so we know later if changes were made to the
+            // page
+            $scope.old_interface = JSON.parse(JSON.stringify($scope.interface));
+          }
+        });
+      }
     }
   ]);