Sha256: 837e21bd424d9ee96511a01d41f9c373b3e44f396938483186e6bd56e02dfa8d

Contents?: true

Size: 1.61 KB

Versions: 13

Compression:

Stored size: 1.61 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 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(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)
          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

Version data entries

13 entries across 13 versions & 7 rubygems

Version Path
fog-nirvanix-1.8.2 lib/fog/openstack/models/compute/address.rb
fog-nirvanix-1.8.1 lib/fog/openstack/models/compute/address.rb
fog-parser-fix-1.6.1 lib/fog/openstack/models/compute/address.rb
fog-test-again-1.6.0 lib/fog/openstack/models/compute/address.rb
fog-parser-fix-1.6.0 lib/fog/openstack/models/compute/address.rb
ey-vendored-backup-3.0.27 vendor/gems/fog/lib/fog/openstack/models/compute/address.rb
fog-sgonyea-1.8.1 lib/fog/openstack/models/compute/address.rb
fog-1.8.0 lib/fog/openstack/models/compute/address.rb
fog-maestrodev-1.7.0.20121114190951 lib/fog/openstack/models/compute/address.rb
fog-1.7.0 lib/fog/openstack/models/compute/address.rb
fog-1.6.0 lib/fog/openstack/models/compute/address.rb
fog-1.5.0 lib/fog/openstack/models/compute/address.rb
fog-1.4.0 lib/fog/openstack/models/compute/address.rb