blib/lib/VM/EC2/Volume.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 20 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 153 CC 6 SCAR 12.5
CC
6
Cov
0%
create_snapshot
6.0
attach
6.0
current_status_async
6.0
CRAP 3.5
1 package VM::EC2::Volume;
2  
3 =head1 NAME
4  
5 VM::EC2::Volume - Object describing an Amazon EBS volume
6  
7 =head1 SYNOPSIS
8  
9 use VM::EC2;
10  
11 $ec2 = VM::EC2->new(...);
12 @vol = $ec2->describe_volumes;
13 for my $vol (@vols) {
14 $id = $vol->volumeId;
15 $size = $vol->size;
16 $snap = $vol->snapshotId;
17 $zone = $vol->availabilityZone;
18 $status = $vol->status;
19 $ctime = $vol->createTime;
20 @attachments = $vol->attachments;
21 $attachment = $vol->attachment;
22 $origin = $vol->from_snapshot;
23 @snapshots = $vol->to_snapshots;
24 }
25 $vols[0]->attach('i-12345','/dev/sdg1');
26 $vols[0]->deleteOnTermination('true');
27 $vols[0]->detach;
28 $vols[0]->create_snapshot('automatic snapshot')
29  
30 =head1 DESCRIPTION
31  
32 This object is used to describe an Amazon EBS volume. It is returned
33 by VM::EC2->describe_volumes().
34  
35 =head1 METHODS
36  
37 The following object methods are supported:
38
39 volumeId -- ID of this volume.
40 size -- Size of this volume (in GB).
41 snapshotId -- ID of snapshot this volume was created from.
42 availabilityZone -- Availability zone in which this volume resides.
43 status -- Volume state, one of "creating", "available",
44 "in-use", "deleting", "deleted", "error"
45 createTime -- Timestamp for when volume was created.
46 volumeType -- The volume type, one of "standard", "io1", or "gp2"
47 iops -- The number of I/O operations per second that the volume
48 supports, an integer between 100 and 4000. Only valid for
49 volumes of type "io1".
50 encrypted -- True if volume is encrypted.
51 tags -- Hashref containing tags associated with this group.
52 See L<VM::EC2::Generic>.
53  
54 In addition, this class provides several convenience functions:
55  
56 =head2 $attachment = $vol->attachment
57  
58 =head2 @attachments = $vol->attachments
59  
60 The attachment() method returns a
61 VM::EC2::BlockDevice::Attachment object describing the
62 attachment of this volume to an instance. If the volume is unused,
63 then this returns undef.
64  
65 The attachments() method is similar, except that it returns a list of
66 the attachments. Currently an EBS volume can only be attached to one
67 instance at a time, but the Amazon call syntax supports multiple
68 attachments and this method is provided for future compatibility.
69  
70 =head2 $attachment = $vol->attach($instance,$device)
71  
72 =head2 $attachment = $vol->attach(-instance_id=>$instance,-device=>$device)
73  
74 Attach this volume to an instance using virtual device $device. Both
75 arguments are required. The result is a
76 VM::EC2::BlockDevice::Attachment object which you can monitor by
77 calling current_status():
78  
79 my $a = $volume->attach('i-12345','/dev/sdg');
80 while ($a->current_status ne 'attached') {
81 sleep 2;
82 }
83 print "volume is ready to go\n";
84  
85 =head2 $attachment = $volume->detach()
86  
87 =head2 $attachment = $volume->detach(-instance_id=>$instance_id,
88 -device=>$device,
89 -force=>$force);
90  
91 Detaches this volume. With no arguments, will detach the volume from
92 whatever instance it is currently attached to. Provide -instance_id
93 and/or -device as a check that you are detaching the volume from the
94 expected instance and device.
95  
96 Optional arguments:
97  
98 -instance_id -- ID of the instance to detach from.
99 -device -- How the device is exposed to the instance.
100 -force -- Force detachment, even if previous attempts were
101 unsuccessful.
102  
103 The result is a VM::EC2::BlockDevice::Attachment object which
104 you can monitor by calling current_status():
105  
106 my $a = $volume->detach;
107 while ($a->current_status ne 'detached') {
108 sleep 2;
109 }
110 print "volume is ready to go\n";
111  
112 =head2 $boolean = $vol->deleteOnTermination([$boolean])
113  
114 Get or set the deleteOnTermination flag for attached volumes. If the volume
115 is unattached, then this causes a fatal error. Called with no arguments, this
116 method returns the current state of the deleteOnTermination flag for the
117 volume's attachment. Called with a true/false argument, the method sets the
118 flag by calling modify_instance_attributes() on the corresponding instance
119 and returns true if successful.
120  
121 =head2 $snap = $vol->from_snapshot
122  
123 Returns the VM::EC2::Snapshot object that this volume was
124 originally derived from. It will return undef if the resource no
125 longer exists, or if the volume was created from scratch.
126  
127 =head2 @snap = $vol->to_snapshots
128  
129 If this volume has been used to create one or more snapshots, this
130 method will return them as a list of VM::EC2::Snapshot objects.
131  
132 =head2 $snapshot = $vol->create_snapshot('Description')
133  
134 Create a snapshot of the volume and return a VM::EC2::Snapshot
135 object. To ensure a consistent snapshot, you should unmount the volume
136 before snapshotting it. The optional argument allows you to add a description to the snapshot.
137  
138 Here is an example:
139  
140 $s = $volume->create_snapshot("Backed up at ".localtime);
141 while ($s->current_status eq 'pending') {
142 print "Progress: ",$s->progress,"% done\n";
143 }
144 print "Snapshot status: ",$s->current_status,"\n";
145  
146 =head2 $status = $vol->current_status
147  
148 This returns the up-to-date status of the volume. It works by calling
149 refresh() and then returning status().
150  
151 =head2 $boolean = $vol->auto_enable_io([$new_boolean])
152  
153 Get or set the auto-enable IO flag.
154  
155 =head2 $boolean = $vol->enable_volume_io()
156  
157 Enable volume I/O after it has been disabled by an Amazon health
158 check. If successful, the call will return true.
159  
160 =head1 STRING OVERLOADING
161  
162 When used in a string context, this object will interpolate the
163 volumeId.
164  
165 =head1 SEE ALSO
166  
167 L<VM::EC2>
168 L<VM::EC2::Generic>
169 L<VM::EC2::Snapshot>
170 L<VM::EC2::BlockDevice>
171 L<VM::EC2::BlockDevice::Attachment>
172  
173 =head1 AUTHOR
174  
175 Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>.
176  
177 Copyright (c) 2011 Ontario Institute for Cancer Research
178  
179 This package and its accompanying libraries is free software; you can
180 redistribute it and/or modify it under the terms of the GPL (either
181 version 1, or at your option, any later version) or the Artistic
182 License 2.0. Refer to LICENSE for the full license text. In addition,
183 please see DISCLAIMER.txt for disclaimers of warranty.
184  
185 =cut
186  
187 use strict;
188 use base 'VM::EC2::Generic';
189 use VM::EC2::BlockDevice::Attachment;
190 use VM::EC2::ProductCode;
191 use Carp 'croak';
192  
193 sub valid_fields {
194 my $self = shift;
195 return qw(volumeId size snapshotId availabilityZone status
196 createTime attachmentSet volumeType iops encrypted tagSet);
197 }
198  
199 sub primary_id {shift->volumeId}
200  
201 sub attachment {
202 my $self = shift;
203 my $attachments = $self->attachmentSet or return;
204 my $id = $attachments->{item}[0] or return;
205 return VM::EC2::BlockDevice::Attachment->new($id,$self->aws)
206 }
207  
208 sub attachments {
209 my $self = shift;
210 my $attachments = $self->attachmentSet or return;
211 my $items = $self->attachmentSet->{item} or return;
212 my @a = map {VM::EC2::BlockDevice::Attachment->new($_,$self->aws)} @$items;
213 return @a;
214 }
215  
216 sub deleteOnTermination {
217 my $self = shift;
218 $self->refresh;
219 my $attachment = $self->attachment or croak "$self is not attached";
220 return $attachment->deleteOnTermination(@_);
221 }
222  
223 sub from_snapshot {
224 my $self = shift;
225 my $sid = $self->snapshotId or return;
226 return $self->aws->describe_snapshots(-filter=>{'snapshot-id' => $sid});
227 }
228  
229 sub to_snapshots {
230 my $self = shift;
231 return $self->aws->describe_snapshots(-filter=>{'volume-id' => $self->volumeId});
232 }
233  
234 sub create_snapshot {
235 my $self = shift;
236 my $description = shift;
237 my @param = (-volume_id=>$self->volumeId);
238 push @param,(-description=>$description) if defined $description;
239 return $self->aws->create_snapshot(@param);
240 }
241  
242 sub attach {
243 my $self = shift;
244 my %args;
245 if (@_==2 && $_[0] !~ /^-/) {
246 @args{'-instance_id','-device'} = @_;
247 } else {
248 %args = @_;
249 }
250 $args{-instance_id} && $args{-device}
251 or croak "usage: \$vol->attach(\$instance_id,\$device)";
252 $args{-volume_id} = $self->volumeId;
253 return $self->aws->attach_volume(%args);
254 }
255  
256 sub detach {
257 my $self = shift;
258 return $self->aws->detach_volume(-volume_id=>$self->volumeId,@_);
259 }
260  
261 sub current_status {
262 my $self = shift;
263 $self->refresh;
264 $self->status;
265 }
266  
267 sub current_status_async {
268 my $self = shift;
269 my $to_caller = VM::EC2->condvar;
270  
271 my $cv = $self->aws->describe_volumes_async($self->volumeId);
272  
273 $cv->cb(sub {
274 my $i = shift->recv;
275 if ($i) {
276 $to_caller->send($i->status);
277 } else {
278 $to_caller->send;
279 }
280 });
281  
282 return $to_caller;
283 }
284  
285  
286 sub refresh {
287 my $self = shift;
288 local $self->aws->{raise_error} = 1;
289 my $v = $self->aws->describe_volumes($self->volumeId);
290 %$self = %$v if $v;
291 return defined $v;
292 }
293  
294 sub auto_enable_io {
295 my $self = shift;
296 return $self->aws->modify_volume_attribute($self,
297 -auto_enable_io => shift) if @_;
298 return $self->aws->describe_volume_attribute($self,'autoEnableIO');
299 }
300  
301 sub enable_io {
302 my $self = shift;
303 $self->aws->enable_volume_io($self);
304 }
305  
306 sub product_codes {
307 my $self = shift;
308 my @codes = $self->aws->describe_volume_attribute($self,'productCodes');
309 return map {VM::EC2::ProductCode->new($_,$self->aws)} @codes;
310 }
311  
312 sub encrypted {
313 my $self = shift;
314 my $enc = $self->SUPER::encrypted;
315 return $enc eq 'true';
316 }
317  
318 1;