Class: Jets::Resource::ApiGateway::RestApi::LogicalId

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
AwsServices
Defined in:
lib/jets/resource/api_gateway/rest_api/logical_id.rb,
lib/jets/resource/api_gateway/rest_api/logical_id/message.rb

Defined Under Namespace

Classes: Message

Constant Summary collapse

TIMEOUT_PERIOD =
120

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

Instance Method Details

#api_gateway_exists?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/jets/resource/api_gateway/rest_api/logical_id.rb', line 86

def api_gateway_exists?
  !!api_gateway_stack_arn
end

#api_gateway_stack_arnObject



81
82
83
84
# File 'lib/jets/resource/api_gateway/rest_api/logical_id.rb', line 81

def api_gateway_stack_arn
  stack = cfn.describe_stacks(stack_name: parent_stack_name).stacks.first
  lookup(stack[:outputs], "ApiGateway") # api_gateway_stack_arn
end

#auto_replace_promptObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jets/resource/api_gateway/rest_api/logical_id.rb', line 18

def auto_replace_prompt
  return if ENV['JETS_API_AUTO_REPLACE']
  return unless ARGV[0] == "deploy"
  case Jets.config.api.auto_replace
  when nil
    puts message.routes_changed
    puts message.custom_domain
    print "Would you like to continue the deployment? (y/N) "
    answer = get_answer
    exit 1 unless answer =~ /^y/
  when false
    puts message.routes_changed
    puts message.auto_replace_disabled
    exit 1
  end
end

#changed?Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/jets/resource/api_gateway/rest_api/logical_id.rb', line 51

def changed?
  change_detection = ChangeDetection.new
  change_detection.changed?
end

#currentObject



74
75
76
77
78
# File 'lib/jets/resource/api_gateway/rest_api/logical_id.rb', line 74

def current
  resources = cfn.describe_stack_resources(stack_name: api_gateway_stack_arn).stack_resources
  rest_api = resources.find { |r| r.resource_type == 'AWS::ApiGateway::RestApi' }
  rest_api.logical_resource_id
end

#defaultObject



94
95
96
# File 'lib/jets/resource/api_gateway/rest_api/logical_id.rb', line 94

def default
  "RestApi"
end

#getObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/jets/resource/api_gateway/rest_api/logical_id.rb', line 6

def get
  return default if ENV['JETS_BUILD_NO_INTERNET']
  return default unless stack_exists?(parent_stack_name) && api_gateway_exists?

  if changed?
    auto_replace_prompt
    new_id
  else
    current
  end
end

#get_answerObject



41
42
43
44
45
46
47
48
49
# File 'lib/jets/resource/api_gateway/rest_api/logical_id.rb', line 41

def get_answer
  Timeout::timeout(TIMEOUT_PERIOD) do
    $stdin.gets
  end
rescue Timeout::Error => e
  puts "#{e.class}: #{e.message}".color(:red)
  puts "Deployment timeout after #{TIMEOUT_PERIOD}s. Waited too long answer. Exiting."
  exit 1
end

#messageObject



35
36
37
# File 'lib/jets/resource/api_gateway/rest_api/logical_id.rb', line 35

def message
  Message.new
end

#new_idObject

Takes current logical id and increments the number that is appended to it.

Examples:

RestApi => RestApi1
RestApi1 => RestApi2
RestApi2 => RestApi3
RestApi7 => RestApi8


64
65
66
67
68
69
70
71
72
# File 'lib/jets/resource/api_gateway/rest_api/logical_id.rb', line 64

def new_id
  regexp = /(\d+)/
  md = current.match(regexp)
  if md
    current.gsub(regexp,'') + (md[1].to_i + 1).to_s
  else
    current + "1"
  end
end

#parent_stack_nameObject



90
91
92
# File 'lib/jets/resource/api_gateway/rest_api/logical_id.rb', line 90

def parent_stack_name
  Jets::Naming.parent_stack_name
end