diff --git a/install.sh b/install.sh index e783622f3b..8c6d4300b3 100755 --- a/install.sh +++ b/install.sh @@ -1546,7 +1546,8 @@ SUNSTONE_PUBLIC_JS_PLUGINS_FILES="\ src/sunstone/public/js/plugins/support-tab.js \ src/sunstone/public/js/plugins/zones-tab.js" -SUNSTONE_ROUTES_FILES="src/sunstone/routes/oneflow.rb" +SUNSTONE_ROUTES_FILES="src/sunstone/routes/oneflow.rb \ + src/sunstone/routes/vcenter.rb" # begin bower diff --git a/src/sunstone/etc/sunstone-server.conf b/src/sunstone/etc/sunstone-server.conf index 75b9a70bb4..1a69e29e06 100644 --- a/src/sunstone/etc/sunstone-server.conf +++ b/src/sunstone/etc/sunstone-server.conf @@ -189,3 +189,4 @@ :routes: - oneflow + - vcenter diff --git a/src/sunstone/public/js/plugins/hosts-tab.js b/src/sunstone/public/js/plugins/hosts-tab.js index a5f61fd886..82723e5691 100644 --- a/src/sunstone/public/js/plugins/hosts-tab.js +++ b/src/sunstone/public/js/plugins/hosts-tab.js @@ -127,21 +127,33 @@ var create_host_tmpl = \ @@ -955,6 +967,12 @@ function setupCreateHostDialog(){ $create_host_dialog.addClass("reveal-modal medium").attr("data-reveal", ""); $create_host_dialog.foundation() + $("#wizard_host_reset_button", $create_host_dialog).on("click", function(){ + $('#create_host_dialog').html(""); + setupCreateHostDialog(); + popUpCreateHostDialog(); + }) + $(".drivers", $create_host_dialog).hide(); $("#host_type_mad", $create_host_dialog).on("change", function(){ @@ -964,21 +982,149 @@ function setupCreateHostDialog(){ if (this.value == "custom") { $(".vcenter_credentials", $create_host_dialog).hide(); $("#vnm_mads", $create_host_dialog).show(); - $("#create_host_submit", $create_host_dialog).removeAttr("disabled"); + $("#create_host_submit", $create_host_dialog).show(); $(".drivers", $create_host_dialog).show(); } else if (this.value == "vcenter") { $("#vnm_mads", $create_host_dialog).hide(); $(".vcenter_credentials", $create_host_dialog).show(); - $("#create_host_submit", $create_host_dialog).attr("disabled", "disabled"); + $("#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(); - $("#create_host_submit", $create_host_dialog).removeAttr("disabled"); + $("#create_host_submit", $create_host_dialog).show(); $(".drivers", $create_host_dialog).hide(); } }) + $("#get_vcenter_clusters", $create_host_dialog).on("click", function(){ + // TODO notify if credentials empty + $(".vcenter_clusters", $create_host_dialog).html( + ''+ + ''+ + ''+ + '') + + $.ajax({ + url: '/vcenter', + 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){ + console.log(response); + $("#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") + + '
' + + '
' + + '
').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") + '' + + '
' + + '
').appendTo(vcenter_container) + + if (clusters.length == 0) { + $('
' + + '
' + + '' + + '
' + + '
').appendTo(vcenter_container) + } else { + $.each(clusters, function(id, cluster_name){ + var row = $('
' + + '
' + + '
' + + '' + + '
' + + '
'+ + '
'+ + '
'+ + '
').appendTo(vcenter_container) + + $(".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: ''+ + ''+ + ''+ + '' + })) + + $('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)) + + $(".template_name", trow).data("template_name", template.name) + $(".template_name", trow).data("one_template", template.one) + }); + }, + error: function(response){ + onError({}, OpenNebula.Error(response)); + } + }); + }) + }); + } + }); + }, + error: function(response){ + onError({}, OpenNebula.Error(response)); + } + }); + + return false; + }) + // Show custom driver input only when custom is selected in selects $('input[name="custom_vmm_mad"],'+ 'input[name="custom_im_mad"],'+ @@ -1047,8 +1193,8 @@ function popUpCreateHostDialog(){ insertSelectOptions('#host_cluster_id',$('div#create_host_dialog'), "Cluster", cluster_id, false); - $('div#create_host_dialog').foundation('reveal', 'open'); - $("input#name",$('div#create_host_dialog')).focus(); + $("#create_host_dialog").foundation('reveal', 'open'); + $("input#name",$("#create_host_dialog")).focus(); return false; } diff --git a/src/sunstone/routes/vcenter.rb b/src/sunstone/routes/vcenter.rb new file mode 100644 index 0000000000..8ecf3f6564 --- /dev/null +++ b/src/sunstone/routes/vcenter.rb @@ -0,0 +1,89 @@ +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L. # +# # +# 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. # +#--------------------------------------------------------------------------- # + +if !ONE_LOCATION + REMOTES_LOCATION="/var/lib/one/remotes" +else + REMOTES_LOCATION=ONE_LOCATION+"/var/remotes" +end + +# TODO vcenter_driver should be stored in RUBY_LIB_LOCATION +$: << REMOTES_LOCATION+"/vmm/vcenter/" + +require 'vcenter_driver' + +helpers do + def vcenter_client + vuser = request.env['HTTP_X_VCENTER_USER'] + vpass = request.env['HTTP_X_VCENTER_PASSWORD'] + vhost = request.env['HTTP_X_VCENTER_HOST'] + + if vuser.nil? || vpass.nil? || vhost.nil? + msg = "You have to provide the vCenter username, password and hostname" + logger.error("[vCenter] " + msg) + error = Error.new(msg) + error 404, error.to_json + end + + return VCenterDriver::VIClient.new_connection( + :user => vuser, + :password => vpass, + :host => vhost) + end + +# def af_format_response(resp) +# if CloudClient::is_error?(resp) +# logger.error("[OneFlow] " + resp.to_s) +# +# error = Error.new(resp.to_s) +# error resp.code.to_i, error.to_json +# else +# body resp.body.to_s +# end +# end +end + +get '/vcenter' do + begin + rs = vcenter_client.hierarchy + [200, rs.to_json] + rescue Exception => e + logger.error("[vCenter] " + e.message) + error = Error.new(e.message) + error 403, error.to_json + end +end + +get '/vcenter/:datacenter/cluster/:name' do + begin + rs = vcenter_client.vm_templates + + templates = rs[params[:datacenter]] + if templates.nil? + msg = "Datacenter " + params[:datacenter] + "not found" + logger.error("[vCenter] " + msg) + error = Error.new(msg) + error 404, error.to_json + end + + ctemplates = templates.select{|t| t[:host] == params[:name]} + [200, ctemplates.to_json] + rescue Exception => e + logger.error("[vCenter] " + e.message) + error = Error.new(e.message) + error 403, error.to_json + end +end \ No newline at end of file