Sha256: ba06ade62dbdfd8c5443a36216762bb2ef1eb5b6e070c20f164c18933f962ec2

Contents?: true

Size: 1.6 KB

Versions: 23

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

23 entries across 23 versions & 4 rubygems

Version Path
vagrant-cloudstack-1.2.0 vendor/bundle/gems/fog-1.32.0/lib/fog/openstack/models/compute/address.rb
fog-1.32.0 lib/fog/openstack/models/compute/address.rb
fog-1.31.0 lib/fog/openstack/models/compute/address.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/fog-1.22.1/lib/fog/openstack/models/compute/address.rb
fog-1.30.0 lib/fog/openstack/models/compute/address.rb
fog-1.29.0 lib/fog/openstack/models/compute/address.rb
fog-1.28.0 lib/fog/openstack/models/compute/address.rb
fog-1.27.0 lib/fog/openstack/models/compute/address.rb
fog-1.26.0 lib/fog/openstack/models/compute/address.rb
fog-1.25.0 lib/fog/openstack/models/compute/address.rb
nsidc-fog-1.24.1 lib/fog/openstack/models/compute/address.rb
fog-1.24.0 lib/fog/openstack/models/compute/address.rb
ns-fog-1.22.11 lib/fog/openstack/models/compute/address.rb
ns-fog-1.22.10 lib/fog/openstack/models/compute/address.rb
ns-fog-1.22.9 lib/fog/openstack/models/compute/address.rb
ns-fog-1.22.8 lib/fog/openstack/models/compute/address.rb
ns-fog-1.22.7 lib/fog/openstack/models/compute/address.rb
ns-fog-1.22.6 lib/fog/openstack/models/compute/address.rb
fog-1.23.0 lib/fog/openstack/models/compute/address.rb
ns-fog-1.22.4 lib/fog/openstack/models/compute/address.rb