| 1 | package VM::EC2::REST::elastic_ip; | ||
| 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 | DescribeAddresses => 'fetch_items,addressesSet,VM::EC2::ElasticAddress', | ||
| 9 | AssociateAddress => sub { | ||
| 10 | my $data = shift; | ||
| 11 | return $data->{associationId} || ($data->{return} eq 'true'); | ||
| 12 | }, | ||
| 13 | DisassociateAddress => 'boolean', | ||
| 14 | AllocateAddress => 'VM::EC2::ElasticAddress', | ||
| 15 | ReleaseAddress => 'boolean', | ||
| 16 | ); | ||
| 17 | |||
| 18 | =head1 NAME VM::EC2::REST::elastic_ip | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | use VM::EC2 ':standard'; | ||
| 23 | |||
| 24 | =head1 METHODS | ||
| 25 | |||
| 26 | The methods in this section allow you to allocate elastic IP | ||
| 27 | addresses, attach them to instances, and delete them. See | ||
| 28 | L<VM::EC2::ElasticAddress>. | ||
| 29 | |||
| 30 | Implemented: | ||
| 31 | AllocateAddress | ||
| 32 | AssociateAddress | ||
| 33 | DescribeAddresses | ||
| 34 | DissociateAddress | ||
| 35 | ReleaseAddress | ||
| 36 | |||
| 37 | Unimplemented: | ||
| 38 | (none) | ||
| 39 | |||
| 40 | =head2 @addr = $ec2->describe_addresses(@public_ips) | ||
| 41 | |||
| 42 | =head2 @addr = $ec2->describe_addresses(-public_ip=>\@addr,-allocation_id=>\@id,-filter->\%filters) | ||
| 43 | |||
| 44 | Queries AWS for a list of elastic IP addresses already allocated to | ||
| 45 | you. All arguments are optional: | ||
| 46 | |||
| 47 | -public_ip -- An IP address (in dotted format) or an arrayref of | ||
| 48 | addresses to return information about. | ||
| 49 | -allocation_id -- An allocation ID or arrayref of such IDs. Only | ||
| 50 | applicable to VPC addresses. | ||
| 51 | -filter -- A hashref of tag=>value pairs to filter the response | ||
| 52 | on. | ||
| 53 | |||
| 54 | The list of applicable filters can be found at | ||
| 55 | http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeAddresses.html. | ||
| 56 | |||
| 57 | This method returns a list of L<VM::EC2::ElasticAddress>. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub describe_addresses { | ||
| 62 | my $self = shift; | ||
| 63 | my %args = $self->args(-public_ip=>@_); | ||
| 64 | my @param; | ||
| 65 | push @param,$self->list_parm('PublicIp',\%args); | ||
| 66 | push @param,$self->list_parm('AllocationId',\%args); | ||
| 67 | push @param,$self->filter_parm(\%args); | ||
| 68 | return $self->call('DescribeAddresses',@param); | ||
| 69 | } | ||
| 70 | |||
| 71 | =head2 $address_info = $ec2->allocate_address([-vpc=>1]) | ||
| 72 | |||
| 73 | Request an elastic IP address. Pass -vpc=>1 to allocate a VPC elastic | ||
| 74 | address. The return object is a VM::EC2::ElasticAddress. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub allocate_address { | ||
| 79 | my $self = shift; | ||
| 80 | my %args = @_; | ||
| 81 | my @param = $args{-vpc} ? (Domain=>'vpc') : (); | ||
| 82 | return $self->call('AllocateAddress',@param); | ||
| 83 | } | ||
| 84 | |||
| 85 | =head2 $boolean = $ec2->release_address($addr) | ||
| 86 | |||
| 87 | Release an elastic IP address. For non-VPC addresses, you may provide | ||
| 88 | either an IP address string, or a VM::EC2::ElasticAddress. For VPC | ||
| 89 | addresses, you must obtain a VM::EC2::ElasticAddress first | ||
| 90 | (e.g. with describe_addresses) and then pass that to the method. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub release_address { | ||
| 95 | my $self = shift; | ||
| 96 | my $addr = shift or croak "Usage: release_address(\$addr)"; | ||
| 97 | my @param = (PublicIp=>$addr); | ||
| 98 | if (my $allocationId = eval {$addr->allocationId}) { | ||
| 99 | @param = (AllocationId=>$allocationId); | ||
| 100 | } | ||
| 101 | return $self->call('ReleaseAddress',@param); | ||
| 102 | } | ||
| 103 | |||
| 104 | =head2 $result = $ec2->associate_address($elastic_addr => $instance_id) | ||
| 105 | |||
| 106 | Associate an elastic address with an instance id. Both arguments are | ||
| 107 | mandatory. If you are associating a VPC elastic IP address with the | ||
| 108 | instance, the result code will indicate the associationId. Otherwise | ||
| 109 | it will be a simple perl truth value ("1") if successful, undef if | ||
| 110 | false. | ||
| 111 | |||
| 112 | If this is an ordinary EC2 Elastic IP address, the first argument may | ||
| 113 | either be an ordinary string (xx.xx.xx.xx format) or a | ||
| 114 | VM::EC2::ElasticAddress object. However, if it is a VPC elastic | ||
| 115 | IP address, then the argument must be a VM::EC2::ElasticAddress | ||
| 116 | as returned by describe_addresses(). The reason for this is that the | ||
| 117 | allocationId must be retrieved from the object in order to use in the | ||
| 118 | call. | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | sub associate_address { | ||
| 123 | my $self = shift; | ||
| 124 | @_ == 2 or croak "Usage: associate_address(\$elastic_addr => \$instance_id)"; | ||
| 125 | my ($addr,$instance) = @_; | ||
| 126 | |||
| 127 | my @param = (InstanceId=>$instance); | ||
| 128 | push @param,eval {$addr->domain eq 'vpc'} ? (AllocationId => $addr->allocationId) | ||
| 129 | : (PublicIp => $addr); | ||
| 130 | return $self->call('AssociateAddress',@param); | ||
| 131 | } | ||
| 132 | |||
| 133 | =head2 $bool = $ec2->disassociate_address($elastic_addr) | ||
| 134 | |||
| 135 | Disassociate an elastic address from whatever instance it is currently | ||
| 136 | associated with, if any. The result will be true if disassociation was | ||
| 137 | successful. | ||
| 138 | |||
| 139 | If this is an ordinary EC2 Elastic IP address, the argument may | ||
| 140 | either be an ordinary string (xx.xx.xx.xx format) or a | ||
| 141 | VM::EC2::ElasticAddress object. However, if it is a VPC elastic | ||
| 142 | IP address, then the argument must be a VM::EC2::ElasticAddress | ||
| 143 | as returned by describe_addresses(). The reason for this is that the | ||
| 144 | allocationId must be retrieved from the object in order to use in the | ||
| 145 | call. | ||
| 146 | |||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub disassociate_address { | ||
| 151 | my $self = shift; | ||
| 152 | @_ == 1 or croak "Usage: disassociate_address(\$elastic_addr)"; | ||
| 153 | my $addr = shift; | ||
| 154 | |||
| 155 | my @param = eval {$addr->domain eq 'vpc'} ? (AssociationId => $addr->associationId) | ||
| 156 | : (PublicIp => $addr); | ||
| 157 | return $self->call('DisassociateAddress',@param); | ||
| 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; |