Class: Jets::Resource::Permission

Inherits:
Base
  • Object
show all
Defined in:
lib/jets/resource/permission.rb

Instance Method Summary collapse

Methods inherited from Base

#replacements, #resource

Constructor Details

#initialize(replacements, associated_resource) ⇒ Permission

Returns a new instance of Permission.



3
4
5
6
# File 'lib/jets/resource/permission.rb', line 3

def initialize(replacements, associated_resource)
  @replacements = replacements
  @associated_resource = associated_resource
end

Instance Method Details

#definitionObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jets/resource/permission.rb', line 8

def definition
  logical_id = permission_logical_id

  definition = {
    logical_id => {
      type: "AWS::Lambda::Permission",
      properties: {
        function_name: "!GetAtt {namespace}LambdaFunction.Arn",
        action: "lambda:InvokeFunction",
        principal: principal
      }
    }
  }

  # From AWS docs: https://amzn.to/2N0QXQL
  # source_arn is "not supported by all event sources"
  definition[logical_id][:properties][:source_arn] = source_arn if source_arn

  definition
end

#permission_logical_idObject



29
30
31
32
33
34
# File 'lib/jets/resource/permission.rb', line 29

def permission_logical_id
  logical_id = "{namespace}_permission"
  md = @associated_resource.logical_id.match(/(\d+)$/)
  counter = md[1] if md
  [logical_id, counter].compact.join('').underscore
end

#principalObject

Auto-detect principal from the associated resources.



37
38
39
# File 'lib/jets/resource/permission.rb', line 37

def principal
  Replacer.principal_map(@associated_resource.type)
end

#source_arnObject



41
42
43
# File 'lib/jets/resource/permission.rb', line 41

def source_arn
  Replacer.source_arn_map(@associated_resource.type)
end