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

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

Instance Method Summary collapse

Methods included from AwsServices

#apigateway, #cfn, #lambda, #logs, #s3, #s3_resource, #sns, #sqs, #sts

Methods included from AwsServices::StackStatus

#lookup, #stack_exists?, #stack_in_progress?

Methods included from Interface

#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



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

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

#add_domain_nameObject



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

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



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

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

Adds route related Resources and Outputs



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/jets/cfn/builders/api_gateway_builder.rb', line 60

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 templates.
  # 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

    resource = Jets::Resource::ApiGateway::Resource.new(path, internal: true)
    add_resource(resource)
    add_outputs(resource.outputs)
  end
end

#add_route53_dnsObject



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

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
17
18
# File 'lib/jets/cfn/builders/api_gateway_builder.rb', line 12

def compose
  return unless @options[:templates] || @options[:stack_type] != :minimal

  add_gateway_rest_api
  add_custom_domain
  add_gateway_routes
end

#template_pathObject

template_path is an interface method



21
22
23
# File 'lib/jets/cfn/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/builders/api_gateway_builder.rb', line 26

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