Sha256: 18be18317dfaa0d5cb532fa3e3ec95db7a1feca37085bb2f3be5d4ed15fd45cf

Contents?: true

Size: 1.42 KB

Versions: 24

Compression:

Stored size: 1.42 KB

Contents

require 'aws-sdk'
require 'awspec/resource_reader'
require 'awspec/helper/finder'

module Awspec::Type
  class Base
    include Awspec::Helper::Finder
    include Awspec::BlackListForwardable

    attr_reader :id, :resource_via_client

    def initialize(display_name = nil)
      @display_name = display_name
      @id = nil
    end

    def to_s
      type = self.class.name.demodulize.underscore
      "#{type} '#{@display_name}'"
    end

    def inspect
      to_s
    end

    def exists?
      id
    end

    def id
      raise 'this method must be override!'
    end

    def resource_via_client
      raise 'this method must be override!'
    end

    def self.aws_resource(resource)
      define_method :resource do
        @resource ||= Awspec::ResourceReader.new(resource.new(id))
      end
    end

    def self.tags_allowed
      define_method :has_tag? do |key, value|
        begin
          tags = resource_via_client.tags
        rescue NoMethodError
          tags = resource_via_client.tag_set
        end
        return false unless tags
        tags.any? { |t| t['key'] == key && t['value'] == value }
      end
    end

    def method_missing(name)
      describe = name.to_sym
      if resource_via_client.members.include?(describe)
        resource_via_client[describe]
      else
        super unless self.respond_to?(:resource)
        method_missing_via_black_list(name, delegate_to: resource)
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
awspec-0.59.0 lib/awspec/type/base.rb
awspec-0.58.0 lib/awspec/type/base.rb
awspec-0.57.1 lib/awspec/type/base.rb
awspec-0.57.0 lib/awspec/type/base.rb