blib/lib/VM/EC2/REST/elastic_network_interface.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 44 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 172 CC 12 SCAR 34.3
CC
12
Cov
0%
create_network_interface
72.0
modify_network_interface_attribute
12.0
attach_network_interface
6.0
CRAP 30.9
1 package VM::EC2::REST::elastic_network_interface;
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 CreateNetworkInterface => 'fetch_one,networkInterface,VM::EC2::NetworkInterface',
9 DeleteNetworkInterface => 'boolean',
10 DescribeNetworkInterfaces => 'fetch_items,networkInterfaceSet,VM::EC2::NetworkInterface',
11 ModifyNetworkInterfaceAttribute => 'boolean',
12 ResetNetworkInterfaceAttribute => 'boolean',
13 AttachNetworkInterface => sub { shift->{attachmentId} },
14 DetachNetworkInterface => 'boolean',
15 );
16  
17 =head1 NAME VM::EC2::REST::elastic_network_interface
18  
19 =head1 SYNOPSIS
20  
21 use VM::EC2 ':vpc';
22  
23 =head1 METHODS
24  
25 These methods create and manage Elastic Network Interfaces (ENI). Once
26 created, an ENI can be attached to instances and/or be associated with
27 a public IP address. ENIs can only be used in conjunction with VPC
28 instances.
29  
30 Implemented:
31 AttachNetworkInterface
32 CreateNetworkInterface
33 DeleteNetworkInterface
34 DescribeNetworkInterfaceAttribute
35 DescribeNetworkInterfaces
36 DetachNetworkInterface
37 ModifyNetworkInterfaceAttribute
38 ResetNetworkInterfaceAttribute
39  
40 Unimplemented:
41 (none)
42  
43 =head2 $interface = $ec2->create_network_interface($subnet_id)
44  
45 =head2 $interface = $ec2->create_network_interface(%args)
46  
47 This method creates an elastic network interface (ENI). If only a
48 single argument is provided, it is treated as the ID of the VPC subnet
49 to associate with the ENI. If multiple arguments are provided, they
50 are treated as -arg=>value parameter pairs.
51  
52 Arguments:
53  
54 The -subnet_id argument is mandatory. Others are optional.
55  
56 -subnet_id -- ID of the VPC subnet to associate with the network
57 interface (mandatory)
58  
59 -private_ip_address -- The primary private IP address of the network interface,
60 or a reference to an array of private IP addresses. In the
61 latter case, the first element of the array becomes the
62 primary address, and the subsequent ones become secondary
63 addresses. If no private IP address is specified, one will
64 be chosen for you. See below for more information on this
65 parameter.
66  
67 -private_ip_addresses -- Same as -private_ip_address, for readability.
68  
69 -secondary_ip_address_count -- An integer requesting this number of secondary IP
70 addresses to be allocated automatically. If present,
71 cannot provide any secondary addresses explicitly.
72  
73 -description -- Description of this ENI.
74  
75 -security_group_id -- Array reference or scalar containing IDs of the security
76 group(s) to assign to this interface.
77  
78 You can assign multiple IP addresses to the interface explicitly, or
79 by allowing EC2 to choose addresses within the designated subnet
80 automatically. The following examples demonstrate the syntax:
81  
82 # one primary address, chosen explicitly
83 -private_ip_address => '192.168.0.12'
84  
85 # one primary address and two secondary addresses, chosen explicitly
86 -private_ip_address => ['192.168.0.12','192.168.0.200','192.168.0.201']
87  
88 # one primary address chosen explicitly, and two secondaries chosen automatically
89 -private_ip_address => ['192.168.0.12','auto','auto']
90  
91 # one primary address chosen explicitly, and two secondaries chosen automatically (another syntax)
92 -private_ip_address => ['192.168.0.12',2]
93  
94 # one primary address chosen automatically, and two secondaries chosen automatically
95 -private_ip_address => [auto,2]
96  
97 You cannot assign some secondary addresses explicitly and others
98 automatically on the same ENI. If you provide no -private_ip_address
99 parameter at all, then a single private IP address will be chosen for
100 you (the same as -private_ip_address=>'auto').
101  
102 The return value is a VM::EC2::NetworkInterface object
103  
104 =cut
105  
106 # NOTE: there is code overlap with network_interface_parm()
107 sub create_network_interface {
108 my $self = shift;
109 my %args = $self->args(-subnet_id=>@_);
110 $args{-subnet_id} or croak "Usage: create_network_interface(-subnet_id=>\$id,\@more_args)";
111 my @parm = $self->single_parm('SubnetId',\%args);
112 push @parm, $self->single_parm('Description',\%args);
113 push @parm, $self->list_parm('SecurityGroupId',\%args);
114  
115 my $address = $args{-private_ip_address} || $args{-private_ip_addresses};
116 my $auto_count;
117  
118 if ($address) {
119 my $c = 0;
120  
121 my @addresses = ref $address && ref $address eq 'ARRAY' ? @$address : ($address);
122 my $primary = shift @addresses;
123 unless ($primary eq 'auto') {
124 push @parm, ("PrivateIpAddresses.$c.PrivateIpAddress" => $primary);
125 push @parm, ("PrivateIpAddresses.$c.Primary" => 'true');
126 }
127  
128 # deal with automatic secondary addresses .. this seems needlessly complex
129 if (my @auto = grep {/auto/i} @addresses) {
130 @auto == @addresses or croak "cannot request both explicit and automatic secondary IP addresses";
131 $auto_count = @auto;
132 }
133 $auto_count = $addresses[0] if @addresses == 1 && $addresses[0] =~ /^\d+$/;
134 $auto_count ||= $args{-secondary_ip_address_count};
135
136 unless ($auto_count) {
137 foreach (@addresses) {
138 $c++;
139 push @parm,("PrivateIpAddresses.$c.PrivateIpAddress" => $_ );
140 push @parm,("PrivateIpAddresses.$c.Primary" => 'false');
141 }
142 }
143 }
144 push @parm,('SecondaryPrivateIpAddressCount'=>$auto_count) if $auto_count ||= $args{-secondary_ip_address_count};
145  
146 $self->call('CreateNetworkInterface',@parm);
147 }
148  
149 =head2 $result = $ec2->delete_network_interface($network_interface_id);
150  
151 =head2 $result = $ec2->delete_network_interface(-network_interface_id => $id);
152  
153 Deletes the specified network interface. Returns a boolean indicating
154 success of the delete operation.
155  
156 =cut
157  
158 sub delete_network_interface {
159 my $self = shift;
160 my %args = $self->args(-network_interface_id => @_);
161 my @param = $self->single_parm(NetworkInterfaceId=>\%args);
162 return $self->call('DeleteNetworkInterface',@param);
163 }
164  
165 =head2 @ifs = $ec2->describe_network_interfaces(@interface_ids)
166  
167 =head2 @ifs = $ec2->describe_network_interfaces(\%filters)
168  
169 =head2 @ifs = $ec2->describe_network_interfaces(-network_interface_id=>\@interface_ids,-filter=>\%filters)
170  
171 Return a list of elastic network interfaces as
172 VM::EC2::VPC::NetworkInterface objects. You may restrict the list by
173 passing a list of network interface IDs, a hashref of filters or by
174 using the full named-parameter form.
175  
176 Optional arguments:
177  
178 -network_interface_id A single network interface ID or an arrayref to
179 a list of IDs.
180  
181 -filter A hashref for filtering on tags and other attributes.
182  
183 The list of valid filters can be found at
184 http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeNetworkInterfaces.html.
185  
186 =cut
187  
188 sub describe_network_interfaces {
189 my $self = shift;
190 my %args = $self->args(-network_interface_id=>@_);
191 my @params = $self->list_parm('NetworkInterfaceId',\%args);
192 push @params,$self->filter_parm(\%args);
193 return $self->call('DescribeNetworkInterfaces',@params);
194 }
195  
196 =head2 @data = $ec2->describe_network_interface_attribute($network_id,$attribute)
197  
198 This method returns network interface attributes. Only one attribute
199 can be retrieved at a time. The following is the list of attributes
200 that can be retrieved:
201  
202 description -- hashref
203 groupSet -- hashref
204 sourceDestCheck -- hashref
205 attachment -- hashref
206  
207 These values can be retrieved more conveniently from the
208 L<VM::EC2::NetworkInterface> object, so there is no attempt to parse
209 the results of this call into Perl objects.
210  
211 =cut
212  
213 sub describe_network_interface_attribute {
214 my $self = shift;
215 @_ == 2 or croak "Usage: describe_network_interface_attribute(\$interface_id,\$attribute_name)";
216 my ($interface_id,$attribute) = @_;
217 my @param = (NetworkInterfaceId=>$interface_id,Attribute=>$attribute);
218 my $result = $self->call('DescribeNetworkInterfaceAttribute',@param);
219 return $result && $result->attribute($attribute);
220 }
221  
222 =head2 $boolean = $ec2->modify_network_interface_attribute($interface_id,-$attribute_name=>$value)
223  
224 This method changes network interface attributes. Only one attribute can be set per call
225 The following is the list of attributes that can be set:
226  
227 -description -- interface description
228 -security_group_id -- single security group ID or arrayref to a list of group ids
229 -source_dest_check -- boolean; if false enables packets to be forwarded, and is necessary
230 for NAT and other router tasks
231 -delete_on_termination -- [$attachment_id=>$delete_on_termination]; Pass this a two-element
232 array reference consisting of the attachment ID and a boolean
233 indicating whether deleteOnTermination should be enabled for
234 this attachment.
235 =cut
236  
237 sub modify_network_interface_attribute {
238 my $self = shift;
239 my $interface_id = shift or croak "Usage: modify_network_interface_attribute(\$interfaceId,%param)";
240 my %args = @_;
241 my @param = (NetworkInterfaceId=>$interface_id);
242 push @param,$self->value_parm($_,\%args) foreach qw(Description SourceDestCheck);
243 push @param,$self->list_parm('SecurityGroupId',\%args);
244 if (my $dot = $args{-delete_on_termination}) {
245 my ($attachment_id,$delete_on_termination) = @$dot;
246 push @param,'Attachment.AttachmentId'=>$attachment_id;
247 push @param,'Attachment.DeleteOnTermination'=>$delete_on_termination ? 'true' : 'false';
248 }
249 return $self->call('ModifyNetworkInterfaceAttribute',@param);
250 }
251  
252 =head2 $boolean = $ec2->reset_network_interface_attribute($interface_id => $attribute_name)
253  
254 This method resets the named network interface attribute to its
255 default value. Only one attribute can be reset per call. The AWS
256 documentation is not completely clear on this point, but it appears
257 that the only attribute that can be reset using this method is:
258  
259 source_dest_check -- Turns on source destination checking
260  
261 For consistency with modify_network_interface_attribute, you may
262 specify attribute names with or without a leading dash, and using
263 either under_score or mixedCase naming:
264  
265 $ec2->reset_network_interface_atribute('eni-12345678' => 'source_dest_check');
266 $ec2->reset_network_interface_atribute('eni-12345678' => '-source_dest_check');
267 $ec2->reset_network_interface_atribute('eni-12345678' => sourceDestCheck);
268  
269 =cut
270  
271 sub reset_network_interface_attribute {
272 my $self = shift;
273 @_ == 2 or croak "Usage: reset_network_interface_attribute(\$interfaceId,\$attribute)";
274 my ($interface_id,$attribute) = @_;
275  
276 $attribute = s/^-//;
277 $attribute = $self->uncanonicalize($attribute);
278 my @param = (NetworkInterfaceId=> $interface_id,
279 Attribute => $attribute
280 );
281 return $self->call('ResetNetworkInterfaceAttribute',@param);
282 }
283  
284 =head2 $attachmentId = $ec2->attach_network_interface($network_interface_id,$instance_id,$device_index)
285  
286 =head2 $attachmentId = $ec2->attach_network_interface(-network_interface_id => $id,
287 -instance_id => $id,
288 -device_index => $index)
289  
290 This method attaches a network interface to an instance using the
291 indicated device index. You can use instance and network interface
292 IDs, or VM::EC2::Instance and VM::EC2::NetworkInterface objects. You
293 may use an integer for -device_index, or use the strings "eth0",
294 "eth1" etc.
295  
296 Required arguments:
297  
298 -network_interface_id ID of the network interface to attach.
299 -instance_id ID of the instance to attach the interface to.
300 -device_index Network device number to use (e.g. 0 for eth0).
301  
302 On success, this method returns the attachmentId of the new attachment
303 (not a VM::EC2::NetworkInterface::Attachment object, due to an AWS API
304 inconsistency).
305  
306 Note that it may be more convenient to attach and detach network
307 interfaces via methods in the VM::EC2::Instance and
308 VM::EC2::NetworkInterface objects:
309  
310 $instance->attach_network_interface($interface=>'eth0');
311 $interface->attach($instance=>'eth0');
312  
313 =cut
314  
315 sub attach_network_interface {
316 my $self = shift;
317 my %args;
318 if ($_[0] !~ /^-/ && @_ == 3) {
319 @args{qw(-network_interface_id -instance_id -device_index)} = @_;
320 } else {
321 %args = @_;
322 }
323 $args{-network_interface_id} && $args{-instance_id} && defined $args{-device_index} or
324 croak "-network_interface_id, -instance_id and -device_index arguments must all be specified";
325  
326 $args{-device_index} =~ s/^eth//;
327
328 my @param = $self->single_parm(NetworkInterfaceId=>\%args);
329 push @param,$self->single_parm(InstanceId=>\%args);
330 push @param,$self->single_parm(DeviceIndex=>\%args);
331 return $self->call('AttachNetworkInterface',@param);
332 }
333  
334 =head2 $boolean = $ec2->detach_network_interface($attachment_id [,$force])
335  
336 This method detaches a network interface from an instance. Both the
337 network interface and instance are specified using their
338 attachmentId. If the $force flag is present, and true, then the
339 detachment will be forced even if the interface is in use.
340  
341 Note that it may be more convenient to attach and detach network
342 interfaces via methods in the VM::EC2::Instance and
343 VM::EC2::NetworkInterface objects:
344  
345 $instance->detach_network_interface($interface);
346 $interface->detach();
347  
348 =cut
349  
350 sub detach_network_interface {
351 my $self = shift;
352 my ($attachment_id,$force) = @_;
353 $attachment_id or croak "Usage: detach_network_interface(\$attachment_id [,\$force])";
354 my @param = (AttachmentId => $attachment_id);
355 push @param,(Force => 'true') if defined $force && $force;
356 return $self->call('DetachNetworkInterface',@param);
357 }
358  
359  
360  
361 =head1 SEE ALSO
362  
363 L<VM::EC2>
364  
365 =head1 AUTHOR
366  
367 Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>.
368  
369 Copyright (c) 2011 Ontario Institute for Cancer Research
370  
371 This package and its accompanying libraries is free software; you can
372 redistribute it and/or modify it under the terms of the GPL (either
373 version 1, or at your option, any later version) or the Artistic
374 License 2.0. Refer to LICENSE for the full license text. In addition,
375 please see DISCLAIMER.txt for disclaimers of warranty.
376  
377 =cut
378  
379  
380 1;