Class: Jets::Cfn::Resource::ApiGateway::RestApi::Routes::Change::Base

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
AwsServices
Defined in:
lib/jets/cfn/resource/api_gateway/rest_api/routes/change/base.rb

Direct Known Subclasses

MediaTypes, Page, Variable

Class Method Summary collapse

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

Class Method Details

.changed?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/jets/cfn/resource/api_gateway/rest_api/routes/change/base.rb', line 6

def self.changed?
  new.changed?
end

Instance Method Details

#controller_action_from_api(function_arn) ⇒ Object

TODO: If this hits the Lambda Rate limit, then list_functions also contains the Lambda function description. So we can paginate through list_functions results and store description from there if needed. Dont think this will be needed though because controller_action_from_string gets called most of the time. Also, user might be able to request their Lambda limit to be increased.



69
70
71
72
73
74
# File 'lib/jets/cfn/resource/api_gateway/rest_api/routes/change/base.rb', line 69

def controller_action_from_api(function_arn)
  desc = lambda_function_description(function_arn)
  controller, action = desc.split('#')
  controller = controller.underscore.sub(/_controller$/,'')
  [controller, action]
end

#controller_action_from_string(function_arn) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/jets/cfn/resource/api_gateway/rest_api/routes/change/base.rb', line 76

def controller_action_from_string(function_arn)
  controller_action = function_arn.sub("#{Jets.project_namespace}-", '')
  md = controller_action.match(/(.*)_controller-(.*)/)
  controller = md[1]
  controller = controller.gsub('-','/')
  action = md[2]
  [controller, action]
end

#deployed_routesObject

Recreate routes from previously deployed stored state in s3



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jets/cfn/resource/api_gateway/rest_api/routes/change/base.rb', line 11

def deployed_routes
  state = Jets::Router::State.new
  data = state.load("routes")
  return [] if data.nil?

  data.map do |item|
    http_method = item.dig('options','method') || item.dig('options','http_method')
    Jets::Router::Route.new(
      http_method: http_method,
      path: item['path'],
      to: item['to'],
      engine: item['engine'],
      internal: item['internal'],
    )
  end
end

#get_controller_action(function_arn) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/jets/cfn/resource/api_gateway/rest_api/routes/change/base.rb', line 56

def get_controller_action(function_arn)
  if function_arn.include?('_controller-')
    controller_action_from_string(function_arn)
  else
    controller_action_from_api(function_arn)
  end
end

#lambda_function_description(function_arn) ⇒ Object



85
86
87
88
# File 'lib/jets/cfn/resource/api_gateway/rest_api/routes/change/base.rb', line 85

def lambda_function_description(function_arn)
  resp = aws_lambda.get_function(function_name: function_arn)
  resp.configuration.description # contains full info: PostsController#index
end

#method_uri(resource_id, http_method) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/jets/cfn/resource/api_gateway/rest_api/routes/change/base.rb', line 34

def method_uri(resource_id, http_method)
  # https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html
  resp = apigateway.get_method(
    rest_api_id: rest_api_id,
    resource_id: resource_id,
    http_method: http_method
  )
  resp.method_integration.uri
end

#new_routesObject



105
106
107
# File 'lib/jets/cfn/resource/api_gateway/rest_api/routes/change/base.rb', line 105

def new_routes
  Jets::Router.routes
end

#recreate_to(method_uri) ⇒ Object

Parses method uri and recreates a Route to argument. So:

"arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:112233445566:function:demo-test-posts_controller-new/invocations"

Returns:

posts#new


49
50
51
52
53
54
# File 'lib/jets/cfn/resource/api_gateway/rest_api/routes/change/base.rb', line 49

def recreate_to(method_uri)
  md = method_uri.match(/function:(.*)\//)
  function_arn = md[1] # IE: demo-dev-posts_controller-new
  controller, action = get_controller_action(function_arn)
  "#{controller}##{action}" # IE: posts#new
end

#rest_api_idObject

Duplicated in rest_api/change_detection.rb, base_path/role.rb, rest_api/routes.rb



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/jets/cfn/resource/api_gateway/rest_api/routes/change/base.rb', line 91

def rest_api_id
  stack_name = Jets::Names.parent_stack_name
  return "RestApi" unless stack_exists?(stack_name)

  stack = cfn.describe_stacks(stack_name: stack_name).stacks.first

  api_gateway_stack_arn = lookup(stack[:outputs], "ApiGateway")

  # resources = cfn.describe_stack_resources(stack_name: api_gateway_stack_arn).stack_resources
  stack = cfn.describe_stacks(stack_name: api_gateway_stack_arn).stacks.first
  lookup(stack[:outputs], "RestApi") # rest_api_id
end

#to(resource_id, http_method) ⇒ Object



29
30
31
32
# File 'lib/jets/cfn/resource/api_gateway/rest_api/routes/change/base.rb', line 29

def to(resource_id, http_method)
  uri = method_uri(resource_id, http_method)
  recreate_to(uri) unless uri.nil?
end