| 1 | package VM::EC2::BlockDevice::Mapping; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | VM::EC2::BlockDevice::Mapping - Object describing an EC2 block device attached to an instance | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use VM::EC2; | ||
| 10 | |||
| 11 | $ec2 = VM::EC2->new(...); | ||
| 12 | $instance = $ec2->describe_instances(-instance_id=>'i-123456'); | ||
| 13 | my @devices = $instance->blockDeviceMapping; | ||
| 14 | for my $dev (@devices) { | ||
| 15 | $dev = $dev->deviceName; | ||
| 16 | $volume_id = $dev->volumeId; | ||
| 17 | $status = $dev->status; | ||
| 18 | $atime = $dev->attachmentTime; | ||
| 19 | $delete = $dev->deleteOnTermination; | ||
| 20 | $volume = $dev->volume; | ||
| 21 | } | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This object represents an Amazon block device associated with an instance; | ||
| 26 | it is returned by Instance->blockDeviceMapping(). | ||
| 27 | |||
| 28 | Please see L<VM::EC2::Generic> for methods shared by all VM::EC2 | ||
| 29 | objects. | ||
| 30 | |||
| 31 | =head1 METHODS | ||
| 32 | |||
| 33 | These object methods are supported: | ||
| 34 | |||
| 35 | deviceName -- Name of the device, such as /dev/sda1. | ||
| 36 | instance -- Instance object associated with this volume. | ||
| 37 | ebs -- A VM::EC2::BlockDevice::Mapping::EBS object | ||
| 38 | describing the characteristics of the attached | ||
| 39 | EBS volume | ||
| 40 | |||
| 41 | For your convenience, a number of the ebs() object's methods are | ||
| 42 | passed through: | ||
| 43 | |||
| 44 | volumeId -- ID of the volume. | ||
| 45 | status -- One of "attaching", "attached", "detaching", "detached" | ||
| 46 | attachTime -- Time this volume was attached | ||
| 47 | deleteOnTermination -- Whether the volume will be deleted when its attached | ||
| 48 | instance is deleted. Note that this will return perl true/false | ||
| 49 | vales, rather than the strings "true" "false". | ||
| 50 | |||
| 51 | The deleteOnTermination() method can be used to retrieve or modify this flag: | ||
| 52 | |||
| 53 | # get current deleteOnTermination flag | ||
| 54 | my $current_flag = $dev->deleteOnTermination; | ||
| 55 | |||
| 56 | # if flag is true, then set it to false | ||
| 57 | if ($current_flag) { $dev->deleteOnTermination(0) } | ||
| 58 | |||
| 59 | In addition, the following convenience function is provided: | ||
| 60 | |||
| 61 | =head2 $volume = $dev->volume | ||
| 62 | |||
| 63 | This returns a VM::EC2::Volume object from which more | ||
| 64 | information about the volume, such as its size, can be derived. | ||
| 65 | |||
| 66 | =head1 STRING OVERLOADING | ||
| 67 | |||
| 68 | When used in a string context, this object will be interpolated as the | ||
| 69 | deviceName. | ||
| 70 | |||
| 71 | =head1 SEE ALSO | ||
| 72 | |||
| 73 | L<VM::EC2> | ||
| 74 | L<VM::EC2::Generic> | ||
| 75 | L<VM::EC2::BlockDevice> | ||
| 76 | L<VM::EC2::BlockDevice::Attachment> | ||
| 77 | L<VM::EC2::BlockDevice::Mapping::EBS> | ||
| 78 | L<VM::EC2::Volume> | ||
| 79 | |||
| 80 | =head1 AUTHOR | ||
| 81 | |||
| 82 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 83 | |||
| 84 | Copyright (c) 2011 Ontario Institute for Cancer Research | ||
| 85 | |||
| 86 | This package and its accompanying libraries is free software; you can | ||
| 87 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 88 | version 1, or at your option, any later version) or the Artistic | ||
| 89 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 90 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | use strict; | ||
| 95 | use base 'VM::EC2::Generic'; | ||
| 96 | use VM::EC2::BlockDevice::Mapping::EBS; | ||
| 97 | |||
| 98 | use overload '""' => sub {shift()->deviceName}, | ||
| 99 | fallback => 1; | ||
| 100 | |||
| 101 | sub valid_fields { | ||
| 102 | my $self = shift; | ||
| 103 | return qw(deviceName ebs); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub ebs { | ||
| 107 | my $self = shift; | ||
| 108 | return $self->{ebs} ||= VM::EC2::BlockDevice::Mapping::EBS->new($self->SUPER::ebs,$self->aws); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub instance { | ||
| 112 | my $self = shift; | ||
| 113 | my $d = $self->{instance}; | ||
| 114 | $self->{instance} = shift if @_; | ||
| 115 | return $d; | ||
| 116 | } | ||
| 117 | sub volumeId { shift->ebs->volumeId } | ||
| 118 | sub status { shift->ebs->status } | ||
| 119 | sub attachTime { shift->ebs->attachTime } | ||
| 120 | sub volume { shift->ebs->volume } | ||
| 121 | |||
| 122 | sub deleteOnTermination { | ||
| 123 | my $self = shift; | ||
| 124 | my $ebs = $self->ebs; | ||
| 125 | my $flag = $ebs->deleteOnTermination; | ||
| 126 | if (@_) { | ||
| 127 | my $deleteOnTermination = shift; | ||
| 128 | $deleteOnTermination ||= 0; | ||
| 129 | my $flag = $self->deviceName.'='.$self->volumeId.":$deleteOnTermination"; | ||
| 130 | return $self->aws->modify_instance_attribute($self->instance,-block_devices=>$flag); | ||
| 131 | } | ||
| 132 | return $flag; | ||
| 133 | } | ||
| 134 | |||
| 135 | 1; | ||
| 136 |