Sha256: 3ba1e67a47caabfbaef0efe6c593e616be53824c036d162b5e6450a4c9b77aa7

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

module Awspec::Type
  class Ec2 < Base
    attr_reader :client, :instance

    def initialize(id)
      super
      @client = @ec2_client
      @instance = find_ec2(id)
      @id = @instance[:instance_id] if @instance
    end

    states = %w(
      pending running shutting-down
      terminated stopping stopped
    )

    states.each do |state|
      define_method state + '?' do
        @instance[:state][:name] == state
      end
    end

    def method_missing(name)
      describe = name.to_s
      if @instance.key?(describe)
        @instance[describe]
      else
        super
      end
    end

    def has_eip?(ip_address = nil)
      option = {
        filters: [{ name: 'instance-id', values: [@id] }]
      }
      option[:public_ips] = [ip_address] if ip_address
      ret = @ec2_client.describe_addresses(option)
      return ret[:addresses].count == 1 if ip_address
      return ret[:addresses].count > 0 unless ip_address
    end

    def has_security_group?(sg_id)
      sgs = @instance[:security_groups]
      ret = sgs.find do |sg|
        sg[:group_id] == sg_id || sg[:group_name] == sg_id
      end
      return true if ret
      sg2 = find_security_group(sg_id)
      sg2[:tags].find do |tag|
        tag[:key] == 'Name' && tag[:value] == sg_id
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
awspec-0.2.2 lib/awspec/type/ec2.rb
awspec-0.2.1 lib/awspec/type/ec2.rb
awspec-0.2.0 lib/awspec/type/ec2.rb