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

#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

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
25
26
# 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.color(:green)} project for environment #{Jets.env.color(:green)}.  Couldn't find #{stack_name.color(: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 && endpoint_available?
    api_gateway_endpoint = get_gateway_endpoint(api_gateway_stack_arn)
    STDOUT.puts "API Gateway Endpoint: #{api_gateway_endpoint}"
    show_custom_domain
  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.color(:green)} is a jets app."
  end
end

#endpoint_available?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/jets/commands/url.rb', line 56

def endpoint_available?
  !endpoint_unavailable?
end

#endpoint_unavailable?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/jets/commands/url.rb', line 50

def endpoint_unavailable?
  return false if Jets::Router.routes.empty?
  resp, status = stack_status
  return false if status.include?("ROLLBACK")
end

#get_gateway_endpoint(api_gateway_stack_arn) ⇒ Object



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

def get_gateway_endpoint(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

#show_custom_domainObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/jets/commands/url.rb', line 39

def show_custom_domain
  return unless endpoint_available? && Jets.custom_domain? && Jets.config.domain.route53

  domain_name = Jets::Resource::ApiGateway::DomainName.new
  # Looks funny but its right.
  # domain_name is a method on the Jets::Resource::ApiGateway::Domain instance
  url = "https://#{domain_name.domain_name}"
  puts "Custom Domain: #{url}"
  puts "App Domain: #{Jets.config.app.domain}" if Jets.config.app.domain
end

#stack_statusObject



62
63
64
65
66
# File 'lib/jets/commands/url.rb', line 62

def stack_status
  resp = cfn.describe_stacks(stack_name: @parent_stack_name)
  status = resp.stacks[0].stack_status
  [resp, status]
end