Skip to content

Commit

Permalink
Merge pull request #14398 from borod108/rfe/new_provider_refresh
Browse files Browse the repository at this point in the history
Use the new OvirtSDK for refresh
  • Loading branch information
blomquisg authored Mar 27, 2017
2 parents 30144fa + 7e639d3 commit e7656ce
Show file tree
Hide file tree
Showing 37 changed files with 17,171 additions and 309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def connect(options = {})
connection = self.class.public_send(connect_method, connect_options)

# Copy the API path to the endpoints table:
default_endpoint.path = version == 4 ? '/ovirt-engine/api' : connection.api_path
default_endpoint.path = version.to_i == 4 ? '/ovirt-engine/api' : connection.api_path

connection
end
Expand Down Expand Up @@ -137,7 +137,7 @@ def with_provider_connection(options = {})

def verify_credentials_for_rhevm(options = {})
require 'ovirt'
with_provider_connection(options, &:api)
with_provider_connection(options) { |connection| connection.test(true) }
rescue SocketError, Errno::EHOSTUNREACH, Errno::ENETUNREACH
_log.warn($ERROR_INFO)
raise MiqException::MiqUnreachableError, $ERROR_INFO
Expand Down Expand Up @@ -230,6 +230,15 @@ def highest_supported_api_version
supported_api_versions.sort.last
end

def highest_allowed_api_version
return 3 unless use_ovirt_sdk?
highest_supported_api_version
end

def use_ovirt_sdk?
::Settings.ems.ems_redhat.use_ovirt_engine_sdk
end

class_methods do
def api3_supported_features
[]
Expand Down Expand Up @@ -276,7 +285,7 @@ def raw_connect_v4(options = {})
:scheme => options[:scheme],
:host => options[:server],
:port => options[:port],
:path => options[:path]
:path => options[:path] || '/ovirt-engine/api'
)

