Class: Jets::Cfn::TemplateBuilders::ControllerBuilder

Inherits:
BaseChildBuilder show all
Defined in:
lib/jets/cfn/template_builders/controller_builder.rb

Instance Method Summary collapse

Methods inherited from BaseChildBuilder

#add_class_iam_policy, #add_common_parameters, #add_function, #add_functions, #add_iam_policy, #initialize, #template_path

Methods included from Interface

#add_output, #add_parameter, #add_resource, #build, #post_process_template, #template, #text, #write

Constructor Details

This class inherits a constructor from Jets::Cfn::TemplateBuilders::BaseChildBuilder

Instance Method Details

#add_api_gateway_parametersObject



11
12
13
14
15
16
17
18
19
# File 'lib/jets/cfn/template_builders/controller_builder.rb', line 11

def add_api_gateway_parameters
  return if Jets::Router.routes.empty?

  add_parameter("RestApi", Description: "RestApi")
  scoped_routes.each do |route|
    map = Jets::Cfn::TemplateMappers::GatewayResourceMapper.new(route.path)
    add_parameter(map.logical_id, Description: map.desc)
  end
end

#add_cors(map) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jets/cfn/template_builders/controller_builder.rb', line 48

def add_cors(map)
  # TODO: provide a way to allow specify CORs domains
  return unless Jets.config.cors

  add_resource(map.cors_logical_id, "AWS::ApiGateway::Method",
    AuthorizationType: "NONE",
    HttpMethod: "OPTIONS",
    MethodResponses: [
      {
        StatusCode: "200",
        ResponseParameters: {
          "method.response.header.Access-Control-AllowOrigin" => true,
          "method.response.header.Access-Control-AllowHeaders" => true,
          "method.response.header.Access-Control-AllowMethods" => true,
          "method.response.header.Access-Control-AllowCredentials" => true
        },
        ResponseModels: {}
      }
    ],
    RequestParameters: {},
    Integration: {
      Type: "MOCK",
      RequestTemplates: {
        "application/json" => "{statusCode:200}"
      },
      IntegrationResponses: [
        {
          StatusCode: "200",
          ResponseParameters: {
            "method.response.header.Access-Control-AllowOrigin" => "'*'",
            "method.response.header.Access-Control-AllowHeaders" => "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent'",
            "method.response.header.Access-Control-AllowMethods" => "'OPTIONS,GET'",
            "method.response.header.Access-Control-AllowCredentials" => "'false'"
          },
          ResponseTemplates: {"application/json" => ""}
        }
      ]
    },
    ResourceId: "!Ref #{map.gateway_resource_logical_id}",
    RestApiId: "!Ref RestApi",
  )
end

#add_permission(map) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/jets/cfn/template_builders/controller_builder.rb', line 91

def add_permission(map)
  add_resource(map.permission_logical_id, "AWS::Lambda::Permission",
    FunctionName: "!GetAtt #{map.lambda_function_logical_id}.Arn",
    Action: "lambda:InvokeFunction",
    Principal: "apigateway.amazonaws.com",
    SourceArn: "!Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${RestApi}/*/*"
  )
end

#add_route(route, map) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jets/cfn/template_builders/controller_builder.rb', line 30

def add_route(route, map)
  # AWS::ApiGateway::Method
  # Example map.logical_id: ApiGatewayMethodPostsControllerIndex
  add_resource(map.logical_id, "AWS::ApiGateway::Method",
    HttpMethod: route.method,
    RequestParameters: {},
    ResourceId: "!Ref #{map.gateway_resource_logical_id}",
    RestApiId: "!Ref RestApi",
    AuthorizationType: "NONE",
    Integration: {
      IntegrationHttpMethod: "POST",
      Type: "AWS_PROXY",
      Uri: "!Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${#{map.lambda_function_logical_id}.Arn}/invocations"
    },
    MethodResponses:[]
  )
end

#add_routesObject



21
22
23
24
25
26
27
28
# File 'lib/jets/cfn/template_builders/controller_builder.rb', line 21

def add_routes
  scoped_routes.each_with_index do |route, i|
    map = Jets::Cfn::TemplateMappers::GatewayMethodMapper.new(route)
    add_route(route, map)
    add_cors(map)
    add_permission(map)
  end
end

#composeObject

compose is an interface method for Interface module



4
5
6
7
8
9
# File 'lib/jets/cfn/template_builders/controller_builder.rb', line 4

def compose
  add_common_parameters
  add_api_gateway_parameters
  add_functions
  add_routes
end

#scoped_routesObject

routes scoped to this controller template.



101
102
103
104
105
# File 'lib/jets/cfn/template_builders/controller_builder.rb', line 101

def scoped_routes
  @routes ||= Jets::Router.routes.select do |route|
    route.controller_name == @app_klass.to_s
  end
end