Sha256: 5a8c46ca110a7cb4e046dbe896a2c9cad9d5670d624dec789b94de4fc634f81e

Contents?: true

Size: 1.91 KB

Versions: 15

Compression:

Stored size: 1.91 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 :association_id,       :aliases => 'associationId'
        attribute :server_id,            :aliases => 'instanceId'
        attribute :network_interface_id, :aliases => 'networkInterfaceId'
        attribute :domain

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

        def destroy
          requires :public_ip

          service.release_address(allocation_id || public_ip)
          true
        end

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

        def server
          service.servers.get(server_id)
        end

        def save
          raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
          data = service.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)
          unless persisted?
            @server = new_server
          else
            @server = nil
            self.server_id = new_server.id
            service.associate_address(server_id, public_ip, network_interface_id, allocation_id)
          end
        end

        def disassociate
          @server = nil
          self.server_id = nil
          if persisted?
            service.disassociate_address(public_ip)
          end
        end

      end

    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
fog-maestrodev-1.18.0.20131125111730 lib/fog/aws/models/compute/address.rb
fog-maestrodev-1.18.0.20131125083406 lib/fog/aws/models/compute/address.rb
fog-maestrodev-1.18.0.20131123105121 lib/fog/aws/models/compute/address.rb
fog-maestrodev-1.18.0.20131122203507 lib/fog/aws/models/compute/address.rb
fog-maestrodev-1.18.0.20131121075022 lib/fog/aws/models/compute/address.rb
fog-maestrodev-1.18.0.20131118164830 lib/fog/aws/models/compute/address.rb
fog-maestrodev-1.18.0.20131115184302 lib/fog/aws/models/compute/address.rb
fog-maestrodev-1.18.0.20131114200144 lib/fog/aws/models/compute/address.rb
fog-maestrodev-1.18.0.20131112185232 lib/fog/aws/models/compute/address.rb
fog-maestrodev-1.18.0.20131111203459 lib/fog/aws/models/compute/address.rb
fog-1.18.0 lib/fog/aws/models/compute/address.rb
fog-1.17.0 lib/fog/aws/models/compute/address.rb
fog-1.16.0 lib/fog/aws/models/compute/address.rb
fog-maestrodev-1.15.0.20130927082724 lib/fog/aws/models/compute/address.rb
fog-maestrodev-1.15.0.20130829165835 lib/fog/aws/models/compute/address.rb