Sha256: 0dfdd4792816a57a63f7642e578fa3e95f8f45f28d2e4ea817613ea076e8d839

Contents?: true

Size: 1.93 KB

Versions: 7

Compression:

Stored size: 1.93 KB

Contents

module Fog
  module AWS
    class EC2

      class Volume < Fog::Model

        identity  :id,         'volumeId'

        attribute :attach_time,       'attachTime'
        attribute :availability_zone, 'availabilityZone'
        attribute :create_time,       'createTime'
        attribute :device
        attribute :instance_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 instance=(new_instance)
          if new_instance
            attach(new_instance)
          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 @instance
            self.instance = @instance
          end
          true
        end

        def snapshots
          requires :id

          connection.snapshots(:volume => self)
        end

        private

        def attach(new_instance)
          if new_record?
            @instance = new_instance
            @availability_zone = new_instance.availability_zone
          elsif new_instance
            @instance = nil
            @instance_id = new_instance.id
            connection.attach_volume(@instance_id, @id, @device)
          end
        end

        def detach
          @instance = nil
          @instance_id = nil
          unless new_record?
            connection.detach_volume(@id)
          end
        end

      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
fog-0.0.39 lib/fog/aws/models/ec2/volume.rb
fog-0.0.38 lib/fog/aws/models/ec2/volume.rb
fog-0.0.37 lib/fog/aws/models/ec2/volume.rb
fog-0.0.36 lib/fog/aws/models/ec2/volume.rb
fog-0.0.35 lib/fog/aws/models/ec2/volume.rb
fog-0.0.34 lib/fog/aws/models/ec2/volume.rb
fog-0.0.33 lib/fog/aws/models/ec2/volume.rb