| 1 | package VM::EC2::REST::tag; | ||||||||||
| 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 | 7 | VM::EC2::Dispatch->register( | |||||||||
| 8 | CreateTags => 'boolean', | ||||||||||
| 9 | DeleteTags => 'boolean', | ||||||||||
| 10 | DescribeTags => 'fetch_items,tagSet,VM::EC2::Tag,nokey', | ||||||||||
| 11 | ); | ||||||||||
| 12 | |||||||||||
| 13 | =head1 NAME VM::EC2::REST::tag | ||||||||||
| 14 | |||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||
| 16 | |||||||||||
| 17 | use VM::EC2 ':standard'; | ||||||||||
| 18 | |||||||||||
| 19 | =head1 METHODS | ||||||||||
| 20 | |||||||||||
| 21 | These methods allow you to create, delete and fetch resource tags. You | ||||||||||
| 22 | may find that you rarely need to use these methods directly because | ||||||||||
| 23 | every object produced by VM::EC2 supports a simple tag interface: | ||||||||||
| 24 | |||||||||||
| 25 | $object = $ec2->describe_volumes(-volume_id=>'vol-12345'); # e.g. | ||||||||||
| 26 | $tags = $object->tags(); | ||||||||||
| 27 | $name = $tags->{Name}; | ||||||||||
| 28 | $object->add_tags(Role => 'Web Server', Status=>'development); | ||||||||||
| 29 | $object->delete_tags(Name=>undef); | ||||||||||
| 30 | |||||||||||
| 31 | See L<VM::EC2::Generic> for a full description of the uniform object | ||||||||||
| 32 | tagging interface. | ||||||||||
| 33 | |||||||||||
| 34 | These methods are most useful when creating and deleting tags for | ||||||||||
| 35 | multiple resources simultaneously. | ||||||||||
| 36 | |||||||||||
| 37 | Implemented: | ||||||||||
| 38 | CreateTags | ||||||||||
| 39 | DeleteTags | ||||||||||
| 40 | DescribeTags | ||||||||||
| 41 | |||||||||||
| 42 | Unimplemented: | ||||||||||
| 43 | (none) | ||||||||||
| 44 | |||||||||||
| 45 | =head2 @t = $ec2->describe_tags(-filter=>\%filters); | ||||||||||
| 46 | |||||||||||
| 47 | Return a series of VM::EC2::Tag objects, each describing an | ||||||||||
| 48 | AMI. A single optional -filter argument is allowed. | ||||||||||
| 49 | |||||||||||
| 50 | Available filters are: key, resource-id, resource-type and value. See | ||||||||||
| 51 | http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeTags.html | ||||||||||
| 52 | |||||||||||
| 53 | =cut | ||||||||||
| 54 | |||||||||||
| 55 | sub describe_tags { | ||||||||||
| 56 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
describe_tags: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 57 | 0 | my %args = @_; | |||||||||
| 58 | 0 | my @params = $self->filter_parm(\%args); | |||||||||
| 59 | 0 | return $self->call('DescribeTags',@params); | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | =head2 $bool = $ec2->create_tags(-resource_id=>\@ids,-tag=>{key1=>value1...}) | ||||||||||
| 63 | |||||||||||
| 64 | Tags the resource indicated by -resource_id with the tag(s) in in the | ||||||||||
| 65 | hashref indicated by -tag. You may specify a single resource by | ||||||||||
| 66 | passing a scalar resourceId to -resource_id, or multiple resources | ||||||||||
| 67 | using an anonymous array. Returns a true value if tagging was | ||||||||||
| 68 | successful. | ||||||||||
| 69 | |||||||||||
| 70 | The method name "add_tags()" is an alias for create_tags(). | ||||||||||
| 71 | |||||||||||
| 72 | You may find it more convenient to tag an object retrieved with any of | ||||||||||
| 73 | the describe() methods using the built-in add_tags() method: | ||||||||||
| 74 | |||||||||||
| 75 | @snap = $ec2->describe_snapshots(-filter=>{status=>'completed'}); | ||||||||||
| 76 | foreach (@snap) {$_->add_tags(ReadyToUse => 'true')} | ||||||||||
| 77 | |||||||||||
| 78 | but if there are many snapshots to tag simultaneously, this will be faster: | ||||||||||
| 79 | |||||||||||
| 80 | @snap = $ec2->describe_snapshots(-filter=>{status=>'completed'}); | ||||||||||
| 81 | $ec2->add_tags(-resource_id=>\@snap,-tag=>{ReadyToUse=>'true'}); | ||||||||||
| 82 | |||||||||||
| 83 | Note that you can tag volumes, snapshots and images owned by other | ||||||||||
| 84 | people. Only you will be able to see these tags. | ||||||||||
| 85 | |||||||||||
| 86 | =cut | ||||||||||
| 87 | |||||||||||
| 88 | sub create_tags { | ||||||||||
| 89 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
create_tags: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 90 | 0 | my %args = @_; | |||||||||
| 91 | 0 | ▶ | $args{-resource_id} or croak "create_tags() -resource_id argument required"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 0 | ▶ | $args{-tag} or croak "create_tags() -tag argument required"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 0 | my @params = $self->list_parm('ResourceId',\%args); | |||||||||
| 94 | 0 | push @params,$self->tagcreate_parm(\%args); | |||||||||
| 95 | 0 | return $self->call('CreateTags',@params); | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | sub add_tags { shift->create_tags(@_) } | ||||||||
|
Subroutine
add_tags: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 99 | |||||||||||
| 100 | =head2 $bool = $ec2->delete_tags(-resource_id=>$id1,-tag=>{key1=>value1...}) | ||||||||||
| 101 | |||||||||||
| 102 | Delete the indicated tags from the indicated resource. Pass an | ||||||||||
| 103 | arrayref to operate on several resources at once. The tag syntax is a | ||||||||||
| 104 | bit tricky. Use a value of undef to delete the tag unconditionally: | ||||||||||
| 105 | |||||||||||
| 106 | -tag => { Role => undef } # deletes any Role tag | ||||||||||
| 107 | |||||||||||
| 108 | Any scalar value will cause the tag to be deleted only if its value | ||||||||||
| 109 | exactly matches the specified value: | ||||||||||
| 110 | |||||||||||
| 111 | -tag => { Role => 'Server' } # only delete the Role tag | ||||||||||
| 112 | # if it currently has the value "Server" | ||||||||||
| 113 | |||||||||||
| 114 | An empty string value ('') will only delete the tag if its value is an | ||||||||||
| 115 | empty string, which is probably not what you want. | ||||||||||
| 116 | |||||||||||
| 117 | Pass an array reference of tag names to delete each of the tag names | ||||||||||
| 118 | unconditionally (same as passing a value of undef): | ||||||||||
| 119 | |||||||||||
| 120 | $ec2->delete_tags(['Name','Role','Description']); | ||||||||||
| 121 | |||||||||||
| 122 | You may find it more convenient to delete tags from objects using | ||||||||||
| 123 | their delete_tags() method: | ||||||||||
| 124 | |||||||||||
| 125 | @snap = $ec2->describe_snapshots(-filter=>{status=>'completed'}); | ||||||||||
| 126 | foreach (@snap) {$_->delete_tags(Role => undef)} | ||||||||||
| 127 | |||||||||||
| 128 | =cut | ||||||||||
| 129 | |||||||||||
| 130 | sub delete_tags { | ||||||||||
| 131 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
delete_tags: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 132 | 0 | my %args = @_; | |||||||||
| 133 | 0 | ▶ | $args{-resource_id} or croak "create_tags() -resource_id argument required"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 0 | ▶ | $args{-tag} or croak "create_tags() -tag argument required"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | my @params = $self->list_parm('ResourceId',\%args); | |||||||||
| 136 | 0 | push @params,$self->tagdelete_parm(\%args); | |||||||||
| 137 | 0 | return $self->call('DeleteTags',@params); | |||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | =head2 @arguments = $ec2->tagcreate_parm(\%args) | ||||||||||
| 141 | |||||||||||
| 142 | =cut | ||||||||||
| 143 | |||||||||||
| 144 | sub tagcreate_parm { | ||||||||||
| 145 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
tagcreate_parm: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 146 | 0 | my $args = shift; | |||||||||
| 147 | 0 | return $self->key_value_parameters('Tag','Key','Value',$args); | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | =head2 @arguments = $ec2->tagdelete_parm(\%args) | ||||||||||
| 151 | |||||||||||
| 152 | =cut | ||||||||||
| 153 | |||||||||||
| 154 | sub tagdelete_parm { | ||||||||||
| 155 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
tagdelete_parm: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 156 | 0 | my $args = shift; | |||||||||
| 157 | 0 | return $self->key_value_parameters('Tag','Key','Value',$args,1); | |||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | =head1 SEE ALSO | ||||||||||
| 161 | |||||||||||
| 162 | L<VM::EC2> | ||||||||||
| 163 | |||||||||||
| 164 | =head1 AUTHOR | ||||||||||
| 165 | |||||||||||
| 166 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||||||||||
| 167 | |||||||||||
| 168 | Copyright (c) 2011 Ontario Institute for Cancer Research | ||||||||||
| 169 | |||||||||||
| 170 | This package and its accompanying libraries is free software; you can | ||||||||||
| 171 | redistribute it and/or modify it under the terms of the GPL (either | ||||||||||
| 172 | version 1, or at your option, any later version) or the Artistic | ||||||||||
| 173 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||||||||||
| 174 | please see DISCLAIMER.txt for disclaimers of warranty. | ||||||||||
| 175 | |||||||||||
| 176 | =cut | ||||||||||
| 177 | |||||||||||
| 178 | |||||||||||
| 179 | 7 | 1; | |||||||||