Sha256: 243168eea9201c1b8123f9d7afff9a8567db0608ea1f2dd3fedf259f4709fab4

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

class Jets::Resource
  class Permission
    extend Memoist

    def initialize(replacements, associated_resource)
      @replacements = replacements
      # puts caller
      # puts "replacements #{replacements.inspect}"
      @associated_resource = associated_resource
    end

    def logical_id
      logical_id = "{namespace}Permission"
      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::Camelizer.camelize(logical_id)
    end

    def type
      attributes['Type']
    end

    def properties
      attributes['Properties']
    end

    def attributes
      attributes = {
        type: "AWS::Lambda::Permission",
        properties: {
          function_name: "!GetAtt {namespace}LambdaFunction.Arn",
          action: "lambda:InvokeFunction",
          principal: principal,
          source_arn: source_arn,
        }
      }
      attributes = replacer.replace_placeholders(attributes)
      Jets::Camelizer.transform(attributes)
    end
    memoize :attributes

    # Auto-detect principal from the associated resources.
    def principal
      Replacer.principal_map(@associated_resource.type)
    end

    def source_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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jets-0.8.8 lib/jets/resource/permission.rb
jets-0.8.6 lib/jets/resource/permission.rb
jets-0.8.5 lib/jets/resource/permission.rb