Sha256: 5faec085ce398e5b6ebde349aaec87aae20821e7427e45a7349007c5f9abe18c

Contents?: true

Size: 1.61 KB

Versions: 48

Compression:

Stored size: 1.61 KB

Contents

module Jets::Commands
  class Url
    include Jets::AwsServices

    def initialize(options)
      @options = options
    end

    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

    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 output value
    def lookup(outputs, key)
      out = outputs.find { |o| o.output_key == key }
      out&.output_value
    end
  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
jets-1.1.5 lib/jets/commands/url.rb
jets-1.1.4 lib/jets/commands/url.rb
jets-1.1.3 lib/jets/commands/url.rb
jets-1.1.2 lib/jets/commands/url.rb
jets-1.1.1 lib/jets/commands/url.rb
jets-1.1.0 lib/jets/commands/url.rb
jets-1.0.18 lib/jets/commands/url.rb
jets-1.0.17 lib/jets/commands/url.rb
jets-1.0.16 lib/jets/commands/url.rb
jets-1.0.15 lib/jets/commands/url.rb
jets-1.0.13 lib/jets/commands/url.rb
jets-1.0.12 lib/jets/commands/url.rb
jets-1.0.11 lib/jets/commands/url.rb
jets-1.0.10 lib/jets/commands/url.rb
jets-1.0.9 lib/jets/commands/url.rb
jets-1.0.8 lib/jets/commands/url.rb
jets-1.0.7 lib/jets/commands/url.rb
jets-1.0.6 lib/jets/commands/url.rb
jets-1.0.5 lib/jets/commands/url.rb
jets-1.0.4 lib/jets/commands/url.rb