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