mirror of
https://github.com/OpenNebula/one.git
synced 2025-01-25 06:03:36 +03:00
F #1808: vCenter importer feedback and class variables
F #1808: code restructure and output
This commit is contained in:
parent
4e6d6a891b
commit
b5debcf606
@ -113,7 +113,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
|
|||||||
if options[:host] && options[:object]
|
if options[:host] && options[:object]
|
||||||
o = options[:object]
|
o = options[:object]
|
||||||
vi_client = VCenterDriver::VIClient.new_from_host(options[:host])
|
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()
|
list = importer.get_list()
|
||||||
helper.list_object(options, list)
|
helper.list_object(options, list)
|
||||||
@ -133,11 +133,14 @@ cmd=CommandParser::CmdParser.new(ARGV) do
|
|||||||
if options[:host] && options[:object]
|
if options[:host] && options[:object]
|
||||||
o = options[:object]
|
o = options[:object]
|
||||||
vi_client = VCenterDriver::VIClient.new_from_host(options[:host])
|
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.get_list()
|
||||||
|
|
||||||
importer.process_import(indexes)
|
importer.process_import(indexes)
|
||||||
|
|
||||||
|
|
||||||
|
importer.stdout
|
||||||
else
|
else
|
||||||
puts "for the moment is not supported without the proper authentication"
|
puts "for the moment is not supported without the proper authentication"
|
||||||
end
|
end
|
||||||
|
@ -98,7 +98,7 @@ helpers do
|
|||||||
host_id = params["host"]
|
host_id = params["host"]
|
||||||
vi_client = VCenterDriver::VIClient.new_from_host(host_id) if host_id
|
vi_client = VCenterDriver::VIClient.new_from_host(host_id) if host_id
|
||||||
one_client = OpenNebula::Client.new
|
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
|
end
|
||||||
|
|
||||||
# def af_format_response(resp)
|
# def af_format_response(resp)
|
||||||
@ -465,11 +465,13 @@ end
|
|||||||
post '/vcenter/datastores' do
|
post '/vcenter/datastores' do
|
||||||
begin
|
begin
|
||||||
$importer.process_import(params["datastores"])
|
$importer.process_import(params["datastores"])
|
||||||
[200, "Dastastores imported successfully".to_json]
|
|
||||||
|
output = $importer.out
|
||||||
|
[200, output.to_json]
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
binding.pry
|
binding.pry
|
||||||
logger.error("[vCenter] " + e.message)
|
logger.error("[vCenter] " + e.message)
|
||||||
error = Error.new(e.message)
|
error = Error.new(e.message)
|
||||||
error 403, error.to_json
|
error 403, error.to_json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -675,7 +675,7 @@ class DsImporter < VCenterDriver::VcImporter
|
|||||||
end
|
end
|
||||||
|
|
||||||
def add_cluster(cid, eid)
|
def add_cluster(cid, eid)
|
||||||
one_cluster = @clusters[cid]
|
one_cluster = @info[:clusters][cid]
|
||||||
raise "no cluster defined" unless one_cluster
|
raise "no cluster defined" unless one_cluster
|
||||||
|
|
||||||
rc = one_cluster.adddatastore(eid)
|
rc = one_cluster.adddatastore(eid)
|
||||||
@ -683,8 +683,8 @@ class DsImporter < VCenterDriver::VcImporter
|
|||||||
|
|
||||||
def remove_default(id)
|
def remove_default(id)
|
||||||
cid = 0
|
cid = 0
|
||||||
@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)
|
||||||
@clusters[cid].deldatastore(id.to_i)
|
@info[:clusters][cid].deldatastore(id.to_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
def import(selected)
|
def import(selected)
|
||||||
@ -701,16 +701,25 @@ class DsImporter < VCenterDriver::VcImporter
|
|||||||
pair = selected[:ds]
|
pair = selected[:ds]
|
||||||
clusters = selected[:cluster]
|
clusters = selected[:cluster]
|
||||||
|
|
||||||
|
res = {id: [], name: selected[:simple_name]}
|
||||||
|
@info[:rollback] = []
|
||||||
pair.each do |ds|
|
pair.each do |ds|
|
||||||
create(ds[:one]) do |one_object|
|
create(ds[:one]) do |one_object|
|
||||||
one_object.info
|
one_object.info
|
||||||
id = one_object['ID']
|
id = one_object['ID']
|
||||||
|
res[:id] << id
|
||||||
|
|
||||||
add_clusters(id, clusters)
|
add_clusters(id, clusters)
|
||||||
|
|
||||||
inner.call(one_object, @vi_client.get_host_credentials)
|
inner.call(one_object, @vi_client.get_host_credentials)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return res
|
||||||
|
end
|
||||||
|
|
||||||
|
def rollback
|
||||||
|
# not implemented
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -2,41 +2,10 @@ module VCenterDriver
|
|||||||
class VcImporter
|
class VcImporter
|
||||||
attr_accessor :list
|
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
|
protected
|
||||||
####################
|
########################################
|
||||||
# ABSTRACT INTERFACE
|
# ABSTRACT INTERFACE
|
||||||
####################
|
########################################
|
||||||
|
|
||||||
MESS = "missing method from parent"
|
MESS = "missing method from parent"
|
||||||
|
|
||||||
@ -44,13 +13,90 @@ module VCenterDriver
|
|||||||
def add_cluster(cid, eid) raise MESS end
|
def add_cluster(cid, eid) raise MESS end
|
||||||
def remove_default(id) raise MESS end
|
def remove_default(id) raise MESS end
|
||||||
def import(selected) 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)
|
def create(info, &block)
|
||||||
resource = VCenterDriver::VIHelper.new_one_item(@one_class)
|
resource = VCenterDriver::VIHelper.new_one_item(@one_class)
|
||||||
|
message = "Error creating the OpenNebula resource"
|
||||||
|
|
||||||
rc = resource.allocate(info)
|
rc = resource.allocate(info)
|
||||||
VCenterDriver::VIHelper.check_error(rc, "Error creating resource")
|
VCenterDriver::VIHelper.check_error(rc, message)
|
||||||
|
|
||||||
rc = block.call(resource)
|
rc = block.call(resource)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -60,12 +106,15 @@ module VCenterDriver
|
|||||||
|
|
||||||
def get_element(ref)
|
def get_element(ref)
|
||||||
raise "the list is empty" if list_empty?
|
raise "the list is empty" if list_empty?
|
||||||
@list[ref] if @list[ref]
|
|
||||||
|
return @list[ref] if @list[ref]
|
||||||
|
|
||||||
|
raise "#{ref} not found!"
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_clusters(one_id, clusters, &block)
|
def add_clusters(one_id, clusters, &block)
|
||||||
clusters.each do |cid|
|
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)
|
rc = add_cluster(cid.to_i, one_id.to_i)
|
||||||
VCenterDriver::VIHelper.check_error(rc, "add element to cluster")
|
VCenterDriver::VIHelper.check_error(rc, "add element to cluster")
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user