Class: Jackal::Cfn::Event

Inherits:
Jackal::Callback
  • Object
show all
Includes:
Utils, Utils::Http
Defined in:
lib/jackal-cfn/event.rb

Overview

Callback for event types

Defined Under Namespace

Modules: InheritedValidity

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Http

#response_endpoint

Methods included from Utils

#snakecase, #transform_parameters

Class Method Details

.inherited(klass) ⇒ Object

Update validity checks in subclasses

Parameters:

  • klass (Class)


34
35
36
37
38
# File 'lib/jackal-cfn/event.rb', line 34

def self.inherited(klass)
  klass.class_eval do
    include InheritedValidity
  end
end

Instance Method Details

#execute(message) ⇒ Object

Generate payload and drop

Parameters:

  • message (Carnivore::Message)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/jackal-cfn/event.rb', line 105

def execute(message)
  data_payload = unpack(message)
  payload = new_payload(
    config[:name],
    :cfn_event => data_payload
  )
  if(config[:reprocess])
    debug "Reprocessing payload through current source (#{destination(:input, payload)})"
    Carnivore::Supervisor.supervisor[destination(:input, payload)].transmit(payload)
    message.confirm!
  else
    job_completed(:jackal_cfn, payload, message)
  end
end

#format_event(evt) ⇒ Smash

Format payload into proper event structure

Parameters:

  • evt (Hash)

Returns:

  • (Smash)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/jackal-cfn/event.rb', line 81

def format_event(evt)
  parts = evt.split("\n").map do |entry|
    chunks = entry.split('=')
    key = chunks.shift.strip
    value = chunks.join.strip.sub(/^'/, '').sub(/'$/, '').strip
    [key, value]
  end
  event = Smash[parts]
  unless(event['ResourceProperties'].to_s.empty?)
    begin
      event['ResourceProperties'] = MultiJson.load(event['ResourceProperties'])
    rescue MultiJson::LoadError => e
      error "Failed to load `ResourceProperties`: #{e.class} - #{e}"
      debug "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
    end
  else
    event['ResourceProperties'] = {}
  end
  Smash.new(Carnivore::Utils.symbolize_hash(event))
end

#unpack(message) ⇒ Smash

Unpack message and create payload

Parameters:

  • message (Carnivore::Message)

Returns:

  • (Smash)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jackal-cfn/event.rb', line 44

def unpack(message)
  payload = super
  if(self.class == Jackal::Cfn::Event)
    begin
      unless(payload.get(:data, :cfn_event))
        payload = format_event(payload.fetch('Body', 'Message', payload))
        payload[:origin_type] = message[:message].get('Body', 'Type')
        payload[:origin_subject] = message[:message].get('Body', 'Subject')
      end
      payload
    rescue
      Smash.new
    end
  else
    payload.to_smash.fetch('Body', payload.to_smash)
  end
end

#valid?(message) ⇒ TrueClass, FalseClass

Determine message validity

Parameters:

  • message (Carnivore::Message)

Returns:

  • (TrueClass, FalseClass)


66
67
68
69
70
71
72
73
74
75
# File 'lib/jackal-cfn/event.rb', line 66

def valid?(message)
  super do |payload|
    if(block_given?)
      yield payload
    else
      payload[:origin_type] == 'Notification' &&
        payload[:origin_subject].downcase.include?('cloudformation notification')
    end
  end
end