From baef40e24a88bb06ae4596f122176e97dad55ee3 Mon Sep 17 00:00:00 2001 From: Abel Coronado Date: Wed, 13 Sep 2017 09:53:12 +0200 Subject: [PATCH] F #5118 Allow SCHEDULED ACTIONS in VM Templates (#469) --- .../form-panels/create/wizard-tabs/actions.js | 54 +-------- .../templates-tab/form-panels/instantiate.js | 113 +++++++++++++++++- .../form-panels/instantiate/templateRow.hbs | 20 ++++ src/sunstone/public/app/utils/actions.js | 75 ++++++++++++ 4 files changed, 208 insertions(+), 54 deletions(-) create mode 100644 src/sunstone/public/app/utils/actions.js diff --git a/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/actions.js b/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/actions.js index 263d5bc2b9..8292f9d3b7 100644 --- a/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/actions.js +++ b/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/actions.js @@ -26,6 +26,7 @@ define(function(require) { var UniqueId = require('utils/unique-id'); var Humanize = require('utils/humanize'); var TemplateUtils = require('utils/template-utils'); + var Actions = require('utils/actions'); var TemplateHTML = require('hbs!./actions/html'); /* @@ -123,7 +124,7 @@ define(function(require) { $(this).parents('tr').remove(); $("#add_scheduling_temp_action", context).removeAttr("disabled"); - $("#sched_temp_actions_body").append(fromJSONtoActionsTable(sched_action)); + $("#sched_temp_actions_body").append(Actions.fromJSONtoActionsTable(sched_action)); return false; }); @@ -154,7 +155,7 @@ define(function(require) { else if (index2 == 1){ var pretty_time = $(this).text(); pretty_time = pretty_time.split(' '); - var date = convertDate(pretty_time[1]); + var date = Actions.convertDate(pretty_time[1]); var time_value = date + ' ' + pretty_time[0]; var epoch_str = new Date(time_value); var time = parseInt(epoch_str.getTime()) / 1000; @@ -170,55 +171,8 @@ define(function(require) { } function _fill(context, templateJSON) { - var actions = fromJSONtoActionsTable(templateJSON.SCHED_ACTION); + var actions = Actions.fromJSONtoActionsTable(templateJSON.SCHED_ACTION); $("#sched_temp_actions_body").append(actions); delete templateJSON['SCHED_ACTION']; } - - function fromJSONtoActionsTable(actions_array) { - var str = "" - - if (!actions_array) { - return ""; - } - - if (!$.isArray(actions_array)) { - var tmp_array = new Array(); - tmp_array[0] = actions_array; - actions_array = tmp_array; - } - - if (!actions_array.length) { - return ""; - } - - $.each(actions_array, function(index, scheduling_action) { - str += fromJSONtoActionRow(scheduling_action); - }); - - return str; - } - - function fromJSONtoActionRow(scheduling_action) { - var time_str = Humanize.prettyTime(scheduling_action.TIME); - - var str = ""; - str += '\ - ' + TemplateUtils.htmlEncode(scheduling_action.ACTION) + '\ - ' + time_str + '\ - \ -
\ - \ -
\ - \ - '; - - return str; - } - - function convertDate(date_string){ - date_string = date_string.split('/'); - return date_string[2] + "-" + date_string[1] + "-" + date_string[0]; - } - }); diff --git a/src/sunstone/public/app/tabs/templates-tab/form-panels/instantiate.js b/src/sunstone/public/app/tabs/templates-tab/form-panels/instantiate.js index ef4f9cb894..2685739a2d 100644 --- a/src/sunstone/public/app/tabs/templates-tab/form-panels/instantiate.js +++ b/src/sunstone/public/app/tabs/templates-tab/form-panels/instantiate.js @@ -38,6 +38,10 @@ define(function(require) { var Config = require('sunstone-config'); var HostsTable = require('tabs/hosts-tab/datatable'); var DatastoresTable = require('tabs/datastores-tab/datatable'); + var Humanize = require('utils/humanize'); + var TemplateUtils = require('utils/template-utils'); + var UniqueId = require('utils/unique-id'); + var Actions = require('utils/actions'); /* CONSTANTS @@ -116,6 +120,77 @@ define(function(require) { $("#vm_n_times_disabled", context).hide(); $("#vm_n_times", context).show(); } + + context.off('click', '#add_scheduling_inst_action'); + context.on('click', '#add_scheduling_inst_action', function() { + $("#add_scheduling_inst_action", context).attr("disabled", "disabled"); + $("#scheduling_inst_actions_table").append('\ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + '); + + return false; + }); + + context.off("click", "#add_inst_action_json"); + context.on("click" , "#add_inst_action_json", function(){ + var date_input_value = $("#date_input", context).val(); + var time_input_value = $("#time_input", context).val(); + + if (date_input_value == "" || time_input_value == ""){ + return false; + } + + var time_value = date_input_value + ' ' + time_input_value; + var epoch_str = new Date(time_value); + var time = parseInt(epoch_str.getTime()) / 1000; + + var new_action = $("#select_new_action", context).val(); + var sched_action = {}; + sched_action.ACTION = new_action; + sched_action.TIME = time; + + $(this).parents('tr').remove(); + $("#add_scheduling_inst_action", context).removeAttr("disabled"); + + $("#sched_inst_actions_body").append(Actions.fromJSONtoActionsTable(sched_action)); + + return false; + }); + + context.on("focusout" , "#time_input", function(){ + $("#time_input").removeAttr("data-invalid"); + $("#time_input").removeAttr("class"); + }); + + context.off("click", ".remove_action_x"); + context.on("click", ".remove_action_x", function(){ + $(this).parents('tr').remove(); + }); } function _calculateCost(){ @@ -245,6 +320,33 @@ define(function(require) { } } + var templateJSON = {}; + var actionsJSON = []; + + $("#scheduling_inst_actions_table tbody tr").each(function(index){ + var first = $(this).children("td")[0]; + if(!$('select', first).html()){ + var actionJSON = {}; + actionJSON.ID = index; + $(this).children("td").each(function(index2){ + if(index2 == 0) + actionJSON.ACTION = $(this).text(); + else if (index2 == 1){ + var pretty_time = $(this).text(); + pretty_time = pretty_time.split(' '); + var date = Actions.convertDate(pretty_time[1]); + var time_value = date + ' ' + pretty_time[0]; + var epoch_str = new Date(time_value); + var time = parseInt(epoch_str.getTime()) / 1000; + actionJSON.TIME = time; + } + }); + } + if (!$.isEmptyObject(actionJSON)) {actionsJSON.push(actionJSON)}; + }); + + tmp_json['SCHED_ACTION'] = actionsJSON; + capacityContext = $(".capacityContext" + template_id, context); $.extend(tmp_json, CapacityInputs.retrieveChanges(capacityContext)); @@ -288,8 +390,8 @@ define(function(require) { } } - that.hostsTable = new HostsTable('HostsTable' + template_json.VMTEMPLATE.ID, options); - that.datastoresTable = new DatastoresTable('DatastoresTable' + template_json.VMTEMPLATE.ID, options); + that.hostsTable = new HostsTable('HostsTable' + UniqueId.id(), options); + that.datastoresTable = new DatastoresTable('DatastoresTable' + UniqueId.id(), options); templatesContext.append( TemplateRowHTML( @@ -302,6 +404,9 @@ define(function(require) { $(".provision_host_selector" + template_json.VMTEMPLATE.ID, context).data("hostsTable", that.hostsTable); $(".provision_ds_selector" + template_json.VMTEMPLATE.ID, context).data("dsTable", that.datastoresTable); + var actions = Actions.fromJSONtoActionsTable(template_json.VMTEMPLATE.TEMPLATE.SCHED_ACTION); + $("#sched_inst_actions_body").append(actions); + var selectOptions = { 'selectOptions': { 'select_callback': function(aData, options) { @@ -436,7 +541,6 @@ define(function(require) { function _onShow(context) { Sunstone.disableFormPanelSubmit(this.tabId); - $("input.instantiate_pers", context).change(); var templatesContext = $(".list_of_templates", context); @@ -462,5 +566,6 @@ define(function(require) { $('#SCHED_REQUIREMENTS' + id, context).val(req_string.join(" | ")); $('#SCHED_DS_REQUIREMENTS' + id, context).val(req_ds_string.join(" | ")); - }; + } + }); diff --git a/src/sunstone/public/app/tabs/templates-tab/form-panels/instantiate/templateRow.hbs b/src/sunstone/public/app/tabs/templates-tab/form-panels/instantiate/templateRow.hbs index a41df9e6cf..36b4072831 100644 --- a/src/sunstone/public/app/tabs/templates-tab/form-panels/instantiate/templateRow.hbs +++ b/src/sunstone/public/app/tabs/templates-tab/form-panels/instantiate/templateRow.hbs @@ -109,6 +109,26 @@ {{/isFeatureEnabled}} +
+
+
+ + {{tr "Actions"}} + + + + + + + + + + + +
{{tr "Action"}} {{tr "Time"}} {{tr "Actions"}}
+
+
+
diff --git a/src/sunstone/public/app/utils/actions.js b/src/sunstone/public/app/utils/actions.js new file mode 100644 index 0000000000..450b69d010 --- /dev/null +++ b/src/sunstone/public/app/utils/actions.js @@ -0,0 +1,75 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2017, 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. */ +/* -------------------------------------------------------------------------- */ + +define(function(require) { + + var Locale = require('utils/locale'); + var TemplateUtils = require('utils/template-utils'); + var Humanize = require('utils/humanize'); + + function _fromJSONtoActionsTable(actions_array) { + var str = "" + + if (!actions_array) { + return ""; + } + + if (!$.isArray(actions_array)) { + var tmp_array = new Array(); + tmp_array[0] = actions_array; + actions_array = tmp_array; + } + + if (!actions_array.length) { + return ""; + } + + $.each(actions_array, function(index, scheduling_action) { + str += fromJSONtoActionRow(scheduling_action); + }); + + return str; + } + + function fromJSONtoActionRow(scheduling_action) { + var time_str = Humanize.prettyTime(scheduling_action.TIME); + + var str = ""; + str += '\ + ' + TemplateUtils.htmlEncode(scheduling_action.ACTION) + '\ + ' + time_str + '\ + \ +
\ + \ +
\ + \ + '; + + return str; + } + + function _convertDate(date_string){ + date_string = date_string.split('/'); + return date_string[2] + "-" + date_string[1] + "-" + date_string[0]; + } + + return { + 'fromJSONtoActionsTable': _fromJSONtoActionsTable, + 'convertDate': _convertDate + }; +}); + + \ No newline at end of file