Skip to content

Commit

Permalink
move iso_datastore from core
Browse files Browse the repository at this point in the history
  • Loading branch information
nasark committed Dec 2, 2022
1 parent 4cae11f commit f62f423
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 3 deletions.
4 changes: 3 additions & 1 deletion app/models/manageiq/providers/ovirt/infra_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ManageIQ::Providers::Ovirt::InfraManager < ManageIQ::Providers::InfraManag
require_nested :EventParser
require_nested :EventTargetParser
require_nested :Folder
require_nested :IsoDatastore
require_nested :RefreshWorker
require_nested :Refresher
require_nested :ResourcePool
Expand All @@ -27,6 +28,7 @@ class ManageIQ::Providers::Ovirt::InfraManager < ManageIQ::Providers::InfraManag
has_many :vm_and_template_ems_custom_fields, :through => :vms_and_templates, :source => :ems_custom_attributes
has_many :external_distributed_virtual_switches, :dependent => :destroy, :foreign_key => :ems_id, :inverse_of => :ext_management_system
has_many :external_distributed_virtual_lans, -> { distinct }, :through => :external_distributed_virtual_switches, :source => :lans
has_many :iso_datastores, :dependent => :destroy, :foreign_key => :ems_id, :inverse_of => :ext_management_system

include HasNetworkManagerMixin

Expand All @@ -42,7 +44,7 @@ class ManageIQ::Providers::Ovirt::InfraManager < ManageIQ::Providers::InfraManag
end

supports :create_iso_datastore do
unsupported_reason_add(:create_iso_datastore, _("Already has an ISO datastore")) if iso_datastore
unsupported_reason_add(:create_iso_datastore, _("Already has an ISO datastore")) if iso_datastores
end

def ensure_managers
Expand Down
52 changes: 52 additions & 0 deletions app/models/manageiq/providers/ovirt/infra_manager/iso_datastore.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class ManageIQ::Providers::Ovirt::InfraManager::IsoDatastore < ManageIQ::Providers::Ovirt::InfraManager::Storage
belongs_to :ext_management_system, :foreign_key => :ems_id

virtual_delegate :name, :to => :ext_management_system, :allow_nil => true, :type => :string

# Synchronize advertised images as a queued task. The
# queue name and the queue zone are derived from the EMS.
#
def synchronize_advertised_images_queue
MiqQueue.put_unless_exists(
:class_name => self.class.name,
:instance_id => id,
:method_name => "synchronize_advertised_images",
:queue_name => ext_management_system.queue_name_for_ems_operations,
:zone => ext_management_system.try(:my_zone),
:role => "ems_operations"
)
end

def advertised_images
return [] unless ext_management_system.kind_of?(ManageIQ::Providers::Ovirt::InfraManager)

ext_management_system.ovirt_services.advertised_images
end

def synchronize_advertised_images
log_for = "ISO Datastore on Management System <#{name}>"

_log.info("Synchronizing images on #{log_for}...")
db_image_hash = iso_images.index_by(&:name)

advertised_images.each do |image_name|
if db_image_hash.include?(image_name)
db_image_hash.delete(image_name)
else
iso_images.create(:name => image_name)
end
end

db_image_hash.each_value(&:destroy)

clear_association_cache
update_attribute(:last_refresh_on, Time.now.utc)

_log.info("Synchronizing images on #{log_for}...Complete")
rescue ManageIQ::Providers::Ovirt::InfraManager::OvirtServices::Error
end

def self.display_name(number = 1)
n_('ISO Datastore', 'ISO Datastores', number)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ def advertised_images

sd_service = ems_service.system_service.storage_domains_service.storage_domain_service(iso_sd.id)
iso_images = sd_service.files_service.list
iso_images.collect(&:name)

iso_images
end
rescue OvirtSDK4::Error => err
name = ext_management_system.try(:name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def templates
collected_inventory[:template]
end

def advertised_images
manager.ovirt_services.advertised_images
end

def collect_networks
collected_inventory[:network]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ def templates
t
end

def advertised_images
[]
end

def parse_targets!
target.targets.each do |t|
case t
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def parse
vms
networks
vnic_profiles
advertised_images

$rhevm_log.info("#{log_header}...Complete")
end
Expand Down Expand Up @@ -113,6 +114,21 @@ def storagedomains
end
end

def advertised_images
_log.info("Synchronizing images...")
collector.advertised_images.each do |image|
ems_ref = ManageIQ::Providers::Ovirt::InfraManager.make_ems_ref(image.href)

persister.storages.find_or_build(ems_ref).assign_attributes(
:name => image.name,
:ems_ref => ems_ref,
:store_type => "ISO",
:type => "ManageIQ::Providers::Ovirt::InfraManager::IsoDatastore"
)
end
_log.info("Synchronizing images...Complete")
end

def datacenters
collector.datacenters.each do |datacenter|
ems_ref = ManageIQ::Providers::Ovirt::InfraManager.make_ems_ref(datacenter.href)
Expand Down
3 changes: 3 additions & 0 deletions spec/factories/iso_datastore.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FactoryBot.define do
factory :iso_datastore, :class => 'ManageIQ::Providers::Ovirt::InfraManager::IsoDatastore'
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
RSpec.describe ManageIQ::Providers::Ovirt::InfraManager::IsoDatastore do
include Spec::Support::ArelHelper

let(:ems) { FactoryBot.create(:ems_redhat) }
let(:iso_datastore) { FactoryBot.create(:iso_datastore, :ext_management_system => ems, :name => ems.name) }

context "queued methods" do
it 'queues a sync task with synchronize_advertised_images_queue' do
queue = iso_datastore.synchronize_advertised_images_queue

expect(queue).to have_attributes(
:class_name => described_class.name,
:method_name => 'synchronize_advertised_images',
:role => 'ems_operations',
:queue_name => 'generic',
:zone => ems.my_zone,
:args => []
)
end
end

describe "#advertised_images" do
subject(:advertised_images) { iso_datastore.advertised_images }

context "ems is not rhv" do
let(:ems) { FactoryBot.create(:ems_vmware) }
it "returns empty array" do
expect(advertised_images).to eq([])
end
end

context "ems is rhv" do
context "supports api4" do
it "send the method to ovirt services v4" do
expect_any_instance_of(ManageIQ::Providers::Redhat::InfraManager::OvirtServices::V4)
.to receive(:advertised_images)
advertised_images
end
end
end

describe "#name" do
it "has a name" do
expect(iso_datastore.name).to eq(ems.name)
end

it "fetches name via sql" do
iso_datastore
expect(virtual_column_sql_value(Storage, "name")).to eq(ems.name)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
context "there are iso domains attached to the data-center" do
context "there are active iso domains" do
it 'returns iso images from an active domain' do
expect(advertised_images).to match_array(%w[iso_1 iso_2])
res = advertised_images.map { |img| img.name }
expect(res).to match_array(%w[iso_1 iso_2])
end
end

Expand Down

0 comments on commit f62f423

Please sign in to comment.