Skip to content

Commit

Permalink
Parse the event url with Regex not split
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Nov 20, 2024
1 parent 12ae768 commit 123d5f0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,37 @@ def parse
case ems_event.event_type
when "MODIFY_URI", "ADD_URI", "DELETE_URI" # Damien: INVALID_URI?
$ibm_power_hmc_log.info("#{self.class}##{__method__} #{ems_event.event_type} #{ems_event.full_data}")
elems = elem(raw_event[:data])
case elems[:type]
elems = raw_event[:data]
.match(%r{/rest/api/uom/(?:ManagedSystem/(?<manager_uuid>[^/]+)/)?(?<type>[^/]+)/(?<uuid>[^/]+)$})
&.named_captures || {}
case elems["type"]
when "ManagedSystem"
new_targets << {:assoc => :hosts, :ems_ref => elems[:uuid]}
new_targets << {:assoc => :hosts, :ems_ref => elems["uuid"]}
when "LogicalPartition", "VirtualIOServer"
# raw_event[:detail] contains information about the properties that
# have changed (e.g. RMCState, PartitionName, PartitionState etc...)
# This may be used to perform quick property REST API calls to the HMC
# instead of querying the full LPAR data.
if elems[:uuid] == NO_UUID_VALUE
$ibm_power_hmc_log.info("#{self.class}##{__method__} #{elems[:type]} Missing LPAR UUID. Escalating to full refresh for EMS: [#{ems.name}], id: [#{ems.id}].")
if elems["uuid"] == NO_UUID_VALUE
$ibm_power_hmc_log.info("#{self.class}##{__method__} #{elems["type"]} Missing LPAR UUID. Escalating to full refresh for EMS: [#{ems.name}], id: [#{ems.id}].")
target_collection << ems
else
new_targets << {:assoc => :vms, :ems_ref => elems[:uuid]}
new_targets << {:assoc => :vms, :ems_ref => elems["uuid"]}
end
when "VirtualSwitch", "VirtualNetwork"
if elems.key?(:manager_uuid)
new_targets << {:assoc => :hosts, :ems_ref => elems[:manager_uuid]}
if elems["manager_uuid"]
new_targets << {:assoc => :hosts, :ems_ref => elems["manager_uuid"]}
end
when "UserTask"
new_targets.concat(handle_usertask(raw_event[:usertask]))
when "Cluster"
new_targets << {:assoc => :storages, :ems_ref => elems[:uuid]}
new_targets << {:assoc => :storages, :ems_ref => elems["uuid"]}
when "SharedProcessorPool", "SharedMemoryPool"
new_targets << {:assoc => :resource_pools, :ems_ref => "#{elems[:manager_uuid]}_#{elems[:uuid]}"}
new_targets << {:assoc => :resource_pools, :ems_ref => "#{elems["manager_uuid"]}_#{elems["uuid"]}"}
end

new_targets.each do |t|
$ibm_power_hmc_log.info("#{self.class}##{__method__} #{elems[:type]} uuid #{t[:ems_ref]}")
$ibm_power_hmc_log.info("#{self.class}##{__method__} #{elems["type"]} uuid #{t[:ems_ref]}")
target_collection.add_target(
:association => t[:assoc],
:manager_ref => {:ems_ref => t[:ems_ref]}
Expand All @@ -63,19 +65,6 @@ def parse

private

def elem(url)
uri = URI(url)
tokens = uri.path.split('/')
elems = {
:type => tokens[-2],
:uuid => tokens[-1]
}
if tokens.length >= 4 && tokens[-4] == "ManagedSystem"
elems[:manager_uuid] = tokens[-3]
end
elems
end

def handle_usertask(usertask)
return [] unless usertask["status"].eql?("Completed")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
expect(parsed_targets).to eq([@ems])
end
end

context "with an invalid EventData url" do
it "returns an empty target set" do
ems_event = create_ems_event("test_data/logical_partition_invalid_event_data.xml")
parsed_targets = described_class.new(ems_event).parse

expect(parsed_targets).to be_empty
end
end
end
it "VirtualIOServer" do
assert_event_triggers_target(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<feed>
<entry>
<id>1afb3833-8a85-369f-977a-bb8741b1a15a</id>
<title>Event</title>
<published>2022-02-02T15:05:12.772Z</published>
<link rel="SELF" href="https://te.st:12443/rest/api/uom/Event/1afb3833-8a85-369f-977a-bb8741b1a15a"/>
<author>
<name>IBM Power Systems Management Console</name>
</author>
<etag:etag xmlns:etag="http://www.ibm.com/xmlns/systems/power/firmware/uom/mc/2012_10/" xmlns="http://www.ibm.com/xmlns/systems/power/firmware/uom/mc/2012_10/">-83524495</etag:etag>
<content type="application/vnd.ibm.powervm.uom+xml; type=Event">
<Event:Event xmlns:Event="http://www.ibm.com/xmlns/systems/power/firmware/uom/mc/2012_10/" xmlns="http://www.ibm.com/xmlns/systems/power/firmware/uom/mc/2012_10/" xmlns:ns2="http://www.w3.org/XML/1998/namespace/k2" schemaVersion="V1_6_0">
<Metadata>
<Atom>
<AtomID>1afb3833-8a85-369f-977a-bb8741b1a15a</AtomID>
<AtomCreated>1639561179310</AtomCreated>
</Atom>
</Metadata>
<EventType kb="ROR" kxe="false">ADD_URI</EventType>
<EventID kb="ROR" kxe="false">1639561179310</EventID>
<EventData kxe="false" kb="ROR">https://te.st:12443/rest/api/uom/ManagedSyste/e4acf909-6d0b-3c03-b75a-4d8495e5fc49/LogicalPartition/</EventData>
<EventDetail kxe="false" kb="ROR">Other</EventDetail>
</Event:Event>
</content>
</entry>
</feed>

0 comments on commit 123d5f0

Please sign in to comment.