Class: Jets::Command::Url

Inherits:
Object
  • Object
show all
Includes:
AwsServices
Defined in:
lib/jets/commands/url/url_command.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.



15
16
17
# File 'lib/jets/commands/url/url_command.rb', line 15

def initialize(options)
  @options = options
end

Instance Method Details

#displayObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jets/commands/url/url_command.rb', line 19

def display
  Jets.boot
  stack_name = Jets::Names.parent_stack_name
  unless stack_exists?(stack_name)
    $stderr.puts "Stack for #{Jets.project_name.color(:green)} project for environment #{Jets.env.color(:green)}.  Couldn't find #{stack_name.color(:green)} stack."
    exit 1
  end

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

  data = {}

  api_gateway_stack_arn = lookup(stack[:outputs], "ApiGateway")
  if api_gateway_stack_arn && endpoint_available?
    data[:api_gateway_endpoint] = get_gateway_endpoint(api_gateway_stack_arn)
    get_custom_domain!(data)
  end

  if data.empty?
    $stderr.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."
    exit 1
  end

  if @options[:format] == "json"
    puts data.to_json
  else
    puts "API Gateway Endpoint: #{data[:api_gateway_endpoint]}"
    puts "Custom Domain: #{data[:custom_domain]}" if data[:custom_domain]
    puts "App Domain: #{data[:app_domain]}" if data[:app_domain]
  end
end

#endpoint_available?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/jets/commands/url/url_command.rb', line 79

def endpoint_available?
  !endpoint_unavailable?
end

#endpoint_unavailable?Boolean

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/jets/commands/url/url_command.rb', line 73

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

#get_custom_domain!(data) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/jets/commands/url/url_command.rb', line 62

def get_custom_domain!(data)
  return unless endpoint_available? && Jets.custom_domain? && Jets.config.domain.route53

  domain_name = Jets::Cfn::Resource::ApiGateway::DomainName.new
  # Looks funny but its right.
  # domain_name is a method on the Jets::Cfn::Resource::ApiGateway::Domain instance
  url = "https://#{domain_name.domain_name}"
  data[:custom_domain] = url
  data[:app_domain] = "https://#{Jets.config.app.domain}" if Jets.config.app.domain
end

#get_gateway_endpoint(api_gateway_stack_arn) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/jets/commands/url/url_command.rb', line 51

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::Cfn::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

#stack_statusObject



85
86
87
88
89
# File 'lib/jets/commands/url/url_command.rb', line 85

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