From f62f4235e934753d6a0732e95a4f7098fcd4c0e9 Mon Sep 17 00:00:00 2001 From: nasark Date: Mon, 14 Nov 2022 11:18:18 -0500 Subject: [PATCH] move iso_datastore from core --- .../manageiq/providers/ovirt/infra_manager.rb | 4 +- .../ovirt/infra_manager/iso_datastore.rb | 52 ++++++++++++++++++ .../ovirt/infra_manager/ovirt_services.rb | 3 +- .../inventory/collector/infra_manager.rb | 4 ++ .../inventory/collector/target_collection.rb | 4 ++ .../ovirt/inventory/parser/infra_manager.rb | 16 ++++++ spec/factories/iso_datastore.rb | 3 ++ .../ovirt/infra_manager/iso_datastore_spec.rb | 53 +++++++++++++++++++ .../infra_manager/ovirt_services/v4_spec.rb | 3 +- 9 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 app/models/manageiq/providers/ovirt/infra_manager/iso_datastore.rb create mode 100644 spec/factories/iso_datastore.rb create mode 100644 spec/models/manageiq/providers/ovirt/infra_manager/iso_datastore_spec.rb diff --git a/app/models/manageiq/providers/ovirt/infra_manager.rb b/app/models/manageiq/providers/ovirt/infra_manager.rb index 19b051b6..e2261981 100644 --- a/app/models/manageiq/providers/ovirt/infra_manager.rb +++ b/app/models/manageiq/providers/ovirt/infra_manager.rb @@ -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 @@ -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 @@ -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 diff --git a/app/models/manageiq/providers/ovirt/infra_manager/iso_datastore.rb b/app/models/manageiq/providers/ovirt/infra_manager/iso_datastore.rb new file mode 100644 index 00000000..70ef5ee5 --- /dev/null +++ b/app/models/manageiq/providers/ovirt/infra_manager/iso_datastore.rb @@ -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 diff --git a/app/models/manageiq/providers/ovirt/infra_manager/ovirt_services.rb b/app/models/manageiq/providers/ovirt/infra_manager/ovirt_services.rb index bfee395f..7befba68 100644 --- a/app/models/manageiq/providers/ovirt/infra_manager/ovirt_services.rb +++ b/app/models/manageiq/providers/ovirt/infra_manager/ovirt_services.rb @@ -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) diff --git a/app/models/manageiq/providers/ovirt/inventory/collector/infra_manager.rb b/app/models/manageiq/providers/ovirt/inventory/collector/infra_manager.rb index 57475a7c..bdc56d5e 100644 --- a/app/models/manageiq/providers/ovirt/inventory/collector/infra_manager.rb +++ b/app/models/manageiq/providers/ovirt/inventory/collector/infra_manager.rb @@ -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 diff --git a/app/models/manageiq/providers/ovirt/inventory/collector/target_collection.rb b/app/models/manageiq/providers/ovirt/inventory/collector/target_collection.rb index 5ae5aae3..3998a78f 100644 --- a/app/models/manageiq/providers/ovirt/inventory/collector/target_collection.rb +++ b/app/models/manageiq/providers/ovirt/inventory/collector/target_collection.rb @@ -150,6 +150,10 @@ def templates t end + def advertised_images + [] + end + def parse_targets! target.targets.each do |t| case t diff --git a/app/models/manageiq/providers/ovirt/inventory/parser/infra_manager.rb b/app/models/manageiq/providers/ovirt/inventory/parser/infra_manager.rb index 1d00e266..b87d3dd7 100644 --- a/app/models/manageiq/providers/ovirt/inventory/parser/infra_manager.rb +++ b/app/models/manageiq/providers/ovirt/inventory/parser/infra_manager.rb @@ -11,6 +11,7 @@ def parse vms networks vnic_profiles + advertised_images $rhevm_log.info("#{log_header}...Complete") end @@ -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) diff --git a/spec/factories/iso_datastore.rb b/spec/factories/iso_datastore.rb new file mode 100644 index 00000000..dce035dc --- /dev/null +++ b/spec/factories/iso_datastore.rb @@ -0,0 +1,3 @@ +FactoryBot.define do + factory :iso_datastore, :class => 'ManageIQ::Providers::Ovirt::InfraManager::IsoDatastore' +end diff --git a/spec/models/manageiq/providers/ovirt/infra_manager/iso_datastore_spec.rb b/spec/models/manageiq/providers/ovirt/infra_manager/iso_datastore_spec.rb new file mode 100644 index 00000000..45c4a8b2 --- /dev/null +++ b/spec/models/manageiq/providers/ovirt/infra_manager/iso_datastore_spec.rb @@ -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 diff --git a/spec/models/manageiq/providers/ovirt/infra_manager/ovirt_services/v4_spec.rb b/spec/models/manageiq/providers/ovirt/infra_manager/ovirt_services/v4_spec.rb index c93b09cb..a54f7be6 100644 --- a/spec/models/manageiq/providers/ovirt/infra_manager/ovirt_services/v4_spec.rb +++ b/spec/models/manageiq/providers/ovirt/infra_manager/ovirt_services/v4_spec.rb @@ -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