From 670c1924dc4ce6113ba4352f144df4c9385b5194 Mon Sep 17 00:00:00 2001 From: Sam Lucidi Date: Thu, 17 Aug 2017 10:07:01 -0400 Subject: [PATCH] Move duplicated collector code into helper module --- .../inventory/collector/cloud_manager.rb | 12 ++------- .../inventory/collector/helper_methods.rb | 24 ++++++++++++++++++ .../inventory/collector/network_manager.rb | 15 ++--------- .../inventory/collector/target_collection.rb | 25 ++----------------- 4 files changed, 30 insertions(+), 46 deletions(-) create mode 100644 app/models/manageiq/providers/openstack/inventory/collector/helper_methods.rb diff --git a/app/models/manageiq/providers/openstack/inventory/collector/cloud_manager.rb b/app/models/manageiq/providers/openstack/inventory/collector/cloud_manager.rb index 8dadb0c5c..b6a1da346 100644 --- a/app/models/manageiq/providers/openstack/inventory/collector/cloud_manager.rb +++ b/app/models/manageiq/providers/openstack/inventory/collector/cloud_manager.rb @@ -1,4 +1,6 @@ class ManageIQ::Providers::Openstack::Inventory::Collector::CloudManager < ManageIQ::Providers::Openstack::Inventory::Collector + include ManageIQ::Providers::Openstack::Inventory::Collector::HelperMethods + def availability_zones_compute @availability_zones_compute ||= safe_list { compute_service.availability_zones.summary } end @@ -43,16 +45,6 @@ def private_flavor(flavor_id) end end - def tenant_ids_with_flavor_access(flavor_id) - unparsed_tenants = safe_get { connection.list_tenants_with_flavor_access(flavor_id) } - flavor_access = unparsed_tenants.try(:data).try(:[], :body).try(:[], "flavor_access") || [] - flavor_access.map! { |t| t['tenant_id'] } - rescue - [] - else - flavor_access - end - def flavors_by_id @flavors_by_id ||= Hash[flavors.collect { |f| [f.id, f] }] end diff --git a/app/models/manageiq/providers/openstack/inventory/collector/helper_methods.rb b/app/models/manageiq/providers/openstack/inventory/collector/helper_methods.rb new file mode 100644 index 000000000..155509ba7 --- /dev/null +++ b/app/models/manageiq/providers/openstack/inventory/collector/helper_methods.rb @@ -0,0 +1,24 @@ +module ManageIQ::Providers::Openstack::Inventory::Collector::HelperMethods + def orchestration_stack_by_resource_id(resource_id) + @resources ||= {} + if @resources.empty? + orchestration_stacks.each do |stack| + resources = orchestration_resources(stack) + resources.each do |r| + @resources[r.physical_resource_id] = r + end + end + end + @resources[resource_id] + end + + def tenant_ids_with_flavor_access(flavor_id) + unparsed_tenants = safe_get { connection.list_tenants_with_flavor_access(flavor_id) } + flavor_access = unparsed_tenants.try(:data).try(:[], :body).try(:[], "flavor_access") || [] + flavor_access.map! { |t| t['tenant_id'] } + rescue + [] + else + flavor_access + end +end diff --git a/app/models/manageiq/providers/openstack/inventory/collector/network_manager.rb b/app/models/manageiq/providers/openstack/inventory/collector/network_manager.rb index 5c46e3591..344b8d0b2 100644 --- a/app/models/manageiq/providers/openstack/inventory/collector/network_manager.rb +++ b/app/models/manageiq/providers/openstack/inventory/collector/network_manager.rb @@ -1,4 +1,6 @@ class ManageIQ::Providers::Openstack::Inventory::Collector::NetworkManager < ManageIQ::Providers::Openstack::Inventory::Collector + include ManageIQ::Providers::Openstack::Inventory::Collector::HelperMethods + def floating_ips return @floating_ips if @floating_ips.any? @floating_ips = network_service.handled_list(:floating_ips, {}, ::Settings.ems.ems_openstack.refresh.is_admin) @@ -49,17 +51,4 @@ def orchestration_resources(stack) @os_handle ||= manager.openstack_handle safe_list { stack.resources } end - - def orchestration_stack_by_resource_id(resource_id) - @resources ||= {} - if @resources.empty? - orchestration_stacks.each do |stack| - resources = orchestration_resources(stack) - resources.each do |r| - @resources[r.physical_resource_id] = r - end - end - end - @resources[resource_id] - end end diff --git a/app/models/manageiq/providers/openstack/inventory/collector/target_collection.rb b/app/models/manageiq/providers/openstack/inventory/collector/target_collection.rb index bb1451516..fcd85d242 100644 --- a/app/models/manageiq/providers/openstack/inventory/collector/target_collection.rb +++ b/app/models/manageiq/providers/openstack/inventory/collector/target_collection.rb @@ -1,4 +1,6 @@ class ManageIQ::Providers::Openstack::Inventory::Collector::TargetCollection < ManageIQ::Providers::Openstack::Inventory::Collector + include ManageIQ::Providers::Openstack::Inventory::Collector::HelperMethods + def initialize(_manager, _target) super parse_targets! @@ -94,16 +96,6 @@ def tenants end.compact end - def tenant_ids_with_flavor_access(flavor_id) - unparsed_tenants = safe_get { connection.list_tenants_with_flavor_access(flavor_id) } - flavor_access = unparsed_tenants.try(:data).try(:[], :body).try(:[], "flavor_access") || [] - flavor_access.map! { |t| t['tenant_id'] } - rescue - [] - else - flavor_access - end - def key_pairs return [] if references(:key_pairs).blank? @key_pairs = references(:key_pairs).collect do |key_pair_id| @@ -137,19 +129,6 @@ def orchestration_resources(stack) safe_list { stack.resources } end - def orchestration_stack_by_resource_id(resource_id) - @resources ||= {} - if @resources.empty? - orchestration_stacks.each do |stack| - resources = orchestration_resources(stack) - resources.each do |r| - @resources[r.physical_resource_id] = r - end - end - end - @resources[resource_id] - end - private def parse_targets!