From 5c0ad0143ecdb9b11f495a120205f50e8fb37a66 Mon Sep 17 00:00:00 2001 From: Hunter Nield Date: Thu, 16 Feb 2012 16:54:19 +0800 Subject: [PATCH] [openstack|compute] Added Address models --- lib/fog/openstack/compute.rb | 2 + lib/fog/openstack/models/compute/address.rb | 71 +++++++++++++++++++ lib/fog/openstack/models/compute/addresses.rb | 30 ++++++++ 3 files changed, 103 insertions(+) create mode 100644 lib/fog/openstack/models/compute/address.rb create mode 100644 lib/fog/openstack/models/compute/addresses.rb diff --git a/lib/fog/openstack/compute.rb b/lib/fog/openstack/compute.rb index ea1aa5d6a..5f866ae5b 100644 --- a/lib/fog/openstack/compute.rb +++ b/lib/fog/openstack/compute.rb @@ -10,6 +10,8 @@ module Fog recognizes :openstack_auth_token, :openstack_management_url, :persistent, :openstack_compute_service_name, :openstack_tenant model_path 'fog/openstack/models/compute' + model :address + collection :addresses model :flavor collection :flavors model :image diff --git a/lib/fog/openstack/models/compute/address.rb b/lib/fog/openstack/models/compute/address.rb new file mode 100644 index 000000000..b9dbb97b7 --- /dev/null +++ b/lib/fog/openstack/models/compute/address.rb @@ -0,0 +1,71 @@ +require 'fog/core/model' + +module Fog + module Compute + class OpenStack + + class Address < Fog::Model + + identity :id + + attribute :ip + attribute :fixed_ip + attribute :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 + + 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 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 + end +end diff --git a/lib/fog/openstack/models/compute/addresses.rb b/lib/fog/openstack/models/compute/addresses.rb new file mode 100644 index 000000000..6d60022e2 --- /dev/null +++ b/lib/fog/openstack/models/compute/addresses.rb @@ -0,0 +1,30 @@ +require 'fog/core/collection' +require 'fog/openstack/models/compute/address' + +module Fog + module Compute + class OpenStack + + class Addresses < Fog::Collection + + model Fog::Compute::OpenStack::Address + + def all + load(connection.list_addresses.body['floating_ips']) + end + + def get(address_id) + + if address = connection.get_address(address_id).body['floating_ip'] + new(address) + end + rescue Fog::Compute::OpenStack::NotFound + nil + end + + end + + end + end +end +