1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Add server attribute to associate and disassociate a server to an address.

This commit is contained in:
Rupak Ganguly 2011-12-09 17:30:45 -05:00
parent 4d13bb0bd8
commit b6e70fab27

View file

@ -12,6 +12,14 @@ module Fog
attribute :fixed_ip
attribute :instance_id
attr_accessor :instance_id
def initialize(attributes = {})
# assign server first to prevent race condition with new_record?
self.server = attributes.delete(:server)
super
end
def destroy
requires :id
@ -19,14 +27,45 @@ module Fog
true
end
def server=(new_server)
if new_server
associate(new_server)
else
disassociate
end
end
def save
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
data = connection.allocate_address.body['floating_ip']
new_attributes = data.reject {|key,value| !['id', 'instance_id', 'ip', 'fixed_ip'].include?(key)}
merge_attributes(new_attributes)
if @server
self.server = @server
end
true
end
private
def associate(new_server)
if new_record?
@server = new_server
else
@server = nil
self.instance_id = new_server.id
connection.associate_address(instance_id, ip)
end
end
def disassociate
@server = nil
unless new_record?
connection.disassociate_address(instance_id, ip)
end
self.instance_id = nil
end
end
end