mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
60 lines
1.3 KiB
Ruby
60 lines
1.3 KiB
Ruby
module Fog
|
|
module AWS
|
|
class EC2
|
|
|
|
def addresses(attributes = {})
|
|
Fog::AWS::EC2::Addresses.new({
|
|
:connection => self
|
|
}.merge!(attributes))
|
|
end
|
|
|
|
class Addresses < Fog::Collection
|
|
|
|
attribute :public_ip
|
|
attribute :instance
|
|
|
|
model Fog::AWS::EC2::Address
|
|
|
|
def initialize(attributes)
|
|
@public_ip ||= []
|
|
super
|
|
end
|
|
|
|
def all(public_ip = @public_ip)
|
|
@public_ip = public_ip
|
|
if @loaded
|
|
clear
|
|
end
|
|
@loaded = true
|
|
data = connection.describe_addresses(public_ip).body
|
|
addresses = []
|
|
data['addressesSet'].each do |address|
|
|
addresses << new(address.reject {|key, value| value.nil? || value.empty? })
|
|
end
|
|
if instance
|
|
addresses = addresses.select {|address| address.instance_id == instance.id}
|
|
end
|
|
self.replace(addresses)
|
|
end
|
|
|
|
def get(public_ip)
|
|
if public_ip
|
|
all(public_ip).first
|
|
end
|
|
rescue Excon::Errors::BadRequest
|
|
nil
|
|
end
|
|
|
|
def new(attributes = {})
|
|
if instance
|
|
super({ :instance => instance }.merge!(attributes))
|
|
else
|
|
super(attributes)
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
end
|
|
end
|