From 68aadedffaa19e0b19e2430786ed44a3fdf3aa88 Mon Sep 17 00:00:00 2001 From: abelCoronado93 Date: Mon, 26 Mar 2018 10:42:54 +0200 Subject: [PATCH] F #1884 : Sunstone Template importation ================================================ F #1884: Generic feedback for vCenter resources F #1884: New vCenter importer for templates [Sinatra] F #1884: New interface for vCenter templates importation F #1884: Improved vCenter Template importation F #1884: Added libs for jGrowl feedback F #1884: Added post path for vCenter templates importation F #1884: Solved bug with master selector in vCenter resources importation F #1884: Network importation makeover and improved --- .../tabs/templates-tab/form-panels/import.js | 56 +- .../templates-tab/form-panels/import/html.hbs | 32 +- .../public/app/utils/vcenter/datastores.js | 24 +- .../public/app/utils/vcenter/images.js | 2 +- .../public/app/utils/vcenter/networks.js | 5 +- .../public/app/utils/vcenter/networks/row.hbs | 73 +-- .../public/app/utils/vcenter/templates.js | 615 ++++-------------- .../app/utils/vcenter/templates/row.hbs | 38 +- .../app/utils/vcenter/vcenter-common.js | 22 +- src/sunstone/routes/vcenter.rb | 30 +- 10 files changed, 282 insertions(+), 615 deletions(-) diff --git a/src/sunstone/public/app/tabs/templates-tab/form-panels/import.js b/src/sunstone/public/app/tabs/templates-tab/form-panels/import.js index 712b604394..7dff1a6504 100644 --- a/src/sunstone/public/app/tabs/templates-tab/form-panels/import.js +++ b/src/sunstone/public/app/tabs/templates-tab/form-panels/import.js @@ -24,6 +24,9 @@ define(function(require) { var Sunstone = require('sunstone'); var Locale = require('utils/locale'); var VCenterTemplates = require('utils/vcenter/templates'); + var HostsTable = require('tabs/hosts-tab/datatable'); + var UniqueId = require('utils/unique-id'); + var Notifier = require('utils/notifier'); /* TEMPLATES @@ -55,6 +58,16 @@ define(function(require) { this.vCenterTemplates = new VCenterTemplates(); + var options = { + 'select': true, + 'selectOptions': { + 'multiple_choice': false, + "filter_fn": function(host) { return host.VM_MAD === "vcenter" && host.IM_MAD === "vcenter"; } + } + }; + this.hostsTable = new HostsTable('HostsTable' + UniqueId.id(), options); + + BaseFormPanel.call(this); } @@ -75,38 +88,36 @@ define(function(require) { function _htmlWizard() { return TemplateHTML({ 'formPanelId': this.formPanelId, + 'vCenterHostsHTML': this.hostsTable.dataTableHTML, 'vCenterTemplatesHTML': this.vCenterTemplates.html() }); } function _setup(context) { var that = this; + this.hostsTable.initialize(); + this.hostsTable.refreshResourceTableSelect(); - $("form.vcenter_credentials", context) - .off('forminvalid.zf.abide').off('formvalid.zf.abide').off("submit"); - - Foundation.reInit($("form.vcenter_credentials", context)); - - $("form.vcenter_credentials", context) - .on('forminvalid.zf.abide', function(ev, frm) { - }) - .on('formvalid.zf.abide', function(ev, frm) { - Sunstone.enableFormPanelSubmit(TAB_ID); - - var vcenter_user = $("#vcenter_user", context).val(); - var vcenter_password = $("#vcenter_password", context).val(); - var vcenter_host = $("#vcenter_host", context).val(); - + $(context).off("click", "#get-vcenter-templates"); + $(context).on("click", "#get-vcenter-templates", function(){ + var selectedHost = that.hostsTable.retrieveResourceTableSelect(); + if (selectedHost !== ""){ that.vCenterTemplates.insert({ container: context, - vcenter_user: vcenter_user, - vcenter_password: vcenter_password, - vcenter_host: vcenter_host + selectedHost: selectedHost }); - }) - .on("submit", function(ev) { - ev.preventDefault(); - }); + $("#import_vcenter_templates", this.parentElement).prop("disabled", false); + } + else { + Notifier.notifyMessage("You need select a vCenter host first"); + } + }); + + $(context).off("click", "#import_vcenter_templates"); + $(context).on("click", "#import_vcenter_templates", function(){ + that.vCenterTemplates.import(context); + return false; + }); return false; } @@ -122,5 +133,6 @@ define(function(require) { } function _onShow(context) { + $("#templates-tab .submit_button").hide(); } }); diff --git a/src/sunstone/public/app/tabs/templates-tab/form-panels/import/html.hbs b/src/sunstone/public/app/tabs/templates-tab/form-panels/import/html.hbs index 823ad0f74a..f85aafcb8b 100644 --- a/src/sunstone/public/app/tabs/templates-tab/form-panels/import/html.hbs +++ b/src/sunstone/public/app/tabs/templates-tab/form-panels/import/html.hbs @@ -15,30 +15,14 @@ {{! -------------------------------------------------------------------------- }}
-
-
-
-
-
- - -
-
- - -
-
- - -
-
- - -
-
-
+
+ {{{vCenterHostsHTML}}} +
+
+ + +
+


