From 7c0a965c10b795f5e1583340233f490eed82eaef Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Wed, 9 May 2018 13:16:16 -0400 Subject: [PATCH] Fix storage parsing to use datastore url Fix the storage parser to use the datastore location as the manager_ref not the managed object reference. --- .../vmware/infra_manager/inventory/parser.rb | 3 +- .../inventory/parser/datastore.rb | 7 ++++- .../inventory/parser/virtual_machine.rb | 23 ++++++++++++++- .../infra_manager/inventory/collector_spec.rb | 28 +++++++++++++++++-- 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/app/models/manageiq/providers/vmware/infra_manager/inventory/parser.rb b/app/models/manageiq/providers/vmware/infra_manager/inventory/parser.rb index 114b71850..e74bbd772 100644 --- a/app/models/manageiq/providers/vmware/infra_manager/inventory/parser.rb +++ b/app/models/manageiq/providers/vmware/infra_manager/inventory/parser.rb @@ -58,7 +58,7 @@ def parse_datacenter(object, kind, props) end def parse_datastore(object, kind, props) - persister.storages.targeted_scope << object._ref + persister.storages.targeted_scope << parse_datastore_location(props) return if kind == "leave" storage_hash = { @@ -226,6 +226,7 @@ def parse_virtual_machine(object, kind, props) parse_virtual_machine_config(vm_hash, props) parse_virtual_machine_resource_config(vm_hash, props) parse_virtual_machine_summary(vm_hash, props) + parse_virtual_machine_storage(vm_hash, props) vm = persister.vms_and_templates.build(vm_hash) diff --git a/app/models/manageiq/providers/vmware/infra_manager/inventory/parser/datastore.rb b/app/models/manageiq/providers/vmware/infra_manager/inventory/parser/datastore.rb index 01714bea3..9b1491e41 100644 --- a/app/models/manageiq/providers/vmware/infra_manager/inventory/parser/datastore.rb +++ b/app/models/manageiq/providers/vmware/infra_manager/inventory/parser/datastore.rb @@ -5,7 +5,7 @@ def parse_datastore_summary(storage_hash, props) return if summary.nil? storage_hash[:name] = summary[:name] - storage_hash[:location] = normalize_storage_uid(summary[:url]) + storage_hash[:location] = parse_datastore_location(props) storage_hash[:store_type] = summary[:type].to_s.upcase storage_hash[:total_space] = summary[:capacity] storage_hash[:free_space] = summary[:freeSpace] @@ -13,6 +13,11 @@ def parse_datastore_summary(storage_hash, props) storage_hash[:multiplehostaccess] = summary[:multipleHostAccess].to_s.downcase == "true" end + def parse_datastore_location(props) + url = props.fetch_path(:summary, :url) + normalize_storage_uid(url) if url + end + def parse_datastore_capability(storage_hash, props) capability = props[:capability] return if capability.nil? diff --git a/app/models/manageiq/providers/vmware/infra_manager/inventory/parser/virtual_machine.rb b/app/models/manageiq/providers/vmware/infra_manager/inventory/parser/virtual_machine.rb index 944eb1898..b4902b3d3 100644 --- a/app/models/manageiq/providers/vmware/infra_manager/inventory/parser/virtual_machine.rb +++ b/app/models/manageiq/providers/vmware/infra_manager/inventory/parser/virtual_machine.rb @@ -60,6 +60,21 @@ def parse_virtual_machine_summary(vm_hash, props) parse_virtual_machine_summary_runtime(vm_hash, props) end + def parse_virtual_machine_storage(vm_hash, props) + vm_path_name = props.fetch_path(:summary, :config, :vmPathName) + return if vm_path_name.nil? + + datastore_name = vm_path_name.gsub(/^\[([^\]]*)\].*/, '\1') + return if datastore_name.nil? + + datastore = props[:datastore].to_a.detect do |ds| + cache.find(ds)&.dig(:summary, :name) == datastore_name + end + + datastore_props = cache.find(datastore) if datastore + vm_hash[:storage] = persister.storages.lazy_find(parse_datastore_location(datastore_props)) if datastore_props + end + def parse_virtual_machine_summary_runtime(vm_hash, props) runtime = props.fetch_path(:summary, :runtime) return if runtime.nil? @@ -187,7 +202,13 @@ def parse_virtual_machine_disks(hardware, props) case backing when RbVmomi::VIM::VirtualDeviceFileBackingInfo disk_hash[:filename] = backing.fileName - disk_hash[:storage] = persister.storages.lazy_find(backing.datastore._ref) unless backing.datastore.nil? + + if backing.datastore + datastore_props = cache.find(backing.datastore) + datastore_location = parse_datastore_location(datastore_props) if datastore_props + + disk_hash[:storage] = persister.storages.lazy_find(datastore_location) if datastore_location + end when RbVmomi::VIM::VirtualDeviceRemoteDeviceBackingInfo disk_hash[:filename] = backing.deviceName end diff --git a/spec/models/manageiq/providers/vmware/infra_manager/inventory/collector_spec.rb b/spec/models/manageiq/providers/vmware/infra_manager/inventory/collector_spec.rb index 026ede4a1..364fc2200 100644 --- a/spec/models/manageiq/providers/vmware/infra_manager/inventory/collector_spec.rb +++ b/spec/models/manageiq/providers/vmware/infra_manager/inventory/collector_spec.rb @@ -25,6 +25,7 @@ assert_table_counts assert_specific_datacenter + assert_specific_datastore assert_specific_folder assert_specific_host assert_specific_cluster @@ -72,9 +73,9 @@ it "deleting a virtual machine" do vm = ems.vms.find_by(:ems_ref => 'vm-107') - expect(vm.archived?).to be_falsy + expect(vm.orphaned?).to be_falsy run_targeted_refresh(targeted_update_set(vm_delete_object_updates)) - expect(vm.reload.archived?).to be_truthy + expect(vm.reload.orphaned?).to be_truthy end def run_targeted_refresh(update_set) @@ -198,6 +199,26 @@ def assert_specific_datacenter expect(datacenter.children.map(&:name)).to match_array(%w(host network datastore vm)) end + def assert_specific_datastore + storage = ems.storages.find_by(:location => "ds:///vmfs/volumes/5280a4c3-b5e2-7dc7-5c31-7a344d35466c/") + + expect(storage).to have_attributes( + :location => "ds:///vmfs/volumes/5280a4c3-b5e2-7dc7-5c31-7a344d35466c/", + :name => "GlobalDS_0", + :store_type => "VMFS", + :total_space => 1_099_511_627_776, + :free_space => 824_633_720_832, + :multiplehostaccess => 1, + :directory_hierarchy_supported => true, + :thin_provisioning_supported => true, + :raw_disk_mappings_supported => true, + ) + + expect(storage.hosts.count).to eq(32) + expect(storage.disks.count).to eq(512) + expect(storage.vms.count).to eq(512) + end + def assert_specific_folder folder = ems.ems_folders.find_by(:ems_ref => "group-d1") @@ -386,6 +407,9 @@ def assert_specific_vm expect(vm.host).not_to be_nil expect(vm.host.ems_ref).to eq("host-17") + expect(vm.storage).not_to be_nil + expect(vm.storage.name).to eq("GlobalDS_0") + expect(vm.parent_blue_folder).not_to be_nil expect(vm.parent_blue_folder.ems_ref).to eq("group-v3")