| 1 | package VM::EC2::NetworkInterface; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | VM::EC2::NetworkInterface - Object describing an Amazon Elastic Network Interface (ENI) | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use VM::EC2; | ||
| 10 | my $ec2 = VM::EC2->new(...); | ||
| 11 | my $interface = $ec2->describe_network_interfaces('eni-12345'); | ||
| 12 | print $interface->subNetId,"\n", | ||
| 13 | $interface->description,"\n", | ||
| 14 | $interface->vpcId,"\n", | ||
| 15 | $interface->status,"\n", | ||
| 16 | $interface->privateIpAddress,"\n", | ||
| 17 | $interface->macAddress,"\n"; | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | This object provides access to information about Amazon Elastic | ||
| 22 | Network Interface objects, which are used in conjunction with virtual | ||
| 23 | private cloud (VPC) instances to create multi-homed web servers, | ||
| 24 | routers, firewalls, and so forth. | ||
| 25 | |||
| 26 | Please see L<VM::EC2::Generic> for methods shared by all VM::EC2 | ||
| 27 | objects. | ||
| 28 | |||
| 29 | =head1 METHODS | ||
| 30 | |||
| 31 | These object methods are supported: | ||
| 32 | |||
| 33 | networkInterfaceId -- The ID of this ENI | ||
| 34 | subnetId -- The ID of the subnet this ENI belongs to | ||
| 35 | vpcId -- The ID of the VPC this ENI belongs to | ||
| 36 | ownerId -- Owner of the ENI | ||
| 37 | status -- ENI status, one of "available" or "in-use" | ||
| 38 | privateIpAddress -- Primary private IP address of the ENI | ||
| 39 | privateDnsName -- Primary private DNS name of the ENI | ||
| 40 | as a set of VM::EC2::Group objects. | ||
| 41 | attachment -- Information about the attachment of this ENI to | ||
| 42 | an instance, as a VM::EC2::NetworkInterface::Attachment | ||
| 43 | object. | ||
| 44 | association -- Information about the association of this ENI with | ||
| 45 | an elastic public IP address. | ||
| 46 | privateIpAddresses -- List of private IP addresses assigned to this ENI, | ||
| 47 | as a list of VM::EC2::NetworkInterface::PrivateIpAddress | ||
| 48 | objects. | ||
| 49 | availabilityZone -- Availability zone for this ENI as a VM::EC2::AvailabilityZone | ||
| 50 | object. | ||
| 51 | macAddress -- MAC address for this interface. | ||
| 52 | |||
| 53 | In addition, this object supports the following convenience methods: | ||
| 54 | |||
| 55 | resetAttributes() -- Return attributes to their default states. Currently only | ||
| 56 | sets the SourceDestCheck value to true. | ||
| 57 | |||
| 58 | description([$new_value]) -- Description of the ENI. Pass a single argument to set a new | ||
| 59 | description | ||
| 60 | |||
| 61 | sourceDestCheck([$boolean])-- Boolean value. If true, prevent this ENI from | ||
| 62 | forwarding packets between subnets. Value can optionally | ||
| 63 | be set | ||
| 64 | |||
| 65 | security_groups([@new_groups]) -- List of security groups this ENI belongs to. Pass a | ||
| 66 | list of new security groups to change this value. | ||
| 67 | |||
| 68 | delete_on_termination([$boolean]) | ||
| 69 | -- Whether the deleteOnTermination flag is set for the current | ||
| 70 | attachment. Pass a boolean value to change the value. | ||
| 71 | |||
| 72 | =head1 Attaching to an instance | ||
| 73 | |||
| 74 | The following methods allow the interface to be attached to, and | ||
| 75 | detached from, instances. | ||
| 76 | |||
| 77 | =head2 $attachment_id = $interface->attach($instance_id => $device) | ||
| 78 | |||
| 79 | =head2 $attachment_id = $interfacee->attach(-instance_id => $id, | ||
| 80 | -device_index => $device) | ||
| 81 | |||
| 82 | This method attaches the network interface an instance using the the | ||
| 83 | indicated device index. You can provide either an instance ID, or a | ||
| 84 | VM::EC2::Instance object. You may use an integer for -device_index, or | ||
| 85 | use the strings "eth0", "eth1" etc. | ||
| 86 | |||
| 87 | Required arguments: | ||
| 88 | |||
| 89 | -instance_id ID of the instance to attach to. | ||
| 90 | -device_index Network device number to use (e.g. 0 for eth0). | ||
| 91 | |||
| 92 | On success, this method returns the attachmentId of the new attachment | ||
| 93 | (not a VM::EC2::NetworkInterface::Attachment object, due to an AWS API | ||
| 94 | inconsistency). | ||
| 95 | |||
| 96 | =head2 $boolean = $interface->detach([$force]) | ||
| 97 | |||
| 98 | This method detaches the network interface from whatever instance it | ||
| 99 | is currently attached to. If a true argument is provided, then the | ||
| 100 | detachment will be forced, even if the interface is in use. | ||
| 101 | |||
| 102 | On success, this method returns a true value. | ||
| 103 | |||
| 104 | =head1 Adding IP addresses | ||
| 105 | |||
| 106 | =head2 $result = $interface->assign_private_ip_addresses(@addresses) | ||
| 107 | |||
| 108 | =head2 $result = $interface->assign_private_ip_addresses(%args) | ||
| 109 | |||
| 110 | Assign one or more secondary private IP addresses to the network | ||
| 111 | interface. You can either set the addresses explicitly, or provide a | ||
| 112 | count of secondary addresses, and let Amazon select them for you. | ||
| 113 | |||
| 114 | In the list argument form, pass a list of desired IP addresses, or a | ||
| 115 | count of the number of addresses to select for you: | ||
| 116 | |||
| 117 | $interface->assign_private_ip_addresses(3); # three automatic addresses | ||
| 118 | $interface->assign_private_ip_addresses('192.168.0.10','192.168.0.11'); | ||
| 119 | |||
| 120 | Required arguments: | ||
| 121 | |||
| 122 | -private_ip_address One or more secondary IP addresses, as a scalar string | ||
| 123 | -private_ip_addresses or array reference. (The two arguments are equivalent). | ||
| 124 | |||
| 125 | Optional arguments: | ||
| 126 | |||
| 127 | -allow_reassignment If true, allow assignment of an IP address is already in | ||
| 128 | use by another network interface or instance. | ||
| 129 | |||
| 130 | The following are valid arguments to -private_ip_address: | ||
| 131 | |||
| 132 | -private_ip_address => '192.168.0.12' # single address | ||
| 133 | -private_ip_address => ['192.168.0.12','192.168.0.13] # multiple addresses | ||
| 134 | -private_ip_address => 3 # autoselect three addresses | ||
| 135 | |||
| 136 | The mixed form of address, such as ['192.168.0.12','auto'] is not allowed in this call. | ||
| 137 | |||
| 138 | On success, this method returns true. | ||
| 139 | |||
| 140 | =head2 $result = $interface->unassign_private_ip_addresses(@addresses) | ||
| 141 | |||
| 142 | =head2 $result = $interface->unassign_private_ip_addresses(-private_ip_address => \@addresses) | ||
| 143 | |||
| 144 | Unassign one or more secondary private IP addresses from the network | ||
| 145 | interface. | ||
| 146 | |||
| 147 | In the list argument form, pass a list of desired IP addresses to unassign. | ||
| 148 | |||
| 149 | $interface->assign_private_ip_addresses('192.168.0.10','192.168.0.11'); | ||
| 150 | |||
| 151 | In the named argument form, use: | ||
| 152 | |||
| 153 | -private_ip_address One or more secondary IP addresses, as a scalar string | ||
| 154 | -private_ip_addresses or array reference. (The two arguments are equivalent). | ||
| 155 | |||
| 156 | The following are valid arguments to -private_ip_address: | ||
| 157 | |||
| 158 | -private_ip_address => '192.168.0.12' # single address | ||
| 159 | -private_ip_address => ['192.168.0.12','192.168.0.13] # multiple addresses | ||
| 160 | |||
| 161 | On success, this method returns true. | ||
| 162 | |||
| 163 | =head1 STRING OVERLOADING | ||
| 164 | |||
| 165 | When used in a string context, this object will be interpolated as the | ||
| 166 | networkInterfaceId | ||
| 167 | |||
| 168 | =head1 SEE ALSO | ||
| 169 | |||
| 170 | L<VM::EC2> | ||
| 171 | L<VM::EC2::Generic> | ||
| 172 | L<VM::EC2::NetworkInterface> | ||
| 173 | L<VM::EC2::NetworkInterface::Attachment> | ||
| 174 | L<VM::EC2::NetworkInterface::Association> | ||
| 175 | |||
| 176 | =head1 AUTHOR | ||
| 177 | |||
| 178 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 179 | |||
| 180 | Copyright (c) 2012 Ontario Institute for Cancer Research | ||
| 181 | |||
| 182 | This package and its accompanying libraries is free software; you can | ||
| 183 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 184 | version 1, or at your option, any later version) or the Artistic | ||
| 185 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 186 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | use strict; | ||
| 191 | use base 'VM::EC2::Generic'; | ||
| 192 | use Carp 'croak'; | ||
| 193 | use VM::EC2::Group; | ||
| 194 | use VM::EC2::NetworkInterface::PrivateIpAddress; | ||
| 195 | use VM::EC2::NetworkInterface::Attachment; | ||
| 196 | use VM::EC2::NetworkInterface::Association; | ||
| 197 | |||
| 198 | sub valid_fields { | ||
| 199 | my $self = shift; | ||
| 200 | return qw(networkInterfaceId subnetId vpcId description ownerId status privateIpAddress privateDnsName | ||
| 201 | sourceDestCheck groupSet attachment association privateIpAddressesSet macAddress requesterManaged | ||
| 202 | availabilityZone); | ||
| 203 | } | ||
| 204 | |||
| 205 | sub refresh { | ||
| 206 | my $self = shift; | ||
| 207 | my $i = shift; | ||
| 208 | local $self->aws->{raise_error} = 1; | ||
| 209 | ($i) = $self->aws->describe_network_interfaces(-network_interface_id=>$self->networkInterfaceId) unless $i; | ||
| 210 | %$self = %$i if $i; | ||
| 211 | return defined $i; | ||
| 212 | } | ||
| 213 | |||
| 214 | sub current_status { | ||
| 215 | my $self = shift; | ||
| 216 | $self->refresh; | ||
| 217 | $self->status; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub primary_id {shift->networkInterfaceId} | ||
| 221 | |||
| 222 | sub groups { | ||
| 223 | my $self = shift; | ||
| 224 | my $groupSet = $self->SUPER::groupSet; | ||
| 225 | return map {VM::EC2::Group->new($_,$self->aws,$self->xmlns,$self->requestId)} | ||
| 226 | @{$groupSet->{item}}; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub privateIpAddresses { | ||
| 230 | my $self = shift; | ||
| 231 | my $set = $self->SUPER::privateIpAddressesSet; | ||
| 232 | return map {VM::EC2::NetworkInterface::PrivateIpAddress->new($_,$self->aws)} @{$set->{item}}; | ||
| 233 | } | ||
| 234 | |||
| 235 | sub attachment { | ||
| 236 | my $self = shift; | ||
| 237 | my $a = $self->SUPER::attachment or return; | ||
| 238 | return VM::EC2::NetworkInterface::Attachment->new($a,$self->aws); | ||
| 239 | } | ||
| 240 | |||
| 241 | sub association { | ||
| 242 | my $self = shift; | ||
| 243 | my $a = $self->SUPER::association or return; | ||
| 244 | return VM::EC2::NetworkInterface::Association->new($a,$self->aws); | ||
| 245 | } | ||
| 246 | |||
| 247 | sub vpc { | ||
| 248 | my $self = shift; | ||
| 249 | return $self->describe_vpcs($self->vpcId); | ||
| 250 | } | ||
| 251 | |||
| 252 | # get/set methods | ||
| 253 | sub description { | ||
| 254 | my $self = shift; | ||
| 255 | my $d = $self->aws->describe_network_interface_attribute($self,'description'); | ||
| 256 | $self->aws->modify_network_interface_attribute($self,-description=>shift) if @_; | ||
| 257 | return $d; | ||
| 258 | } | ||
| 259 | |||
| 260 | sub security_groups { | ||
| 261 | my $self = shift; | ||
| 262 | my @d = $self->aws->describe_network_interface_attribute($self,'groupSet'); | ||
| 263 | $self->aws->modify_network_interface_attribute($self,-security_group_id=>\@_) if @_; | ||
| 264 | return map {VM::EC2::Group->new($_,$self->aws)} @d; | ||
| 265 | } | ||
| 266 | |||
| 267 | sub source_dest_check { | ||
| 268 | my $self = shift; | ||
| 269 | my $d = $self->aws->describe_network_interface_attribute($self,'sourceDestCheck'); | ||
| 270 | $self->aws->modify_network_interface_attribute($self,-source_dest_check=>(shift() ? 'true' : 'false')) if @_; | ||
| 271 | return $d eq 'true'; | ||
| 272 | } | ||
| 273 | |||
| 274 | sub reset_attributes { | ||
| 275 | my $self = shift; | ||
| 276 | return $self->aws->reset_network_interface_attribute($self=>'sourceDestCheck'); | ||
| 277 | } | ||
| 278 | |||
| 279 | sub delete_on_termination { | ||
| 280 | my $self = shift; | ||
| 281 | my $d = $self->aws->describe_network_interface_attribute($self,'attachment') or return; | ||
| 282 | my $att = VM::EC2::NetworkInterface::Attachment->new($d,$self->aws); | ||
| 283 | $self->aws->modify_network_interface_attribute($self,-delete_on_termination=>[$att=>shift]) if @_; | ||
| 284 | return $att->delete_on_termination; | ||
| 285 | } | ||
| 286 | |||
| 287 | sub availabilityZone { | ||
| 288 | my $self = shift; | ||
| 289 | my $z = $self->SUPER::availabilityZone or return; | ||
| 290 | return $self->aws->describe_availability_zones($z); | ||
| 291 | } | ||
| 292 | |||
| 293 | sub attach { | ||
| 294 | my $self = shift; | ||
| 295 | my %args; | ||
| 296 | if (@_==2 && $_[0] !~ /^-/) { | ||
| 297 | @args{qw(-instance_id -device_index)} = @_; | ||
| 298 | } else { | ||
| 299 | %args = @_; | ||
| 300 | } | ||
| 301 | $args{-instance_id} && $args{-device_index} | ||
| 302 | or croak "usage: \$interface->attach(\$instance_id,\$device_index)"; | ||
| 303 | $args{-network_interface_id} = $self->networkInterfaceId; | ||
| 304 | my $result = $self->aws->attach_network_interface(%args); | ||
| 305 | $self->refresh if $result; | ||
| 306 | eval {$args{-instance_id}->refresh} if $result; | ||
| 307 | return $result; | ||
| 308 | } | ||
| 309 | |||
| 310 | sub detach { | ||
| 311 | my $self = shift; | ||
| 312 | my $force = shift; | ||
| 313 | my $attachment = $self->attachment; | ||
| 314 | $attachment or croak "$self is not attached"; | ||
| 315 | my $result = $self->aws->detach_network_interface($attachment,$force); | ||
| 316 | $self->refresh if $result; | ||
| 317 | return $result; | ||
| 318 | } | ||
| 319 | |||
| 320 | sub assign_private_ip_addresses { | ||
| 321 | my $self = shift; | ||
| 322 | my %args; | ||
| 323 | if (@_ && $_[0] !~ /^-/) { | ||
| 324 | %args = (-private_ip_address => @_==1 ? $_[0] : \@_); | ||
| 325 | } else { | ||
| 326 | %args = @_; | ||
| 327 | } | ||
| 328 | my $result = $self->aws->assign_private_ip_addresses(-network_interface_id=>$self,%args); | ||
| 329 | $self->refresh if $result; | ||
| 330 | return $result; | ||
| 331 | } | ||
| 332 | |||
| 333 | sub unassign_private_ip_addresses { | ||
| 334 | my $self = shift; | ||
| 335 | my %args; | ||
| 336 | if (@_ && $_[0] !~ /^-/) { | ||
| 337 | %args = (-private_ip_address => \@_); | ||
| 338 | } else { | ||
| 339 | %args = @_; | ||
| 340 | } | ||
| 341 | my $result = $self->aws->unassign_private_ip_addresses(-network_interface_id=>$self,%args); | ||
| 342 | $self->refresh if $result; | ||
| 343 | return $result; | ||
| 344 | } | ||
| 345 | |||
| 346 | 1; | ||
| 347 |