From 6b78d2a3f3a01197091a37a036adbf3ab7f71764 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Fri, 26 Sep 2014 13:08:09 +0200 Subject: [PATCH] feature #2911: Create hosts and templates from the vCenter dialog --- src/sunstone/public/js/plugins/hosts-tab.js | 244 +++++++++++++++----- 1 file changed, 186 insertions(+), 58 deletions(-) diff --git a/src/sunstone/public/js/plugins/hosts-tab.js b/src/sunstone/public/js/plugins/hosts-tab.js index 82723e5691..562315e18d 100644 --- a/src/sunstone/public/js/plugins/hosts-tab.js +++ b/src/sunstone/public/js/plugins/hosts-tab.js @@ -27,10 +27,6 @@ var create_host_tmpl =
\
\
\ -
\ - \ - \ -
\
\ \ \
\ -
\ -
\
\ \
\
\
\ +
\ +
\ +
\ + \ + \ +
\
\
\
\ @@ -982,16 +982,19 @@ function setupCreateHostDialog(){ if (this.value == "custom") { $(".vcenter_credentials", $create_host_dialog).hide(); $("#vnm_mads", $create_host_dialog).show(); + $("#name", $create_host_dialog).show(); $("#create_host_submit", $create_host_dialog).show(); $(".drivers", $create_host_dialog).show(); } else if (this.value == "vcenter") { $("#vnm_mads", $create_host_dialog).hide(); + $("#name", $create_host_dialog).hide(); $(".vcenter_credentials", $create_host_dialog).show(); $("#create_host_submit", $create_host_dialog).hide(); $(".drivers", $create_host_dialog).hide(); } else { $(".vcenter_credentials", $create_host_dialog).hide(); $("#vnm_mads", $create_host_dialog).show(); + $("#name", $create_host_dialog).show(); $("#create_host_submit", $create_host_dialog).show(); $(".drivers", $create_host_dialog).hide(); } @@ -1008,7 +1011,7 @@ function setupCreateHostDialog(){ $.ajax({ url: '/vcenter', type: "GET", - //data: {timeout: timeout}, + data: {timeout: false}, dataType: "json", headers: { "X_VCENTER_USER": $("#vcenter_user", $create_host_dialog).val(), @@ -1017,24 +1020,26 @@ function setupCreateHostDialog(){ }, success: function(response){ console.log(response); + $("#vcenter_user", $create_host_dialog).attr("disabled", "disabled") + $("#vcenter_password", $create_host_dialog).attr("disabled", "disabled") + $("#vcenter_host", $create_host_dialog).attr("disabled", "disabled") $("#get_vcenter_clusters", $create_host_dialog).hide(); $(".import_vcenter_clusters_div", $create_host_dialog).show(); var vcenter_container = $(".vcenter_clusters", $create_host_dialog); vcenter_container.html(""); - $('
' + '
' + - '
' + - datacenter_name + ' ' + tr("Clusters") + - '
' + + '

' + tr("Please select the vCenter Clusters to be imported to OpenNebula. Each vCenter Cluster will be included as a new OpenNebula Host") + '

' + '
' + '
').appendTo(vcenter_container) $.each(response, function(datacenter_name, clusters){ $('
' + '
' + - '' + tr("Please select the vCenter Clusters and Templates to be imported to OpenNebula. Each vCenter Cluster will be included as a new OpenNebula Host") + '' + + '
' + + datacenter_name + ' ' + tr("Clusters") + + '
' + '
' + '
').appendTo(vcenter_container) @@ -1050,12 +1055,16 @@ function setupCreateHostDialog(){ $.each(clusters, function(id, cluster_name){ var row = $('
' + '
' + - '
' + + '
' + '' + + '
'+ + '
'+ '
' + + '
'+ + '
'+ '
'+ '
'+ '
'+ @@ -1064,67 +1073,186 @@ function setupCreateHostDialog(){ $(".cluster_name", row).data("cluster_name", cluster_name) $(".cluster_name", row).data("datacenter_name", datacenter_name) $(".cluster_name", row).on("change", function(){ - var path = '/vcenter/' + $(this).data("datacenter_name") + '/cluster/' + $(this).data("cluster_name"); - var templates_container = $(".vcenter_templates", $(this).closest(".vcenter_cluster")) - templates_container.html(generateAdvancedSection({ - html_id: path, - title: tr("Templates"), - content: ''+ - ''+ - ''+ - '' - })) + var templates_container = $(".vcenter_templates", $(this).closest(".vcenter_cluster")); + if ($(this).is(":checked")) { + var path = '/vcenter/' + $(this).data("datacenter_name") + '/cluster/' + $(this).data("cluster_name"); + templates_container.html(generateAdvancedSection({ + html_id: path, + title: tr("Templates"), + content: ''+ + ''+ + ''+ + '' + })) - $('a', templates_container).trigger("click") + $('a', templates_container).trigger("click") - $.ajax({ - url: path, - type: "GET", - //data: {timeout: timeout}, - dataType: "json", - headers: { - "X_VCENTER_USER": $("#vcenter_user", $create_host_dialog).val(), - "X_VCENTER_PASSWORD": $("#vcenter_password", $create_host_dialog).val(), - "X_VCENTER_HOST": $("#vcenter_host", $create_host_dialog).val() - }, - success: function(response){ - $(".content", templates_container).html(""); - console.log(response); - $.each(response, function(id, template){ - var trow = $('
' + - '
' + - '
' + - '' + - '
' + - '
'+ - '
'+ - '
'+ - '
').appendTo($(".content", templates_container)) + $.ajax({ + url: path, + type: "GET", + data: {timeout: false}, + dataType: "json", + headers: { + "X_VCENTER_USER": $("#vcenter_user", $create_host_dialog).val(), + "X_VCENTER_PASSWORD": $("#vcenter_password", $create_host_dialog).val(), + "X_VCENTER_HOST": $("#vcenter_host", $create_host_dialog).val() + }, + success: function(response){ + $(".content", templates_container).html(""); + console.log(response); + $.each(response, function(id, template){ + var trow = $('
' + + '
' + + '
' + + '' + + '
'+ + '
'+ + '
' + + '
'+ + '
'+ + '
'+ + '
'+ + '
'+ + '
').appendTo($(".content", templates_container)) - $(".template_name", trow).data("template_name", template.name) - $(".template_name", trow).data("one_template", template.one) - }); - }, - error: function(response){ + $(".template_name", trow).data("template_name", template.name) + $(".template_name", trow).data("one_template", template.one) + }); + }, + error: function(response){ + templates_container.html(""); onError({}, OpenNebula.Error(response)); - } - }); + } + }); + } else { + templates_container.html(""); + } }) }); } }); }, error: function(response){ - onError({}, OpenNebula.Error(response)); + $(".vcenter_clusters", $create_host_dialog).html('') + onError({}, OpenNebula.Error(response)); } }); return false; }) + + $("#import_vcenter_clusters", $create_host_dialog).on("click", function(){ + $(this).hide(); + + var cluster_id = $('#host_cluster_id .resource_list_select', $create_host_dialog).val(); + if (!cluster_id) cluster_id = "-1"; + + $.each($(".cluster_name:checked", $create_host_dialog), function(){ + console.log($(this).data("cluster_name")); + + var context = $(this).closest(".vcenter_cluster"); + $(".vcenter_host_result:not(.success)", context).html(''+ + ''+ + ''+ + ''); + + var host_json = { + "host": { + "name": $(this).data("cluster_name"), + "vm_mad": "vcenter", + "vnm_mad": "dummy", + "im_mad": "vcenter", + "cluster_id": cluster_id + } + }; + + OpenNebula.Host.create({ + timeout: true, + data: host_json, + success: function (cluster_context){ + return function(request, response) { + $(".vcenter_host_result", cluster_context).addClass("success").html( + ''+ + ''+ + ''+ + ''); + + $(".vcenter_host_response", cluster_context).html('

'+ + tr("Host created successfully")+' ID:'+response.HOST.ID+ + '

'); + + var template_raw = + "VCENTER_USER=\"" + $("#vcenter_user", $create_host_dialog).val() + "\"\n" + + "VCENTER_PASSWORD=\"" + $("#vcenter_password", $create_host_dialog).val() + "\"\n" + + "VCENTER_HOST=\"" + $("#vcenter_host", $create_host_dialog).val() + "\"\n"; + + Sunstone.runAction("Host.update_template", response.HOST.ID, template_raw); + + $.each($(".template_name:checked", cluster_context), function(){ + console.log($(this).data("one_template")); + + var template_context = $(this).closest(".vcenter_template"); + + $(".vcenter_template_result:not(.success)", template_context).html( + ''+ + ''+ + ''+ + ''); + + var template_json = { + "vmtemplate": { + "template_raw": $(this).data("one_template") + } + }; + + OpenNebula.Template.create({ + timeout: true, + data: template_json, + success: function(request, response) { + $(".vcenter_template_result", template_context).addClass("success").html( + ''+ + ''+ + ''+ + ''); + + $(".vcenter_template_response", template_context).html('

'+ + tr("Template created successfully")+' ID:'+response.VMTEMPLATE.ID+ + '

'); + }, + error: function (request, error_json){ + $(".vcenter_template_result", template_context).html(''+ + ''+ + ''+ + ''); + + $(".vcenter_template_response", template_context).html('

'+ + (error_json.error.message || tr("Cannot contact server: is it running and reachable?"))+ + '

'); + } + }); + }) + }; + }(context), + error: function (request, error_json){ + $(".vcenter_host_result", context).html(''+ + ''+ + ''+ + ''); + + $(".vcenter_host_response", context).html('

'+ + (error_json.error.message || tr("Cannot contact server: is it running and reachable?"))+ + '

'); + } + }); + }) + + return false + }); + // Show custom driver input only when custom is selected in selects $('input[name="custom_vmm_mad"],'+ 'input[name="custom_im_mad"],'+