Sha256: 491b01762bf5696f4c6151d3396f99ef210627c2fd340f830c0501c61e1b86d2

Contents?: true

Size: 1.66 KB

Versions: 22

Compression:

Stored size: 1.66 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
      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

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

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
jets-0.8.0 lib/jets/commands/url.rb
jets-0.7.1 lib/jets/commands/url.rb
jets-0.7.0 lib/jets/commands/url.rb
jets-0.6.9 lib/jets/commands/url.rb
jets-0.6.8 lib/jets/commands/url.rb
jets-0.6.7 lib/jets/commands/url.rb
jets-0.6.6 lib/jets/commands/url.rb
jets-0.6.5 lib/jets/commands/url.rb
jets-0.6.4 lib/jets/commands/url.rb
jets-0.6.3 lib/jets/commands/url.rb
jets-0.6.2 lib/jets/commands/url.rb
jets-0.6.1 lib/jets/commands/url.rb
jets-0.6.0 lib/jets/commands/url.rb
jets-0.5.8 lib/jets/commands/url.rb
jets-0.5.7 lib/jets/commands/url.rb
jets-0.5.6 lib/jets/commands/url.rb
jets-0.5.5 lib/jets/commands/url.rb
jets-0.5.4 lib/jets/commands/url.rb
jets-0.5.3 lib/jets/commands/url.rb
jets-0.5.2 lib/jets/commands/url.rb