From a501281aeeb10b11d7b98e679f9e490ce1648a8f Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Tue, 15 May 2018 12:52:29 -0400 Subject: [PATCH 1/3] Set the root folder's parent to the EMS Set the root folder's parent to be the EMS, this has to be done in its own inventory_collection because there is no inventory object to track the EMS. --- .../inventory/inventory_collections.rb | 20 +++++++++++++++++++ .../infra_manager/inventory/persister.rb | 6 ++++++ .../infra_manager/inventory/collector_spec.rb | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/models/manageiq/providers/vmware/infra_manager/inventory/inventory_collections.rb b/app/models/manageiq/providers/vmware/infra_manager/inventory/inventory_collections.rb index 64a387519..6f871bf1b 100644 --- a/app/models/manageiq/providers/vmware/infra_manager/inventory/inventory_collections.rb +++ b/app/models/manageiq/providers/vmware/infra_manager/inventory/inventory_collections.rb @@ -105,6 +105,26 @@ def vm_resource_pools(extra_attributes = {}) relationships(:resource_pool, :ems_metadata, "ResourcePool", :vm_resource_pools, extra_attributes) end + def root_folder_relationship(extra_attributes = {}) + root_folder_save_block = lambda do |ems, inventory_collection| + folder_inv_collection = inventory_collection.dependency_attributes[:ems_folders]&.first + return if folder_inv_collection.nil? + + # All folders must have a parent except for the root folder + root_folder_obj = folder_inv_collection.data.detect { |obj| obj.data[:parent].nil? } + return if root_folder_obj.nil? + + root_folder = folder_inv_collection.model_class.find(root_folder_obj.id) + root_folder.with_relationship_type(:ems_metadata) { root_folder.parent = ems } + end + + attributes = { + :association => :root_folder_relationships, + :custom_save_block => root_folder_save_block, + } + attributes.merge!(extra_attributes) + end + def relationships(relationship_key, relationship_type, parent_type, association, extra_attributes = {}) relationship_save_block = lambda do |_ems, inventory_collection| parents = Hash.new { |h, k| h[k] = {} } diff --git a/app/models/manageiq/providers/vmware/infra_manager/inventory/persister.rb b/app/models/manageiq/providers/vmware/infra_manager/inventory/persister.rb index 57267edc8..c88ef4a65 100644 --- a/app/models/manageiq/providers/vmware/infra_manager/inventory/persister.rb +++ b/app/models/manageiq/providers/vmware/infra_manager/inventory/persister.rb @@ -24,6 +24,12 @@ def initialize_inventory_collections :dependency_attributes => {:vms_and_templates => [collections[:vms_and_templates]]}, ) ) + + add_inventory_collection( + default_inventory_collections.root_folder_relationship( + :dependency_attributes => {:ems_folders => [collections[:ems_folders]]}, + ) + ) end def default_inventory_collections 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 b9f2dd1a3..acc8ffa1b 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 @@ -231,7 +231,7 @@ def assert_specific_folder :uid_ems => "group-d1", ) - expect(folder.parent).to be_nil + expect(folder.parent).to eq(ems) expect(folder.children.count).to eq(4) expect(folder.children.map(&:name)).to match_array(%w(DC0 DC1 DC2 DC3)) end From dc358bf42fdc5ffa9a15ead7d796b4d942e21d6b Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Tue, 15 May 2018 13:22:13 -0400 Subject: [PATCH 2/3] Set the hidden property of EmsFolder --- .../providers/vmware/infra_manager/inventory/parser.rb | 7 +++++++ .../vmware/infra_manager/inventory/collector_spec.rb | 1 + 2 files changed, 8 insertions(+) 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 e74bbd772..bb6f94018 100644 --- a/app/models/manageiq/providers/vmware/infra_manager/inventory/parser.rb +++ b/app/models/manageiq/providers/vmware/infra_manager/inventory/parser.rb @@ -99,12 +99,19 @@ def parse_folder(object, kind, props) persister.ems_folders.targeted_scope << object._ref return if kind == "leave" + # "Hidden" folders are folders which exist in the VIM API but are not shown + # on the vSphere UI. These folders are the root folder above the datacenters + # named "Datacenters", and the 4 child folders of each datacenter (datastore, + # host, network, vm) + hidden = props[:parent].nil? || props[:parent].kind_of?(RbVmomi::VIM::Datacenter) + folder_hash = { :ems_ref => object._ref, :uid_ems => object._ref, :type => "EmsFolder", :name => CGI.unescape(props[:name]), :parent => lazy_find_managed_object(props[:parent]), + :hidden => hidden, } persister.ems_folders.build(folder_hash) 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 acc8ffa1b..d7b7b9146 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 @@ -229,6 +229,7 @@ def assert_specific_folder :ems_ref => "group-d1", :name => "Datacenters", :uid_ems => "group-d1", + :hidden => true, ) expect(folder.parent).to eq(ems) From 292cc37ba2cbe2f36758a5fd66cb2716dba92b6c Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Tue, 15 May 2018 13:32:36 -0400 Subject: [PATCH 3/3] Set is_default for resource_pools --- .../vmware/infra_manager/inventory/parser.rb | 26 +++++++++++++++---- .../infra_manager/inventory/collector_spec.rb | 3 ++- 2 files changed, 23 insertions(+), 6 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 bb6f94018..bd0f6284e 100644 --- a/app/models/manageiq/providers/vmware/infra_manager/inventory/parser.rb +++ b/app/models/manageiq/providers/vmware/infra_manager/inventory/parser.rb @@ -187,12 +187,28 @@ def parse_resource_pool(object, kind, props) persister.resource_pools.targeted_scope << object._ref return if kind == "leave" + parent = props[:parent] + + # Default resource pools are ones whose parent is a Cluster or a Host, + # resource pools which show up on the vSphere UI all have resource pools + # as parents. + is_default = parent && !parent.kind_of?(RbVmomi::VIM::ResourcePool) + name = if is_default + cached_parent = cache.find(parent) if parent + parent_model = persister.vim_class_to_collection(parent).base_class_name + + "Default for #{Dictionary.gettext(parent_model, :type => :model, :notfound => :titleize)} #{cached_parent[:name]}" + else + CGI.unescape(props[:name]) + end + rp_hash = { - :ems_ref => object._ref, - :uid_ems => object._ref, - :name => CGI.unescape(props[:name]), - :vapp => object.kind_of?(RbVmomi::VIM::VirtualApp), - :parent => lazy_find_managed_object(props[:parent]), + :ems_ref => object._ref, + :uid_ems => object._ref, + :name => name, + :vapp => object.kind_of?(RbVmomi::VIM::VirtualApp), + :parent => lazy_find_managed_object(parent), + :is_default => is_default, } parse_resource_pool_memory_allocation(rp_hash, props) 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 d7b7b9146..10484465d 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 @@ -311,8 +311,9 @@ def assert_specific_resource_pool :memory_reserve_expand => true, :memory_shares => 163_840, :memory_shares_level => "normal", - :name => "Resources", + :name => "Default for Cluster / Deployment Role DC0_C1", :vapp => false, + :is_default => true, ) expect(resource_pool.parent.ems_ref).to eq("domain-c91")