| 1 | package VM::EC2::BlockDevice::Attachment; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | VM::EC2::BlockDevice::Attachment - Object describing the attachment of an EBS volume to an instance | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use VM::EC2; | ||
| 10 | |||
| 11 | $ec2 = VM::EC2->new(...); | ||
| 12 | $volume = $ec2->describe_volumes(-volume_id=>'vol-12345'); | ||
| 13 | $attachment = $ec2->attachment; | ||
| 14 | |||
| 15 | $volId = $attachment->volumeId; | ||
| 16 | $device = $attachment->device; | ||
| 17 | $instanceId = $attachment->instanceId; | ||
| 18 | $status = $attachment->status; | ||
| 19 | $time = $attachment->attachTime; | ||
| 20 | $delete = $attachment->deleteOnTermination; | ||
| 21 | $attachment->deleteOnTermination(1); # change delete flag | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This object is used to describe the attachment of an Amazon EBS volume | ||
| 26 | to an instance. It is returned by VM::EC2::Volume->attachment(). | ||
| 27 | |||
| 28 | =head1 METHODS | ||
| 29 | |||
| 30 | The following object methods are supported: | ||
| 31 | |||
| 32 | volumeId -- ID of the volume. | ||
| 33 | instanceId -- ID of the instance | ||
| 34 | status -- Attachment state, one of "attaching", "attached", | ||
| 35 | "detaching", "detached". | ||
| 36 | attachTime -- Timestamp for when volume was attached | ||
| 37 | deleteOnTermination -- True if the EBS volume will be deleted when its | ||
| 38 | attached instance terminates. Note that this is a | ||
| 39 | Perl true, and not the string "true". | ||
| 40 | |||
| 41 | The deleteOnTermination method is slightly more sophisticated than | ||
| 42 | the result from the standard AWS API because it returns the CURRENT | ||
| 43 | deleteOnTermination flag for the attachment, which might have been | ||
| 44 | changed by VM::EC2->modify_instance_attributes(). You may also change | ||
| 45 | the deleteOnTermination state by passing a boolean argument to the | ||
| 46 | method: | ||
| 47 | |||
| 48 | $attachment->deleteOnTermination(1); | ||
| 49 | |||
| 50 | In addition, this class provides several convenience functions: | ||
| 51 | |||
| 52 | =head2 $instance = $attachment->instance | ||
| 53 | |||
| 54 | Returns the VM::EC2::Instance corresponding to this attachment. | ||
| 55 | |||
| 56 | =head2 $volume = $attachment->volume | ||
| 57 | |||
| 58 | Returns the VM::EC2::Volume object corresponding to this | ||
| 59 | attachment. | ||
| 60 | |||
| 61 | =head2 $device = $attachment->deviceName | ||
| 62 | |||
| 63 | Alias for device() to be compatible with VM::EC2::BlockDevice::Mapping call. | ||
| 64 | |||
| 65 | =head2 $result = $attachment->deleteOnTermination($boolean) | ||
| 66 | |||
| 67 | Change the deleteOnTermination flag on this attachment. | ||
| 68 | |||
| 69 | =head2 $status = $attachment->current_status | ||
| 70 | |||
| 71 | Refreshes the information in the object and returns status(). | ||
| 72 | |||
| 73 | =head2 $attachment->refresh | ||
| 74 | |||
| 75 | Calls AWS to refresh the attachment information. | ||
| 76 | |||
| 77 | =head1 STRING OVERLOADING | ||
| 78 | |||
| 79 | When used in a string context, this object will interpolate into a | ||
| 80 | string of the format "volumeId=>instanceId". | ||
| 81 | |||
| 82 | =head1 SEE ALSO | ||
| 83 | |||
| 84 | L<VM::EC2> | ||
| 85 | L<VM::EC2::Generic> | ||
| 86 | L<VM::EC2::Instance> | ||
| 87 | L<VM::EC2::Volume> | ||
| 88 | |||
| 89 | =head1 AUTHOR | ||
| 90 | |||
| 91 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 92 | |||
| 93 | Copyright (c) 2011 Ontario Institute for Cancer Research | ||
| 94 | |||
| 95 | This package and its accompanying libraries is free software; you can | ||
| 96 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 97 | version 1, or at your option, any later version) or the Artistic | ||
| 98 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 99 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | use strict; | ||
| 104 | use base 'VM::EC2::Generic'; | ||
| 105 | |||
| 106 | sub valid_fields { | ||
| 107 | my $self = shift; | ||
| 108 | return qw(volumeId instanceId device status attachTime deleteOnTermination); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub primary_id { | ||
| 112 | my $self = shift; | ||
| 113 | return join ('=>',$self->volumeId,$self->instanceId); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub current_status { | ||
| 117 | my $self = shift; | ||
| 118 | my $v = $self->aws->describe_volumes($self->volumeId) or return; | ||
| 119 | my $a = $v->attachment or return 'detached'; | ||
| 120 | return $a->status; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub current_status_async { | ||
| 124 | my $self = shift; | ||
| 125 | my $to_caller = VM::EC2->condvar; | ||
| 126 | |||
| 127 | my $cv = $self->aws->describe_volumes_async($self->volumeId); | ||
| 128 | |||
| 129 | $cv->cb(sub { | ||
| 130 | my $v = shift->recv; | ||
| 131 | my $a = $v->attachment; | ||
| 132 | if ($a) { | ||
| 133 | $to_caller->send($a->status); | ||
| 134 | } else { | ||
| 135 | $to_caller->send('detached'); | ||
| 136 | } | ||
| 137 | |||
| 138 | }); | ||
| 139 | |||
| 140 | return $to_caller; | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | sub refresh { | ||
| 145 | my $self = shift; | ||
| 146 | my $v = $self->aws->describe_volumes($self->volumeId); | ||
| 147 | my $a = $v && $v->attachment; | ||
| 148 | %$self = %$a if $a; | ||
| 149 | return defined $a; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub deviceName { shift->device } | ||
| 153 | |||
| 154 | sub deleteOnTermination { | ||
| 155 | my $self = shift; | ||
| 156 | |||
| 157 | if (@_) { | ||
| 158 | my $deleteOnTermination = shift; | ||
| 159 | $deleteOnTermination ||= 0; | ||
| 160 | my $flag = $self->device.'='.$self->volumeId.":$deleteOnTermination"; | ||
| 161 | return $self->aws->modify_instance_attribute($self->instanceId,-block_devices=>$flag); | ||
| 162 | } | ||
| 163 | |||
| 164 | my $device = $self->device; | ||
| 165 | my $instance = $self->instance or die $self->aws->error_str; | ||
| 166 | my @mapping = $instance->blockDeviceMapping; | ||
| 167 | my ($map) = grep {$_ eq $device} @mapping; | ||
| 168 | $map or die "Didn't find blockDeviceMapping corresponding to this attachment"; | ||
| 169 | return $map->deleteOnTermination; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub instance { | ||
| 173 | my $self = shift; | ||
| 174 | return $self->{instance} if exists $self->{instance}; | ||
| 175 | my @i = $self->aws->describe_instances(-instance_id => $self->instanceId); | ||
| 176 | @i == 1 or die "describe_instances(-instance_id=>",$self->instanceId,") returned more than one volume"; | ||
| 177 | return $self->{instance} = $i[0]; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub volume { | ||
| 181 | my $self = shift; | ||
| 182 | return $self->{volume} if exists $self->{volume}; | ||
| 183 | my @i = $self->aws->describe_volumes(-volume_id => $self->volumeId); | ||
| 184 | @i == 1 or die "describe_volumes(-volume_id=>",$self->volumeId,") returned more than one volume"; | ||
| 185 | return $self->{volume} = $i[0]; | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | 1; |