From d2204ab4d6251c6f676f4eb744310150e0ca2bee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Christian=20Gonz=C3=A1lez?= <cgonzalez@opennebula.systems>
Date: Tue, 11 Dec 2018 10:07:49 +0100
Subject: [PATCH] F #2138 Add Sunstone changes

---
 .../OpenNebulaJSON/VirtualMachineJSON.rb      | 10 ++++----
 src/sunstone/public/app/opennebula/vm.js      |  8 +++++++
 src/sunstone/public/app/sunstone.js           |  9 +++++++
 src/sunstone/public/app/sunstone/buttons.hbs  |  5 ++++
 .../public/app/tabs/vms-tab/actions.js        | 24 +++++++++++++++++++
 .../public/app/tabs/vms-tab/buttons.js        | 16 +++++++++++--
 .../app/tabs/vms-tab/dialogs/migrate.js       | 12 +++++++++-
 .../app/tabs/vms-tab/utils/state-actions.js   |  4 ++--
 8 files changed, 79 insertions(+), 9 deletions(-)

diff --git a/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb b/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb
index 7f54955db5..fb92b83260 100644
--- a/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb
+++ b/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb
@@ -45,8 +45,10 @@ module OpenNebulaJSON
             rc = case action_hash['perform']
                  when "deploy"       then self.deploy(action_hash['params'])
                  when "hold"         then self.hold
-                 when "livemigrate"  then self.migrate(action_hash['params'], true)
-                 when "migrate"      then self.migrate(action_hash['params'], false)
+                 when "livemigrate"  then self.migrate(action_hash['params'], true, 0)
+                 when "migrate"      then self.migrate(action_hash['params'], false, 0)
+                 when "migrate_poff"      then self.migrate(action_hash['params'], false, 1)
+                 when "migrate_poff_hard" then self.migrate(action_hash['params'], false, 2)
                  when "resume"       then self.resume
                  when "release"      then self.release
                  when "stop"         then self.stop
@@ -107,8 +109,8 @@ module OpenNebulaJSON
             super(params['hard'])
         end
 
-        def migrate(params=Hash.new, live=false)
-            super(params['host_id'], live, params['enforce'], params['ds_id'])
+        def migrate(params=Hash.new, live=false, mtype)
+            super(params['host_id'], live, params['enforce'], params['ds_id'], mtype)
         end
 
         def disk_saveas(params=Hash.new)
