Skip to content

Commit

Permalink
handle snapshot creation event
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Zagaynov committed Jun 11, 2018
1 parent d814392 commit 7ded5d6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/models/manageiq/providers/amazon/cloud_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ManageIQ::Providers::Amazon::CloudManager < ManageIQ::Providers::CloudMana
require_nested :Template
require_nested :Vm

OrchestrationTemplate.register_eligible_manager(self)
self::OrchestrationTemplate.register_eligible_manager(self)

include ManageIQ::Providers::Amazon::ManagerMixin

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,27 @@ def parse_event(message)
# Aws Config Events
event["eventType"] = parse_event_type(event)
event["event_source"] = :config

elsif event.fetch_path("detail", "eventType") == "AwsApiCall"
# CloudWatch with CloudTrail for API requests Events
event["eventType"] = "AWS_API_CALL_" + event.fetch_path("detail", "eventName")
event["event_source"] = :cloud_watch_api

elsif event["detail-type"] == "EC2 Instance State-change Notification"
# CloudWatch EC2 Events
state = "_#{event.fetch_path("detail", "state")}" if event.fetch_path("detail", "state")
event["eventType"] = "#{event["detail-type"].tr(" ", "_").tr("-", "_")}#{state}"
event["event_source"] = :cloud_watch_ec2

elsif event['detail-type'] == 'EBS Snapshot Notification'
event['eventType'] = event['detail-type'].gsub(/[\s-]/, '_')
event['event_source'] = :cloud_watch_ec2_ebs_snapshot

elsif event["AlarmName"]
# CloudWatch Alarm
event["eventType"] = "AWS_ALARM_#{event["AlarmName"]}"
event["event_source"] = :cloud_watch_alarm

else
# Not recognized event, ignoring...
$log.debug("#{log_header} Parsed event from SNS Message not recognized #{event}")
Expand Down
14 changes: 11 additions & 3 deletions app/models/manageiq/providers/amazon/cloud_manager/event_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def self.parse_cloud_watch_ec2_event!(event, event_hash)
event_hash[:availability_zone_ems_ref] = nil # Can't get it, needs to go through VM
end

def self.parse_cloud_watch_ec2_ebs_snapshot_event!(event, event_hash)
event_hash[:message] = event['detail']
event_hash[:timestamp] = event['time']
event_hash[:vm_ems_ref] = nil
event_hash[:availability_zone_ems_ref] = nil
end

def self.parse_cloud_watch_alarm_event!(event, event_hash)
event_hash[:message] = event["AlarmName"]
event_hash[:timestamp] = event["StateChangeTime"]
Expand All @@ -38,12 +45,13 @@ def self.event_to_hash(event, ems_id)
:ems_id => ems_id
}

unless %i(config cloud_watch_api cloud_watch_ec2 cloud_watch_alarm).include?(event["event_source"])
parse_method_name = "parse_#{event["event_source"]}_event!"
if singleton_class.method_defined?(parse_method_name)
send(parse_method_name, event, event_hash)
else
raise "Unsupported event source #{event["event_source"]}"
end

send("parse_#{event["event_source"]}_event!", event, event_hash)

log_header = "ems_id: [#{ems_id}] " unless ems_id.nil?
_log.debug("#{log_header}event: [#{event[:message]}]")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def parse_ems_event_targets(event)
event.full_data.fetch_path("detail", "responseElements") || {})
when :cloud_watch_ec2
collect_cloudwatch_ec2_references!(target_collection, event.full_data)
when :cloud_watch_ec2_ebs_snapshot
collect_cloudwatch_ec2_ebs_snapshot_references!(target_collection, event.full_data)
when :config
collect_config_references!(target_collection, event.full_data)
end
Expand All @@ -54,6 +56,12 @@ def collect_cloudwatch_ec2_references!(target_collection, event_data)
add_target(target_collection, :vms, instance_id) if instance_id
end

def collect_cloudwatch_ec2_ebs_snapshot_references!(target_collection, event_data)
if (snapshot_id = event_data.fetch_path('detail', 'snapshot_id'))
add_target(target_collection, :cloud_volume_snapshots, snapshot_id.split('/').last)
end
end

def collect_config_references!(target_collection, event_data)
resource_type = event_data.fetch_path("configurationItem", "resourceType")
resource_id = event_data.fetch_path("configurationItem", "resourceId")
Expand Down

0 comments on commit 7ded5d6

Please sign in to comment.