Class: Jets::Cfn::Params::Api::Methods

Inherits:
Base
  • Object
show all
Defined in:
lib/jets/cfn/params/api/methods.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize, #load_template, #params

Constructor Details

This class inherits a constructor from Jets::Cfn::Params::Api::Base

Instance Method Details

#api_authorizer_stack_value(authorizer_id) ⇒ Object

IE: !GetAtt MainAuthorizer.Outputs.MainAuthorizerProtectAuthorizer



36
37
38
39
40
# File 'lib/jets/cfn/params/api/methods.rb', line 36

def api_authorizer_stack_value(authorizer_id)
  stack = authorizer_id.sub(/(.*?)Authorizer.*Authorizer$/, '\1') # IE: Main
  stack += "Authorizer" # IE: MainAuthorizer
  "!GetAtt #{stack}.Outputs.#{authorizer_id}"
end

#api_method_stack_value(resource_id) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/jets/cfn/params/api/methods.rb', line 58

def api_method_stack_value(resource_id)
  # IE: !GetAtt ApiResources1.Outputs.UpApiResource
  api_stack = if resource_id == "RootResourceId"
        "ApiGateway"
      else
        Jets::Cfn::Params::Api::Resources.stack_logical_id(resource_id)
      end
  "!GetAtt #{api_stack}.Outputs.#{resource_id}"
end

#buildObject

interface method



4
5
6
7
8
9
# File 'lib/jets/cfn/params/api/methods.rb', line 4

def build
  @params.merge!(RestApi: "!GetAtt ApiGateway.Outputs.RestApi") # common
  @template[:Resources].each do |logical_id, resource|
    create_params_from_resource(resource)
  end
end

#controller_stack_value(function_name) ⇒ Object

function_name: UpControllerIndexLambdaFunction



52
53
54
55
56
# File 'lib/jets/cfn/params/api/methods.rb', line 52

def controller_stack_value(function_name)
  controller = function_name.sub(/Controller.*/, "Controller")
  # IE: !GetAtt UpController.Outputs.UpControllerIndexLambdaFunction
  "!GetAtt #{controller}.Outputs.#{function_name}"
end

#create_authorizer_param(resource) ⇒ Object



28
29
30
31
32
33
# File 'lib/jets/cfn/params/api/methods.rb', line 28

def create_authorizer_param(resource)
  authorizer_id_ref = resource[:Properties][:AuthorizerId]
  return unless authorizer_id_ref
  authorizer_id = authorizer_id_ref.sub("!Ref ", "")
  @params.merge!(authorizer_id => api_authorizer_stack_value(authorizer_id))
end

#create_function_name_param(resource) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/jets/cfn/params/api/methods.rb', line 42

def create_function_name_param(resource)
  # Type can be: AWS_PROXY or MOCK (cors)
  return unless resource[:Properties][:Integration][:Type] == "AWS_PROXY"
  uri = resource[:Properties][:Integration][:Uri]
  md = uri.match(%r|functions/\${(.*)}/invocations|)
  function_name = md[1]
  @params.merge!(function_name => controller_stack_value(function_name))
end

#create_params_from_resource(resource) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jets/cfn/params/api/methods.rb', line 11

def create_params_from_resource(resource)
  case resource[:Type]
  when "AWS::ApiGateway::Method"
    # function name
    create_function_name_param(resource)
    # authorizer id
    create_authorizer_param(resource)
    # resource id
    resource_id = resource[:Properties][:ResourceId].sub("!Ref ", "")
    @params.merge!(resource_id => api_method_stack_value(resource_id))
  when "AWS::Lambda::Permission"
    # function name
    function_name = resource[:Properties][:FunctionName].sub("!Ref ", "")
    @params.merge!(function_name => controller_stack_value(function_name))
  end
end