From f220bdd59fef02b5d13ef9bb11abeb7fd5a340e2 Mon Sep 17 00:00:00 2001 From: Xin Date: Fri, 22 Jul 2016 01:01:32 -0700 Subject: [PATCH] support import/export config Change-Id: Ia7802e8236ca2f90b97331961b9cf205cbd521aa --- .../public/ui/scripts/controllers/about.js | 16 ++++++++++- .../public/ui/scripts/controllers/config.js | 17 ++++++++++++ .../ui/scripts/controllers/config_storage.js | 27 ++++++++++++++----- kb_server/public/ui/views/config.html | 9 +++++++ kb_server/public/ui/views/config_storage.html | 9 +++++++ 5 files changed, 71 insertions(+), 7 deletions(-) diff --git a/kb_server/public/ui/scripts/controllers/about.js b/kb_server/public/ui/scripts/controllers/about.js index 5fb12c1..226483f 100644 --- a/kb_server/public/ui/scripts/controllers/about.js +++ b/kb_server/public/ui/scripts/controllers/about.js @@ -178,7 +178,7 @@ angular.module("kbWebApp").controller("AboutCtrl", function($scope, $http, $loca }, this.setLogNum = function(lognumber) { return logNum = lognumber; }; -}).service("showAlert", function($mdDialog) { +}).service("showAlert", function($mdDialog, $q) { this.showAlert = function(words, ev) { var alert = $mdDialog.alert({ title: "Attention", @@ -188,5 +188,19 @@ angular.module("kbWebApp").controller("AboutCtrl", function($scope, $http, $loca $mdDialog.show(alert)["finally"](function() { alert = void 0; }); + }, this.showPrompt = function($scope, $event) { + var deferred = $q.defer(), parentEl = angular.element(document.body); + return $mdDialog.show({ + parent: parentEl, + targetEvent: $event, + template: '

Configuration Import

Paste Json Format Configuration Below:

Cancel Submit
', + controller: function($scope, $mdDialog, kbCookie) { + $scope.cancel = function() { + $mdDialog.cancel(), deferred.reject(); + }, $scope.answer = function() { + kbCookie.setConfig(angular.fromJson($scope.dialog.result)), $mdDialog.hide(), deferred.resolve(); + }; + } + }), deferred.promise; }; }); \ No newline at end of file diff --git a/kb_server/public/ui/scripts/controllers/config.js b/kb_server/public/ui/scripts/controllers/config.js index 3d07c1e..c849775 100644 --- a/kb_server/public/ui/scripts/controllers/config.js +++ b/kb_server/public/ui/scripts/controllers/config.js @@ -32,6 +32,14 @@ angular.module("kbWebApp").controller("ConfigCtrl", function($scope, $http, $loc function reset() { $scope.selectedA = [], $scope.selectedB = [], $scope.selectedC = [], $scope.toggle = !1; } + function downloadFile(fileName, content) { + var aLink = document.createElement("a"), blob = new Blob([ content ]), evt = document.createEvent("HTMLEvents"); + evt.initEvent("click", !1, !1), aLink.download = fileName, aLink.href = URL.createObjectURL(blob), + aLink.dispatchEvent(evt); + } + function to2(num) { + return 10 > num ? "0" + num : 99 > num ? "" + num : -1; + } this.awesomeThings = [ "HTML5 Boilerplate", "AngularJS", "Karma" ], "" === kbCookie.getSessionID() ? $location.path("/Login") : kbCookie.checkMode("http"), $(window).on("hashchange", locationChange.change()), $scope.sessionID = kbCookie.getSessionID(), $scope.status = kbCookie.getStatus(), $scope.dashstatus1 = "active", $scope.dashstatus1vis = !0, @@ -209,5 +217,14 @@ angular.module("kbWebApp").controller("ConfigCtrl", function($scope, $http, $loc clients_rack: "" }), init(), $scope.getTopology()) : 3 == $scope.availability_zone && ($scope.config.server.availability_zone = "", $scope.config.client.availability_zone = ""); + }, $scope["import"] = function() { + showAlert.showPrompt($scope).then(function(response) { + $scope.config = kbCookie.getConfig(), "READY" !== $scope.status && "STAGED" !== $scope.status && "" !== $scope.status || $scope.changeConfig(), + console.log("import config"); + }, function(response) {}); + }, $scope["export"] = function() { + "READY" !== $scope.status && "STAGED" !== $scope.status && "" !== $scope.status || $scope.changeConfig(); + var date = new Date(), m = to2(date.getMonth() + 1), d = to2(date.getDate()), h = to2(date.getHours()), min = to2(date.getMinutes()), filename = "ConfigFile" + m + d + h + min + ".json", myresult = JSON.stringify(kbCookie.getConfig(), null, " "); + downloadFile(filename, myresult); }; }); \ No newline at end of file diff --git a/kb_server/public/ui/scripts/controllers/config_storage.js b/kb_server/public/ui/scripts/controllers/config_storage.js index 2ecfe65..d759bdd 100644 --- a/kb_server/public/ui/scripts/controllers/config_storage.js +++ b/kb_server/public/ui/scripts/controllers/config_storage.js @@ -17,6 +17,14 @@ permissions and limitations under the License. "use strict"; angular.module("kbWebApp").controller("StorageConfigCtrl", function($scope, $http, $location, showAlert, kbHttp, kbCookie, locationChange) { + function downloadFile(fileName, content) { + var aLink = document.createElement("a"), blob = new Blob([ content ]), evt = document.createEvent("HTMLEvents"); + evt.initEvent("click", !1, !1), aLink.download = fileName, aLink.href = URL.createObjectURL(blob), + aLink.dispatchEvent(evt); + } + function to2(num) { + return 10 > num ? "0" + num : 99 > num ? "" + num : -1; + } this.awesomeThings = [ "HTML5 Boilerplate", "AngularJS", "Karma" ], "" === kbCookie.getSessionID() ? $location.path("/Login") : kbCookie.checkMode("storage"), $(window).on("hashchange", locationChange.change()), $scope.sessionID = kbCookie.getSessionID(), $scope.status = kbCookie.getStatus(), $scope.dash1status1 = "active", $scope.dash1status1vis = !0, @@ -140,18 +148,25 @@ angular.module("kbWebApp").controller("StorageConfigCtrl", function($scope, $htt console.log("get running config error:"), console.log(response); }); }, $scope.getRunConfig(), $scope.changeConfig = function() { - "READY" === $scope.status || "" === $scope.status ? 1 == $scope.server.$valid && 1 == $scope.general.$valid ? (kbCookie.setConfig($scope.config), + "READY" === $scope.status || "STAGED" === $scope.status || "" === $scope.status ? 1 == $scope.server.$valid && 1 == $scope.general.$valid ? (kbCookie.setConfig($scope.config), $scope.chaCon = { kb_cfg: {}, topo_cfg: {} - }, $scope.chaCon.kb_cfg = kbCookie.getConfig(), kbCookie.setTopology({ - servers_rack: "", - clients_rack: "" - }), $scope.chaCon.topo_cfg = kbCookie.getTopology(), $scope.config.server.availability_zone = "", - $scope.config.client.availability_zone = "", console.log($scope.chaCon), kbHttp.putMethod("/config/running_config/" + $scope.sessionID, $scope.chaCon).then(function(response) { + }, $scope.chaCon.kb_cfg = kbCookie.getConfig(), $scope.chaCon.topo_cfg = kbCookie.getTopology(), + $scope.config.server.availability_zone = "", $scope.config.client.availability_zone = "", + console.log($scope.chaCon), kbHttp.putMethod("/config/running_config/" + $scope.sessionID, $scope.chaCon).then(function(response) { console.log("change running config"); }, function(response) { showAlert.showAlert("Failed to update configuration!"); })) : showAlert.showAlert("Please check your inputs!") : showAlert.showAlert("Configuration cannot be changed now!"); + }, $scope["import"] = function() { + showAlert.showPrompt($scope).then(function(response) { + $scope.config = kbCookie.getConfig(), "READY" !== $scope.status && "STAGED" !== $scope.status && "" !== $scope.status || $scope.changeConfig(), + console.log("import config"); + }, function(response) {}); + }, $scope["export"] = function() { + "READY" !== $scope.status && "STAGED" !== $scope.status && "" !== $scope.status || $scope.changeConfig(); + var date = new Date(), m = to2(date.getMonth() + 1), d = to2(date.getDate()), h = to2(date.getHours()), min = to2(date.getMinutes()), filename = "ConfigFile" + m + d + h + min + ".json", myresult = JSON.stringify(kbCookie.getConfig(), null, " "); + downloadFile(filename, myresult); }; }); \ No newline at end of file diff --git a/kb_server/public/ui/views/config.html b/kb_server/public/ui/views/config.html index f6a5000..708f33a 100644 --- a/kb_server/public/ui/views/config.html +++ b/kb_server/public/ui/views/config.html @@ -17,6 +17,15 @@ permissions and limitations under the License.
+
+Action + + +
diff --git a/kb_server/public/ui/views/config_storage.html b/kb_server/public/ui/views/config_storage.html index a22ce9c..b2b75e9 100644 --- a/kb_server/public/ui/views/config_storage.html +++ b/kb_server/public/ui/views/config_storage.html @@ -17,6 +17,15 @@ permissions and limitations under the License.
+
+Action + + +