Sha256: 2604fec7db63afb7d54a66ce2d3df6b69ec64191d9bbbd4391c4b61d5417d2a0

Contents?: true

Size: 1.85 KB

Versions: 24

Compression:

Stored size: 1.85 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 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

24 entries across 24 versions & 6 rubygems

Version Path
gapinc-fog-1.12.1.2.1 lib/fog/aws/models/compute/address.rb
fog-1.15.0 lib/fog/aws/models/compute/address.rb
gapinc-fog-1.14.0 lib/fog/aws/models/compute/address.rb
fog-maestrodev-1.14.0.20130806165225 lib/fog/aws/models/compute/address.rb
fog-1.14.0 lib/fog/aws/models/compute/address.rb
fog-1.13.0 lib/fog/aws/models/compute/address.rb
gapinc-fog-1.12.1.2 lib/fog/aws/models/compute/address.rb
gapinc-fog-1.12.1.1 lib/fog/aws/models/compute/address.rb
gapinc-fog-1.12.1a lib/fog/aws/models/compute/address.rb
gapinc-fog-1.12.1 lib/fog/aws/models/compute/address.rb
fog-1.12.1 lib/fog/aws/models/compute/address.rb
fog-1.12.0 lib/fog/aws/models/compute/address.rb
hpfog-0.0.20 lib/fog/aws/models/compute/address.rb
vagrant-shell-0.2.6 vendor/bundle/gems/fog-1.10.1/lib/fog/aws/models/compute/address.rb
vagrant-shell-0.2.5 vendor/bundle/gems/fog-1.10.1/lib/fog/aws/models/compute/address.rb
fog-1.11.1 lib/fog/aws/models/compute/address.rb
fog-1.11.0 lib/fog/aws/models/compute/address.rb
fog-1.10.1 lib/fog/aws/models/compute/address.rb
fog-test-me-1.10.0 lib/fog/aws/models/compute/address.rb
fog-1.10.0 lib/fog/aws/models/compute/address.rb