1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/aws/models/compute/address.rb

70 lines
1.5 KiB
Ruby
Raw Normal View History

2010-10-04 17:02:08 -04:00
require 'fog/core/model'
2010-03-16 18:46:21 -04:00
2009-08-13 01:33:35 -04:00
module Fog
module AWS
2010-09-08 17:40:02 -04:00
class Compute
2009-08-13 01:33:35 -04:00
class Address < Fog::Model
identity :public_ip, :aliases => 'publicIp'
2009-08-13 01:33:35 -04:00
attribute :server_id, :aliases => 'instanceId'
def initialize(attributes = {})
# assign server first to prevent race condition with new_record?
self.server = attributes.delete(:server)
super
end
2009-09-20 12:21:03 -04:00
def destroy
requires :public_ip
connection.release_address(public_ip)
true
2009-08-13 01:33:35 -04:00
end
2010-01-08 14:29:07 -05:00
def server=(new_server)
if new_server
associate(new_server)
else
2009-11-12 02:17:50 -05:00
disassociate
end
2009-10-06 21:55:39 -04:00
end
2009-08-13 01:33:35 -04:00
def save
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
data = connection.allocate_address.body
new_attributes = data.reject {|key,value| key == 'requestId'}
merge_attributes(new_attributes)
2010-01-08 14:29:07 -05:00
if @server
self.server = @server
end
true
2009-08-13 01:33:35 -04:00
end
private
2010-01-08 14:29:07 -05:00
def associate(new_server)
if new_record?
2010-01-08 14:29:07 -05:00
@server = new_server
else
2010-01-08 14:29:07 -05:00
@server = nil
self.server_id = new_server.id
2010-01-08 14:29:07 -05:00
connection.associate_address(@server_id, @public_ip)
end
end
def disassociate
2010-01-08 14:29:07 -05:00
@server = nil
self.server_id = nil
unless new_record?
connection.disassociate_address(@public_ip)
end
end
2009-08-13 01:33:35 -04:00
end
end
end
end