diff --git a/src/sunstone/public/app/opennebula/vm.js b/src/sunstone/public/app/opennebula/vm.js
index b807148ea2..6bc12bd510 100644
--- a/src/sunstone/public/app/opennebula/vm.js
+++ b/src/sunstone/public/app/opennebula/vm.js
@@ -447,6 +447,14 @@ define(function(require) {
       var action_obj = params.data.extra_param;
       OpenNebulaAction.simple_action(params, RESOURCE, "migrate", action_obj);
     },
+    "migrate_poff": function(params) {
+      var action_obj = params.data.extra_param;
+      OpenNebulaAction.simple_action(params, RESOURCE, "migrate_poff", action_obj);
+    },
+    "migrate_poff_hard": function(params) {
+      var action_obj = params.data.extra_param;
+      OpenNebulaAction.simple_action(params, RESOURCE, "migrate_poff_hard", action_obj);
+    },
     "disk_saveas": function(params) {
       var action_obj = params.data.extra_param;
       OpenNebulaAction.simple_action(params, RESOURCE, "disk_saveas", action_obj);
diff --git a/src/sunstone/public/app/sunstone.js b/src/sunstone/public/app/sunstone.js
index 2ffde83410..ac1581c8b8 100644
--- a/src/sunstone/public/app/sunstone.js
+++ b/src/sunstone/public/app/sunstone.js
@@ -408,6 +408,11 @@ define(function(require) {
           text = button.text;
           buttonCode = "<li><a class=\"" + strClass.join(" ") + "\" href=\"" + buttonName + "\">" + text + "</a></li>";
           break;
+        case "vmsmigration_buttons":
+          buttonContext = $("#" + customId + "vmsmigration_buttons", buttonsRow);
+          text = button.text;
+          buttonCode = "<li><a class=\"" + strClass.join(" ") + "\" href=\"" + buttonName + "\">" + text + "</a></li>";
+          break;
         case "more_select":
           buttonContext = $("#" + customId + "more_buttons", buttonsRow);
           text = button.text;
@@ -461,6 +466,10 @@ define(function(require) {
         $("button[data-toggle=" + customId + "vmsplanification_buttons]", actionBlock).remove();
       }
 
+      if ($("#" + customId + "vmsmigration_buttons li", actionBlock).length == 0) {
+        $("button[data-toggle=" + customId + "vmsmigration_buttons]", actionBlock).remove();
+      }
+
       if ($("#" + customId + "vmsdelete_buttons li", actionBlock).length == 0) {
         $("button[data-toggle=" + customId + "vmsdelete_buttons]", actionBlock).remove();
       }
diff --git a/src/sunstone/public/app/sunstone/buttons.hbs b/src/sunstone/public/app/sunstone/buttons.hbs
index 904fa7d084..27c9996b6c 100644
--- a/src/sunstone/public/app/sunstone/buttons.hbs
+++ b/src/sunstone/public/app/sunstone/buttons.hbs
@@ -65,6 +65,11 @@
     </button>
     <ul id='{{customId}}vmsplanification_buttons' class='only-sunstone-info only-sunstone-list dropdown-pane menu vertical' data-dropdown></ul>
 
+    <button type='button' data-toggle='{{customId}}vmsmigration_buttons' class='only-sunstone-info only-sunstone-list top_button  button secondary dropdown'>
+      <i class='fas fa-share-square'/>
+    </button>
+    <ul id='{{customId}}vmsmigration_buttons' class='only-sunstone-info only-sunstone-list dropdown-pane menu vertical' data-dropdown></ul>
+
   </span>
   <span>
     <button type='button' data-toggle='{{customId}}more_buttons' class='only-sunstone-info only-sunstone-list top_button  button secondary dropdown'>
diff --git a/src/sunstone/public/app/tabs/vms-tab/actions.js b/src/sunstone/public/app/tabs/vms-tab/actions.js
index 20ff661c35..11e23bb144 100644
--- a/src/sunstone/public/app/tabs/vms-tab/actions.js
+++ b/src/sunstone/public/app/tabs/vms-tab/actions.js
@@ -86,6 +86,8 @@ define(function(require) {
     "VM.append_template": _commonActions.appendTemplate(),
     "VM.deploy_action": _commonActions.singleAction('deploy'),
     "VM.migrate_action": _commonActions.singleAction('migrate'),
+    "VM.migrate_poff_action": _commonActions.singleAction('migrate_poff'),
+    "VM.migrate_poff_hard_action": _commonActions.singleAction('migrate_poff_hard'),
     "VM.migrate_live_action": _commonActions.singleAction('livemigrate'),
     "VM.attachdisk": _commonActions.singleAction('attachdisk'),
     "VM.detachdisk": _commonActions.singleAction('detachdisk'),
@@ -135,6 +137,27 @@ define(function(require) {
        var dialog = Sunstone.getDialog(MIGRATE_DIALOG_ID);
        dialog.reset();
        dialog.setLive(false);
+       dialog.setType(0);
+       dialog.show();
+     }
+    },
+    "VM.migrate_poff" : {
+      type: "custom",
+      call: function() {
+       var dialog = Sunstone.getDialog(MIGRATE_DIALOG_ID);
+       dialog.reset();
+       dialog.setLive(false);
+       dialog.setType(1);
+       dialog.show();
+     }
+    },
+    "VM.migrate_poff_hard" : {
+      type: "custom",
+      call: function() {
+       var dialog = Sunstone.getDialog(MIGRATE_DIALOG_ID);
+       dialog.reset();
+       dialog.setLive(false);
+       dialog.setType(2);
        dialog.show();
      }
     },
@@ -144,6 +167,7 @@ define(function(require) {
        var dialog = Sunstone.getDialog(MIGRATE_DIALOG_ID);
        dialog.reset();
        dialog.setLive(true);
+       dialog.setType(0);
        dialog.show();
      }
     },
diff --git a/src/sunstone/public/app/tabs/vms-tab/buttons.js b/src/sunstone/public/app/tabs/vms-tab/buttons.js
index 2746c5d632..e575519119 100644
--- a/src/sunstone/public/app/tabs/vms-tab/buttons.js
+++ b/src/sunstone/public/app/tabs/vms-tab/buttons.js
@@ -57,13 +57,25 @@ define(function(require) {
     "VM.migrate" : {
       type: "action",
       text: Locale.tr("Migrate"),
-      layout: "vmsplanification_buttons",
+      layout: "vmsmigration_buttons",
+      custom_classes : "state-dependent"
+    },
+    "VM.migrate_poff" : {
+      type: "action",
+      text: Locale.tr("Migrate") + ' <span class="label secondary radius">' + Locale.tr("Poweroff") + '</span>',
+      layout: "vmsmigration_buttons",
+      custom_classes : "state-dependent"
+    },
+    "VM.migrate_poff_hard" : {
+      type: "action",
+      text: Locale.tr("Migrate") + ' <span class="label secondary radius">' + Locale.tr("Poweroff-hard") + '</span>',
+      layout: "vmsmigration_buttons",
       custom_classes : "state-dependent"
     },
     "VM.migrate_live" : {
       type: "action",
       text: Locale.tr("Migrate") + ' <span class="label secondary radius">' + Locale.tr("live") + '</span>',
-      layout: "vmsplanification_buttons",
+      layout: "vmsmigration_buttons",
       custom_classes : "state-dependent"
     },
     "VM.hold" : {
diff --git a/src/sunstone/public/app/tabs/vms-tab/dialogs/migrate.js b/src/sunstone/public/app/tabs/vms-tab/dialogs/migrate.js
index 3a0a4e93b7..06326b2085 100644
--- a/src/sunstone/public/app/tabs/vms-tab/dialogs/migrate.js
+++ b/src/sunstone/public/app/tabs/vms-tab/dialogs/migrate.js
@@ -62,6 +62,7 @@ define(function(require) {
   Dialog.prototype.onShow = _onShow;
   Dialog.prototype.setup = _setup;
   Dialog.prototype.setLive = _setLive;
+  Dialog.prototype.setType = _setType;
 
   return Dialog;
 
@@ -103,8 +104,12 @@ define(function(require) {
       $.each(Sunstone.getDataTable(TAB_ID).elements(), function(index, elem) {
         if (that.live) {
           Sunstone.runAction("VM.migrate_live_action", elem, extra_info);
-        } else {
+        } else if (that.type == 0) {
           Sunstone.runAction("VM.migrate_action", elem, extra_info);
+        } else if (that.type == 1){
+          Sunstone.runAction("VM.migrate_poff_action", elem, extra_info);
+        } else if (that.type == 2){
+          Sunstone.runAction("VM.migrate_poff_hard_action", elem, extra_info);
         }
       });
 
@@ -152,4 +157,9 @@ define(function(require) {
   function _setLive(live) {
     this.live = live;
   }
+
+  // @param [Int] type Set migration type
+  function _setType(type) {
+    this.type = type;
+  }
 });
diff --git a/src/sunstone/public/app/tabs/vms-tab/utils/state-actions.js b/src/sunstone/public/app/tabs/vms-tab/utils/state-actions.js
index 4803a4d947..fabc0b58f8 100644
--- a/src/sunstone/public/app/tabs/vms-tab/utils/state-actions.js
+++ b/src/sunstone/public/app/tabs/vms-tab/utils/state-actions.js
@@ -60,7 +60,7 @@ define(function(require) {
   LCM_STATE_ACTIONS[ OpenNebulaVM.LCM_STATES.PROLOG ] = ["VM.updateconf"];
   LCM_STATE_ACTIONS[ OpenNebulaVM.LCM_STATES.BOOT ] = [];
   LCM_STATE_ACTIONS[ OpenNebulaVM.LCM_STATES.RUNNING ] =
-    ["VM.stop", "VM.suspend", "VM.reboot", "VM.reboot_hard", "VM.resched", "VM.unresched", "VM.poweroff", "VM.poweroff_hard", "VM.undeploy", "VM.undeploy_hard", "VM.migrate", "VM.migrate_live", "VM.attachdisk", "VM.detachdisk", "VM.attachnic", "VM.detachnic", "VM.disk_saveas", "VM.disk_snapshot_create", "VM.disk_snapshot_delete", "VM.terminate", "VM.terminate_hard", "VM.disk_resize"];
+    ["VM.stop", "VM.suspend", "VM.reboot", "VM.reboot_hard", "VM.resched", "VM.unresched", "VM.poweroff", "VM.poweroff_hard", "VM.undeploy", "VM.undeploy_hard", "VM.migrate", "VM.migrate_live", "VM.migrate_poff", "VM.migrate_poff_hard", "VM.attachdisk", "VM.detachdisk", "VM.attachnic", "VM.detachnic", "VM.disk_saveas", "VM.disk_snapshot_create", "VM.disk_snapshot_delete", "VM.terminate", "VM.terminate_hard", "VM.disk_resize"];
   LCM_STATE_ACTIONS[ OpenNebulaVM.LCM_STATES.MIGRATE ] = [];
   LCM_STATE_ACTIONS[ OpenNebulaVM.LCM_STATES.SAVE_STOP ] = [];
   LCM_STATE_ACTIONS[ OpenNebulaVM.LCM_STATES.SAVE_SUSPEND ] = [];
@@ -74,7 +74,7 @@ define(function(require) {
   LCM_STATE_ACTIONS[ OpenNebulaVM.LCM_STATES.FAILURE ] = ["VM.terminate"];
   LCM_STATE_ACTIONS[ OpenNebulaVM.LCM_STATES.CLEANUP_RESUBMIT ] = ["VM.updateconf"];
   LCM_STATE_ACTIONS[ OpenNebulaVM.LCM_STATES.UNKNOWN ] =
-    ["VM.resched", "VM.unresched", "VM.poweroff", "VM.poweroff_hard", "VM.undeploy", "VM.undeploy_hard", "VM.migrate", "VM.migrate_live", "VM.resume", "VM.terminate", "VM.terminate_hard"];
+    ["VM.resched", "VM.unresched", "VM.poweroff", "VM.poweroff_hard", "VM.undeploy", "VM.undeploy_hard", "VM.migrate", "VM.migrate_live", "VM.migrate_poff", "VM.migrate_poff_hard", "VM.resume", "VM.terminate", "VM.terminate_hard"];
   LCM_STATE_ACTIONS[ OpenNebulaVM.LCM_STATES.HOTPLUG ] = [];
   LCM_STATE_ACTIONS[ OpenNebulaVM.LCM_STATES.SHUTDOWN_POWEROFF ] = ["VM.updateconf"];
   LCM_STATE_ACTIONS[ OpenNebulaVM.LCM_STATES.BOOT_UNKNOWN ] = [];