From 2d922a4ad6bdbea58ae624491bb4dba9fbda2271 Mon Sep 17 00:00:00 2001 From: Frederick Borges Date: Fri, 30 Jul 2021 12:48:05 +0200 Subject: [PATCH] B #5289: Sunstone with new schedule actions API (#1390) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tino Vázquez --- share/linters/.rubocop.yml | 1 - .../OpenNebulaJSON/VirtualMachineJSON.rb | 237 ++++-- src/sunstone/models/sunstone_vm_helper.rb | 1 + src/sunstone/public/app/opennebula/vm.js | 12 + src/sunstone/public/app/sunstone.js | 4 +- .../form-panels/update.js | 2 - .../panels/sched-actions/html.hbs | 24 +- .../panels/sched_actions.js | 435 +--------- .../form-panels/create.js | 126 +-- .../form-panels/instantiate.js | 89 +- .../form-panels/create/wizard-tabs/actions.js | 92 +-- .../templates-tab/form-panels/instantiate.js | 103 +-- .../public/app/tabs/vms-tab/actions.js | 3 + .../app/tabs/vms-tab/form-panels/create.js | 2 - .../public/app/tabs/vms-tab/panels/actions.js | 196 +---- .../tabs/vms-tab/utils/datatable-common.js | 8 +- src/sunstone/public/app/utils/actions.js | 82 -- src/sunstone/public/app/utils/humanize.js | 10 +- src/sunstone/public/app/utils/leases.js | 277 ------- .../public/app/utils/schedule_action.js | 769 +++++++++++++++--- .../public/app/utils/schedule_action/html.hbs | 27 +- .../app/utils/schedule_action/table-row.hbs | 45 + .../app/utils/schedule_action/table.hbs | 21 +- 23 files changed, 1067 insertions(+), 1499 deletions(-) delete mode 100644 src/sunstone/public/app/utils/actions.js delete mode 100644 src/sunstone/public/app/utils/leases.js create mode 100644 src/sunstone/public/app/utils/schedule_action/table-row.hbs diff --git a/share/linters/.rubocop.yml b/share/linters/.rubocop.yml index 5904c4063a..5f87416bf6 100644 --- a/share/linters/.rubocop.yml +++ b/share/linters/.rubocop.yml @@ -254,7 +254,6 @@ AllCops: - src/sunstone/models/OpenNebulaJSON/UserJSON.rb - src/sunstone/models/OpenNebulaJSON/AclJSON.rb - src/sunstone/models/OpenNebulaJSON/JSONUtils.rb - - src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb - src/sunstone/models/OpenNebulaJSON/MarketPlaceAppJSON.rb - src/sunstone/models/OpenNebulaJSON/ImageJSON.rb - src/sunstone/models/OpenNebulaJSON/ZoneJSON.rb diff --git a/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb b/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb index 65d97b2505..93225d5558 100644 --- a/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb +++ b/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb @@ -19,7 +19,9 @@ require 'opennebula/virtual_machine_ext' module OpenNebulaJSON + # Sunstone VirtualMachineJSON class class VirtualMachineJSON < OpenNebula::VirtualMachine + include JSONUtils def create(template_json) @@ -34,158 +36,211 @@ module OpenNebulaJSON template = template_to_str(vm_hash) end - self.allocate(template) - end + allocate(template) + end def perform_action(template_json) - action_hash = parse_json(template_json,'action') + action_hash = parse_json(template_json, 'action') if OpenNebula.is_error?(action_hash) return action_hash end - 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, 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 - when "suspend" then self.suspend - when "disk_saveas" then self.disk_saveas(action_hash['params']) - when "snapshot_create" then self.snapshot_create(action_hash['params']) - when "snapshot_revert" then self.snapshot_revert(action_hash['params']) - when "snapshot_delete" then self.snapshot_delete(action_hash['params']) - when "disk_snapshot_create" then self.disk_snapshot_create(action_hash['params']) - when "disk_snapshot_revert" then self.disk_snapshot_revert(action_hash['params']) - when "disk_snapshot_rename" then self.disk_snapshot_rename(action_hash['params']) - when "disk_snapshot_delete" then self.disk_snapshot_delete(action_hash['params']) - when "terminate" then self.terminate(action_hash['params']) - when "reboot" then self.reboot(action_hash['params']) - when "poweroff" then self.poweroff(action_hash['params']) - when "chown" then self.chown(action_hash['params']) - when "chmod" then self.chmod_octet(action_hash['params']) - when "resize" then self.resize(action_hash['params']) - when "attachdisk" then self.disk_attach(action_hash['params']) - when "detachdisk" then self.disk_detach(action_hash['params']) - when "attachnic" then self.nic_attach(action_hash['params']) - when "detachnic" then self.nic_detach(action_hash['params']) - when "update" then self.update(action_hash['params']) - when "updateconf" then self.updateconf(action_hash['params']) - when "rename" then self.rename(action_hash['params']) - when "undeploy" then self.undeploy(action_hash['params']) - when "resched" then self.resched - when "unresched" then self.unresched - when "recover" then self.recover(action_hash['params']) - when "save_as_template" then self.save_as_template(action_hash['params']) - when "disk_resize" then self.disk_resize(action_hash['params']) - when "lock" then self.lock(action_hash['params']['level'].to_i) - when "unlock" then self.unlock() - else - error_msg = "#{action_hash['perform']} action not " << - " available for this resource" - OpenNebula::Error.new(error_msg) - end + case action_hash['perform'] + when 'deploy' + deploy(action_hash['params']) + when 'hold' + hold + when 'livemigrate' + migrate(0, action_hash['params'], true) + when 'migrate' + migrate(0, action_hash['params'], false) + when 'migrate_poff' + migrate(1, action_hash['params'], false) + when 'migrate_poff_hard' + migrate(2, action_hash['params'], false) + when 'resume' + resume + when 'release' + release + when 'stop' + stop + when 'suspend' + suspend + when 'disk_saveas' + disk_saveas(action_hash['params']) + when 'snapshot_create' + snapshot_create(action_hash['params']) + when 'snapshot_revert' + snapshot_revert(action_hash['params']) + when 'snapshot_delete' + snapshot_delete(action_hash['params']) + when 'disk_snapshot_create' + disk_snapshot_create(action_hash['params']) + when 'disk_snapshot_revert' + disk_snapshot_revert(action_hash['params']) + when 'disk_snapshot_rename' + disk_snapshot_rename(action_hash['params']) + when 'disk_snapshot_delete' + disk_snapshot_delete(action_hash['params']) + when 'terminate' + terminate(action_hash['params']) + when 'reboot' + reboot(action_hash['params']) + when 'poweroff' + poweroff(action_hash['params']) + when 'chown' + chown(action_hash['params']) + when 'chmod' + chmod_octet(action_hash['params']) + when 'resize' + resize(action_hash['params']) + when 'attachdisk' + disk_attach(action_hash['params']) + when 'detachdisk' + disk_detach(action_hash['params']) + when 'attachnic' + nic_attach(action_hash['params']) + when 'detachnic' + nic_detach(action_hash['params']) + when 'update' + update(action_hash['params']) + when 'updateconf' + updateconf(action_hash['params']) + when 'rename' + rename(action_hash['params']) + when 'undeploy' + undeploy(action_hash['params']) + when 'resched' + resched + when 'unresched' + unresched + when 'recover' + recover(action_hash['params']) + when 'save_as_template' + save_as_template(action_hash['params']) + when 'disk_resize' + disk_resize(action_hash['params']) + when 'lock' + lock(action_hash['params']['level'].to_i) + when 'unlock' + unlock + when 'sched_action_add' + sched_action_add(action_hash['params']) + when 'sched_action_update' + sched_action_update(action_hash['params']) + when 'sched_action_delete' + sched_action_delete(action_hash['params']) + else + error_msg = "#{action_hash['perform']} action not " \ + ' available for this resource' + OpenNebula::Error.new(error_msg) + end end - def deploy(params=Hash.new) + def deploy(params = {}) super(params['host_id'], params['enforce'], params['ds_id']) end - def undeploy(params=Hash.new) + def undeploy(params = {}) super(params['hard']) end - def terminate(params=Hash.new) + def terminate(params = {}) super(params['hard']) end - def reboot(params=Hash.new) + def reboot(params = {}) super(params['hard']) end - def poweroff(params=Hash.new) + def poweroff(params = {}) super(params['hard']) end - def migrate(params=Hash.new, live=false, mtype) - super(params['host_id'], live, params['enforce'], params['ds_id'], mtype) + def migrate(mtype, params = {}, live = false) + super(params['host_id'], + live, + params['enforce'], + params['ds_id'], + mtype + ) end - def disk_saveas(params=Hash.new) + def disk_saveas(params = {}) super(params['disk_id'].to_i, params['image_name'], params['type'], params['snapshot_id'].to_i) end - def disk_resize(params=Hash.new) + def disk_resize(params = {}) super(params['disk_id'].to_i, params['new_size']) end - def snapshot_create(params=Hash.new) + def snapshot_create(params = {}) super(params['snapshot_name']) end - def snapshot_revert(params=Hash.new) + def snapshot_revert(params = {}) super(params['snapshot_id'].to_i) end - def snapshot_delete(params=Hash.new) + def snapshot_delete(params = {}) super(params['snapshot_id'].to_i) end - def disk_snapshot_create(params=Hash.new) + def disk_snapshot_create(params = {}) super(params['disk_id'].to_i, params['snapshot_name']) end - def disk_snapshot_revert(params=Hash.new) + def disk_snapshot_revert(params = {}) super(params['disk_id'].to_i, params['snapshot_id'].to_i) end - def disk_snapshot_rename(params=Hash.new) - super(params['disk_id'].to_i, params['snapshot_id'].to_i, params['new_name']) + def disk_snapshot_rename(params = {}) + super(params['disk_id'].to_i, + params['snapshot_id'].to_i, + params['new_name'] + ) end - def disk_snapshot_delete(params=Hash.new) + def disk_snapshot_delete(params = {}) super(params['disk_id'].to_i, params['snapshot_id'].to_i) end - def chown(params=Hash.new) - super(params['owner_id'].to_i,params['group_id'].to_i) + def chown(params = {}) + super(params['owner_id'].to_i, params['group_id'].to_i) end - def chmod_octet(params=Hash.new) + def chmod_octet(params = {}) super(params['octet']) end - def resize(params=Hash.new) + def resize(params = {}) template_json = params['vm_template'] template = template_to_str(template_json) super(template, params['enforce']) end - def disk_attach(params=Hash.new) + def disk_attach(params = {}) template_json = params['disk_template'] template = template_to_str(template_json) super(template) end - def disk_detach(params=Hash.new) + def disk_detach(params = {}) super(params['disk_id'].to_i) end - def nic_attach(params=Hash.new) + def nic_attach(params = {}) template_json = params['nic_template'] template = template_to_str(template_json) super(template) end - def nic_detach(params=Hash.new) + def nic_detach(params = {}) super(params['nic_id'].to_i) end - def update(params=Hash.new) + def update(params = {}) if !params['append'].nil? super(params['template_raw'], params['append']) else @@ -193,31 +248,47 @@ module OpenNebulaJSON end end - def updateconf(params=Hash.new) + def updateconf(params = {}) super(params['template_raw']) end - def rename(params=Hash.new) + def rename(params = {}) super(params['name']) end - def recover(params=Hash.new) + def recover(params = {}) super(params['result'].to_i) end - def save_as_template(params=Hash.new) + def save_as_template(params = {}) begin - vm_new = VirtualMachine.new(VirtualMachine.build_xml(@pe_id), @client) - + vm_new = VirtualMachine.new( + VirtualMachine.build_xml(@pe_id), + @client + ) + vm_new.extend(VirtualMachineExt) - + vm_new.save_as_template(params['name'], params['description'], :persistent => params['persistent']) - - rescue Exception => e + rescue StandardError => e OpenNebula::Error.new(e.message) end end + + def sched_action_add(params = {}) + super(params['sched_template']) + end + + def sched_action_update(params = {}) + super(params['sched_id'], params['sched_template']) + end + + def sched_action_delete(params = {}) + super(params['sched_id']) + end + end + end diff --git a/src/sunstone/models/sunstone_vm_helper.rb b/src/sunstone/models/sunstone_vm_helper.rb index cba5134e61..d8c7094f86 100644 --- a/src/sunstone/models/sunstone_vm_helper.rb +++ b/src/sunstone/models/sunstone_vm_helper.rb @@ -14,6 +14,7 @@ # limitations under the License. # #--------------------------------------------------------------------------- # +# Sunstone VMHelper module module SunstoneVMHelper class << self diff --git a/src/sunstone/public/app/opennebula/vm.js b/src/sunstone/public/app/opennebula/vm.js index a09bc389bd..c425bf5591 100644 --- a/src/sunstone/public/app/opennebula/vm.js +++ b/src/sunstone/public/app/opennebula/vm.js @@ -827,6 +827,18 @@ define(function(require) { return OpenNebulaAction.getName(id, RESOURCE); }, "isvCenterVM": isVCenterVM, + "sched_action_add" : function(params) { + var action_obj = {"sched_template": params.data.extra_param}; + OpenNebulaAction.simple_action(params, RESOURCE, "sched_action_add", action_obj); + }, + "sched_action_update" : function(params) { + var action_obj = params.data.extra_param; + OpenNebulaAction.simple_action(params, RESOURCE, "sched_action_update", action_obj); + }, + "sched_action_delete" : function(params) { + var action_obj = { "sched_id" : params.data.extra_param }; + OpenNebulaAction.simple_action(params, RESOURCE, "sched_action_delete", action_obj); + } }; function hostnameStr(element, navigationLink = false) { diff --git a/src/sunstone/public/app/sunstone.js b/src/sunstone/public/app/sunstone.js index cc07a799e8..e02d38a6a0 100644 --- a/src/sunstone/public/app/sunstone.js +++ b/src/sunstone/public/app/sunstone.js @@ -941,7 +941,7 @@ define(function(require) { //can be use to run action depending on conditions and notify them //if desired. Returns 1 if some problem has been detected: i.e //the condition to run the action is not met, the action is not found - var _runAction = function(action, dataArg, extraParam) { + var _runAction = function(action, dataArg, extraParam, callback) { var actions = SunstoneCfg["actions"]; if (!actions[action]) { Notifier.notifyError("Action " + action + " not defined"); @@ -962,7 +962,7 @@ define(function(require) { } var call = actionCfg["call"]; - var callback = actionCfg["callback"]; + var callback = callback || actionCfg["callback"]; var err = actionCfg["error"]; switch (actionCfg.type){ diff --git a/src/sunstone/public/app/tabs/oneflow-services-tab/form-panels/update.js b/src/sunstone/public/app/tabs/oneflow-services-tab/form-panels/update.js index 03796ae8b1..db45c29862 100644 --- a/src/sunstone/public/app/tabs/oneflow-services-tab/form-panels/update.js +++ b/src/sunstone/public/app/tabs/oneflow-services-tab/form-panels/update.js @@ -59,8 +59,6 @@ define(function(require) { FormPanel.prototype.fill = _fill; FormPanel.prototype.addRoleTab = _add_role_tab; - - return FormPanel; /* diff --git a/src/sunstone/public/app/tabs/oneflow-services-tab/panels/sched-actions/html.hbs b/src/sunstone/public/app/tabs/oneflow-services-tab/panels/sched-actions/html.hbs index f5720b66ff..f73225e171 100644 --- a/src/sunstone/public/app/tabs/oneflow-services-tab/panels/sched-actions/html.hbs +++ b/src/sunstone/public/app/tabs/oneflow-services-tab/panels/sched-actions/html.hbs @@ -1,11 +1,19 @@ {{! -------------------------------------------------------------------------- }} -{{! Copyright 2002-2021, OpenNebula Project, OpenNebula Systems }} {{! }} -{{! Licensed under the Apache License, Version 2.0 (the "License"); you may }} {{! not use this -file except in compliance with the License. You may obtain }} {{! a copy of the License at }} {{! }} {{! http://www.apache.org/licenses/LICENSE-2.0 -}} {{! }} {{! Unless required by applicable law or agreed to in writing, software }} {{! distributed under the License is -distributed on an "AS IS" BASIS, }} {{! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }} {{! See -the License for the specific language governing permissions and }} {{! limitations under the License. }} {{! -------------------------------------------------------------------------- -}} +{{! Copyright 2002-2021, OpenNebula Project, OpenNebula Systems }} +{{! }} +{{! Licensed under the Apache License, Version 2.0 (the "License"); you may }} +{{! not use this file except in compliance with the License. You may obtain }} +{{! a copy of the License at }} +{{! }} +{{! http://www.apache.org/licenses/LICENSE-2.0 }} +{{! }} +{{! Unless required by applicable law or agreed to in writing, software }} +{{! distributed under the License is distributed on an "AS IS" BASIS, }} +{{! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }} +{{! See the License for the specific language governing permissions and }} +{{! limitations under the License. }} +{{! -------------------------------------------------------------------------- }} +
@@ -37,7 +45,7 @@ the License for the specific language governing permissions and }} {{! limitatio
- +
\ No newline at end of file diff --git a/src/sunstone/public/app/tabs/oneflow-services-tab/panels/sched_actions.js b/src/sunstone/public/app/tabs/oneflow-services-tab/panels/sched_actions.js index 008290f296..2191fa86e9 100644 --- a/src/sunstone/public/app/tabs/oneflow-services-tab/panels/sched_actions.js +++ b/src/sunstone/public/app/tabs/oneflow-services-tab/panels/sched_actions.js @@ -27,7 +27,6 @@ define(function(require) { var Notifier = require("utils/notifier"); var ScheduleActions = require("utils/schedule_action"); var TemplateUtils = require("utils/template-utils"); - var Leases = require("utils/leases"); var OpenNebulaAction = require("../../../opennebula/action"); var Humanize = require("utils/humanize"); @@ -56,28 +55,6 @@ define(function(require) { this.id = (info && info.DOCUMENT && info.DOCUMENT.ID) || "0"; this.body = (info && info.DOCUMENT && info.DOCUMENT.TEMPLATE && info.DOCUMENT.TEMPLATE.BODY) || {}; this.data = (this.body && this.body.roles) || []; - - this.actions = [ - "terminate", - "terminate-hard", - "hold", - "release", - "stop", - "suspend", - "resume", - "reboot", - "reboot-hard", - "poweroff", - "poweroff-hard", - "undeploy", - "undeploy-hard", - "snapshot-create", - "snapshot-delete", - "snapshot-revert", - "disk-snapshot-create", - "disk-snapshot-delete", - "disk-snapshot-revert" - ]; return this; } @@ -91,93 +68,20 @@ define(function(require) { FUNCTION DEFINITIONS */ - function actionRow(scheduling_action) { - var done_str = scheduling_action.DONE ? (Humanize.prettyTime(scheduling_action.DONE)) : ""; - var message_str = scheduling_action.MESSAGE ? scheduling_action.MESSAGE : ""; - var action_id = scheduling_action.ID || ""; - var update_sched = ""; - if(action_id){ - update_sched = ""; - } - var str = "" + done_str + "\ - " + TemplateUtils.htmlEncode(message_str) + "\ - \ -
\ -
\ - \ -
\ -
"+update_sched+"
\ - \ - "; - return str; - } - - function actionsTable(parseData) { - var str = ""; - var empty = "\ - \ - " + Locale.tr("No actions to show") + "\ - "; - if (!parseData) { - return empty; - } else { - var sched_actions = Array.isArray(parseData)? parseData : [parseData]; - if (sched_actions.length <= 0) { - return empty; - } - $.each(sched_actions, function(index, scheduling_action) { - if(scheduling_action && scheduling_action.ID){ - var className = ""; - var idTD = ""; - var id = 0; - if(scheduling_action.ID){ - className = "_"+scheduling_action.ID; - idTD = "" + TemplateUtils.htmlEncode(scheduling_action.ID) + ""; - id = scheduling_action.ID; - } - str += ""; - str += idTD; - var actions = ScheduleActions.fromJSONtoActionsTable([scheduling_action], id, true); - str += actions; - str += actionRow(scheduling_action); - } - }); - } - return str; - } - - - function _htmlSchedActions(vmTemplateContents){ - var parseData = TemplateUtils.stringToTemplate(vmTemplateContents); - return "
\ -
\ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - "+ - actionsTable(parseData && parseData.SCHED_ACTION) + - "\ -
" + Locale.tr("ID") + "" + Locale.tr("Action") + "" + Locale.tr("Time") + "" + Locale.tr("Rep") + "" + Locale.tr("End") + "" + - Leases.html() + - "
\ -
\ -
"; + function _htmlSchedActions(sched_actions_array){ + return ScheduleActions.htmlTable( + resource = RESOURCE_SCHED_ACTIONS, + leases = true, + body = ScheduleActions.getScheduleActionTableContent( + sched_actions_array + ), + isVM = false, + canAdd = true + ); } function _html() { - var optionsActions = this.actions.map(function(ac){ + var optionsActions = ScheduleActions.defaultActions.map(function(ac){ return ""; }).join(""); @@ -192,317 +96,30 @@ define(function(require) { optionsRoles.unshift(""); optionsRoles = optionsRoles.join(""); + var that = this; + var selector = "#service_sched_action_tab .sched_place"; + ScheduleActions.updateServiceHTMLTable( + that, + selector, + _htmlSchedActions + ); + return TemplateHTML({ - sched_actions_table: _htmlSchedActions(this.data[0].vm_template_contents), + sched_actions_table: _htmlSchedActions(), actions: optionsActions, res: RESOURCE, roles: optionsRoles }); } - - function findMaxID(items) { - return items.reduce((acc, val) => { - acc = ( acc === undefined || val.ID > acc ) ? val.ID : acc; - return acc; - }, 0); - } - - function addID(actns){ - acc = findMaxID(actns); - return actns.map(el=>{ - if(el && !el.ID){ - acc = el.ID = parseInt(acc, 10)+1; - } - return el; - }); - } - - /*** update schedule actions ***/ - function updateNodes(that, callback, type = "replace"){ - if(callback && typeof callback === "function" && that && that.data){ - var roles = Array.isArray(that.data)? that.data : [that.data]; - var updateService = true; - roles.forEach(function(element){ - if(element && element.vm_template_contents !== undefined && element.nodes){ - var jsonTemplateContents = TemplateUtils.stringToTemplate(element.vm_template_contents); - var nodes = Array.isArray(element.nodes)? element.nodes : [element.nodes]; - nodes.forEach(element => { - if(element && element.vm_info && element.vm_info.VM && element.vm_info.VM.ID){ - OpenNebulaAction.show({ - data:{ - id: element.vm_info.VM.ID - }, - success: function(req, res){ - var executedCallback = callback(jsonTemplateContents && jsonTemplateContents.SCHED_ACTION); - var userTemplate = res && res.VM && res.VM.USER_TEMPLATE || {}; - var newUserTemplate = {}; - switch (type) { - case "add": - var userTemplateSchedActions = userTemplate && userTemplate.SCHED_ACTION || []; - if(userTemplateSchedActions && executedCallback){ - var newActions = Array.isArray(userTemplateSchedActions) ? userTemplateSchedActions : [userTemplateSchedActions]; - if(Array.isArray(executedCallback)){ - newActions = newActions.concat(executedCallback); - }else{ - newActions.push(executedCallback); - } - newUserTemplate = addID(newActions); - } - break; - default: - if(executedCallback){ - newUserTemplate = addID(executedCallback); - } - break; - } - userTemplate.SCHED_ACTION = newUserTemplate; - //this update the VM - OpenNebulaAction.simple_action( - { - data:{ - id: element.vm_info.VM.ID - }, - success: function(req){ - if(userTemplate && userTemplate.SCHED_ACTION){ - $(".sched_place").empty().append(_htmlSchedActions(TemplateUtils.templateToString({SCHED_ACTION: userTemplate.SCHED_ACTION}))); - //update service - if(updateService){ - updateService = false; - //this set a new vm_template_contents in roles - var rols = that.data.forEach( - function(element){ - element.vm_template_contents = TemplateUtils.templateToString({SCHED_ACTION: userTemplate.SCHED_ACTION}); - return element; - } - ); - Service.update( - { - data:{ - id: that.id, - extra_param: JSON.stringify(that.body) - }, - success: function(){ - updateService = true; - Notifier.notifyCustom( - Locale.tr("Service Updated"), - "" - ); - }, - error: function(req, resp){ - updateService = true; - Notifier.notifyError( - (resp && resp.error && resp.error.message) || Locale.tr("Cannot update the Service: ")+(that.id || "") - ); - } - } - ); - } - } - }, - error: function(req, resp){ - Notifier.notifyError( - (resp && resp.error && resp.error.message) || Locale.tr("Cannot update the VM: ")+element.vm_info.VM.ID - ); - } - }, - "VM", - "update", - { - template_raw: TemplateUtils.templateToString(userTemplate) - } - ); - }, - error: function(){ - Notifier.notifyError(Locale.tr("Cannot get information of the VM: ")+element.vm_info.VM.ID); - } - }, "VM"); - } - }); - } - }); - } - } - - function functionButtons(context, that, actions){ - var CREATE = true; - - function clear(){ - CREATE = true; - } - - function renderCreateForm(){ - if(CREATE){ - ScheduleActions.htmlNewAction(actions, context, RESOURCE_SCHED_ACTIONS); - ScheduleActions.setup(context); - CREATE = false; - } - return false; - }; - - //add - context.off("click", "#add_scheduling_"+RESOURCE_SCHED_ACTIONS+"_action").on("click" , "#add_scheduling_"+RESOURCE_SCHED_ACTIONS+"_action", function(e){ - e.preventDefault(); - renderCreateForm(); - $("#edit_"+RESOURCE_SCHED_ACTIONS+"_action_json").hide(); - $("#add_"+RESOURCE_SCHED_ACTIONS+"_action_json").show(); - }); - context.off("click", "#add_"+RESOURCE_SCHED_ACTIONS+"_action_json").on("click" , "#add_"+RESOURCE_SCHED_ACTIONS+"_action_json", function(e) { - e.preventDefault(); - var sched_action = ScheduleActions.retrieveNewAction(context); - if (sched_action != false) { - $("#no_actions_tr", context).remove(); - $("#sched_vms_actions_body").prepend(ScheduleActions.fromJSONtoActionsTable(sched_action)); - } - updateNodes(that, function(){ - return sched_action; - },"add"); - clear(); - }); - - //update - context.off("click", ".edit_action_x").on("click", ".edit_action_x", function(e) { - e.preventDefault(); - var id = $(this).attr("data_id"); - if(id && id.length){ - renderCreateForm(); - $("#edit_"+RESOURCE_SCHED_ACTIONS+"_action_json").show().attr("data_id", id); - $("#add_"+RESOURCE_SCHED_ACTIONS+"_action_json").hide(); - ScheduleActions.fill($(this),context); - } - }); - context.off("click" , "#edit_"+RESOURCE_SCHED_ACTIONS+"_action_json").on("click" , "#edit_"+RESOURCE_SCHED_ACTIONS+"_action_json", function(e){ - e.preventDefault(); - var id = $(this).attr("data_id"); - if(id && id.length){ - $(".wickedpicker").hide(); - var sched_action = ScheduleActions.retrieveNewAction(context); - if (sched_action != false) { - var sched_actions = ScheduleActions.retrieve(context); - if(Array.isArray(sched_actions)){ - sched_actions = sched_actions.map( - function(action){ - if(action && action.ID && action.ID===id){ - return sched_action; - }else{ - return action; - } - } - ); - } - updateNodes(that, function(){ - return sched_actions; - },"replace"); - } - clear(); - } - return false; - }); - - //delete - context.off("click", ".remove_action_x").on("click", ".remove_action_x", function(e) { - e.preventDefault(); - var index = this.id.substring(6, this.id.length); - updateNodes(that, function(schedAction){ - var actions = Array.isArray(schedAction)? schedAction : [schedAction]; - actions.forEach(function(el, i){ - if(el && el.ID && String(el.ID) === index){ - delete actions[i]; - return; - } - }); - return actions; - }, "replace"); - clear(); - }); - } - + function _setup(context) { var that = this; - that.formContext = context; - Leases.actions(that, RESOURCE_SCHED_ACTIONS, "add-service", function(schedActions){ - updateNodes(that, function(){ - return schedActions; - },"add"); - }); - var actions = ScheduleActions.defaultActions; - - $("select#select_new_action").off("change").on("change",function(){ - var snap_name = $("#snapname"); - var snap_id = $("#snapid"); - var disk_id = $("#diskid"); - switch ($(this).val()) { - case "snapshot-create": - snap_name.removeClass("hide"); - snap_id.addClass("hide").val(""); - disk_id.addClass("hide").val(""); - break; - case "snapshot-revert": - snap_name.addClass("hide").val(""); - snap_id.removeClass("hide"); - disk_id.addClass("hide").val(""); - break; - case "snapshot-delete": - snap_name.addClass("hide").val(""); - snap_id.removeClass("hide"); - disk_id.addClass("hide").val(""); - break; - case "disk-snapshot-create": - snap_name.removeClass("hide"); - snap_id.addClass("hide").val(""); - disk_id.removeClass("hide"); - break; - case "disk-snapshot-revert": - snap_name.addClass("hide").val(""); - snap_id.removeClass("hide"); - disk_id.removeClass("hide"); - break; - case "disk-snapshot-delete": - snap_name.addClass("hide").val(""); - snap_id.removeClass("hide"); - disk_id.removeClass("hide"); - break; - default: - snap_name.addClass("hide").val(""); - snap_id.addClass("hide").val(""); - disk_id.addClass("hide").val(""); - break; - } - }); - - context.off("click", "#add_"+RESOURCE+"_action_json").on("click", "#add_"+RESOURCE+"_action_json", function(){ - var new_action = $("select#select_new_action").val(); - var role = $("select#role_name").val(); - var snap_name = $("#snapname").val(); - var snap_id = $("#snapid").val(); - var disk_id = $("#diskid").val(); - if(new_action){ - var actionJSON = {}; - actionJSON.error = function(e){ - Notifier.notifyError((e && e.error && e.error.message) || Locale.tr("Error")); - }; - actionJSON.success = function(e){ - Notifier.notifyMessage(Locale.tr("Bulk Action Created")); - }; - actionJSON.data = {}; - actionJSON.data.id = that.id; - actionJSON.data.action = {perform: new_action}; - actionJSON.data.action.params = {}; - if(that.actions.includes(new_action)){ - var rawData = [disk_id,snap_id,snap_name]; - var args = rawData.filter(function (e) {return e;}).join(); - if(args){ - actionJSON.data.action.params.args = args; - } - } - if(role!=="" && role!==undefined){ - actionJSON.data.roleName = role; - } - Actions.addFlowAction(actionJSON,RESOURCE); - } - return false; - }); - functionButtons(context, that, actions); + ScheduleActions.setupButtons( + RESOURCE_SCHED_ACTIONS, + context, + that + ); } }); diff --git a/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/create.js b/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/create.js index 9daaf0b6b0..bd9e555533 100644 --- a/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/create.js +++ b/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/create.js @@ -27,7 +27,6 @@ define(function(require) { var TemplateUtils = require("utils/template-utils"); var UserInputs = require("utils/user-inputs"); var ScheduleActions = require("utils/schedule_action"); - var Leases = require("utils/leases"); /* TEMPLATES @@ -43,11 +42,7 @@ define(function(require) { var FORM_PANEL_ID = require("./create/formPanelId"); var TAB_ID = require("../tabId"); var RESOURCE = "service_create"; - var CREATE = true; - var actions = ScheduleActions.defaultActions; - function clear(){ - CREATE = true; - } + /* CONSTRUCTOR */ @@ -102,102 +97,36 @@ define(function(require) { }); } - function renderCreateForm(context) { - if(CREATE){ - ScheduleActions.htmlNewAction(actions, context, RESOURCE); - ScheduleActions.setup(context); - CREATE=false; - } - } - - function sched_actions_events(context){ - $(".edit_action_x").off("click").on("click", function(e){ - e.preventDefault(); - var id = $(this).attr("data_id"); - if(id && id.length){ - contextRow = $(this).closest("tr.tr_action"); - renderCreateForm(context); - $("#edit_"+RESOURCE+"_action_json").show().attr("data_id", id); - $("#add_"+RESOURCE+"_action_json").hide(); - ScheduleActions.fill($(this),context); - sched_actions_events(context); - } - }); - - $("#add_scheduling_"+RESOURCE+"_action").off("click"); - $("#add_scheduling_"+RESOURCE+"_action").on("click", function(e){ - e.preventDefault(); - renderCreateForm(context); - $("#edit_"+RESOURCE+"_action_json").hide(); - $("#add_"+RESOURCE+"_action_json").show(); - sched_actions_events(context); - }); - - $(".remove_action_x").off("click").on("click", function(){ - $(this).parents("tr").remove(); - sched_actions_events(context); - }); - - $("#add_"+RESOURCE+"_action_json").off("click").on("click", function(){ - var sched_action = ScheduleActions.retrieveNewAction(context); - if (sched_action) { - $("#no_actions_tr", context).remove(); - $("#sched_"+RESOURCE+"_actions_body").prepend(ScheduleActions.fromJSONtoActionsTable(sched_action)); - } - $("#input_sched_action_form").remove(); - sched_actions_events(context); - clear(); - return false; - }); - - $("#edit_"+RESOURCE+"_action_json").off("click").on("click", function(e){ - e.preventDefault(); - var id = $(this).attr("data_id"); - if(id && id.length && contextRow){ - $(".wickedpicker").hide(); - var sched_action = ScheduleActions.retrieveNewAction(context); - if (sched_action != false) { - sched_action.ID = id; - contextRow.replaceWith(ScheduleActions.fromJSONtoActionsTable(sched_action)); - contextRow = undefined; - $("#input_sched_action_form").remove(); - } - sched_actions_events(context); - clear(); - } - return false; - }); - } - function _setup(context) { - clear(); this.networksType = [ { value: "template_id", text: "Create", select: "vntemplates", extra: true }, { value: "reserve_from", text: "Reserve", select: "networks", extra: true }, { value: "id", text: "Existing", select: "networks", extra: false }, ]; - + // reset global variables on form this.roleTabObjects = {}; var numberOfNetworks = 0; var roles_index = 0; - var that = this; //this render a schedule action form - $(".service_schedule_actions").append(ScheduleActions.htmlTable(RESOURCE, Leases.html())); - //this are actions of Leases button - var objLeases = $.extend(true, {}, that); - objLeases.resource = "template"; - objLeases.__proto__ = FormPanel.prototype; - Leases.actions(objLeases, "", "", function(){ - sched_actions_events(context); - }); - - //end of render a schedule action form + $(".service_schedule_actions").append( + ScheduleActions.htmlTable( + resource = RESOURCE, + leases = true, + body = ScheduleActions.getScheduleActionTableContent(), + isVM = false, + canAdd = true + ) + ); // this is a logic to add, remove and edit schedule_action - sched_actions_events(context); + ScheduleActions.setupButtons( + RESOURCE, + context, + that + ); // end of logic to add, remove and edit schedule_action $(".add_service_network", context).unbind("click"); @@ -449,7 +378,6 @@ define(function(require) { json_template["labels"] = currentInfo.TEMPLATE.BODY.labels; } - clear(); if (this.action == "create") { Sunstone.runAction("ServiceTemplate.create", json_template ); return false; @@ -489,23 +417,29 @@ define(function(require) { this.resourceId = element.ID; this.old_template = element.TEMPLATE.BODY; + var arraySchedActions = []; + //fill schedule actions - if(this.old_template && this.old_template.roles && this.old_template.roles[0] && this.old_template.roles[0].vm_template_contents){ + if(this.old_template && + this.old_template.roles && + this.old_template.roles[0] && + this.old_template.roles[0].vm_template_contents + ){ var vm_template_contents = TemplateUtils.stringToTemplate(this.old_template.roles[0].vm_template_contents); if(vm_template_contents && vm_template_contents.SCHED_ACTION){ - var arraySchedActions = Array.isArray(vm_template_contents.SCHED_ACTION)? vm_template_contents.SCHED_ACTION : [vm_template_contents.SCHED_ACTION]; - arraySchedActions.forEach(function(schedAction){ - $("#no_actions_tr", context).remove(); - $("#sched_"+RESOURCE+"_actions_body").prepend(ScheduleActions.fromJSONtoActionsTable(schedAction)); - }); - sched_actions_events(context); + arraySchedActions = Array.isArray(vm_template_contents.SCHED_ACTION)? vm_template_contents.SCHED_ACTION : [vm_template_contents.SCHED_ACTION]; } + + $("#sched_service_create_actions_body").html( + ScheduleActions.getScheduleActionTableContent( + arraySchedActions + ) + ); } // Populates the Avanced mode Tab $("#template", context).val(JSON.stringify(element.TEMPLATE.BODY, null, " ")); - $("#service_name", context).attr("disabled", "disabled"); $("#service_name", context).val(element.NAME); diff --git a/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate.js b/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate.js index 6fbb848184..f5e6e2313a 100644 --- a/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate.js +++ b/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate.js @@ -33,6 +33,7 @@ define(function(require) { var Config = require("sunstone-config"); var TemplateUtils = require("utils/template-utils"); var ServiceUtils = require("utils/service-utils"); + var ScheduleActions = require("utils/schedule_action"); /* TEMPLATES @@ -48,6 +49,7 @@ define(function(require) { var TAB_ID = require("../tabId"); var vm_group = "VM_GROUP"; var classButton = "small button leases right radius"; + var RESOURCE_SCHED_ACTIONS = "inst_flow"; /* CONSTRUCTOR @@ -89,15 +91,13 @@ define(function(require) { "userInputsHTML": UserInputs.html(), }; if(config && config.system_config && config.system_config.leases){ - values_hbs.userInputsCharters = $("
").append( - $("
",{style:"display:inline-block;clear:both;width:100%"}).append( - $("