Sha256: 0bafbe5597f0366f147d837d772750c4d96875287d56a7f21e6863d88fba5d07

Contents?: true

Size: 1.94 KB

Versions: 21

Compression:

Stored size: 1.94 KB

Contents

require 'fog/model'

module Fog
  module AWS
    module EC2

      class Volume < Fog::Model

        identity  :id,         'volumeId'

        attribute :attached_at,       'attachTime'
        attribute :availability_zone, 'availabilityZone'
        attribute :created_at,        'createTime'
        attribute :device
        attribute :server_id,         'instanceId'
        attribute :size
        attribute :snapshot_id,       'snapshotId'
        attribute :status
        
        def initialize(attributes = {})
          if attributes['attachmentSet']
            attributes.merge!(attributes.delete('attachmentSet').first || {})
          end
          super
        end

        def destroy
          requires :id

          connection.delete_volume(@id)
          true
        end

        def server=(new_server)
          requires :device

          if new_server
            attach(new_server)
          else
            detach
          end
        end

        def save
          requires :availability_zone, :size

          data = connection.create_volume(@availability_zone, @size, @snapshot_id).body
          new_attributes = data.reject {|key,value| key == 'requestId'}
          merge_attributes(new_attributes)
          if @server
            self.server = @server
          end
          true
        end

        def snapshots
          requires :id

          connection.snapshots(:volume => self)
        end

        private

        def attach(new_server)
          if new_record?
            @server = new_server
            @availability_zone = new_server.availability_zone
          elsif new_server
            @server = nil
            @server_id = new_server.id
            connection.attach_volume(@server_id, @id, @device)
          end
        end

        def detach
          @server = nil
          @server_id = nil
          unless new_record?
            connection.detach_volume(@id)
          end
        end

      end

    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
fog-0.0.77 lib/fog/aws/models/ec2/volume.rb
fog-0.0.76 lib/fog/aws/models/ec2/volume.rb
fog-0.0.75 lib/fog/aws/models/ec2/volume.rb
fog-0.0.74 lib/fog/aws/models/ec2/volume.rb
fog-0.0.73 lib/fog/aws/models/ec2/volume.rb
fog-0.0.72 lib/fog/aws/models/ec2/volume.rb
fog-0.0.71 lib/fog/aws/models/ec2/volume.rb
fog-0.0.70 lib/fog/aws/models/ec2/volume.rb
fog-0.0.69 lib/fog/aws/models/ec2/volume.rb
fog-0.0.68 lib/fog/aws/models/ec2/volume.rb
fog-0.0.67 lib/fog/aws/models/ec2/volume.rb
fog-0.0.66 lib/fog/aws/models/ec2/volume.rb
fog-0.0.65 lib/fog/aws/models/ec2/volume.rb
fog-0.0.64 lib/fog/aws/models/ec2/volume.rb
fog-0.0.63 lib/fog/aws/models/ec2/volume.rb
fog-0.0.62 lib/fog/aws/models/ec2/volume.rb
fog-0.0.61 lib/fog/aws/models/ec2/volume.rb
fog-0.0.60 lib/fog/aws/models/ec2/volume.rb
fog-0.0.59 lib/fog/aws/models/ec2/volume.rb
fog-0.0.58 lib/fog/aws/models/ec2/volume.rb