Set DefaultGateway on "Save settings"

Set the DefaultGateway field when the user selects "Save settings"
and accepts the popup. Moved to q.all() with a promise array.
Later commits will add to this array and do a check to make sure
the field changed before setting the value.

Tested: Set the DefaultGateway on a Witherspoon
Change-Id: Ica6abe14169f00bc3195f34a08db61ddbc09fa92
Signed-off-by: Gunnar Mills <gmills@us.ibm.com>
diff --git a/app/common/services/api-utils.js b/app/common/services/api-utils.js
index 71756d4..c8617d3 100644
--- a/app/common/services/api-utils.js
+++ b/app/common/services/api-utils.js
@@ -209,6 +209,22 @@
                 return response.data;
               });
         },
+        setDefaultGateway: function(defaultGateway) {
+          return $http({
+                   method: 'PUT',
+                   url: DataService.getHost() +
+                       '/xyz/openbmc_project/network/config/attr/DefaultGateway',
+                   headers: {
+                     'Accept': 'application/json',
+                     'Content-Type': 'application/json'
+                   },
+                   withCredentials: true,
+                   data: JSON.stringify({'data': defaultGateway})
+                 })
+              .then(function(response) {
+                return response.data;
+              });
+        },
         getLEDState: function() {
           var deferred = $q.defer();
           $http({
diff --git a/app/configuration/controllers/network-controller.js b/app/configuration/controllers/network-controller.js
index 8acc69e..5fe1594 100644
--- a/app/configuration/controllers/network-controller.js
+++ b/app/configuration/controllers/network-controller.js
@@ -10,8 +10,8 @@
   'use strict';
 
   angular.module('app.configuration').controller('networkController', [
-    '$scope', '$window', 'APIUtils', 'dataService', '$route',
-    function($scope, $window, APIUtils, dataService, $route) {
+    '$scope', '$window', 'APIUtils', 'dataService', '$route', '$q',
+    function($scope, $window, APIUtils, dataService, $route, $q) {
       $scope.dataService = dataService;
       $scope.network = {};
       $scope.interface = {};
@@ -33,20 +33,41 @@
         $scope.confirm_settings = false;
         $scope.set_network_error = '';
         $scope.set_network_success = false;
+        var promises = [];
+
         // TODO openbmc/openbmc#3165: check if the network settings
         // changed before setting
-        APIUtils
+        promises.push(setMACAddress());
+        promises.push(setDefaultGateway());
+        $q.all(promises).finally(function() {
+          if (!$scope.set_network_error) {
+            $scope.set_network_success = true;
+          }
+        });
+
+      };
+
+      function setMACAddress() {
+        return APIUtils
             .setMACAddress(
                 $scope.selectedInterface, $scope.interface.MACAddress)
             .then(
-                function(data) {
-                  $scope.set_network_success = true;
-                },
+                function(data) {},
                 function(error) {
-                  console.log(error);
+                  console.log(JSON.stringify(error));
                   $scope.set_network_error = 'MAC Address';
                 });
-      };
+      }
+
+      function setDefaultGateway() {
+        return APIUtils.setDefaultGateway($scope.defaultgateway)
+            .then(
+                function(data) {},
+                function(error) {
+                  console.log(JSON.stringify(error));
+                  $scope.set_network_error = 'Default Gateway';
+                });
+      }
       $scope.refresh = function() {
         $route.reload();
       };