Class: VGH::EC2::Volume

Inherits:
Object
  • Object
show all
Defined in:
lib/vgh/ec2/volume.rb

Overview

Collects information about the EBS volumes attached to the current instance.

Usage

volumes = Volume.new
puts volumes.list
puts volumes.list_tagged('MyTag')

Instance Method Summary collapse

Instance Method Details

#instanceInstance

The current instance object

Returns:

  • (Instance)

    An instance object



44
45
46
# File 'lib/vgh/ec2/volume.rb', line 44

def instance
  @instance ||= ec2.instances[instance_id]
end

#listArray

Creates an array with the IDs of all the volumes attached to the current instance

Returns:

  • (Array)


15
16
17
18
19
# File 'lib/vgh/ec2/volume.rb', line 15

def list
  @list = []
  mappings.map {|device, info| @list.push(info.volume.id)}
  return @list
end

#list_tagged(tag_key) ⇒ Array

Creates an array with the IDs of all the volumes attached to the current instance, that contain a specific tag key

Parameters:

  • tag_key (String)

    The Tag to look for

Returns:

  • (Array)


25
26
27
28
29
30
31
32
33
# File 'lib/vgh/ec2/volume.rb', line 25

def list_tagged(tag_key)
  @list_tagged = []
  list.each {|vid|
    volume_tags(vid).map {|volume_tag|
      @list_tagged.push(vid) if volume_tag.key == tag_key
    }
  }
  return @list_tagged
end

#mappingsHash

Returns a Hash containing the block device mappings of the current instance.

Returns:

  • (Hash)


37
38
39
40
# File 'lib/vgh/ec2/volume.rb', line 37

def mappings
  message.info "Creating a list of volumes..."
  @mappings ||= instance.block_device_mappings
end

#name_tag(volume_id) ⇒ String

Get volume’s Name tag

Parameters:

  • volume_id (String)

    The ID of the volume

Returns:

  • (String)

    The tag of the volume or (NOTAG) if a tag does not exists.



51
52
53
54
55
56
57
# File 'lib/vgh/ec2/volume.rb', line 51

def name_tag(volume_id)
  @name_tag = 'NOTAG'
  volume_tags(volume_id).each {|tag|
    @name_tag = tag.value if tag.key == 'Name'
  }
  return @name_tag
end

#volume_tags(volume_id) ⇒ TagsCollection

Returns a collection of tags for the specified volume.

Parameters:

  • volume_id (String)

    The id of the volume to query for tags.

Returns:

  • (TagsCollection)

    An array of tag objects



62
63
64
65
66
# File 'lib/vgh/ec2/volume.rb', line 62

def volume_tags(volume_id)
  @volume_tags = ec2.tags.
    filter('resource-type', 'volume').
    filter('resource-id', volume_id)
end