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
|
2011-06-16 19:28:54 -04:00
|
|
|
module Compute
|
|
|
|
class AWS
|
2009-08-13 01:33:35 -04:00
|
|
|
|
|
|
|
class Address < Fog::Model
|
|
|
|
|
2010-09-07 14:30:02 -04:00
|
|
|
identity :public_ip, :aliases => 'publicIp'
|
2009-08-13 01:33:35 -04:00
|
|
|
|
2010-09-07 14:30:02 -04:00
|
|
|
attribute :server_id, :aliases => 'instanceId'
|
2009-09-19 15:21:31 -04:00
|
|
|
|
2010-05-10 18:01:19 -04:00
|
|
|
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
|
2009-11-21 16:56:39 -05:00
|
|
|
requires :public_ip
|
|
|
|
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.release_address(public_ip)
|
2009-09-05 23:50:40 -04:00
|
|
|
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)
|
2009-11-12 01:22:13 -05:00
|
|
|
else
|
2009-11-12 02:17:50 -05:00
|
|
|
disassociate
|
2009-10-20 22:39:57 -04:00
|
|
|
end
|
2009-10-06 21:55:39 -04:00
|
|
|
end
|
|
|
|
|
2009-08-13 01:33:35 -04:00
|
|
|
def save
|
2010-10-04 19:08:34 -04:00
|
|
|
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
|
2010-11-19 16:45:45 -05:00
|
|
|
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
|
2009-10-20 22:39:57 -04:00
|
|
|
end
|
2009-09-05 23:50:40 -04:00
|
|
|
true
|
2009-08-13 01:33:35 -04:00
|
|
|
end
|
|
|
|
|
2009-11-12 01:22:13 -05:00
|
|
|
private
|
|
|
|
|
2010-01-08 14:29:07 -05:00
|
|
|
def associate(new_server)
|
2009-11-12 01:22:13 -05:00
|
|
|
if new_record?
|
2010-01-08 14:29:07 -05:00
|
|
|
@server = new_server
|
2009-11-12 01:22:13 -05:00
|
|
|
else
|
2010-01-08 14:29:07 -05:00
|
|
|
@server = nil
|
2010-11-19 16:45:45 -05:00
|
|
|
self.server_id = new_server.id
|
2010-11-30 19:43:28 -05:00
|
|
|
connection.associate_address(server_id, public_ip)
|
2009-11-12 01:22:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def disassociate
|
2010-01-08 14:29:07 -05:00
|
|
|
@server = nil
|
2010-11-19 16:45:45 -05:00
|
|
|
self.server_id = nil
|
2009-11-12 01:22:13 -05:00
|
|
|
unless new_record?
|
2010-11-30 19:43:28 -05:00
|
|
|
connection.disassociate_address(public_ip)
|
2009-11-12 01:22:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-13 01:33:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|