MT#45670 NGCP::Template::Object: Add new get_hosts() method

This should help making use of get_hosts() from template files.

Change-Id: I0af925d3f118c04679f61be28b4db1bbb88d2b06
mr12.3
Guillem Jover 1 year ago
parent d207a67c6e
commit 367bbeb62e

@ -241,6 +241,31 @@ sub get_mgmt_node
return $self->get_mgmt_pairname();
}
sub get_hosts
{
my ($self, $filter) = @_;
my $site = $filter->{site} // 'current';
$filter->{status} //= [ qw(online inactive) ];
my %status = (
online => 0,
offline => 0,
inactive => 0,
);
$status{$_} = 1 foreach @{$filter->{status}};
my $hosts = $self->{config}{sites}{$site}{hosts};
my @hosts;
foreach my $host (sort keys %{$hosts}) {
next unless $status{$hosts->{$host}{status}};
push @hosts, $host;
}
return sort @hosts;
}
sub get_instances
{
my ($self, $hostname) = @_;
@ -416,6 +441,27 @@ Returns the NGCP management node pairname.
This function is a deprecated alias for $t->get_mgmt_pairname(). It will be
removed in the future.
=item @hosts = $t->get_hosts($filter)
Returns an array of hosts that match the %filter criteria.
If no %filter has been specified, it returns all hosts.
The current filter options are:
=over
=item B<site>
This is a scalar that specifies the site name to use.
It defaults to B<current>.
=item B<status>
This is an arrayref that contains a list of the status names to filter on,
from one of B<online>, B<offline> and B<inactive>.
The default status list is B<online> and B<inactive>.
=back
=item $ssh_pub_keye = $t->get_ssh_pub_key($key_type)
Returns the SSH public key with $key_type ('rsa', 'ed25519', etc.) from

@ -6,7 +6,7 @@ use warnings;
use Cwd;
use Test::More;
plan tests => 49;
plan tests => 56;
use_ok('NGCP::Template::Object');
@ -20,9 +20,11 @@ my $cfg_ce = {
hosts => {
self => {
role => [ qw(db lb proxy rtp) ],
status => 'online',
},
},
};
$cfg_ce->{sites}{current} = $cfg_ce;
my $cfg_pro = {
general => {
@ -35,15 +37,19 @@ my $cfg_pro = {
sp1 => {
role => [ qw(mgmt db lb proxy rtp storage) ],
peer => 'sp2',
status => 'online',
},
sp2 => {
peer => 'sp3',
status => 'inactive',
},
sp3 => {
peer => 'sp1',
status => 'offline',
},
},
};
$cfg_pro->{sites}{current} = $cfg_pro;
my $cfg_carrier = {
general => {
@ -60,19 +66,24 @@ my $cfg_carrier = {
role => [ qw(li proxy) ],
peer => 'prx01b',
dbnode => 'db01',
status => 'online',
},
web01a => {
role => [ qw(mgmt) ],
status => 'online',
},
web01b => {
role => [ qw(mgmt) ],
status => 'inactive',
},
web01c => {
role => [ qw(mgmt) ],
status => 'offline',
},
},
};
$cfg_carrier->{sites}{current} = $cfg_carrier;
my $obj_ce = NGCP::Template::Object->new($cfg_ce);
@ -179,6 +190,22 @@ is($obj_carrier->get_mgmt_pairname(), 'web01', 'carrier has web01 as mgmt pairna
is($obj_ce->get_dbnode('non-existent'), 'self', 'host unknown has self as dbnode');
is($obj_carrier->get_dbnode('prx01a'), 'db01', 'host prx01a has db01 as dbnode');
# Check get_hosts().
is_deeply([ $obj_ce->get_hosts() ],
[ qw(self) ], 'host list for CE (default)');
is_deeply([ $obj_pro->get_hosts() ],
[ qw(sp1 sp2) ], 'host list for PRO (default)');
is_deeply([ $obj_pro->get_hosts({ status => [ qw(online offline) ] }) ],
[ qw(sp1 sp3) ], 'host list for PRO (online offline)');
is_deeply([ $obj_pro->get_hosts({ status => [ qw(inactive) ] }) ],
[ qw(sp2) ], 'host list for PRO (inactive)');
is_deeply([ $obj_carrier->get_hosts() ],
[ qw(prx01a web01a web01b) ], 'host list for Carrier (default)');
is_deeply([ $obj_carrier->get_hosts({ status => [ qw(online offline) ] }) ],
[ qw(prx01a web01a web01c) ], 'host list for Carrier (online offline)');
is_deeply([ $obj_carrier->get_hosts({ status => [ qw(inactive) ] }) ],
[ qw(web01b) ], 'host list for Carrier (inactive)');
# Check net_ip_expand().
is($obj_ce->net_ip_expand('1:2::5:20'),
'1:2:0:0:0:0:5:20',

Loading…
Cancel
Save