TT#50701 Refactor IPv6 address expansion into an object function

This centralizes the logic so that we have a single implementation.

Change-Id: I7a8e60ddd6acde915047b4c9bfc8be737b40c884
changes/36/26736/6
Guillem Jover 7 years ago
parent 5bcd29f226
commit c0fd959126

@ -165,6 +165,20 @@ sub get_ssh_pub_key
return $ssh_pub_key;
}
sub net_ip_expand
{
my ($self, $ip) = @_;
while ($ip =~ m/::/ && (() = $ip =~ m/:/g) < 8) {
$ip =~ s/::/::0:/;
}
$ip =~ s/::/:/;
$ip =~ s/^:/0:/;
$ip =~ s/:$/:0/;
return $ip;
}
1;
__END__
@ -230,6 +244,10 @@ Returns the NGCP management node shared name.
Returns the SSH public key with $key_type ('rsa', 'ed25519', etc.) from
the ngcpcfg shared-files storage.
=item $ip = $t->net_ip_expand($ipaddr)
Returns the expanded form of the IP address. Supports IPv4 and IPv6.
=back
=head1 AUTHOR

@ -43,12 +43,7 @@ FOREACH host IN hosts.keys.sort;
blktmp.processed_hosts.$peer = 1;
FOREACH ip IN hosts.$host.$realiface.shared_v6ip;
IF argv.format == 'expand';
WHILE ip.match('::') && ip.match(':', 1).size < 8;
ip = ip.replace('::', '::0:');
END;
ip = ip.replace('::', ':');
ip = ip.replace('^:', '0:');
ip = ip.replace(':$', ':0');
ip = ngcp.net_ip_expand(ip);
END;
NEXT UNLESS ip;
out.push(ip);

@ -40,12 +40,7 @@ IF status.item(hosts.${argv.host}.status);
FOREACH ip IN hosts.${argv.host}.$realiface.shared_v6ip;
IF argv.format == 'expand';
WHILE ip.match('::') && ip.match(':', 1).size < 8;
ip = ip.replace('::', '::0:');
END;
ip = ip.replace('::', ':');
ip = ip.replace('^:', '0:');
ip = ip.replace(':$', ':0');
ip = ngcp.net_ip_expand(ip);
END;
NEXT UNLESS ip;
out.push(ip);

@ -39,12 +39,7 @@ FOREACH host IN hosts.keys.sort;
FOREACH ip IN hosts.$host.$realiface.v6ip;
IF argv.format == 'expand';
WHILE ip.match('::') && ip.match(':', 1).size < 8;
ip = ip.replace('::', '::0:');
END;
ip = ip.replace('::', ':');
ip = ip.replace('^:', '0:');
ip = ip.replace(':$', ':0');
ip = ngcp.net_ip_expand(ip);
END;
NEXT UNLESS ip;
out.push(ip);

@ -41,12 +41,7 @@ IF status.item(hosts.${argv.host}.status);
FOREACH ip IN hosts.${argv.host}.$realiface.v6ip;
IF argv.format == 'expand';
WHILE ip.match('::') && ip.match(':', 1).size < 8;
ip = ip.replace('::', '::0:');
END;
ip = ip.replace('::', ':');
ip = ip.replace('^:', '0:');
ip = ip.replace(':$', ':0');
ip = ngcp.net_ip_expand(ip);
END;
NEXT UNLESS ip;
out.push(ip);

@ -5,7 +5,7 @@ use warnings;
use Test::More;
plan tests => 25;
plan tests => 28;
use_ok('NGCP::Template::Object');
@ -95,3 +95,14 @@ is($obj->get_mgmt_node(), 'sp', 'spce has sp as mgmt node');
local $obj->{config}{general}{ngcp_type} = 'carrier';
is($obj->get_mgmt_node(), 'web01', 'carrier has web01 as mgmt node');
}
# Check net_ip_expand().
is($obj->net_ip_expand('1:2::5:20'),
'1:2:0:0:0:0:5:20',
'normalize IPv6 1:2::5:20');
is($obj->net_ip_expand('001:2::0005:020'),
'001:2:0:0:0:0:0005:020',
'normalize IPv6 001::0005:020');
is($obj->net_ip_expand('1::0'),
'1:0:0:0:0:0:0:0',
'normalize IPv6 1::0');

Loading…
Cancel
Save