Sha256: 8b7ad7839fc99a38f87072b4cd28511e9dffe909e852aac559be51ba43419657

Contents?: true

Size: 1.41 KB

Versions: 13

Compression:

Stored size: 1.41 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?


      if changed?
        new_id
      else
        current
      end
    end

    def changed?
      change_detection = ChangeDetection.new
      change_detection.changed?
    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

13 entries across 13 versions & 1 rubygems

Version Path
jets-1.5.9 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.5.8 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.5.7 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.5.6 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.5.5 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.5.4 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.5.3 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.5.2 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.5.1 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.5.0 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.4.11 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.4.10 lib/jets/resource/api_gateway/rest_api/logical_id.rb
jets-1.4.9 lib/jets/resource/api_gateway/rest_api/logical_id.rb