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

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 30 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 234 CC 21 SCAR 33.9
CC
21
Cov
0%
AUTOLOAD
110.0
delete_tags
30.0
can
12.0
CRAP 29.7
1 package VM::EC2::Generic;
2  
3 =head1 NAME
4  
5 VM::EC2::Generic - Base class for VM::EC2 objects
6  
7 =head1 SYNOPSIS
8  
9 use VM::EC2;
10  
11 my $ec2 = VM::EC2->new(-access_key => 'access key id',
12 -secret_key => 'aws_secret_key',
13 -endpoint => 'http://ec2.amazonaws.com');
14  
15 my $object = $ec2->some_method(...);
16  
17 # getting data fields
18 my @field_names = $object->fields;
19  
20 # invoking data fields as methods
21 my $request_id = $object->requestId;
22 my $xmlns = $object->xmlns;
23  
24 # tagging
25 my $tags = $object->tags;
26  
27 if ($tags->{Role} eq 'WebServer') {
28 $object->delete_tags(Role=>undef);
29 $object->add_tags(Role => 'Web Server',
30 Status => 'development');
31 }
32  
33 # get the parsed XML object as a hash
34 my $hashref = $object->payload;
35  
36 # get the parsed XML object as a Data::Dumper string
37 my $text = $object->as_string;
38  
39 # get the VM::EC2 object back
40 my $ec2 = $object->ec2;
41  
42 # get the most recent error string
43 warn $object->error_str;
44  
45 =head1 DESCRIPTION
46  
47 This is a common base class for objects returned from VM::EC2. It
48 provides a number of generic methods that are used in subclasses, but
49 is not intended to be used directly.
50  
51 =head1 METHODS
52  
53 =cut
54  
55 use strict;
56 use Carp 'croak';
57 use Data::Dumper;
58 use VM::EC2 'tag';
59  
60 our $AUTOLOAD;
61 $Data::Dumper::Terse++;
62 $Data::Dumper::Indent=1;
63  
64 use overload
65 '""' => sub {my $self = shift;
66 return $self->short_name;
67 },
68 fallback => 1;
69  
70 sub AUTOLOAD {
71 my $self = shift;
72 my ($pack,$func_name) = $AUTOLOAD=~/(.+)::([^:]+)$/;
73 return if $func_name eq 'DESTROY';
74 my %fields = map {$_=>1} $self->valid_fields;
75 my $mixed = VM::EC2->uncanonicalize($func_name);# mixedCase
76 my $flat = VM::EC2->canonicalize($func_name); # underscore_style
77 $flat =~ s/^-//;
78  
79 if ($mixed eq $flat) {
80 return $self->{data}{$mixed} if $fields{$mixed};
81 return $self->{data}{ucfirst $mixed} if $fields{ucfirst $mixed};
82 croak "Can't locate object method \"$func_name\" via package \"$pack\"";
83 }
84  
85 if ($func_name eq $flat && $self->can($mixed)) {
86 return $self->$mixed(@_);
87 } elsif ($func_name eq $mixed && $self->can($flat)) {
88 return $self->$flat(@_);
89 } elsif ($fields{$mixed}) {
90 return $self->{data}{$mixed} if $fields{$mixed};
91 } elsif ($fields{ucfirst($mixed)}) {
92 # very occasionally an API field breaks Amazon's coding
93 # conventions and starts with an uppercase
94 return $self->{data}{ucfirst($mixed)};
95 } else {
96 croak "Can't locate object method \"$func_name\" via package \"$pack\"";
97 }
98 }
99  
100 sub can {
101 my $self = shift;
102 my $method = shift;
103  
104 my $can = $self->SUPER::can($method);
105 return $can if $can;
106
107 my %fields = map {$_=>1} $self->valid_fields;
108 return \&AUTOLOAD if $fields{$method};
109  
110 return;
111 }
112  
113 =head2 $object = VM::EC2::Generic->new($payload,$ec2 [,$xmlns, $requestId])
114  
115 Given the parsed XML generated by VM::EC2::Dispatch and the VM::EC2
116 object, return a new object. Two optional additional arguments provide
117 the seldom-needed XML namespace and ID of the request that generated
118 this object.
119  
120 =cut
121  
122 sub new {
123 my $self = shift;
124 @_ >= 2 or croak "Usage: $self->new(\$data,\$ec2)";
125 my ($data,$ec2,$xmlns,$requestid) = @_;
126 return bless {data => $data,
127 aws => $ec2,
128 xmlns => $xmlns,
129 requestId => $requestid
130 },ref $self || $self;
131 }
132  
133 =head2 $ec2 = $object->ec2
134  
135 =head2 $ec2 = $object->aws
136  
137 Return the VM::EC2 object that generated this object. This method can
138 be called as either ec2() (preferred) or aws() (deprecated).
139  
140 =cut
141  
142 sub ec2 {
143 my $self = shift;
144 my $d = $self->{aws};
145 $self->{aws} = shift if @_;
146 $d;
147 }
148  
149 sub aws {shift->ec2}
150  
151 =head2 $id = $object->primary_id (optional method)
152  
153 Resources that have unique Amazon identifiers, such as images,
154 instances and volumes, implement the primary_id() method to return
155 that identifier. Resources that do not have unique identifiers, will
156 throw an exception if this method is called. This method is in
157 addition to the resource-specific ID. For example, volumes have a
158 unique ID, and this ID can be fetched with either of:
159  
160 $vol->volumeId;
161  
162 or
163  
164 $vol->primary_id;
165  
166 =back
167  
168 =head2 $xmlns = $object->xmlns
169  
170 Return the XML namespace of the request that generated this object, if
171 any. All objects generated by direct requests on the VM::EC2 object
172 will return this field, but objects returned via methods calls on
173 these objects (objects once removed) may not.
174  
175 =cut
176  
177 sub xmlns { shift->{xmlns} }
178  
179 =head2 $id = $object->requestId
180  
181 Return the ID of the reuqest that generated this object, if any. All
182 objects generated by direct requests on the VM::EC2 object will return
183 this field, but objects returned via methods calls on these objects
184 (objects once removed) may not.
185  
186 =cut
187  
188 sub requestId { shift->{requestId} }
189  
190 =head2 $name = $object->short_name
191  
192 Return a short name for this object for use in string
193 interpolation. If the object has a primary_id() method, then this
194 returns that ID. Otherwise it returns the default Perl object name
195 (VM::EC2::Generic=HASH(0x99f3850). Some classes override short_name()
196 in order to customized information about the object. See for example
197 L<VM::EC2::SecurityGroup::IpPermission>.
198  
199 =cut
200  
201 sub short_name {
202 my $self = shift;
203 if ($self->can('primary_id')) {
204 return $self->primary_id;
205 } else {
206 return overload::StrVal($self);
207 }
208 }
209  
210 =head2 $hashref = $object->payload
211  
212 Return the parsed XML hashref that underlies this object. See
213 L<VM::EC2::Dispatch>.
214  
215 =cut
216  
217 sub payload { shift->{data} }
218  
219  
220 =head2 @fields = $object->fields
221  
222 Return the data field names that are valid for an object of this
223 type. These field names correspond to tags in the XML
224 returned from Amazon and can then be used as method calls.
225  
226 Internally, this method is called valid_fields()
227  
228 =cut
229  
230 sub fields { shift->valid_fields }
231  
232 sub valid_fields {
233 return qw(xmlns requestId)
234 }
235  
236 =head2 $text = $object->as_string
237  
238 Return a Data::Dumper representation of the contents of this object's
239 payload.
240  
241 =cut
242  
243 sub as_string {
244 my $self = shift;
245 return Dumper($self->{data});
246 }
247  
248 =head2 $hashref = $object->tags
249  
250 =head2 $hashref = $object->tagSet
251  
252 Return the metadata tags assigned to this resource, if any, as a
253 hashref. Both tags() and tagSet() work identically.
254  
255 =cut
256  
257 sub tags {
258 my $self = shift;
259 my $result = {};
260 my $set = $self->{data}{tagSet} or return $result;
261 my $innerhash = $set->{item} or return $result;
262 for my $key (keys %$innerhash) {
263 $result->{$key} = $innerhash->{$key}{value};
264 }
265 return $result;
266 }
267  
268 sub tagSet {
269 return shift->tags();
270 }
271  
272  
273 =head2 $boolean = $object->add_tags(Tag1=>'value1',Tag2=>'value2',...)
274  
275 =head2 $boolean = $object->add_tags(\%hash)
276  
277 Add one or more tags to the object. You may provide either a list of
278 tag/value pairs or a hashref. If no tag of the indicated name exsists
279 it will be created. If there is already a tag by this name, it will
280 be set to the provided value. The result code is true if the Amazon
281 resource was successfully updated.
282  
283 Also see VM::EC2->add_tags() for a way of tagging multiple resources
284 simultaneously.
285  
286 The alias add_tag() is also provided as a convenience.
287  
288 =cut
289  
290 sub add_tags {
291 my $self = shift;
292 my $taglist = ref $_[0] && ref $_[0] eq 'HASH' ? shift : {@_};
293 $self->can('primary_id') or croak "You cannot tag objects of type ",ref $self;
294 $self->aws->create_tags(-resource_id => $self->primary_id,
295 -tag => $taglist);
296 }
297  
298 sub add_tag { shift->add_tags(@_) }
299  
300 =head2 $boolean = $object->delete_tags(@args)
301  
302 Delete the indicated tags from the indicated resource. There are
303 several variants you may use:
304  
305 # delete Foo tag if it has value "bar" and Buzz tag if it has value 'bazz'
306 $i->delete_tags({Foo=>'bar',Buzz=>'bazz'})
307  
308 # same as above but using a list rather than a hashref
309 $i->delete_tags(Foo=>'bar',Buzz=>'bazz')
310  
311 # delete Foo tag if it has any value, Buzz if it has value 'bazz'
312 $i->delete_tags({Foo=>undef,Buzz=>'bazz'})
313  
314 # delete Foo and Buzz tags unconditionally
315 $i->delete_tags(['Foo','Buzz'])
316  
317 # delete Foo tag unconditionally
318 $i->delete_tags('Foo');
319  
320 Also see VM::EC2->delete_tags() for a way of deleting tags from multiple
321 resources simultaneously.
322  
323 =cut
324  
325 sub delete_tags {
326 my $self = shift;
327 my $taglist;
328  
329 if (ref $_[0]) {
330 if (ref $_[0] eq 'HASH') {
331 $taglist = shift;
332 } elsif (ref $_[0] eq 'ARRAY') {
333 $taglist = {map {$_=>undef} @{$_[0]} };
334 }
335 } else {
336 if (@_ == 1) {
337 $taglist = {shift()=>undef};
338 } else {
339 $taglist = {@_};
340 }
341 }
342  
343 $self->can('primary_id') or croak "You cannot delete tags from objects of type ",ref $self;
344 $self->aws->delete_tags(-resource_id => $self->primary_id,
345 -tag => $taglist);
346 }
347  
348 sub _object_args {
349 my $self = shift;
350 return ($self->aws,$self->xmlns,$self->requestId);
351 }
352  
353 =head2 $xml = $object->as_xml
354  
355 Returns an XML version of the object. The object will already been
356 parsed by XML::Simple at this point, and so the data returned by this
357 method will not be identical to the XML returned by AWS.
358  
359 =cut
360  
361 sub as_xml {
362 my $self = shift;
363 XML::Simple->new->XMLout($self->payload,
364 NoAttr => 1,
365 KeyAttr => ['key'],
366 RootName => 'xml',
367 );
368 }
369  
370 =head2 $value = $object->attribute('tag_name')
371  
372 Returns the value of a tag in the XML returned from AWS, using a
373 simple heuristic. If the requested tag has a nested tag named <value>
374 it will return the contents of <value>. If the tag has one or more
375 nested tags named <item>, it will return a list of hashrefs located
376 within the <item> tag. Otherwise it will return the contents of
377 <tag_name>.
378  
379 =cut
380  
381 sub attribute {
382 my $self = shift;
383 my $attr = shift;
384 my $payload = $self->payload or return;
385 my $hr = $payload->{$attr} or return;
386 return $hr->{value} if $hr->{value};
387 return @{$hr->{item}} if $hr->{item};
388 return $hr;
389 }
390  
391 =head2 $string = $object->error_str
392  
393 Returns the error string for the last operation, if any, as reported
394 by VM::EC2.
395  
396 =cut
397  
398 sub error_str {
399 my $self = shift;
400 my $ec2 = $self->ec2 or return;
401 $ec2->error_str;
402 }
403  
404 =head2 $string = $object->error
405  
406 Returns the L<VM::EC2::Error> object from the last operation, if any,
407 as reported by VM::EC2.
408  
409 =cut
410  
411 sub error {
412 my $self = shift;
413 my $ec2 = $self->ec2 or return;
414 $ec2->error;
415 }
416  
417 =head1 STRING OVERLOADING
418  
419 This base class and its subclasses use string overloading so that the
420 object looks and acts like a simple string when used in a string
421 context (such as when printed or combined with other
422 strings). Typically the string corresponds to the Amazon resource ID
423 such as "ami-12345" and is generated by the short_name() method.
424  
425 You can sort and compare the objects as if they were strings, but
426 despite this, object method calls work in the usual way.
427  
428 =head1 SEE ALSO
429  
430 L<VM::EC2>
431 L<VM::EC2::Dispatch>
432 L<VM::EC2::Generic>
433 L<VM::EC2::BlockDevice>
434 L<VM::EC2::BlockDevice::Attachment>
435 L<VM::EC2::BlockDevice::Mapping>
436 L<VM::EC2::BlockDevice::Mapping::EBS>
437 L<VM::EC2::ConsoleOutput>
438 L<VM::EC2::Error>
439 L<VM::EC2::Generic>
440 L<VM::EC2::Group>
441 L<VM::EC2::Image>
442 L<VM::EC2::Instance>
443 L<VM::EC2::Instance::Set>
444 L<VM::EC2::Instance::State>
445 L<VM::EC2::Instance::State::Change>
446 L<VM::EC2::Instance::State::Reason>
447 L<VM::EC2::Region>
448 L<VM::EC2::ReservationSet>
449 L<VM::EC2::SecurityGroup>
450 L<VM::EC2::Snapshot>
451 L<VM::EC2::Tag>
452 L<VM::EC2::Volume>
453  
454 =head1 AUTHOR
455  
456 Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>.
457  
458 Copyright (c) 2011 Ontario Institute for Cancer Research
459  
460 This package and its accompanying libraries is free software; you can
461 redistribute it and/or modify it under the terms of the GPL (either
462 version 1, or at your option, any later version) or the Artistic
463 License 2.0. Refer to LICENSE for the full license text. In addition,
464 please see DISCLAIMER.txt for disclaimers of warranty.
465  
466 =cut
467  
468 1;
469