lib/VM/EC2/REST/elastic_network_interface.pm

filter: Statementstmt 9.2% 8 / 87 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 18.1% 2 / 11 Podpod 0.0% 0 / 8 totaltotal 5.2% 10 / 191 CC 31 SCAR 47.8
CC
31
Cov
5%
create_network_interface
272.0
attach_network_interface
42.0
modify_network_interface_attribute
30.0
CRAP 119.3
« lib/VM/EC2/ParmParser.pm ↑ Summary lib/VM/EC2/REST/route_table.pm »
1 package VM::EC2::REST::elastic_network_interface;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶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 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 0 ▶ AttachNetworkInterface => sub { shift->{attachmentId} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
14 1 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 0 ▶ my $self = shift;
Subroutine create_network_interface: not called CC=16 SCAR=56.1
Pod undocumented
109 0 my %args = $self->args(-subnet_id=>@_);
110 0 ▶ $args{-subnet_id} or croak "Usage: create_network_interface(-subnet_id=>\$id,\@more_args)";
Branch: 1 branch
BranchTrueFalseTotal
$args{"-subnet_id"} or croak("Usage: create_network_interface(-subnet_id=>\$id,\@more_args)") 0 0 0
111 0 my @parm = $self->single_parm('SubnetId',\%args);
112 0 push @parm, $self->single_parm('Description',\%args);
113 0 push @parm, $self->list_parm('SecurityGroupId',\%args);
114  
115 0 ▶ my $address = $args{-private_ip_address} || $args{-private_ip_addresses};
Condition0%0/3
$args{'-private_ip_address'} || $args{'-private_ip_addresses'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'-private_ip_address'} || $args{'-private_ip_addresses'}
$args{'-private_ip_address'} $args{'-private_ip_addresses'}
Truth table0%0/3
A || B
A= $args{'-private_ip_address'}B= $args{'-private_ip_addresses'}
ABresult
1X1
011
000
116 0 my $auto_count;
117  
118 0 ▶ if ($address) {
Branch: 1 branch
BranchTrueFalseTotal
if ($address) 0 0 0
119 0 my $c = 0;
120  
121 0 ▶ my @addresses = ref $address && ref $address eq 'ARRAY' ? @$address : ($address);
Branch: 1 branch
BranchTrueFalseTotal
ref $address && ref $address eq 'ARRAY' ? : 0 0 0
Condition0%0/3
ref $address and ref $address eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $address and ref $address eq "ARRAY"
ref $address ref $address eq "ARRAY"
Truth table0%0/3
A and B
A= ref $addressB= ref $address eq "ARRAY"
ABresult
0X0
100
111
122 0 my $primary = shift @addresses;
123 0 ▶ unless ($primary eq 'auto') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($primary eq "auto") 0 0 0
124 0 push @parm, ("PrivateIpAddresses.$c.PrivateIpAddress" => $primary);
125 0 push @parm, ("PrivateIpAddresses.$c.Primary" => 'true');
126 }
127  
128 # deal with automatic secondary addresses .. this seems needlessly complex
129 0 ▶ if (my @auto = grep {/auto/i} @addresses) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@auto) = grep({/auto/i;} @addresses)) 0 0 0
130 0 ▶ @auto == @addresses or croak "cannot request both explicit and automatic secondary IP addresses";
Branch: 1 branch
BranchTrueFalseTotal
@auto == @addresses or croak("cannot request both explicit and automatic secondary IP addresses") 0 0 0
131 0 $auto_count = @auto;
132 }
133 0 ▶ $auto_count = $addresses[0] if @addresses == 1 && $addresses[0] =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if @addresses == 1 and $addresses[0] =~ /^\d+$/ 0 0 0
Condition0%0/3
@addresses == 1 and $addresses[0] =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
@addresses == 1 and $addresses[0] =~ /^\d+$/
@addresses == 1 $addresses[0] =~ /^\d+$/
Truth table0%0/3
A and B
A= @addresses == 1B= $addresses[0] =~ /^\d+$/
ABresult
0X0
100
111
134 0 ▶ $auto_count ||= $args{-secondary_ip_address_count};
Condition0%0/3
$auto_count ||= $args{"-secondary_ip_address_count"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$auto_count ||= $args{"-secondary_ip_address_count"}
$auto_count $args{"-secondary_ip_address_count"}
Truth table0%0/3
A ||= B
A= $auto_countB= $args{"-secondary_ip_address_count"}
ABresult
1X1
011
000
135
136 0 ▶ unless ($auto_count) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($auto_count) 0 0 0
137 0 foreach (@addresses) {
138 0 $c++;
139 0 push @parm,("PrivateIpAddresses.$c.PrivateIpAddress" => $_ );
140 0 push @parm,("PrivateIpAddresses.$c.Primary" => 'false');
141 }
142 }
143 }
144 0 ▶ push @parm,('SecondaryPrivateIpAddressCount'=>$auto_count) if $auto_count ||= $args{-secondary_ip_address_count};
Branch: 1 branch
BranchTrueFalseTotal
if $auto_count ||= $args{"-secondary_ip_address_count"} 0 0 0
Condition0%0/3
$auto_count ||= $args{"-secondary_ip_address_count"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$auto_count ||= $args{"-secondary_ip_address_count"}
$auto_count $args{"-secondary_ip_address_count"}
Truth table0%0/3
A ||= B
A= $auto_countB= $args{"-secondary_ip_address_count"}
ABresult
1X1
011
000
145  
146 0 $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 0 ▶ my $self = shift;
Subroutine delete_network_interface: not called CC=1 SCAR=6.9
Pod undocumented
160 0 my %args = $self->args(-network_interface_id => @_);
161 0 my @param = $self->single_parm(NetworkInterfaceId=>\%args);
162 0 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 0 ▶ my $self = shift;
Subroutine describe_network_interfaces: not called CC=1 SCAR=6.9
Pod undocumented
190 0 my %args = $self->args(-network_interface_id=>@_);
191 0 my @params = $self->list_parm('NetworkInterfaceId',\%args);
192 0 push @params,$self->filter_parm(\%args);
193 0 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 0 ▶ my $self = shift;
Subroutine describe_network_interface_attribute: not called CC=3 SCAR=24.8
Pod undocumented
215 0 ▶ @_ == 2 or croak "Usage: describe_network_interface_attribute(\$interface_id,\$attribute_name)";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or croak("Usage: describe_network_interface_attribute(\$interface_id,\$attribute_name)") 0 0 0
216 0 my ($interface_id,$attribute) = @_;
217 0 my @param = (NetworkInterfaceId=>$interface_id,Attribute=>$attribute);
218 0 my $result = $self->call('DescribeNetworkInterfaceAttribute',@param);
219 0 ▶ return $result && $result->attribute($attribute);
Condition0%0/3
$result && $result->attribute($attribute)
!ll&&!rl&&r
000
MC/DC0%0/2
$result && $result->attribute($attribute)
$result $result->attribute($attribute)
Truth table0%0/3
A && B
A= $resultB= $result->attribute($attribute)
ABresult
0X0
100
111
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 0 ▶ my $self = shift;
Subroutine modify_network_interface_attribute: not called CC=5 SCAR=34.0
Pod undocumented
239 0 ▶ my $interface_id = shift or croak "Usage: modify_network_interface_attribute(\$interfaceId,%param)";
Branch: 1 branch
BranchTrueFalseTotal
my $interface_id = shift() or croak("Usage: modify_network_interface_attribute(\$interfaceId,%param)") 0 0 0
240 0 my %args = @_;
241 0 my @param = (NetworkInterfaceId=>$interface_id);
242 0 push @param,$self->value_parm($_,\%args) foreach qw(Description SourceDestCheck);
243 0 push @param,$self->list_parm('SecurityGroupId',\%args);
244 0 ▶ if (my $dot = $args{-delete_on_termination}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dot = $args{"-delete_on_termination"}) 0 0 0
245 0 my ($attachment_id,$delete_on_termination) = @$dot;
246 0 push @param,'Attachment.AttachmentId'=>$attachment_id;
247 0 ▶ push @param,'Attachment.DeleteOnTermination'=>$delete_on_termination ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$delete_on_termination ? : 0 0 0
248 }
249 0 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 0 ▶ my $self = shift;
Subroutine reset_network_interface_attribute: not called CC=2 SCAR=17.9
Pod undocumented
273 0 ▶ @_ == 2 or croak "Usage: reset_network_interface_attribute(\$interfaceId,\$attribute)";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or croak("Usage: reset_network_interface_attribute(\$interfaceId,\$attribute)") 0 0 0
274 0 my ($interface_id,$attribute) = @_;
275  
276 0 $attribute = s/^-//;
277 0 $attribute = $self->uncanonicalize($attribute);
278 0 my @param = (NetworkInterfaceId=> $interface_id,
279 Attribute => $attribute
280 );
281 0 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 0 ▶ my $self = shift;
Subroutine attach_network_interface: not called CC=6 SCAR=37.4
Pod undocumented
317 0 my %args;
318 0 ▶ if ($_[0] !~ /^-/ && @_ == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[0] =~ /^-/ and @_ == 3) { } 0 0 0
Condition0%0/3
not $_[0] =~ /^-/ and @_ == 3
!ll&&!rl&&r
000
MC/DC0%0/2
not $_[0] =~ /^-/ and @_ == 3
not $_[0] =~ /^-/ @_ == 3
Truth table0%0/3
not(A) and B
A= not $_[0] =~ /^-/B= @_ == 3
ABresult
0X0
100
111
319 0 @args{qw(-network_interface_id -instance_id -device_index)} = @_;
320 } else {
321 0 %args = @_;
322 }
323 $args{-network_interface_id} && $args{-instance_id} && defined $args{-device_index} or
324 0 ▶ croak "-network_interface_id, -instance_id and -device_index arguments must all be specified";
Branch: 1 branch
BranchTrueFalseTotal
$args{"-network_interface_id"} and $args{"-instance_id"} and defined $args{"-device_index"} or croak("-network_interface_id, -instance_id and -device_index arguments must all be specified") 0 0 0
Condition0%0/6
$args{"-network_interface_id"} and $args{"-instance_id"} and defined $args{"-device_index"}
!ll&&!rl&&r
000
$args{"-network_interface_id"} and $args{"-instance_id"}
!ll&&!rl&&r
000
MC/DC0%0/3
$args{"-network_interface_id"} and $args{"-instance_id"} and defined $args{"-device_index"}
$args{"-network_interface_id"} $args{"-instance_id"} defined $args{"-device_index"}
Truth table0%0/4
A and B and C
A= $args{"-network_interface_id"}B= $args{"-instance_id"}C= defined $args{"-device_index"}
ABCresult
0XX0
10X0
1100
1111
325  
326 0 $args{-device_index} =~ s/^eth//;
327
328 0 my @param = $self->single_parm(NetworkInterfaceId=>\%args);
329 0 push @param,$self->single_parm(InstanceId=>\%args);
330 0 push @param,$self->single_parm(DeviceIndex=>\%args);
331 0 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 0 ▶ my $self = shift;
Subroutine detach_network_interface: not called CC=4 SCAR=30.0
Pod undocumented
352 0 my ($attachment_id,$force) = @_;
353 0 ▶ $attachment_id or croak "Usage: detach_network_interface(\$attachment_id [,\$force])";
Branch: 1 branch
BranchTrueFalseTotal
$attachment_id or croak("Usage: detach_network_interface(\$attachment_id [,\$force])") 0 0 0
354 0 my @param = (AttachmentId => $attachment_id);
355 0 ▶ push @param,(Force => 'true') if defined $force && $force;
Branch: 1 branch
BranchTrueFalseTotal
if defined $force and $force 0 0 0
Condition0%0/3
defined $force and $force
!ll&&!rl&&r
000
MC/DC0%0/2
defined $force and $force
defined $force $force
Truth table0%0/3
A and B
A= defined $forceB= $force
ABresult
0X0
100
111
356 0 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 1;
« lib/VM/EC2/ParmParser.pm ↑ Summary lib/VM/EC2/REST/route_table.pm »