-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #175 from mansam/event-target-parser-network-manager
Improve Targeted Refresh for Cloud and Network managers (cherry picked from commit 92aed61) https://bugzilla.redhat.com/show_bug.cgi?id=1533225
- Loading branch information
Showing
37 changed files
with
22,686 additions
and
22,523 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/models/manageiq/providers/openstack/network_manager/event_target_parser.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
class ManageIQ::Providers::Openstack::NetworkManager::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.parent_manager, :event => ems_event) | ||
|
||
# there's almost always a tenant id regardless of event type | ||
collect_identity_tenant_references!(target_collection, ems_event) | ||
|
||
target_type = if ems_event.event_type.start_with?("floatingip.") | ||
:floating_ips | ||
elsif ems_event.event_type.start_with?("router.") | ||
:routers | ||
elsif ems_event.event_type.start_with?("port.") | ||
:ports | ||
elsif ems_event.event_type.start_with?("network.") | ||
:networks | ||
elsif ems_event.event_type.start_with?("security_group.") | ||
:security_groups | ||
end | ||
|
||
resource_id = ems_event.full_data.fetch_path(:payload, 'resource_id') | ||
add_target(target_collection, target_type, resource_id) if resource_id | ||
|
||
target_collection.targets | ||
end | ||
|
||
def collect_identity_tenant_references!(target_collection, ems_event) | ||
tenant_id = ems_event.full_data.fetch_path(:payload, 'tenant_id') || ems_event.full_data.fetch_path(:payload, 'project_id') || ems_event.full_data.fetch_path(:payload, 'initiator', 'project_id') | ||
add_target(target_collection, :cloud_tenants, tenant_id) if tenant_id | ||
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 | ||
end |
40 changes: 1 addition & 39 deletions
40
app/models/manageiq/providers/openstack/network_manager/refresher.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
spec/models/manageiq/providers/openstack/cloud_manager/event_target_parser_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
describe ManageIQ::Providers::Openstack::CloudManager::EventTargetParser do | ||
before :each do | ||
_guid, _server, zone = EvmSpecHelper.create_guid_miq_server_zone | ||
@ems = FactoryGirl.create(:ems_openstack, :zone => zone) | ||
|
||
allow_any_instance_of(EmsEvent).to receive(:handle_event) | ||
allow(EmsEvent).to receive(:create_completed_event) | ||
end | ||
|
||
context "Openstack Event Parsing" do | ||
it "parses compute.instance events" do | ||
ems_event = create_ems_event("compute.instance.create.end", "instance_id" => "instance_id_test", | ||
"tenant_id" => "tenant_id_test") | ||
parsed_targets = described_class.new(ems_event).parse | ||
expect(parsed_targets.size).to eq(2) | ||
expect(target_references(parsed_targets)).to( | ||
match_array( | ||
[ | ||
[:vms, {:ems_ref => "instance_id_test"}], | ||
[:cloud_tenants, {:ems_ref => "tenant_id_test"}] | ||
] | ||
) | ||
) | ||
end | ||
|
||
it "parses identity.project events" do | ||
ems_event = create_ems_event("identity.project.create.end", "project_id" => "tenant_id_test") | ||
parsed_targets = described_class.new(ems_event).parse | ||
expect(parsed_targets.size).to eq(1) | ||
expect(target_references(parsed_targets)).to( | ||
match_array( | ||
[ | ||
[:cloud_tenants, {:ems_ref => "tenant_id_test"}] | ||
] | ||
) | ||
) | ||
end | ||
|
||
it "parses orchestration.stack events" do | ||
ems_event = create_ems_event("orchestration.stack.create.end", "stack_id" => "stack_id_test") | ||
parsed_targets = described_class.new(ems_event).parse | ||
expect(parsed_targets.size).to eq(1) | ||
expect(target_references(parsed_targets)).to( | ||
match_array( | ||
[ | ||
[:orchestration_stacks, {:ems_ref => "stack_id_test"}] | ||
] | ||
) | ||
) | ||
end | ||
|
||
it "parses image events" do | ||
ems_event = create_ems_event("image.create.end", "resource_id" => "image_id_test") | ||
parsed_targets = described_class.new(ems_event).parse | ||
expect(parsed_targets.size).to eq(1) | ||
expect(target_references(parsed_targets)).to( | ||
match_array( | ||
[ | ||
[:images, {:ems_ref => "image_id_test"}] | ||
] | ||
) | ||
) | ||
end | ||
|
||
it "parses host aggregate events" do | ||
ems_event = create_ems_event("aggregate.create.end", "service" => "aggregate.id_test") | ||
parsed_targets = described_class.new(ems_event).parse | ||
expect(parsed_targets.size).to eq(1) | ||
expect(target_references(parsed_targets)).to( | ||
match_array( | ||
[ | ||
[:host_aggregates, {:ems_ref => "id_test"}] | ||
] | ||
) | ||
) | ||
end | ||
end | ||
|
||
def target_references(parsed_targets) | ||
parsed_targets.map { |x| [x.association, x.manager_ref] }.uniq | ||
end | ||
|
||
def create_ems_event(event_type, payload) | ||
event_hash = { | ||
:event_type => event_type, | ||
:source => "OPENSTACK", | ||
:message => payload, | ||
:timestamp => "2016-03-13T16:59:01.760000", | ||
:username => "", | ||
:full_data => {:payload => payload }, | ||
:ems_id => @ems.id | ||
} | ||
EmsEvent.add(@ems.id, event_hash) | ||
end | ||
end |
Oops, something went wrong.