Class: Jets::Commands::Url

Inherits:
Object
  • Object
show all
Includes:
AwsServices
Defined in:
lib/jets/commands/url.rb

Instance Method Summary collapse

Methods included from AwsServices

#cfn, #lambda, #s3, #s3_resource, #stack_exists?, #stack_in_progress?, #sts

Constructor Details

#initialize(options) ⇒ Url

Returns a new instance of Url.



5
6
7
# File 'lib/jets/commands/url.rb', line 5

def initialize(options)
  @options = options
end

Instance Method Details

#displayObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jets/commands/url.rb', line 9

def display
  stack_name = Jets::Naming.parent_stack_name
  stack = cfn.describe_stacks(stack_name: stack_name).stacks.first

  unless stack
    puts "Stack for '#{Jets.config.project_name} project for environment #{Jets.env}.  Couldn't find '#{stack_name}' stack."
    exit
  end

  api_gateway_stack_arn = lookup(stack[:outputs], "ApiGateway")
  if api_gateway_stack_arn
    STDOUT.puts get_url(api_gateway_stack_arn)
  else
    puts "API Gateway not found. This jets app does have an API Gateway associated with it.  Please double check your config/routes.rb if you were expecting to see a url for the app."
  end
end

#get_url(api_gateway_stack_arn) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jets/commands/url.rb', line 26

def get_url(api_gateway_stack_arn)
  stack = cfn.describe_stacks(stack_name: api_gateway_stack_arn).stacks.first
  rest_api = lookup(stack[:outputs], "RestApi")
  region_id = lookup(stack[:outputs], "Region")
  # Abusing ApiGatewayDeploymentMapper
  #   set path=nil,s3_bucket=nil
  #   not using any methods that rely on the initialization parameters
  map = Jets::Cfn::TemplateMappers::ApiGatewayDeploymentMapper.new(path=nil,s3_bucket=nil)
  # https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-call-api.html
  # https://my-api-id.execute-api.region-id.amazonaws.com/stage-name/{resourcePath}
  "https://#{rest_api}.execute-api.#{region_id}.amazonaws.com/#{map.stage_name}"
end

#lookup(outputs, key) ⇒ Object

Lookup output value



40
41
42
43
# File 'lib/jets/commands/url.rb', line 40

def lookup(outputs, key)
  o = outputs.find { |o| o.output_key == key }
  o.output_value
end