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 = {})
|
2012-12-19 14:50:41 +00:00
|
|
|
# assign server first to prevent race condition with persisted?
|
2010-05-10 15:01:19 -07:00
|
|
|
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
|
|
|
|
|
2012-12-22 23:31:31 +00:00
|
|
|
service.release_address(allocation_id || 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
|
2012-07-18 12:11:31 -03:00
|
|
|
|
2012-04-18 15:56:00 -07:00
|
|
|
def server
|
2012-12-22 23:31:31 +00:00
|
|
|
service.servers.get(server_id)
|
2012-04-18 15:56:00 -07:00
|
|
|
end
|
2009-10-06 18:55:39 -07:00
|
|
|
|
2009-08-12 22:33:35 -07:00
|
|
|
def save
|
2012-12-23 02:45:05 +00:00
|
|
|
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
|
2012-12-22 23:31:31 +00:00
|
|
|
data = service.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)
|
2012-12-19 14:50:41 +00:00
|
|
|
unless persisted?
|
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-12-22 23:31:31 +00:00
|
|
|
service.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
|
2012-12-19 14:50:41 +00:00
|
|
|
if persisted?
|
2012-12-22 23:31:31 +00:00
|
|
|
service.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
|