{{{vCenterTemplatesHTML}}}
diff --git a/src/sunstone/public/app/utils/vcenter/datastores.js b/src/sunstone/public/app/utils/vcenter/datastores.js index a5a5d23631..9589305d26 100644 --- a/src/sunstone/public/app/utils/vcenter/datastores.js +++ b/src/sunstone/public/app/utils/vcenter/datastores.js @@ -31,6 +31,7 @@ define(function(require) { var FieldsetTableHTML = require("hbs!./common/fieldset-table"); var path = "/vcenter/datastores"; + var resource = "Datastore"; function VCenterDatastores() { return this; @@ -97,8 +98,10 @@ define(function(require) { var newdiv = $(content).appendTo($(".vcenter_datacenter_list", context)); var tbody = $("#" + tableId + " tbody", context); + var ds_num = 0; $.each(response, function(datastore_name, element){ if (element.cluster.length !== 0){ + ds_num++; var opts = { name: element.simple_name, vcenter_ref: element.ref, datacenter: element.datacenter, cluster: element.cluster, free_mb: element.free_mb, total_mb: element.total_mb }; var trow = $(RowTemplate(opts)).appendTo(tbody); $(".check_item", trow).data("import_data", element); @@ -124,6 +127,8 @@ define(function(require) { elementsTable.initialize(); + $("a.vcenter-table-select-all", context).text(Locale.tr("Select all %1$s Datastores", ds_num)); + VCenterCommon.setupTable({ context : newdiv, allSelected : Locale.tr("All %1$s Datastores selected."), @@ -145,7 +150,6 @@ define(function(require) { } function _import(context) { - var that = this; var vcenter_refs = []; var table = $("table.vcenter_import_table", context); @@ -167,24 +171,14 @@ define(function(require) { data: { datastores: vcenter_refs, timeout: false }, dataType: "json", success: function(response){ - var success = response.success; - if (success.length !== 0){ - $.each(success, function(key, value){ - var ds1 = Navigation.link(value.id[0], "datastores-tab", value.id[0]); - var ds2 = Navigation.link(value.id[1], "datastores-tab", value.id[1]); - Notifier.notifyMessage("Datastore " + value.name + " imported as " + ds1 + " " + ds2 + " successfully"); - }); - } - var error = response.error; - if (error.length !== 0){ - $.each(error, function(key, value){ - Notifier.notifyError("Datastore with ref " + value + " could not be imported"); - }); - } + VCenterCommon.jGrowlSuccess({success : response.success, resource : resource, link_tab : "datastores-tab"}); + VCenterCommon.jGrowlFailure({error : response.error, resource : resource}); + $("#get-vcenter-ds").click(); }, error: function (request, error_json) { Notifier.notifyError(request.responseJSON.error.message); + $("#get-vcenter-ds").click(); } }); } diff --git a/src/sunstone/public/app/utils/vcenter/images.js b/src/sunstone/public/app/utils/vcenter/images.js index 16986bfb08..807b5a88ba 100644 --- a/src/sunstone/public/app/utils/vcenter/images.js +++ b/src/sunstone/public/app/utils/vcenter/images.js @@ -131,7 +131,7 @@ define(function(require) { elementsTable.initialize(); - $("a.vcenter-table-select-all").text(Locale.tr("Select all %1$s Images", response.length)); + $("a.vcenter-table-select-all", context).text(Locale.tr("Select all %1$s Images", response.length)); VCenterCommon.setupTable({ context : newdiv, diff --git a/src/sunstone/public/app/utils/vcenter/networks.js b/src/sunstone/public/app/utils/vcenter/networks.js index 7413fb0946..0041b982d8 100644 --- a/src/sunstone/public/app/utils/vcenter/networks.js +++ b/src/sunstone/public/app/utils/vcenter/networks.js @@ -93,7 +93,8 @@ define(function(require) { toggleAdvanced : true, columns : [ '', - Locale.tr("Network") + Locale.tr("Network"), + "" ] }); @@ -126,7 +127,7 @@ define(function(require) { elementsTable.initialize(); - $("a.vcenter-table-select-all").text(Locale.tr("Select all %1$s Networks", elements.length)); + $("a.vcenter-table-select-all", context).text(Locale.tr("Select all %1$s Networks", elements.length)); VCenterCommon.setupTable({ context : newdiv, diff --git a/src/sunstone/public/app/utils/vcenter/networks/row.hbs b/src/sunstone/public/app/utils/vcenter/networks/row.hbs index 062df07c41..0a49d72c14 100644 --- a/src/sunstone/public/app/utils/vcenter/networks/row.hbs +++ b/src/sunstone/public/app/utils/vcenter/networks/row.hbs @@ -15,8 +15,8 @@ {{! -------------------------------------------------------------------------- }} - - + +
@@ -27,50 +27,49 @@ {{data.name}} - {{data.type}} - - Cluster: {{data.cluster}} - - Location: {{data.cluster_location}} - - OpenNebula Cluster ID: {{data.one_cluster_id}} - {{#if data.vlan}} - VLAN: {{data.vlan}}{{/if}} + {{#if data.cluster}} - {{tr "Cluster"}}: {{data.cluster}}{{/if}} + {{#if data.cluster_location}} - {{tr "Location"}}: {{data.cluster_location}}{{/if}} + {{#if data.one_cluster_id}} - {{tr "OpenNebula Cluster ID"}}: {{data.one_cluster_id}}{{/if}} + {{#if data.vlan}} - {{tr "VLAN"}}: {{data.vlan}}{{/if}}
- - - + + + \ No newline at end of file diff --git a/src/sunstone/public/app/utils/vcenter/vcenter-common.js b/src/sunstone/public/app/utils/vcenter/vcenter-common.js index a61ebf7b26..ed83e08dd8 100644 --- a/src/sunstone/public/app/utils/vcenter/vcenter-common.js +++ b/src/sunstone/public/app/utils/vcenter/vcenter-common.js @@ -16,6 +16,8 @@ define(function(require) { var sprintf = require("sprintf").sprintf + var Notifier = require("utils/notifier"); + var Navigation = require('utils/navigation'); function _html() { return '
'; @@ -127,11 +129,29 @@ define(function(require) { opts.message); } + function _jGrowlSuccess(opts){ + var success = opts.success; + $.each(success, function(key, value){ + var rs1 = Navigation.link(value.id[0], opts.link_tab, value.id[0]); + var rs2 = Navigation.link(value.id[1], opts.link_tab, value.id[1]); + Notifier.notifyMessage(opts.resource + " " + value.name + " imported as " + rs1 + " " + rs2 + " successfully"); + }); + } + + function _jGrowlFailure(opts){ + var error = opts.error; + $.each(error, function(key, value){ + Notifier.notifyError(opts.resource + " with ref " + value + " could not be imported"); + }); + } + return { 'html': _html, 'setupTable': _setupTable, 'importLoading': _importLoading, 'importSuccess': _importSuccess, - 'importFailure': _importFailure + 'importFailure': _importFailure, + 'jGrowlSuccess': _jGrowlSuccess, + 'jGrowlFailure': _jGrowlFailure }; }); diff --git a/src/sunstone/routes/vcenter.rb b/src/sunstone/routes/vcenter.rb index e40453de0e..7262011c6e 100644 --- a/src/sunstone/routes/vcenter.rb +++ b/src/sunstone/routes/vcenter.rb @@ -137,27 +137,21 @@ end get '/vcenter/templates' do begin - dc_folder = VCenterDriver::DatacenterFolder.new(vcenter_client) + new_vcenter_importer("templates") - tpool = VCenterDriver::VIHelper.one_pool(OpenNebula::TemplatePool, false) + [200, $importer.get_list.to_json] + rescue Exception => e + logger.error("[vCenter] " + e.message) + error = Error.new(e.message) + error 403, error.to_json + end +end - if tpool.respond_to?(:message) - msg = "Could not get OpenNebula TemplatePool: #{tpool.message}" - logger.error("[vCenter] " + msg) - error = Error.new(msg) - error 404, error.to_json - end +post '/vcenter/templates' do + begin + $importer.process_import(params["templates"], params["opts"]) - templates = dc_folder.get_unimported_templates(vcenter_client, tpool) - - if templates.nil? - msg = "No datacenter found" - logger.error("[vCenter] " + msg) - error = Error.new(msg) - error 404, error.to_json - end - - [200, templates.to_json] + [200, $importer.output.to_json] rescue Exception => e logger.error("[vCenter] " + e.message) error = Error.new(e.message)