Sha256: c0b158296c157cf7243ea508a0c40d6cb6ddeac5e4c136ec52f3e546da6d9663

Contents?: true

Size: 1.36 KB

Versions: 8

Compression:

Stored size: 1.36 KB

Contents

require 'fog/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
          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

8 entries across 8 versions & 1 rubygems

Version Path
fog-0.3.7 lib/fog/aws/models/compute/address.rb
fog-0.3.6 lib/fog/aws/models/compute/address.rb
fog-0.3.5 lib/fog/aws/models/compute/address.rb
fog-0.3.4 lib/fog/aws/models/compute/address.rb
fog-0.3.3 lib/fog/aws/models/compute/address.rb
fog-0.3.2 lib/fog/aws/models/compute/address.rb
fog-0.3.1 lib/fog/aws/models/compute/address.rb
fog-0.3.0 lib/fog/aws/models/compute/address.rb