Sha256: 5180ae29248c0780aa4de4e8b5abc1d981f2494281b960c96e784565f2decde1

Contents?: true

Size: 1.6 KB

Versions: 71

Compression:

Stored size: 1.6 KB

Contents

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
          service.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 = service.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
            service.associate_address(instance_id, ip)
          end
        end

        def disassociate
          @server = nil
          if persisted?
            service.disassociate_address(instance_id, ip)
          end
          self.instance_id = nil
        end

      end

    end
  end
end

Version data entries

71 entries across 71 versions & 7 rubygems

Version Path
fog-1.22.0 lib/fog/openstack/models/compute/address.rb
fog-1.21.0 lib/fog/openstack/models/compute/address.rb
fog-maestrodev-1.20.0.20140305101839 lib/fog/openstack/models/compute/address.rb
fog-maestrodev-1.20.0.20140305101305 lib/fog/openstack/models/compute/address.rb
cifrado-0.2.1 vendor/fog/lib/fog/openstack/models/compute/address.rb
cifrado-0.2.0 vendor/fog/lib/fog/openstack/models/compute/address.rb
fog-maestrodev-1.19.0.20140212012611 lib/fog/openstack/models/compute/address.rb
fog-1.20.0 lib/fog/openstack/models/compute/address.rb
fog-maestrodev-1.19.0.20140110004459 lib/fog/openstack/models/compute/address.rb
fog-maestrodev-1.19.0.20140110003812 lib/fog/openstack/models/compute/address.rb
fog-maestrodev-1.19.0.20140109202555 lib/fog/openstack/models/compute/address.rb
fog-maestrodev-1.19.0.20140107192102 lib/fog/openstack/models/compute/address.rb
fog-maestrodev-1.19.0.20140107142106 lib/fog/openstack/models/compute/address.rb
fog-maestrodev-1.19.0.20131219203941 lib/fog/openstack/models/compute/address.rb
fog-maestrodev-1.18.0.20131219193542 lib/fog/openstack/models/compute/address.rb
fog-1.19.0 lib/fog/openstack/models/compute/address.rb
fog-maestrodev-1.18.0.20131219033443 lib/fog/openstack/models/compute/address.rb
fog-maestrodev-1.18.0.20131219032002 lib/fog/openstack/models/compute/address.rb
fog-maestrodev-1.18.0.20131219030716 lib/fog/openstack/models/compute/address.rb
fog-maestrodev-1.18.0.20131219022322 lib/fog/openstack/models/compute/address.rb