Sha256: ff7aa478814fdb90a7e3dc4097d649db81d048136088adda437b63b399a6e9a0

Contents?: true

Size: 1.46 KB

Versions: 15

Compression:

Stored size: 1.46 KB

Contents

require 'fog/core/model'

module Fog
  module AWS
    class Compute

      class Address < Fog::Model

        identity  :public_ip, :aliases => 'publicIp'

        attribute :server_id, :aliases => 'instanceId'

        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 save
          raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
          data = connection.allocate_address
          @public_ip = data.body['publicIp']
          if @server
            self.server = @server
          end
          true
        end

        private

        def associate(new_server)
          if new_record?
            @server = new_server
          else
            @server = nil
            @server_id = new_server.id
            connection.associate_address(@server_id, @public_ip)
          end
        end

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

      end

    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
fog-0.3.22 lib/fog/aws/models/compute/address.rb
fog-0.3.21 lib/fog/aws/models/compute/address.rb
fog-0.3.20 lib/fog/aws/models/compute/address.rb
fog-0.3.19 lib/fog/aws/models/compute/address.rb
fog-0.3.18 lib/fog/aws/models/compute/address.rb
fog-0.3.17 lib/fog/aws/models/compute/address.rb
fog-0.3.16 lib/fog/aws/models/compute/address.rb
fog-0.3.15 lib/fog/aws/models/compute/address.rb
fog-0.3.14 lib/fog/aws/models/compute/address.rb
fog-0.3.13 lib/fog/aws/models/compute/address.rb
fog-0.3.12 lib/fog/aws/models/compute/address.rb
fog-0.3.11 lib/fog/aws/models/compute/address.rb
fog-0.3.10 lib/fog/aws/models/compute/address.rb
fog-0.3.9 lib/fog/aws/models/compute/address.rb
fog-0.3.8 lib/fog/aws/models/compute/address.rb