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

Inherits:
Object
  • Object
show all
Extended by:
Memoist
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'] || 200)

Instance Method Summary collapse

Methods included from AwsServices

#apigateway, #aws_lambda, #aws_options, #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.



7
8
9
10
# File 'lib/jets/cfn/builders/api_gateway_builder.rb', line 7

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

Instance Method Details

#add_custom_domainObject



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

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

#add_domain_nameObject



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

def add_domain_name
  add_outputs(create_domain_name)
end

#add_gateway_rest_apiObject

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



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

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



117
118
119
120
121
122
123
124
125
# File 'lib/jets/cfn/builders/api_gateway_builder.rb', line 117

def add_gateway_routes
  # Reject homepage. Otherwise we have 200 - 1 resources on the first page.
  # There's a next call in ApiResources.add_gateway_resources to skip the homepage.
  page_builder = Jets::Cfn::Builders::PageBuilder.new
  pages = page_builder.build
  pages.each_with_index do |paths, i|
    ApiResourcesBuilder.new(@options, paths, i+1).build
  end
end

#add_route53_dnsObject



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

def add_route53_dns
  dns = Jets::Resource::Route53::RecordSet.new
  if !existing_domain_name?(dns.domain_name) or existing_dns_record_on_stack?
    add_resource(dns)
    add_outputs(dns.outputs)
  end
end

#api_gateway_physical_resource_idObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/jets/cfn/builders/api_gateway_builder.rb', line 102

def api_gateway_physical_resource_id
  resp = cfn.describe_stack_resource(
    stack_name: Jets::Names.parent_stack_name,
    logical_resource_id: "ApiGateway"
  )
  resp&.stack_resource_detail&.physical_resource_id
# IE: Aws::CloudFormation::Errors::ValidationError (Resource ApiGateway does not exist for stack demo-dev)
rescue Aws::CloudFormation::Errors::ValidationError
  nil
end

#composeObject

compose is an interface method



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

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

#create_domain_nameObject



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

def create_domain_name
  resource = Jets::Resource::ApiGateway::DomainName.new

  return {
    "DomainName" => resource.domain_name
  } if (existing_domain_name?(resource) and !existing_domain_name_on_stack?)

  add_resource(resource)
  return resource.outputs
end

#existing_dns_record_on_stack?Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
# File 'lib/jets/cfn/builders/api_gateway_builder.rb', line 91

def existing_dns_record_on_stack?
  cfn.describe_stack_resource(
    stack_name: api_gateway_physical_resource_id,
    logical_resource_id: "DnsRecord"
  )
  true
# IE: Aws::CloudFormation::Errors::ValidationError (Resource DnsRecord does not exist for stack demo-dev)
rescue Aws::CloudFormation::Errors::ValidationError
  false
end

#existing_domain_name?(resource) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
# File 'lib/jets/cfn/builders/api_gateway_builder.rb', line 69

def existing_domain_name?(resource)
  apigateway.get_domain_name({
    domain_name: resource.domain_name
  })
  true
# IE: Aws::APIGateway::Errors::NotFoundException Invalid domain name identifier specified
rescue Aws::APIGateway::Errors::NotFoundException
  false
end

#existing_domain_name_on_stack?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
# File 'lib/jets/cfn/builders/api_gateway_builder.rb', line 80

def existing_domain_name_on_stack?
  cfn.describe_stack_resource(
    stack_name: api_gateway_physical_resource_id,
    logical_resource_id: "DomainName"
  )
  true
# IE: Aws::CloudFormation::Errors::ValidationError (Resource DomainName does not exist for stack demo-dev)
rescue Aws::CloudFormation::Errors::ValidationError
  false
end

#template_pathObject

template_path is an interface method



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

def template_path
  Jets::Names.api_gateway_template_path
end

#writeObject

do not bother writing a template if routes are empty



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

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