1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-11 05:17:41 +03:00

F #1808: vCenter importer feedback and class variables

F #1808: code restructure and output
This commit is contained in:
semedi 2018-03-20 16:35:12 +01:00
parent 4e6d6a891b
commit b5debcf606
4 changed files with 108 additions and 45 deletions

View File

@ -113,7 +113,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
if options[:host] && options[:object]
o = options[:object]
vi_client = VCenterDriver::VIClient.new_from_host(options[:host])
importer = VCenterDriver::VcImporter.new_(helper.client, vi_client, o)
importer = VCenterDriver::VcImporter.new_child(helper.client, vi_client, o)
list = importer.get_list()
helper.list_object(options, list)
@ -133,11 +133,14 @@ cmd=CommandParser::CmdParser.new(ARGV) do
if options[:host] && options[:object]
o = options[:object]
vi_client = VCenterDriver::VIClient.new_from_host(options[:host])
importer = VCenterDriver::VcImporter.new_(helper.client, vi_client, o)
importer = VCenterDriver::VcImporter.new_child(helper.client, vi_client, o)
importer.get_list()
importer.process_import(indexes)
importer.stdout
else
puts "for the moment is not supported without the proper authentication"
end

View File

@ -98,7 +98,7 @@ helpers do
host_id = params["host"]
vi_client = VCenterDriver::VIClient.new_from_host(host_id) if host_id
one_client = OpenNebula::Client.new
$importer = VCenterDriver::VcImporter.new_(one_client, vi_client, type)
$importer = VCenterDriver::VcImporter.new_child(one_client, vi_client, type)
end
# def af_format_response(resp)
@ -465,11 +465,13 @@ end
post '/vcenter/datastores' do
begin
$importer.process_import(params["datastores"])
[200, "Dastastores imported successfully".to_json]
output = $importer.out
[200, output.to_json]
rescue Exception => e
binding.pry
logger.error("[vCenter] " + e.message)
error = Error.new(e.message)
error 403, error.to_json
end
end
end

View File

@ -675,7 +675,7 @@ class DsImporter < VCenterDriver::VcImporter
end
def add_cluster(cid, eid)
one_cluster = @clusters[cid]
one_cluster = @info[:clusters][cid]
raise "no cluster defined" unless one_cluster
rc = one_cluster.adddatastore(eid)
@ -683,8 +683,8 @@ class DsImporter < VCenterDriver::VcImporter
def remove_default(id)
cid = 0
@clusters[cid] ||= VCenterDriver::VIHelper.one_item(OpenNebula::Cluster, cid.to_s, false)
@clusters[cid].deldatastore(id.to_i)
@info[:clusters][cid] ||= VCenterDriver::VIHelper.one_item(OpenNebula::Cluster, cid.to_s, false)
@info[:clusters][cid].deldatastore(id.to_i)
end
def import(selected)
@ -701,16 +701,25 @@ class DsImporter < VCenterDriver::VcImporter
pair = selected[:ds]
clusters = selected[:cluster]
res = {id: [], name: selected[:simple_name]}
@info[:rollback] = []
pair.each do |ds|
create(ds[:one]) do |one_object|
one_object.info
id = one_object['ID']
res[:id] << id
add_clusters(id, clusters)
inner.call(one_object, @vi_client.get_host_credentials)
end
end
return res
end
def rollback
# not implemented
end
end

View File

@ -2,41 +2,10 @@ module VCenterDriver
class VcImporter
attr_accessor :list
##################
# Constructors
##################
def initialize(one_client, vi_client)
@vi_client = vi_client
@one_client = one_client
@list = {}
@clusters = {}
end
def self.new_(one_client, vi_client, type)
case type
when "datastores"
VCenterDriver::DsImporter.new(one_client, vi_client)
else
raise "unknown object type"
end
end
def process_import(indexes)
raise "the list is empty" if list_empty?
indexes = indexes.gsub(/\s+/, "").split(",")
indexes.each do |index|
# select object from importer mem
selected = get_element(index)
import(selected)
end
end
protected
####################
########################################
# ABSTRACT INTERFACE
####################
########################################
MESS = "missing method from parent"
@ -44,13 +13,90 @@ module VCenterDriver
def add_cluster(cid, eid) raise MESS end
def remove_default(id) raise MESS end
def import(selected) raise MESS end
def rollback; raise MESS end
########################################
###############
public
########################################
# Constructors
########################################
def initialize(one_client, vi_client)
@vi_client = vi_client
@one_client = one_client
@list = {}
@info = {}
@info[:clusters] = {}
end
def self.new_child(one_client, vi_client, type)
case type
when "datastores"
VCenterDriver::DsImporter.new(one_client, vi_client)
else
raise "unknown object type"
end
end
########################################
def one_str
return @one_class.to_s.split('::').last if @one_class
"OpenNebula object"
end
def stdout
@info[:success].each do |o|
puts "Datastore #{o[:name]} with ids: #{o[:id].join(' ')} created!"
puts
end
@info[:error].each do |index|
e = @info[index][:e]
puts "Error: Couldn't import #{index} due to #{e.message}!"
puts
end
end
def output
{ sucess: @info[:sucess], error: @out[:error] }
end
def process_import(indexes, opts = {})
raise "the list is empty" if list_empty?
indexes = indexes.gsub(/\s+/, "").split(",")
@info[:success] = []
@info[:error] = []
indexes.each do |index|
begin
@info[index] = {}
@info[index][:opts] = opts[index]
# select object from importer mem
selected = get_element(index)
@info[:success] << import(selected)
rescue Exception => e
@info[:error] << index
@info[index][:e] = e
rollback
end
end
end
protected
def create(info, &block)
resource = VCenterDriver::VIHelper.new_one_item(@one_class)
message = "Error creating the OpenNebula resource"
rc = resource.allocate(info)
VCenterDriver::VIHelper.check_error(rc, "Error creating resource")
VCenterDriver::VIHelper.check_error(rc, message)
rc = block.call(resource)
end
@ -60,12 +106,15 @@ module VCenterDriver
def get_element(ref)
raise "the list is empty" if list_empty?
@list[ref] if @list[ref]
return @list[ref] if @list[ref]
raise "#{ref} not found!"
end
def add_clusters(one_id, clusters, &block)
clusters.each do |cid|
@clusters[cid] ||= VCenterDriver::VIHelper.one_item(OpenNebula::Cluster, cid.to_s, false)
@info[:clusters][cid] ||= VCenterDriver::VIHelper.one_item(OpenNebula::Cluster, cid.to_s, false)
rc = add_cluster(cid.to_i, one_id.to_i)
VCenterDriver::VIHelper.check_error(rc, "add element to cluster")
end