| 1 | package VM::EC2::REST::tag; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use VM::EC2 ''; # important not to import anything! | ||
| 5 | package VM::EC2; # add methods to VM::EC2 | ||
| 6 | |||
| 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 | my $self = shift; | ||
| 57 | my %args = @_; | ||
| 58 | my @params = $self->filter_parm(\%args); | ||
| 59 | 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 | my $self = shift; | ||
| 90 | my %args = @_; | ||
| 91 | $args{-resource_id} or croak "create_tags() -resource_id argument required"; | ||
| 92 | $args{-tag} or croak "create_tags() -tag argument required"; | ||
| 93 | my @params = $self->list_parm('ResourceId',\%args); | ||
| 94 | push @params,$self->tagcreate_parm(\%args); | ||
| 95 | return $self->call('CreateTags',@params); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub add_tags { shift->create_tags(@_) } | ||
| 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 | my $self = shift; | ||
| 132 | my %args = @_; | ||
| 133 | $args{-resource_id} or croak "create_tags() -resource_id argument required"; | ||
| 134 | $args{-tag} or croak "create_tags() -tag argument required"; | ||
| 135 | my @params = $self->list_parm('ResourceId',\%args); | ||
| 136 | push @params,$self->tagdelete_parm(\%args); | ||
| 137 | return $self->call('DeleteTags',@params); | ||
| 138 | } | ||
| 139 | |||
| 140 | =head2 @arguments = $ec2->tagcreate_parm(\%args) | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | sub tagcreate_parm { | ||
| 145 | my $self = shift; | ||
| 146 | my $args = shift; | ||
| 147 | 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 | my $self = shift; | ||
| 156 | my $args = shift; | ||
| 157 | 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 | 1; |