1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/openstack/models/compute/address.rb
Paul Thornthwaite 40c0cd7122 Make use of #persisted? method
In many places we were checking for identity which was the shorthand for
checking if the resource had been saved by the service.

The #persisted? method was added to show a clearer intent and also offer
minimal ActiveModel interface
2012-12-23 02:45:05 +00:00

71 lines
1.6 KiB
Ruby

require 'fog/core/model'
module Fog
module Compute
class OpenStack
class Address < Fog::Model
identity :id
attribute :ip
attribute :pool
attribute :fixed_ip
attribute :instance_id
def initialize(attributes = {})
# assign server first to prevent race condition with persisted?
self.server = attributes.delete(:server)
super
end
def destroy
requires :id
connection.release_address(id)
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 persisted?
data = connection.allocate_address(pool).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)
unless persisted?
@server = new_server
else
@server = nil
self.instance_id = new_server.id
connection.associate_address(instance_id, ip)
end
end
def disassociate
@server = nil
if persisted?
connection.disassociate_address(instance_id, ip)
end
self.instance_id = nil
end
end
end
end
end