From a8f160af48f69ef2bbf6a49e27468371ce6bebcf Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Tue, 25 May 2021 16:56:40 -0400 Subject: [PATCH] fix(cgi-bin): add logic to build get_networks response --- cgi-bin/get_networks | 145 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 141 insertions(+), 4 deletions(-) diff --git a/cgi-bin/get_networks b/cgi-bin/get_networks index f20ec7dc..8a507a4d 100644 --- a/cgi-bin/get_networks +++ b/cgi-bin/get_networks @@ -38,6 +38,145 @@ sub handle_invalid_uuid $anvil->nice_exit({ exit_code => 1 }); } +sub get_anvil_networks +{ + my $parameters = shift; + my $anvil_uuid = $parameters->{anvil_uuid}; + + my @host_uuids = ( + $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid}, + $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node2_host_uuid} + ); + my $anvil_networks = []; + + foreach (@host_uuids) + { + my $host_networks = get_host_networks({ host_uuid => $_ }); + + push(@$anvil_networks, $host_networks); + } + + return $anvil_networks; +} + +sub get_host_networks +{ + my $parameters = shift; + my $host_uuid = $parameters->{host_uuid}; + my $host_name = $anvil->data->{hosts}{host_uuid}{$host_uuid}{short_host_name}; + + # Note: the subprocess name matches its definition; there is a typo though. + $anvil->Network->load_interfces({ host_uuid => $host_uuid, host => $host_name }); + + my $host_networks_data = $anvil->data->{network}{$host_name}; + my $interfaces = $host_networks_data->{interface}; + my $bond_uuids = $host_networks_data->{bond_uuid}; + my $bond_uuid_index_map = {}; + my $host_networks = { + host_uuid => $host_uuid, + host_name => $host_name, + bonds => [] + }; + + my $build_bond_link_hash = sub { + my $parameters = shift; + my $bond_data = $parameters->{bond_data}; + my $link_name = $parameters->{link_name}; + my $link_data = $parameters->{link_data}; + + return { + link_name => $link_name, + link_uuid => $link_data->{uuid}, + link_speed => $link_data->{speed}, + link_state => "", + is_active => $link_name eq $bond_data->{active_interface} ? \1 : \0 + }; + }; + + foreach my $interface_name (sort { $a cmp $b } keys %$interfaces) + { + my $interface = $interfaces->{$interface_name}; + # Only a bond's link will have the bond_uuid entry. + my $bond_uuid = $interface->{bond_uuid}; + + $anvil->Log->variables({ + source => $THIS_FILE, + line => __LINE__, + level => 2, + list => { + interface_name => $interface_name, + bond_uuid => $bond_uuid + } + }); + + if ($bond_uuid) + { + my $bond_name = $bond_uuids->{$bond_uuid}{name}; + my $bond_index = $bond_uuid_index_map->{$bond_uuid}; + + $anvil->Log->variables({ + source => $THIS_FILE, + line => __LINE__, + level => 2, + list => { + bond_name => $bond_name, + bond_index => $bond_index + } + }); + + if (defined $bond_index) + { + my $bond = @{$host_networks->{bonds}}[$bond_index]; + + $anvil->Log->variables({ + source => $THIS_FILE, + line => __LINE__, + level => 2, + list => { + message => "INDEX DEFINED", + bond => Dumper($bond) + } + }); + + push(@{$bond->{links}}, &$build_bond_link_hash({ + bond_data => $interfaces->{$bond_name}, + link_name => $interface_name, + link_data => $interface + })); + } + else + { + my $bond = { bond_name => $bond_name, bond_uuid => $bond_uuid, links => [] }; + my $link = &$build_bond_link_hash({ + bond_data => $interfaces->{$bond_name}, + link_name => $interface_name, + link_data => $interface + }); + + $anvil->Log->variables({ + source => $THIS_FILE, + line => __LINE__, + level => 2, + list => { + message => "INDEX UNDEFINED", + bond => Dumper($bond), + 'link' => Dumper($link) + } + }); + + push(@{$bond->{links}}, $link); + + push(@{$host_networks->{bonds}}, $bond); + + # Get the index of the most recently inserted item and map it to the bond's UUID. + $bond_uuid_index_map->{$bond_uuid} = $#{$host_networks->{bonds}}; + } + } + } + + return $host_networks; +} + $anvil->Get->switches; # Temporary; for debugging @@ -69,7 +208,9 @@ if ($anvil_uuid) { if (exists $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}) { + my $anvil_networks = get_anvil_networks({ anvil_uuid => $anvil_uuid }); + $response_body->{nodes} = $anvil_networks; } else { @@ -81,8 +222,4 @@ else handle_invalid_uuid({ name => $anvil_uuid_variable_name, uuid => $anvil_uuid }); } -my @host_uuids = ( - $anvil->data->{anvils}{anvil_uuid}{} -) - print JSON->new->utf8->encode($response_body)."\n";