OvirtSDK4::Connection.new(
Expand Down Expand Up @@ -310,7 +319,14 @@ def raw_connect_v3(options = {})
params[:timeout] = read_timeout if read_timeout
params[:open_timeout] = open_timeout if open_timeout
const = options[:service] == "Inventory" ? OvirtInventory : Ovirt.const_get(options[:service])
const.new(params)
conn = const.new(params)
OvirtConnectionDecorator.new(conn)
end

class OvirtConnectionDecorator < SimpleDelegator
def test(_raise_exceptions)
api
end
end

def default_history_database_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def self.parse_new_target(full_data, message, ems, event_type)

def self.parse_new_vm(vm, message, event_type, ems)
ems_ref = ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(vm[:href])
parser = ManageIQ::Providers::Redhat::InfraManager::Refresh::Parse::ParserBuilder.new(ems).build
parser = ManageIQ::Providers::Redhat::InfraManager::Refresh::Parse::ParserBuilder.new(ems, :force_version => 3).build
parser.create_vm_hash(ems_ref.include?('/templates/'), ems_ref, vm[:id], parse_target_name(message, event_type))
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ManageIQ::Providers::Redhat::InfraManager::Inventory
require_nested :Inventory
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module ManageIQ::Providers::Redhat::InfraManager::Inventory
class Builder
attr_reader :ext_management_system

def initialize(ems)
@ext_management_system = ems
end

def build
strategy_model = ManageIQ::Providers::Redhat::InfraManager::Inventory::Strategies
api_version = ext_management_system.highest_allowed_api_version
"#{strategy_model}::V#{api_version}".constantize
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module ManageIQ::Providers::Redhat::InfraManager::Inventory
class Error < StandardError; end
class VmNotReadyToBoot < Error; end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module ManageIQ::Providers::Redhat::InfraManager::Inventory::Strategies
class V3
attr_reader :ext_management_system

def initialize(args)
@ext_management_system = args[:ems]
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
module ManageIQ::Providers::Redhat::InfraManager::Inventory::Strategies
class V4
attr_accessor :connection
attr_reader :ems

def initialize(args)
@ems = args[:ems]
end

def host_targeted_refresh(target)
ems.with_provider_connection(:version => 4) do |connection|
@connection = connection
res = {}
res[:host] = collect_host(get_uuid_from_target(target))
res
end
end

def vm_targeted_refresh(target)
ems.with_provider_connection(:version => 4) do |connection|
@connection = connection
vm_id = get_uuid_from_target(target)
res = {}
res[:cluster] = collect_clusters
res[:datacenter] = collect_datacenters
res[:vm] = collect_vm_by_uuid(vm_id)
res[:storage] = target.storages.empty? ? collect_storages : collect_storage(target.storages.map { |s| get_uuid_from_target(s) })
res[:template] = search_templates("vm.id=#{vm_id}")
res
end
end

def get_uuid_from_href(ems_ref)
URI(ems_ref).path.split('/').last
end

def get_uuid_from_target(object)
get_uuid_from_href(object.ems_ref)
end

def refresh
ems.with_provider_connection(:version => 4) do |connection|
@connection = connection
res = {}
res[:cluster] = collect_clusters
res[:storage] = collect_storages
res[:host] = collect_hosts
res[:vm] = collect_vms
res[:template] = collect_templates
res[:network] = collect_networks
res[:datacenter] = collect_datacenters
res
end
end

def collect_clusters
connection.system_service.clusters_service.list
end

def collect_storages
connection.system_service.storage_domains_service.list
end

def collect_storage(uuids)
uuids.collect do |uuid|
connection.system_service.storage_domains_service.storage_domain_service(uuid).get
end
end

def collect_hosts
connection.system_service.hosts_service.list.collect do |h|
HostPreloadedAttributesDecorator.new(h, connection)
end
end

def collect_host(uuid)
host = connection.system_service.hosts_service.host_service(uuid).get
[HostPreloadedAttributesDecorator.new(host, connection)]
end

def collect_vms
connection.system_service.vms_service.list.collect do |vm|
VmPreloadedAttributesDecorator.new(vm, connection)
end
end

def collect_vm_by_uuid(uuid)
vm = connection.system_service.vms_service.vm_service(uuid).get
[VmPreloadedAttributesDecorator.new(vm, connection)]
end

def collect_templates
connection.system_service.templates_service.list.collect do |template|
TemplatePreloadedAttributesDecorator.new(template, connection)
end
end

def search_templates(search)
connection.system_service.templates_service.list(:search => search).collect do |template|
TemplatePreloadedAttributesDecorator.new(template, connection)
end
end

def collect_networks
connection.system_service.networks_service.list
end

def collect_datacenters
connection.system_service.data_centers_service.list.collect do |datacenter|
DatacenterPreloadedAttributesDecorator.new(datacenter, connection)
end
end

def api
ems.with_provider_connection(:version => 4) do |connection|
connection.system_service.get.product_info.version.full_version
end
end

def service
ems.with_provider_connection(:version => 4) do |connection|
OpenStruct.new(:version_string => connection.system_service.get.product_info.version.full_version)
end
end

class HostPreloadedAttributesDecorator < SimpleDelegator
attr_reader :nics, :statistics
def initialize(host, connection)
@obj = host
@nics = connection.follow_link(host.nics)
@statistics = connection.follow_link(host.statistics)
super(host)
end
end

class DatacenterPreloadedAttributesDecorator < SimpleDelegator
attr_reader :storage_domains
def initialize(datacenter, connection)
@obj = datacenter
@storage_domains = connection.follow_link(datacenter.storage_domains)
super(datacenter)
end
end

class VmPreloadedAttributesDecorator < SimpleDelegator
attr_reader :disks, :nics, :reported_devices, :snapshots
def initialize(vm, connection)
@obj = vm
@disks = self.class.get_attached_disks(vm, connection)
@nics = connection.follow_link(vm.nics)
@reported_devices = connection.follow_link(vm.reported_devices)
@snapshots = connection.follow_link(vm.snapshots)
super(vm)
end

def self.get_attached_disks(vm, connection)
AttachedDisksFetcher.get_attached_disks(vm, connection)
end
end

class AttachedDisksFetcher
def self.get_attached_disks(disks_owner, connection)
attachments = connection.follow_link(disks_owner.disk_attachments)
attachments.map do |attachment|
res = connection.follow_link(attachment.disk)
res.interface = attachment.interface
res.bootable = attachment.bootable
res.active = attachment.active
res
end
end
end

class TemplatePreloadedAttributesDecorator < SimpleDelegator
attr_reader :disks, :nics
def initialize(template, connection)
@obj = template
@disks = AttachedDisksFetcher.get_attached_disks(template, connection)
@nics = connection.follow_link(template.nics)
super(template)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module ManageIQ::Providers::Redhat::InfraManager::Refresh::Parse
class ParserBuilder
attr_reader :ext_management_system
attr_reader :ext_management_system, :force_version

def initialize(ems)
def initialize(ems, options = {})
@ext_management_system = ems
@force_version = options[:force_version]
end

def build
parse_model = ManageIQ::Providers::Redhat::InfraManager::Refresh::Parse::Strategies
api_version = ext_management_system.highest_supported_api_version
api_version = force_version || ext_management_system.highest_allowed_api_version
"#{parse_model}::Api#{api_version}".constantize
end
end
Expand Down
Loading

0 comments on commit e7656ce

Please sign in to comment.