lib/jets/resource/permission.rb in jets-0.8.0 vs lib/jets/resource/permission.rb in jets-0.8.1

- old
+ new

@@ -1,43 +1,60 @@ -module Jets::Resource +class Jets::Resource class Permission extend Memoist - def initialize(task, resource) - @task = task - @resource_attributes = resource + def initialize(replacements, associated_resource) + @replacements = replacements + # puts caller + # puts "replacements #{replacements.inspect}" + @associated_resource = associated_resource end - def attributes + def logical_id logical_id = "{namespace}Permission" - md = @resource_attributes.logical_id.match(/(\d+)/) - if md - counter = md[1] - end + md = @associated_resource.logical_id.match(/(\d+)/) + counter = md[1] if md logical_id = [logical_id, counter].compact.join('') + # replace possible {namespace} in the logical id + logical_id = replacer.replace_value(logical_id) + Jets::Pascalize.pascalize_string(logical_id) + end + def type + attributes['Type'] + end + + def properties + attributes['Properties'] + end + + def attributes attributes = { - logical_id => { - type: "AWS::Lambda::Permission", - properties: { - function_name: "!GetAtt {namespace}LambdaFunction.Arn", - action: "lambda:InvokeFunction", - principal: principal, - source_arn: source_arn, - } + type: "AWS::Lambda::Permission", + properties: { + function_name: "!GetAtt {namespace}LambdaFunction.Arn", + action: "lambda:InvokeFunction", + principal: principal, + source_arn: source_arn, } } - Attributes.new(attributes, @task) + attributes = replacer.replace_placeholders(attributes) + Jets::Pascalize.pascalize(attributes) end memoize :attributes # Auto-detect principal from the associated resources. def principal - Replacer.principal_map(@resource_attributes.type) + Replacer.principal_map(@associated_resource.type) end def source_arn - default_arn = "!GetAtt #{@resource_attributes.logical_id}.Arn" - Replacer.source_arn_map(@resource_attributes.type) || default_arn + default_arn = "!GetAtt #{@associated_resource.logical_id}.Arn" + Replacer.source_arn_map(@associated_resource.type) || default_arn end + + def replacer + Replacer.new(@replacements) + end + memoize :replacer end end