Sha256: ef0acf25dae177eb4a945a1972db7fe8d9ddb323cd3633e7b3fc0b441d5600f0

Contents?: true

Size: 1.33 KB

Versions: 50

Compression:

Stored size: 1.33 KB

Contents

require 'fog/model'

module Fog
  module AWS
    module EC2

      class Address < Fog::Model

        identity  :public_ip,   'publicIp'

        attribute :server_id, '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

50 entries across 50 versions & 2 rubygems

Version Path
fog-0.2.10 lib/fog/aws/models/ec2/address.rb
fog-0.2.9 lib/fog/aws/models/ec2/address.rb
fog-0.2.8 lib/fog/aws/models/ec2/address.rb
fog-0.2.7 lib/fog/aws/models/ec2/address.rb
fog-0.2.6 lib/fog/aws/models/ec2/address.rb
fog-0.2.5 lib/fog/aws/models/ec2/address.rb
fog-0.2.4 lib/fog/aws/models/ec2/address.rb
fog-0.2.3 lib/fog/aws/models/ec2/address.rb
fog-0.2.2 lib/fog/aws/models/ec2/address.rb
fog-0.2.1 lib/fog/aws/models/ec2/address.rb
fog-0.2.0 lib/fog/aws/models/ec2/address.rb
fog-0.1.10 lib/fog/aws/models/ec2/address.rb
fog-0.1.9 lib/fog/aws/models/ec2/address.rb
fog-0.1.8 lib/fog/aws/models/ec2/address.rb
fog-0.1.7 lib/fog/aws/models/ec2/address.rb
fog-0.1.6 lib/fog/aws/models/ec2/address.rb
fog-0.1.5 lib/fog/aws/models/ec2/address.rb
fog-0.1.4 lib/fog/aws/models/ec2/address.rb
fog-0.1.3 lib/fog/aws/models/ec2/address.rb
fog-0.1.2 lib/fog/aws/models/ec2/address.rb