Class: Jets::Cfn::Builders::ApiGatewayBuilder

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

Constant Summary collapse

PAGE_LIMIT =

Adds route related Resources and Outputs Delegates to ApiResourcesBuilder

Integer(ENV['JETS_AWS_OUTPUTS_LIMIT'] || 60)

Instance Method Summary collapse

Methods included from AwsServices

#apigateway, #aws_lambda, #cfn, #dynamodb, #logs, #s3, #s3_resource, #sns, #sqs, #sts

Methods included from AwsServices::StackStatus

#lookup, #stack_exists?, #stack_in_progress?

Methods included from AwsServices::GlobalMemoist

included

Methods included from Interface

#add_description, #add_output, #add_outputs, #add_parameter, #add_parameters, #add_resource, #add_resources, #add_template_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/builders/api_gateway_builder.rb', line 6

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

Instance Method Details

#add_custom_domainObject



39
40
41
42
43
# File 'lib/jets/cfn/builders/api_gateway_builder.rb', line 39

def add_custom_domain
  return unless Jets.custom_domain?
  add_domain_name
  add_route53_dns if Jets.config.domain.route53
end

#add_domain_nameObject



45
46
47
48
49
# File 'lib/jets/cfn/builders/api_gateway_builder.rb', line 45

def add_domain_name
  domain_name = Jets::Resource::ApiGateway::DomainName.new
  add_resource(domain_name)
  add_outputs(domain_name.outputs)
end

#add_gateway_rest_apiObject

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



29
30
31
32
33
34
35
36
37
# File 'lib/jets/cfn/builders/api_gateway_builder.rb', line 29

def add_gateway_rest_api
  rest_api = Jets::Resource::ApiGateway::RestApi.new
  add_resource(rest_api)
  add_outputs(rest_api.outputs)

  deployment = Jets::Resource::ApiGateway::Deployment.new
  outputs = deployment.outputs(true)
  add_output("RestApiUrl", Value: outputs["RestApiUrl"])
end

#add_gateway_routesObject



60
61
62
63
64
65
66
67
# File 'lib/jets/cfn/builders/api_gateway_builder.rb', line 60

def add_gateway_routes
  # Reject homepage. Otherwise we have 60 - 1 resources on the first page.
  # There's a next call in ApiResources.add_gateway_resources to skip the homepage.
  all_paths = Jets::Router.all_paths.reject { |p| p == '' }
  all_paths.each_slice(PAGE_LIMIT).each_with_index do |paths, i|
    ApiResourcesBuilder.new(@options, paths, i+1).build
  end
end

#add_route53_dnsObject



51
52
53
54
55
# File 'lib/jets/cfn/builders/api_gateway_builder.rb', line 51

def add_route53_dns
  dns = Jets::Resource::Route53::RecordSet.new
  add_resource(dns)
  add_outputs(dns.outputs)
end

#composeObject

compose is an interface method



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

def compose
  add_gateway_routes # "child template": build before add_gateway_rest_api. RestApi logical id and change detection is dependent on it.
  add_gateway_rest_api # changes parent template
  add_custom_domain    # changes parent template
end

#template_pathObject

template_path is an interface method



19
20
21
# File 'lib/jets/cfn/builders/api_gateway_builder.rb', line 19

def template_path
  Jets::Naming.api_gateway_template_path
end

#writeObject

do not bother writing a template if routes are empty



24
25
26
# File 'lib/jets/cfn/builders/api_gateway_builder.rb', line 24

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