Class: Jets::Resource::ApiGateway::RestApi::Routes::Change::Page

Inherits:
Base
  • Object
show all
Defined in:
lib/jets/resource/api_gateway/rest_api/routes/change/page.rb

Instance Method Summary collapse

Methods inherited from Base

changed?, #controller_action_from_api, #controller_action_from_string, #deployed_routes, #get_controller_action, #lambda_function_description, #method_uri, #new_routes, #recreate_to, #rest_api_id, #to

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

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/jets/resource/api_gateway/rest_api/routes/change/page.rb', line 3

def changed?
  route_page_moved? || old_api_template?
end

#deployed_pagesObject



28
29
30
# File 'lib/jets/resource/api_gateway/rest_api/routes/change/page.rb', line 28

def deployed_pages
  remote_logical_ids_map
end

#local_logical_ids_map(path_expression = "#{Jets::Names.template_path_prefix}-api-resources-*.yml") ⇒ Object

logical id to page map Important: In Cfn::Builders::ApiGatewayBuilder, the add_gateway_routes and ApiResourcesBuilder needs to run before the parent add_gateway_rest_api method.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jets/resource/api_gateway/rest_api/routes/change/page.rb', line 36

def local_logical_ids_map(path_expression="#{Jets::Names.template_path_prefix}-api-resources-*.yml")
  logical_ids = {} # logical id => page number

  Dir.glob(path_expression).each do |path|
    md = path.match(/-api-resources-(\d+).yml/)
    page_number = md[1]

    template = Jets::Cfn::BuiltTemplate.get(path)
    template['Resources'].keys.each do |logical_id|
      logical_ids[logical_id] = page_number
    end
  end

  logical_ids
end

#moved?(new_pages, deployed_pages) ⇒ Boolean

Routes page to logical ids

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
# File 'lib/jets/resource/api_gateway/rest_api/routes/change/page.rb', line 12

def moved?(new_pages, deployed_pages)
  not_moved = true # page has not moved
  new_pages.each do |logical_id, new_page_number|
    if !deployed_pages[logical_id].nil? && deployed_pages[logical_id] != new_page_number
      not_moved = false # page has moved
      break
    end
  end
  !not_moved # moved
end

#new_pagesObject



23
24
25
# File 'lib/jets/resource/api_gateway/rest_api/routes/change/page.rb', line 23

def new_pages
  local_logical_ids_map
end

#old_api_template?Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jets/resource/api_gateway/rest_api/routes/change/page.rb', line 73

def old_api_template?
  logical_resource_ids = parent_resources.map(&:logical_resource_id)

  api_gateway_found = logical_resource_ids.detect do |logical_id|
    logical_id == "ApiGateway"
  end
  return false unless api_gateway_found

  api_resources_found = logical_resource_ids.detect do |logical_id|
    logical_id.match(/^ApiResources\d+$/)
  end
  !api_resources_found # if api_resources_found then it's the new structure. so opposite is old structure
end

#parent_resourcesObject



87
88
89
90
# File 'lib/jets/resource/api_gateway/rest_api/routes/change/page.rb', line 87

def parent_resources
  resp = cfn.describe_stack_resources(stack_name: Jets::Names.parent_stack_name)
  resp.stack_resources
end

#remote_logical_ids_mapObject

aws cloudformation describe-stack-resources –stack-name demo-dev-ApiResources1-DYGLIEY3VAWT | jq -r ‘.StackResources[].LogicalResourceId’



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jets/resource/api_gateway/rest_api/routes/change/page.rb', line 53

def remote_logical_ids_map
  logical_ids = {} # logical id => page number

  parent_resources.each do |resource|
    stack_name = resource.physical_resource_id # full physical id can be used as stack name also
    regexp = Regexp.new("#{Jets.config.project_namespace}-ApiResources(\\d+)-") # tricky to escape \d pattern
    md = stack_name.match(regexp)
    if md
      page_number = md[1]

      resp = cfn.describe_stack_resources(stack_name: stack_name)
      resp.stack_resources.map(&:logical_resource_id).each do |logical_id|
        logical_ids[logical_id] = page_number
      end
    end
  end

  logical_ids
end

#route_page_moved?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/jets/resource/api_gateway/rest_api/routes/change/page.rb', line 7

def route_page_moved?
  moved?(new_pages, deployed_pages)
end