Class: Jets::Cfn::TemplateBuilders::ApiGatewayBuilder

Inherits:
Object
  • Object
show all
Includes:
AwsServices, Interface
Defined in:
lib/jets/cfn/template_builders/api_gateway_builder.rb

Instance Method Summary collapse

Methods included from AwsServices

#cfn, #lambda, #s3, #s3_resource, #stack_exists?, #stack_in_progress?, #sts

Methods included from Interface

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

Constructor Details

#initialize(options = {}) ⇒ ApiGatewayBuilder

Returns a new instance of ApiGatewayBuilder.



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

def initialize(options={})
  @options = options
  @template = ActiveSupport::HashWithIndifferentAccess.new(Resources: {})
end

Instance Method Details

#add_gateway_rest_apiObject

If the are routes in config/routes.rb add Gateway API in parent stack



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jets/cfn/template_builders/api_gateway_builder.rb', line 31

def add_gateway_rest_api
  add_resource("RestApi", "AWS::ApiGateway::RestApi",
    Name: Jets::Naming.gateway_api_name
  )

  stage_name = Jets::Cfn::TemplateMappers::ApiGatewayDeploymentMapper.stage_name
  add_output("RestApi", Value: "!Ref RestApi")
  add_output("RestApiUrl", Value: "!Sub 'https://${RestApi}.execute-api.${AWS::Region}.amazonaws.com/#{stage_name}/'")
  add_output("Region", Value: "!Ref AWS::Region")
  add_output("RootResourceId", Value: "!GetAtt RestApi.RootResourceId")
end

#add_gateway_routesObject

Adds route related Resources and Outputs



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jets/cfn/template_builders/api_gateway_builder.rb', line 44

def add_gateway_routes
  # The routes required a Gateway Resource to contain them.
  # TODO: Support more routes. Right now outputing all routes in 1 template will hit the 60 routes limit.
  # Will have to either output them as a joined string or break this up to multiple tempaltes.
  # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html
  # Outputs: Maximum number of outputs that you can declare in your AWS CloudFormation template. 60 outputs
  # Output name: Maximum size of an output name. 255 characters.
  #
  # Note we must use .all_paths, not .routes here because we need to
  # build the parent ApiGateway::Resource nodes also
  Jets::Router.all_paths.each do |path|
    homepage = path == ''
    next if homepage # handled by RootResourceId output already

    map = Jets::Cfn::TemplateMappers::GatewayResourceMapper.new(path)

    unless homepage # no AWS::ApiGateway::Resource for the top level route
      add_resource(map.logical_id, "AWS::ApiGateway::Resource",
        ParentId: map.parent_id,
        PathPart: map.path_part,
        RestApiId: "!Ref RestApi"
      )
    end

    add_output(map.logical_id, Value: "!Ref #{map.logical_id}")
  end
end

#composeObject

compose is an interface method



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

def compose
  return if @options[:stack_type] == :minimal

  puts "Building API Gateway template."
  add_gateway_rest_api
  add_gateway_routes
end

#template_pathObject

template_path is an interface method



21
22
23
# File 'lib/jets/cfn/template_builders/api_gateway_builder.rb', line 21

def template_path
  Jets::Naming.api_gateway_template_path
end

#writeObject

do not bother writing a template if routes are empty



26
27
28
# File 'lib/jets/cfn/template_builders/api_gateway_builder.rb', line 26

def write
  super unless Jets::Router.routes.empty?
end