lib/VM/EC2/REST/ami.pm

filter: Statementstmt 8.0% 8 / 100 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 11.7% 2 / 17 Podpod 0.0% 0 / 8 totaltotal 4.9% 10 / 203 CC 29 SCAR 36.5
CC
29
Cov
5%
copy_image
90.0
register_image
72.0
create_image
30.0
CRAP 38.4
« lib/VM/EC2/REST/elastic_load_balancer.pm ↑ Summary lib/VM/EC2/REST/ebs.pm »
1 package VM::EC2::REST::ami;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use VM::EC2 ''; # important not to import anything!
Subroutine BEGIN: called CC=1 SCAR=0.0
5 package VM::EC2; # add methods to VM::EC2
6  
7 VM::EC2::Dispatch->register(
8 DescribeImages => 'fetch_items,imagesSet,VM::EC2::Image',
9 0 ▶ CreateImage => sub { shift->{imageId} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
10 0 ▶ RegisterImage => sub { shift->{imageId} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
11 DeregisterImage => 'boolean',
12 ModifyImageAttribute => 'boolean',
13 ResetImageAttribute => 'boolean',
14 0 ▶ CopyImage => sub { shift->{imageId} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
15 7 );
16  
17 =head1 NAME VM::EC2::REST::ami
18  
19 =head1 SYNOPSIS
20  
21 use VM::EC2 ':standard';
22  
23 =head1 METHODS
24  
25 These are methods that allow you to fetch and manipulate Amazon Machine Images.
26  
27 Implemented:
28 CopyImage
29 CreateImage
30 DeregisterImage
31 DescribeImageAttribute
32 DescribeImages
33 ModifyImageAttribute
34 RegisterImage
35 ResetImageAttribute
36  
37 Unimplemented:
38 (none)
39  
40 =head1 EC2 AMAZON MACHINE IMAGES
41  
42 The methods in this section allow you to query and manipulate Amazon
43 machine images (AMIs). See L<VM::EC2::Image>.
44  
45 =head2 @i = $ec2->describe_images(@image_ids)
46  
47 =head2 @i = $ec2->describe_images(-image_id=>\@id,-executable_by=>$id,
48 -owner=>$id, -filter=>\%filters)
49  
50 Return a series of VM::EC2::Image objects, each describing an
51 AMI. Optional arguments:
52  
53 -image_id The id of the image, either a string scalar or an
54 arrayref.
55  
56 -executable_by Filter by images executable by the indicated user account, or
57 one of the aliases "self" or "all".
58  
59 -owner Filter by owner account number or one of the aliases "self",
60 "aws-marketplace", "amazon" or "all".
61  
62 -filter Tags and other filters to apply
63  
64 If there are no other arguments, you may omit the -filter argument
65 name and call describe_images() with a single hashref consisting of
66 the search filters you wish to apply.
67  
68 The full list of image filters can be found at:
69 http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeImages.html
70  
71 =cut
72  
73 sub describe_images {
74 0 ▶ my $self = shift;
Subroutine describe_images: not called CC=2 SCAR=17.9
Pod undocumented
75 0 my %args = $self->args(-image_id=>@_);
76 0 my @params;
77 0 push @params,$self->list_parm($_,\%args) foreach qw(ExecutableBy ImageId Owner);
78 0 push @params,$self->filter_parm(\%args);
79 0 return $self->call('DescribeImages',@params);
80 }
81  
82 =head2 $image = $ec2->create_image(-instance_id=>$id,-name=>$name,%other_args)
83  
84 Create an image from an EBS-backed instance and return a
85 VM::EC2::Image object. The instance must be in the "stopped" or
86 "running" state. In the latter case, Amazon will stop the instance,
87 create the image, and then restart it unless the -no_reboot argument
88 is provided.
89  
90 Arguments:
91  
92 -instance_id ID of the instance to create an image from. (required)
93 -name Name for the image that will be created. (required)
94 -description Description of the new image.
95 -no_reboot If true, don't reboot the instance.
96 -block_device_mapping
97 Block device mapping as a scalar or array ref. See
98 run_instances() for the syntax.
99 -block_devices Alias of the above
100  
101 =cut
102  
103 sub create_image {
104 0 ▶ my $self = shift;
Subroutine create_image: not called CC=5 SCAR=34.0
Pod undocumented
105 0 my %args = @_;
106 $args{-instance_id} && $args{-name}
107 0 ▶ or croak "Usage: create_image(-instance_id=>\$id,-name=>\$name)";
Branch: 1 branch
BranchTrueFalseTotal
$args{"-instance_id"} and $args{"-name"} or croak("Usage: create_image(-instance_id=>\$id,-name=>\$name)") 0 0 0
Condition0%0/3
$args{"-instance_id"} and $args{"-name"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"-instance_id"} and $args{"-name"}
$args{"-instance_id"} $args{"-name"}
Truth table0%0/3
A and B
A= $args{"-instance_id"}B= $args{"-name"}
ABresult
0X0
100
111
108 0 ▶ $args{-block_device_mapping} ||= $args{-block_devices};
Condition0%0/3
$args{"-block_device_mapping"} ||= $args{"-block_devices"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"-block_device_mapping"} ||= $args{"-block_devices"}
$args{"-block_device_mapping"} $args{"-block_devices"}
Truth table0%0/3
A ||= B
A= $args{"-block_device_mapping"}B= $args{"-block_devices"}
ABresult
1X1
011
000
109 0 my @param = $self->single_parm('InstanceId',\%args);
110 0 push @param,$self->single_parm('Name',\%args);
111 0 push @param,$self->single_parm('Description',\%args);
112 0 push @param,$self->boolean_parm('NoReboot',\%args);
113 0 push @param,$self->block_device_parm($args{-block_device_mapping});
114  
115 0 my $cv = $self->condvar;
116 {
117 0 local $VM::EC2::ASYNC = 1;
118 $self->call('CreateImage',@param)->cb(
119 sub {
120 0 ▶ my $img_id = shift->recv;
Subroutine __ANON__: not called CC=1 SCAR=6.9
121 0 my $timer; $timer = AnyEvent->timer(after => 0.5,
122 interval => 1,
123 cb => sub {
124 0 ▶ my $di = $self->describe_images_async($img_id);
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 $di->cb(sub {
126 0 ▶ if (my $img = shift->recv) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (my $img = (shift())->recv) 0 0 0
127 0 $cv->send($img);
128 0 undef $timer;
129 0 }});
130 0 });
131 0 });
132 }
133 0 ▶ return $VM::EC2::ASYNC ? $cv : $cv->recv;
Branch: 1 branch
BranchTrueFalseTotal
$VM::EC2::ASYNC ? : 0 0 0
134 }
135  
136 =head2 $image = $ec2->register_image(-name=>$name,%other_args)
137  
138 Register an image, creating an AMI. This can be used to create an AMI
139 from a S3-backed instance-store bundle, or to create an AMI from a
140 snapshot of an EBS-backed root volume.
141  
142 Required arguments:
143  
144 -name Name for the image that will be created.
145  
146 Arguments required for an EBS-backed image:
147  
148 -root_device_name The root device name, e.g. /dev/sda1
149 -block_device_mapping The block device mapping strings, including the
150 snapshot ID for the root volume. This can
151 be either a scalar string or an arrayref.
152 See run_instances() for a description of the
153 syntax.
154 -block_devices Alias of the above.
155  
156 Arguments required for an instance-store image:
157  
158 -image_location Full path to the AMI manifest in Amazon S3 storage.
159  
160 Common optional arguments:
161  
162 -description Description of the AMI
163 -architecture Architecture of the image ("i386" or "x86_64")
164 -kernel_id ID of the kernel to use
165 -ramdisk_id ID of the RAM disk to use
166 -virtualization_type Type of virtualization ("paravirtual" or "hvm")
167 Default is "paravirtual"
168 -sriov_net_support Set to "simple" to enable enhanced networking for the AMI
169
170 While you do not have to specify the kernel ID, it is strongly
171 recommended that you do so. Otherwise the kernel will have to be
172 specified for run_instances().
173  
174 Note: Immediately after registering the image you can add tags to it
175 and use modify_image_attribute to change launch permissions, etc.
176  
177 =cut
178  
179 sub register_image {
180 0 ▶ my $self = shift;
Subroutine register_image: not called CC=8 SCAR=42.8
Pod undocumented
181 0 my %args = @_;
182  
183 0 ▶ $args{-name} or croak "register_image(): -name argument required";
Branch: 1 branch
BranchTrueFalseTotal
$args{"-name"} or croak("register_image(): -name argument required") 0 0 0
184 0 ▶ $args{-block_device_mapping} ||= $args{-block_devices};
Condition0%0/3
$args{"-block_device_mapping"} ||= $args{"-block_devices"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"-block_device_mapping"} ||= $args{"-block_devices"}
$args{"-block_device_mapping"} $args{"-block_devices"}
Truth table0%0/3
A ||= B
A= $args{"-block_device_mapping"}B= $args{"-block_devices"}
ABresult
1X1
011
000
185 0 ▶ if (!$args{-image_location}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"-image_location"}) 0 0 0
186 $args{-root_device_name} && $args{-block_device_mapping}
187 0 ▶ or croak "register_image(): either provide -image_location to create an instance-store AMI\nor both the -root_device_name && -block_device_mapping arguments to create an EBS-backed AMI.";
Branch: 1 branch
BranchTrueFalseTotal
$args{"-root_device_name"} and $args{"-block_device_mapping"} or croak("register_image(): either provide -image_location to create an instance-store AMI\nor both the -root_device_name && -block_device_mapping arguments to create an EBS-backed AMI.") 0 0 0
Condition0%0/3
$args{"-root_device_name"} and $args{"-block_device_mapping"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"-root_device_name"} and $args{"-block_device_mapping"}
$args{"-root_device_name"} $args{"-block_device_mapping"}
Truth table0%0/3
A and B
A= $args{"-root_device_name"}B= $args{"-block_device_mapping"}
ABresult
0X0
100
111
188 }
189  
190 0 my @param;
191 0 for my $a (qw(Name RootDeviceName ImageLocation Description
192 Architecture KernelId RamdiskId VirtualizationType
193 SriovNetSupport)) {
194 0 push @param,$self->single_parm($a,\%args);
195 }
196 0 ▶ push @param,$self->block_device_parm($args{-block_devices} || $args{-block_device_mapping});
Condition0%0/3
$args{'-block_devices'} || $args{'-block_device_mapping'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'-block_devices'} || $args{'-block_device_mapping'}
$args{'-block_devices'} $args{'-block_device_mapping'}
Truth table0%0/3
A || B
A= $args{'-block_devices'}B= $args{'-block_device_mapping'}
ABresult
1X1
011
000
197  
198 0 return $self->call('RegisterImage',@param);
199 }
200  
201 =head2 $result = $ec2->deregister_image($image_id)
202  
203 Deletes the registered image and returns true if successful.
204  
205 =cut
206  
207 sub deregister_image {
208 0 ▶ my $self = shift;
Subroutine deregister_image: not called CC=1 SCAR=6.9
Pod undocumented
209 0 my %args = $self->args(-image_id => @_);
210 0 my @param = $self->single_parm(ImageId=>\%args);
211 0 return $self->call('DeregisterImage',@param);
212 }
213  
214 =head2 @data = $ec2->describe_image_attribute($image_id,$attribute)
215  
216 This method returns image attributes. Only one attribute can be
217 retrieved at a time. The following is the list of attributes that can be
218 retrieved:
219  
220 description -- scalar
221 kernel -- scalar
222 ramdisk -- scalar
223 launchPermission -- list of scalar
224 productCodes -- array
225 blockDeviceMapping -- list of hashref
226 sriovNetSupport -- scalar
227  
228 All of these values can be retrieved more conveniently from the
229 L<VM::EC2::Image> object returned from describe_images(), so there is
230 no attempt to parse the results of this call into Perl objects. In
231 particular, 'blockDeviceMapping' is returned as a raw hashrefs (there
232 also seems to be an AWS bug that causes fetching this attribute to return an
233 AuthFailure error).
234  
235 Please see the VM::EC2::Image launchPermissions() and
236 blockDeviceMapping() methods for more convenient ways to get this
237 data.
238  
239 =cut
240  
241 sub describe_image_attribute {
242 0 ▶ my $self = shift;
Subroutine describe_image_attribute: not called CC=3 SCAR=24.8
Pod undocumented
243 0 ▶ @_ == 2 or croak "Usage: describe_image_attribute(\$instance_id,\$attribute_name)";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or croak("Usage: describe_image_attribute(\$instance_id,\$attribute_name)") 0 0 0
244 0 my ($instance_id,$attribute) = @_;
245 0 my @param = (ImageId=>$instance_id,Attribute=>$attribute);
246 0 my $result = $self->call('DescribeImageAttribute',@param);
247 0 ▶ return $result && $result->attribute($attribute);
Condition0%0/3
$result && $result->attribute($attribute)
!ll&&!rl&&r
000
MC/DC0%0/2
$result && $result->attribute($attribute)
$result $result->attribute($attribute)
Truth table0%0/3
A && B
A= $resultB= $result->attribute($attribute)
ABresult
0X0
100
111
248 }
249  
250  
251 =head2 $boolean = $ec2->modify_image_attribute($image_id,-$attribute_name=>$value)
252  
253 This method changes image attributes. The first argument is the image
254 ID, and this is followed by the attribute name and the value to change
255 it to.
256  
257 The following is the list of attributes that can be set:
258  
259 -launch_add_user -- scalar or arrayref of UserIds to grant launch permissions to
260 -launch_add_group -- scalar or arrayref of Groups to remove launch permissions from
261 (only currently valid value is "all")
262 -launch_remove_user -- scalar or arrayref of UserIds to remove from launch permissions
263 -launch_remove_group -- scalar or arrayref of Groups to remove from launch permissions
264 -product_code -- scalar or array of product codes to add
265 -description -- scalar new description
266  
267 You can abbreviate the launch permission arguments to -add_user,
268 -add_group, -remove_user, -remove_group, etc.
269  
270 Only one attribute can be changed in a single request.
271  
272 For example:
273  
274 $ec2->modify_image_attribute('ami-12345',-product_code=>['abcde','ghijk']);
275  
276 The result code is true if the attribute was successfully modified,
277 false otherwise. In the latter case, $ec2->error() will provide the
278 error message.
279  
280 To make an image public, specify -launch_add_group=>'all':
281  
282 $ec2->modify_image_attribute('i-12345',-launch_add_group=>'all');
283  
284 Also see L<VM::EC2::Image> for shortcut methods. For example:
285  
286 $image->add_authorized_users(1234567,999991);
287  
288 =cut
289  
290 sub modify_image_attribute {
291 0 ▶ my $self = shift;
Subroutine modify_image_attribute: not called CC=4 SCAR=30.0
Pod undocumented
292 0 ▶ my $image_id = shift or croak "Usage: modify_image_attribute(\$imageId,%param)";
Branch: 1 branch
BranchTrueFalseTotal
my $image_id = shift() or croak("Usage: modify_image_attribute(\$imageId,%param)") 0 0 0
293 0 my %args = @_;
294  
295 # shortcuts
296 0 foreach (qw(add_user remove_user add_group remove_group)) {
297 0 ▶ $args{"-launch_$_"} ||= $args{"-$_"};
Condition0%0/3
$args{"-launch_$_"} ||= $args{"-$_"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"-launch_$_"} ||= $args{"-$_"}
$args{"-launch_$_"} $args{"-$_"}
Truth table0%0/3
A ||= B
A= $args{"-launch_$_"}B= $args{"-$_"}
ABresult
1X1
011
000
298 }
299  
300 0 my @param = (ImageId=>$image_id);
301 0 push @param,$self->value_parm('Description',\%args);
302 0 push @param,$self->list_parm('ProductCode',\%args);
303 0 push @param,$self->launch_perm_parm('Add','UserId', $args{-launch_add_user});
304 0 push @param,$self->launch_perm_parm('Remove','UserId',$args{-launch_remove_user});
305 0 push @param,$self->launch_perm_parm('Add','Group', $args{-launch_add_group});
306 0 push @param,$self->launch_perm_parm('Remove','Group', $args{-launch_remove_group});
307 0 return $self->call('ModifyImageAttribute',@param);
308 }
309  
310 =head2 $boolean = $ec2->reset_image_attribute($image_id,$attribute_name)
311  
312 This method resets an attribute of the given snapshot to its default
313 value. The valid attributes are:
314  
315 launchPermission
316  
317  
318 =cut
319  
320 sub reset_image_attribute {
321 0 ▶ my $self = shift;
Subroutine reset_image_attribute: not called CC=3 SCAR=24.8
Pod undocumented
322 0 ▶ @_ == 2 or
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or croak("Usage: reset_image_attribute(\$imageId,\$attribute_name)") 0 0 0
323 croak "Usage: reset_image_attribute(\$imageId,\$attribute_name)";
324 0 my ($image_id,$attribute) = @_;
325 0 my %valid = map {$_=>1} qw(launchPermission);
326 0 ▶ $valid{$attribute} or croak "attribute to reset must be one of ",join(' ',map{"'$_'"} keys %valid);
Branch: 1 branch
BranchTrueFalseTotal
$valid{$attribute} or croak("attribute to reset must be one of ", join(" ", map({"'${_}'";} keys %valid))) 0 0 0
327 0 return $self->call('ResetImageAttribute',
328 ImageId => $image_id,
329 Attribute => $attribute);
330 }
331  
332 =head2 $image = $ec2->copy_image(-source_region => $src,
333 -source_image_id => $id,
334 -name => $name,
335 -description => $desc,
336 -client_token => $token)
337  
338 Initiates the copy of an AMI from the specified source region to the
339 region in which the API call is executed.
340  
341 Required arguments:
342  
343 -source_region -- The ID of the AWS region that contains the AMI to be
344 copied (source).
345  
346 -source_image_id -- The ID of the Amazon EC2 AMI to copy.
347  
348 Optional arguments:
349  
350 -name -- The name of the new EC2 AMI in the destination region.
351  
352 -description -- A description of the new AMI in the destination region.
353  
354 -client_token -- Unique, case-sensitive identifier you provide to ensure
355 idempotency of the request.
356  
357 Returns a L<VM::EC2::Image> object on success;
358  
359 =cut
360  
361 sub copy_image {
362 0 ▶ my $self = shift;
Subroutine copy_image: not called CC=9 SCAR=45.0
Pod undocumented
363 0 my %args = @_;
364 0 ▶ $args{-description} ||= $args{-desc};
Condition0%0/3
$args{"-description"} ||= $args{"-desc"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"-description"} ||= $args{"-desc"}
$args{"-description"} $args{"-desc"}
Truth table0%0/3
A ||= B
A= $args{"-description"}B= $args{"-desc"}
ABresult
1X1
011
000
365 0 ▶ $args{-source_region} ||= $args{-region};
Condition0%0/3
$args{"-source_region"} ||= $args{"-region"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"-source_region"} ||= $args{"-region"}
$args{"-source_region"} $args{"-region"}
Truth table0%0/3
A ||= B
A= $args{"-source_region"}B= $args{"-region"}
ABresult
1X1
011
000
366 0 ▶ $args{-source_image_id} ||= $args{-image_id};
Condition0%0/3
$args{"-source_image_id"} ||= $args{"-image_id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"-source_image_id"} ||= $args{"-image_id"}
$args{"-source_image_id"} $args{"-image_id"}
Truth table0%0/3
A ||= B
A= $args{"-source_image_id"}B= $args{"-image_id"}
ABresult
1X1
011
000
367 0 ▶ $args{-source_region} or croak "copy_image(): -source_region argument required";
Branch: 1 branch
BranchTrueFalseTotal
$args{"-source_region"} or croak("copy_image(): -source_region argument required") 0 0 0
368 0 ▶ $args{-source_image_id} or croak "copy_image(): -source_image_id argument required";
Branch: 1 branch
BranchTrueFalseTotal
$args{"-source_image_id"} or croak("copy_image(): -source_image_id argument required") 0 0 0
369 0 my @params;
370 push @params, $self->single_parm($_,\%args)
371 0 foreach qw(SourceRegion SourceImageId Name Description ClientToken);
372 0 ▶ my $image_id = $self->call('CopyImage',@params) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $image_id = $self->call("CopyImage", @params) or return 0 0 0
373 0 return eval {
374 0 my $image;
375 0 ▶ local $SIG{ALRM} = sub {die "timeout"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
376 0 alarm(60);
377 0 ▶ until ($image = $self->describe_images($image_id)) { sleep 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($image = $self->describe_images($image_id)) 0 0 0
378 0 alarm(0);
379 0 $image;
380 };
381 }
382  
383  
384 =head1 SEE ALSO
385  
386 L<VM::EC2>
387  
388 =head1 AUTHOR
389  
390 Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>.
391  
392 Copyright (c) 2011 Ontario Institute for Cancer Research
393  
394 This package and its accompanying libraries is free software; you can
395 redistribute it and/or modify it under the terms of the GPL (either
396 version 1, or at your option, any later version) or the Artistic
397 License 2.0. Refer to LICENSE for the full license text. In addition,
398 please see DISCLAIMER.txt for disclaimers of warranty.
399  
400 =cut
401  
402  
403 7 1;
« lib/VM/EC2/REST/elastic_load_balancer.pm ↑ Summary lib/VM/EC2/REST/ebs.pm »