Class: Jackal::Cfn::HashExtractor

Inherits:
Resource
  • Object
show all
Defined in:
lib/jackal-cfn/resource/hash_extractor.rb

Overview

Extract value from hash

Expected resource:

{
  "Type": "Custom::HashExtractor",
  "Properties": {
    "Parameters": {
      "Key": "path.to.value.in.hash",
      "Value": JSON
    }
  }
}

Constant Summary

Constants inherited from Resource

Resource::VALID_RESOURCE_STATUS

Instance Method Summary collapse

Methods inherited from Resource

#build_response, #failure_wrap, inherited, #physical_resource_id, #respond_to_stack, #setup, #unpack, #valid?

Methods included from Utils::Http

#response_endpoint

Methods included from Utils

#snakecase, #transform_parameters

Instance Method Details

#execute(message) ⇒ Object

Process message, send value back to CFN

Parameters:

  • message (Carnivore::Message)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jackal-cfn/resource/hash_extractor.rb', line 22

def execute(message)
  failure_wrap(message) do |payload|
    cfn_resource = rekey_hash(payload.get(:data, :cfn_resource))
    properties = rekey_hash(cfn_resource[:resource_properties])
    parameters = rekey_hash(properties[:parameters])
    cfn_response = build_response(cfn_resource)
    key = parameters[:key].split('.')
    value = parameters[:value]
    unless(value.is_a?(String))
      unless(cfn_resource[:request_type].to_sym == :delete)
        raise TypeError.new("Expecting `String` value but received `#{value.class}`")
      end
      return_value = nil
    else
      value = MultiJson.load(value).to_smash
      return_value = value.get(*key)
      if(return_value.is_a?(Enumerable))
        return_value = MultiJson.dump(return_value)
      end
    end
    cfn_response['Data']['Payload'] = return_value
    respond_to_stack(cfn_response, cfn_resource[:response_url])
    job_completed(:jackal_cfn, payload, message)
  end
end