Sha256: 6f75411da0fbda717a21da98ba93c4e17a9a63281f9bb014ce821afa1f4f9734

Contents?: true

Size: 1.86 KB

Versions: 12

Compression:

Stored size: 1.86 KB

Contents

require 'fog/core/model'

module Fog
  module Compute
    class AWS

      class Address < Fog::Model

        identity  :public_ip,            :aliases => 'publicIp'

        attribute :allocation_id,        :aliases => 'allocationId'
        attribute :server_id,            :aliases => 'instanceId'
        attribute :network_interface_id, :aliases => 'networkInterfaceId'
        attribute :domain

        def initialize(attributes = {})
          # assign server first to prevent race condition with new_record?
          self.server = attributes.delete(:server)
          super
        end

        def destroy
          requires :public_ip

          connection.release_address(allocation_id || public_ip)
          true
        end

        def server=(new_server)
          if new_server
            associate(new_server)
          else
            disassociate
          end
        end

        def server
          connection.servers.get(server_id)
        end

        def save
          raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
          data = connection.allocate_address(domain).body
          new_attributes = data.reject {|key,value| key == 'requestId'}
          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.server_id = new_server.id
            connection.associate_address(server_id, public_ip, network_interface_id, allocation_id)
          end
        end

        def disassociate
          @server = nil
          self.server_id = nil
          unless new_record?
            connection.disassociate_address(public_ip)
          end
        end

      end

    end
  end
end

Version data entries

12 entries across 12 versions & 7 rubygems

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