refines use of truthy values for Json true/false values

This commit is contained in:
Jed Reynolds
2018-10-23 22:54:45 -07:00
parent b176608400
commit 9dfb2574fb
3 changed files with 47 additions and 15 deletions

View File

@@ -67,7 +67,7 @@ sub json_request {
}
return {};
}
#print Dumper($response);
#print Dumper($response->content);
return $::Decoder->decode($response->content);
}
@@ -107,9 +107,10 @@ sub flatten_list {
my $list_name = shift;
my $rh_irefs = {};
return if (!defined $rh_list);
#print "\n- FF ------------------------------------------------------------\n";
#print Dumper($rh_list);
#print "\n~ FF ------------------------------------------------------------\n";
if (!defined $rh_list->{$list_name}) {
print Dumper($rh_list);
print "flatten_list: $list_name not found\n";
return;
}
@@ -123,14 +124,23 @@ sub flatten_list {
print "-------------------------------------------------\n";
return;
}
for (my $i=0; $i < @{$rh_list->{$list_name}}; $i++) {
my @k = keys(%{$rh_list->{$list_name}[$i]});
#print Dumper(\@k);
my $id = $k[0];
#print "ID[$id]\n";
$rh_irefs->{$id} = $rh_list->{$list_name}[$i]->{$id};
#print "\n- FG -------------------------------------------------------------------\n";
#print Dumper($rh_list->{$list_name});
#print "\n~ FG -------------------------------------------------------------------\n";
my $v = @{$rh_list->{$list_name}};
my @k = (@{$rh_list->{$list_name}});
for (my $i=0; $i < $v; $i++) {
my @rh_k = keys %{$k[$i]};
my @rh_v = values %{$k[$i]};
my $rh_id = $rh_k[0];
#print "\n- FG -------------------------------------------------------------------\n";
#print Dumper($rh_id);
#print "\n~ FG -------------------------------------------------------------------\n";
$rh_irefs->{$rh_id} = $rh_v[0];
}
#print "\n- FG -------------------------------------------------------------------\n";
#print Dumper($rh_irefs);
#print "\n~ FG -------------------------------------------------------------------\n";
$rh_list->{"flat_list"} = $rh_irefs;
}

View File

@@ -59,22 +59,42 @@ my @ports_up= ();
# TODO: make this a JsonUtils::list_ports()
$uri = "/port/1/${des_resource}/list?fields=alias,device,down,phantom,port";
logg("requesting $uri");
logg("requesting $uri\n");
$rh = json_request($uri);
#print Dumper($rh);
flatten_list($rh, 'interfaces');
#print "\n- 1 -------------------------------------------------------------\n";
#print Dumper($rh->{'flat_list'});
#print "\n- 2 -------------------------------------------------------------\n";
#print Dumper(\(keys %{$rh->{'flat_list'}}));
for my $rh_p (keys %{$rh->{'flat_list'}}) {
print " $rh_p ".$rh->{'flat_list'}->{$rh_p}->{'down'};
if ("false" eq $rh->{'flat_list'}->{$rh_p}->{'down'}) {
#print "\n- 1 -------------------------------------------------------------\n";
#print Dumper($rh->{'flat_list'}->{$rh_p});
#print "\n".ref ($rh->{'flat_list'}->{$rh_p}->{'down'});
#print "\n".$rh->{'flat_list'}->{$rh_p}->{'down'}();
#print "\n- 2 -------------------------------------------------------------\n";
#.$rh->{'flat_list'}->{$rh_p}->{'down'};
my $onoff = $rh->{'flat_list'}->{$rh_p}->{'down'};
#print "truth? $onoff\n";
if ($onoff) { # should force a truthy value
push(@ports_up, $rh_p);
}
}
#print "\n- 3 -------------------------------------------------------------\n";
#print Dumper(\@ports_up);
#print "\n- 4 -------------------------------------------------------------\n";
# find first station
my $rh_sta;
print Dumper(\@ports_up);
#print "\n- 5 -------------------------------------------------------------\n";
#print Dumper(\@ports_up);
#print "\n- 5 -------------------------------------------------------------\n";
for my $rh_up (@ports_up) {
#print "\n- 6 -------------------------------------------------------------\n";
#print Dumper($rh->{'flat_list'});
#print "\n- 6 -------------------------------------------------------------\n";
my $eid = $rh->{'flat_list'}->{$rh_up}->{'port'};
print ("rhup: $rh_up; EID $eid\n");
my @hunks = split(/[.]/, $eid);
if ($hunks[1]) {
$rh_sta = $rh_up;

View File

@@ -65,8 +65,10 @@ $uri = "/port/1/${des_resource}/list?fields=alias,device,down,phantom,port";
$rh = json_request($uri);
flatten_list($rh, 'interfaces');
for my $rh_p (keys %{$rh->{'flat_list'}}) {
print " down? $rh->{'flat_list'}->{$rh_p}->{'down'} ";
if ($rh->{'flat_list'}->{$rh_p}->{'down'} eq "false") {
# truthy value evaluates better
my $onoff = $rh->{'flat_list'}->{$rh_p}->{'down'};
print "$rh_p down? $onoff ";
if ($onoff) {
push(@ports_up, $rh_p);
}
}