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, #logs, #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
  unless stack_exists?(stack_name)
    puts "Stack for #{Jets.config.project_name.colorize(:green)} project for environment #{Jets.env.colorize(:green)}.  Couldn't find #{stack_name.colorize(:green)} stack."
    exit
  end

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

  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. Also check that #{stack_name.colorize(:green)} is a jets app."
  end
end

#get_url(api_gateway_stack_arn) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# 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")
  stage_name = Jets::Resource::ApiGateway::Deployment.stage_name

  # 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/#{stage_name}"
end

#lookup(outputs, key) ⇒ Object

Lookup output value



38
39
40
41
# File 'lib/jets/commands/url.rb', line 38

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