From 73b0a8602788211b014c1a9d388d045d5e0d90a7 Mon Sep 17 00:00:00 2001 From: Sam Lucidi Date: Thu, 24 Aug 2017 13:38:38 -0400 Subject: [PATCH] Add class for parsing refresh targets from EmsEvents --- .../cloud_manager/event_target_parser.rb | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 app/models/manageiq/providers/openstack/cloud_manager/event_target_parser.rb diff --git a/app/models/manageiq/providers/openstack/cloud_manager/event_target_parser.rb b/app/models/manageiq/providers/openstack/cloud_manager/event_target_parser.rb new file mode 100644 index 000000000..095355a1b --- /dev/null +++ b/app/models/manageiq/providers/openstack/cloud_manager/event_target_parser.rb @@ -0,0 +1,42 @@ +class ManageIQ::Providers::Openstack::CloudManager::EventTargetParser + attr_reader :ems_event + + # @param ems_event [EmsEvent] EmsEvent object to be parsed to derive an object to be refreshed + def initialize(ems_event) + @ems_event = ems_event + end + + # Parses all targets present in the EmsEvent givin in the initializer + # @return [Array] Array of ManagerRefresh::Target objects + def parse + parse_ems_event_targets(ems_event) + end + + private + + # Parses list of ManagerRefresh::Target(s) out of the given EmsEvent + # + # @param ems_event [EmsEvent] EmsEvent object + # @return [Array] Array of ManagerRefresh::Target objects + def parse_ems_event_targets(ems_event) + target_collection = ManagerRefresh::TargetCollection.new(:manager => ems_event.ext_management_system, :event => ems_event) + if ems_event.event_type.start_with?("compute.instance") + collect_compute_instance_references!(target_collection, ems_event) + end + + target_collection.targets + end + + def parsed_targets(target_collection = {}) + target_collection.select { |_target_class, references| references[:manager_ref].present? } + end + + def add_target(target_collection, association, ref) + target_collection.add_target(:association => association, :manager_ref => {:ems_ref => ref}) + end + + def collect_compute_instance_references!(target_collection, ems_event) + instance_id = ems_event.full_data.fetch_path(:payload, 'instance_id') + add_target(target_collection, :vms, instance_id) if instance_id + end +end