mirror of https://github.com/sipwise/ngcpcfg.git
will provide a new instance_info variable with the all the content defined at network.yml for the instance removed intance_name since instance_info.name has the same value Change-Id: I349d0b430b59280bc383d46d5ba6a61f5d1f9046mr10.3.1
parent
e736773711
commit
664ad7ddfe
@ -0,0 +1,65 @@
|
||||
#!/usr/bin/perl -CSD
|
||||
# Purpose: generate yaml output with instance_info for bash script
|
||||
################################################################################
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use YAML::XS;
|
||||
use Time::Piece;
|
||||
use Getopt::Long qw(:config posix_default bundling_values no_ignorecase);
|
||||
|
||||
my $DEBUG = $ENV{DEBUG} || 0;
|
||||
my $HNAME = $ENV{HNAME} // '';
|
||||
my $TIME_FORMAT = $ENV{TIME_FORMAT} // '%F %T';
|
||||
$TIME_FORMAT =~ s/^\+//;
|
||||
|
||||
my $NETWORK_CONFIG = $ENV{NETWORK_CONFIG};
|
||||
|
||||
sub usage {
|
||||
print <<HELP;
|
||||
Usage: $0 [<option>...] instance_name
|
||||
|
||||
Options:
|
||||
-h, --help This help message.
|
||||
HELP
|
||||
}
|
||||
|
||||
if (@ARGV != 1) {
|
||||
error("wrong number of arguments");
|
||||
usage();
|
||||
exit 1;
|
||||
}
|
||||
my %options = ( help => sub { usage(); exit 0; }, );
|
||||
|
||||
error("NETWORK_CONFIG is not defined") unless $NETWORK_CONFIG;
|
||||
|
||||
GetOptions( \%options, 'help|?', );
|
||||
|
||||
my $yaml = YAML::XS::LoadFile($NETWORK_CONFIG);
|
||||
|
||||
sub output_prefix {
|
||||
my $t = Time::Piece->new;
|
||||
my $timestamp = $t->strftime($TIME_FORMAT);
|
||||
|
||||
return "$timestamp $HNAME";
|
||||
}
|
||||
|
||||
sub error {
|
||||
my $prefix = output_prefix();
|
||||
die "$prefix: Error: @_\n";
|
||||
}
|
||||
|
||||
exit 1 unless defined $yaml->{instances};
|
||||
my $instance_name = $ARGV[0];
|
||||
|
||||
foreach my $instance ( sort @{ $yaml->{instances} } ) {
|
||||
if ($instance->{name} eq $instance_name) {
|
||||
my $out = {
|
||||
instance_info => $instance,
|
||||
};
|
||||
print Dump($out);
|
||||
exit 0;
|
||||
}
|
||||
}
|
||||
exit 1;
|
||||
@ -0,0 +1,14 @@
|
||||
---
|
||||
instance_info:
|
||||
host: lb01b
|
||||
interfaces:
|
||||
- ip: 172.30.52.204
|
||||
name: vlan1719
|
||||
type: sip_ext
|
||||
- ip: 172.30.52.146
|
||||
name: vlan1720
|
||||
type: sip_int
|
||||
label: lb
|
||||
name: A
|
||||
service: kamailio-lb
|
||||
status: online
|
||||
@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env py.test-3
|
||||
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
import re
|
||||
from fixtures.fs import check_stdoutput
|
||||
|
||||
|
||||
def test_instance_info_noargs(helpercli):
|
||||
out = helpercli(
|
||||
"instance-info",
|
||||
env={
|
||||
"NETWORK_CONFIG": "fixtures/repos/network_carrier_instances.yml",
|
||||
},
|
||||
)
|
||||
assert out.returncode != 0
|
||||
assert "Error: wrong number of arguments" in out.stderr
|
||||
|
||||
|
||||
def test_instances_info_no_instance(helpercli):
|
||||
out = helpercli(
|
||||
"instance-info",
|
||||
"C",
|
||||
env={
|
||||
"NETWORK_CONFIG": "fixtures/repos/network_carrier_instances.yml",
|
||||
},
|
||||
)
|
||||
assert out.returncode != 0
|
||||
|
||||
|
||||
def test_instance_info(helpercli, tmpdir):
|
||||
out = helpercli(
|
||||
"instance-info",
|
||||
"A",
|
||||
env={
|
||||
"NETWORK_CONFIG": "fixtures/repos/network_carrier_instances.yml",
|
||||
},
|
||||
)
|
||||
|
||||
test_file = "fixtures/output/instance_info_A"
|
||||
assert out.returncode == 0
|
||||
check_stdoutput(out.stdout, test_file, tmpdir)
|
||||
Loading…
Reference in new issue