Sha256: 3eda6e41a84413157f0e149402fe82b6c47a108dccaa3d4b767fed9ad58fc624

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

require 'rackspace-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(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

1 entries across 1 versions & 1 rubygems

Version Path
rackspace-fog-1.4.2 lib/rackspace-fog/aws/models/compute/address.rb