Sha256: 5150d0c447197d90e3039e38bacfbcf594ad939196a8eee887c0da6b4793be2f

Contents?: true

Size: 1.62 KB

Versions: 17

Compression:

Stored size: 1.62 KB

Contents

require 'fog/openstack/models/model'

module Fog
  module Compute
    class OpenStack
      class Address < Fog::OpenStack::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

17 entries across 17 versions & 2 rubygems

Version Path
fog-openstack-0.1.10 lib/fog/openstack/models/compute/address.rb
fog-openstack-0.1.9 lib/fog/openstack/models/compute/address.rb
fog-openstack-0.1.8 lib/fog/openstack/models/compute/address.rb
fog-openstack-0.1.7 lib/fog/openstack/models/compute/address.rb
fog-openstack-0.1.6 lib/fog/openstack/models/compute/address.rb
fog-openstack-0.1.5 lib/fog/openstack/models/compute/address.rb
fog-openstack-0.1.4 lib/fog/openstack/models/compute/address.rb
fog-openstack-0.1.3 lib/fog/openstack/models/compute/address.rb
fog-openstack-0.1.2 lib/fog/openstack/models/compute/address.rb
fog-openstack-0.1.1 lib/fog/openstack/models/compute/address.rb
fog-openstack-0.1.0 lib/fog/openstack/models/compute/address.rb
fog-1.37.0 lib/fog/openstack/models/compute/address.rb
fog-1.36.0 lib/fog/openstack/models/compute/address.rb
fog-1.35.0 lib/fog/openstack/models/compute/address.rb
fog-2.0.0.pre.0 lib/fog/openstack/models/compute/address.rb
fog-1.34.0 lib/fog/openstack/models/compute/address.rb
fog-1.33.0 lib/fog/openstack/models/compute/address.rb