Sha256: 1644481b8beae7e42f48df953b4169410ce05d60e1226964868632e97d492a90

Contents?: true

Size: 1.37 KB

Versions: 21

Compression:

Stored size: 1.37 KB

Contents

class Jets::Resource::ApiGateway::RestApi
  class LogicalId
    extend Memoist
    include Jets::AwsServices

    def get
      return default unless stack_exists?(parent_stack_name) && api_gateway_exists?

      change_detection = ChangeDetection.new
      if change_detection.changed?
        new_id
      else
        current
      end
    end

    # Takes current logical id and increments the number that is appended to it.
    #
    # Examples:
    #
    #   RestApi => RestApi1
    #   RestApi1 => RestApi2
    #   RestApi2 => RestApi3
    #   RestApi7 => RestApi8
    def new_id
      regexp = /(\d+)/
      md = current.match(regexp)
      if md
        current.gsub(regexp,'') + (md[1].to_i + 1).to_s
      else
        current + "1"
      end
    end

    def current
      resources = cfn.describe_stack_resources(stack_name: api_gateway_stack_arn).stack_resources
      rest_api = resources.find { |r| r.resource_type == 'AWS::ApiGateway::RestApi' }
      rest_api.logical_resource_id
    end
    memoize :current

    def api_gateway_stack_arn
      stack = cfn.describe_stacks(stack_name: parent_stack_name).stacks.first
      lookup(stack[:outputs], "ApiGateway") # api_gateway_stack_arn
    end

    def api_gateway_exists?
      !!api_gateway_stack_arn
    end

    def parent_stack_name
      Jets::Naming.parent_stack_name
    end

    def default
      "RestApi"
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
jets-1.4.8 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.4.7 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.4.6 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.4.5 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.4.4 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.4.3 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.4.2 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.4.1 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.4.0 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.3.9 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.3.8 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.3.7 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.3.6 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.3.5 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.3.4 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.3.3 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.3.2 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.3.1 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.3.0 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.2.1 lib/jets/resource/api_gateway/rest_api/logical_id.rb