2010-03-16 18:46:21 -04:00
|
|
|
require 'fog/model'
|
|
|
|
|
2009-08-13 01:33:35 -04:00
|
|
|
module Fog
|
|
|
|
module AWS
|
2010-09-03 04:11:45 -04:00
|
|
|
class EC2
|
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
|
|
|
|
|
2009-08-13 01:33:35 -04: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
|
|
|
|
data = connection.allocate_address
|
|
|
|
@public_ip = data.body['publicIp']
|
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
|
|
|
|
@server_id = new_server.id
|
|
|
|
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
|
|
|
|
@server_id = nil
|
2009-11-12 01:22:13 -05:00
|
|
|
unless new_record?
|
|
|
|
connection.disassociate_address(@public_ip)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-13 01:33:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|