* Fixed the order or table creations in anvil.sql (as well as fixed a missing comma)

* Fixed / added 'delete' parameters for Database->insert_or_update_bridges(), ->insert_or_update_bonds(), ->insert_or_update_ip_addresses() and ->insert_or_update_network_interfaces().

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 5 years ago
parent c34f56f2c8
commit 974d3c3411
  1. 308
      Anvil/Tools/Database.pm
  2. 16
      Anvil/Tools/Network.pm
  3. 232
      share/anvil.sql
  4. 4
      tools/test.pl

@ -2126,6 +2126,10 @@ This is the MTU (maximum transfer unit, size in bytes) of the bridge.
This is set to C<< yes >> or C<< no >> to indicate if spanning tree protocol is enabled on the switch. This is set to C<< yes >> or C<< no >> to indicate if spanning tree protocol is enabled on the switch.
=head2 delete (optional, default '0')
If set to C<< 1 >>, C<< bridge_id >> will be set to C<< DELETED >>, indicating that the bridge has been deleted from the system. If set, only C<< bridge_uuid >> or C<< bridge_name >> is needed.
=cut =cut
sub insert_or_update_bridges sub insert_or_update_bridges
{ {
@ -2138,6 +2142,7 @@ sub insert_or_update_bridges
my $uuid = defined $parameter->{uuid} ? $parameter->{uuid} : ""; my $uuid = defined $parameter->{uuid} ? $parameter->{uuid} : "";
my $file = defined $parameter->{file} ? $parameter->{file} : ""; my $file = defined $parameter->{file} ? $parameter->{file} : "";
my $line = defined $parameter->{line} ? $parameter->{line} : ""; my $line = defined $parameter->{line} ? $parameter->{line} : "";
my $delete = defined $parameter->{'delete'} ? $parameter->{'delete'} : "";
my $bridge_uuid = defined $parameter->{bridge_uuid} ? $parameter->{bridge_uuid} : ""; my $bridge_uuid = defined $parameter->{bridge_uuid} ? $parameter->{bridge_uuid} : "";
my $bridge_host_uuid = defined $parameter->{bridge_host_uuid} ? $parameter->{bridge_host_uuid} : $anvil->data->{sys}{host_uuid}; my $bridge_host_uuid = defined $parameter->{bridge_host_uuid} ? $parameter->{bridge_host_uuid} : $anvil->data->{sys}{host_uuid};
my $bridge_name = defined $parameter->{bridge_name} ? $parameter->{bridge_name} : ""; my $bridge_name = defined $parameter->{bridge_name} ? $parameter->{bridge_name} : "";
@ -2149,6 +2154,7 @@ sub insert_or_update_bridges
uuid => $uuid, uuid => $uuid,
file => $file, file => $file,
line => $line, line => $line,
'delete' => $delete,
bridge_uuid => $bridge_uuid, bridge_uuid => $bridge_uuid,
bridge_host_uuid => $bridge_host_uuid, bridge_host_uuid => $bridge_host_uuid,
bridge_name => $bridge_name, bridge_name => $bridge_name,
@ -2157,12 +2163,15 @@ sub insert_or_update_bridges
bridge_mtu => $bridge_mtu, bridge_mtu => $bridge_mtu,
bridge_stp_enabled => $bridge_stp_enabled, bridge_stp_enabled => $bridge_stp_enabled,
}}); }});
if (not $bridge_name) if (not $delete)
{ {
# Throw an error and exit. if (not $bridge_name)
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_bridges()", parameter => "bridge_name" }}); {
return(""); # Throw an error and exit.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_bridges()", parameter => "bridge_name" }});
return("");
}
} }
# If we don't have a UUID, see if we can find one for the given bridge server name. # If we don't have a UUID, see if we can find one for the given bridge server name.
@ -2193,6 +2202,45 @@ AND
} }
} }
if ($delete)
{
if (not $bridge_uuid)
{
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_bridges()", parameter => "bridge_uuid" }});
return("");
}
else
{
# Delete it
my $query = "SELECT bridge_id FROM bridges WHERE bridge_uuid = ".$anvil->Database->quote($bridge_uuid).";";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
my $results = $anvil->Database->query({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
my $count = @{$results};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
results => $results,
count => $count,
}});
if ($count)
{
my $old_bridge_id = $results->[0]->[0];
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { old_bridge_id => $old_bridge_id }});
if ($old_bridge_id ne "DELETED")
{
my $query = "UPDATE bridges SET bridge_id = 'DELETED' WHERE bridge_uuid = ".$anvil->Database->quote($bridge_uuid).";";
$anvil->Database->write({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
}
return($bridge_uuid);
}
else
{
# Not found.
return("");
}
}
}
# If I still don't have an bridge_uuid, we're INSERT'ing . # If I still don't have an bridge_uuid, we're INSERT'ing .
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { bridge_uuid => $bridge_uuid }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { bridge_uuid => $bridge_uuid }});
if (not $bridge_uuid) if (not $bridge_uuid)
@ -2348,6 +2396,10 @@ If set, this is the file name logged as the source of any INSERTs or UPDATEs.
If set, this is the file line number logged as the source of any INSERTs or UPDATEs. If set, this is the file line number logged as the source of any INSERTs or UPDATEs.
=head3 delete (optional, default '0')
If set to C<< 1 >>, C<< bond_operational >> gets set to C<< DELETED >>. In this case, either C<< bond_uuid >> or C<< bond_name >> is required, and nothing else is.
=head3 bond_uuid (optional) =head3 bond_uuid (optional)
If not passed, a check will be made to see if an existing entry is found for C<< bond_name >>. If found, that entry will be updated. If not found, a new record will be inserted. If not passed, a check will be made to see if an existing entry is found for C<< bond_name >>. If found, that entry will be updated. If not found, a new record will be inserted.
@ -2416,6 +2468,7 @@ sub insert_or_update_bonds
my $uuid = defined $parameter->{uuid} ? $parameter->{uuid} : ""; my $uuid = defined $parameter->{uuid} ? $parameter->{uuid} : "";
my $file = defined $parameter->{file} ? $parameter->{file} : ""; my $file = defined $parameter->{file} ? $parameter->{file} : "";
my $line = defined $parameter->{line} ? $parameter->{line} : ""; my $line = defined $parameter->{line} ? $parameter->{line} : "";
my $delete = defined $parameter->{'delete'} ? $parameter->{'delete'} : "";
my $bond_uuid = defined $parameter->{bond_uuid} ? $parameter->{bond_uuid} : ""; my $bond_uuid = defined $parameter->{bond_uuid} ? $parameter->{bond_uuid} : "";
my $bond_host_uuid = defined $parameter->{bond_host_uuid} ? $parameter->{bond_host_uuid} : $anvil->data->{sys}{host_uuid}; my $bond_host_uuid = defined $parameter->{bond_host_uuid} ? $parameter->{bond_host_uuid} : $anvil->data->{sys}{host_uuid};
my $bond_name = defined $parameter->{bond_name} ? $parameter->{bond_name} : ""; my $bond_name = defined $parameter->{bond_name} ? $parameter->{bond_name} : "";
@ -2434,6 +2487,7 @@ sub insert_or_update_bonds
uuid => $uuid, uuid => $uuid,
file => $file, file => $file,
line => $line, line => $line,
'delete' => $delete,
bond_uuid => $bond_uuid, bond_uuid => $bond_uuid,
bond_host_uuid => $bond_host_uuid, bond_host_uuid => $bond_host_uuid,
bond_name => $bond_name, bond_name => $bond_name,
@ -2450,23 +2504,26 @@ sub insert_or_update_bonds
bond_bridge_uuid => $bond_bridge_uuid, bond_bridge_uuid => $bond_bridge_uuid,
}}); }});
if (not $bond_name) if (not $delete)
{
# Throw an error and exit.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_bonds()", parameter => "bond_name" }});
return("");
}
if (not $bond_mode)
{ {
# Throw an error and exit. if (not $bond_name)
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_bonds()", parameter => "bond_mode" }}); {
return(""); # Throw an error and exit.
} $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_bonds()", parameter => "bond_name" }});
if (not $bond_bridge_uuid) return("");
{ }
# This has to be 'NULL' if not defined. if (not $bond_mode)
$bond_bridge_uuid = 'NULL'; {
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { bond_bridge_uuid => $bond_bridge_uuid }}); # Throw an error and exit.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_bonds()", parameter => "bond_mode" }});
return("");
}
if (not $bond_bridge_uuid)
{
# This has to be 'NULL' if not defined.
$bond_bridge_uuid = 'NULL';
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { bond_bridge_uuid => $bond_bridge_uuid }});
}
} }
# If we don't have a UUID, see if we can find one for the given bond server name. # If we don't have a UUID, see if we can find one for the given bond server name.
@ -2497,6 +2554,45 @@ AND
} }
} }
if ($delete)
{
if (not $bond_uuid)
{
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_bonds()", parameter => "bond_uuid" }});
return("");
}
else
{
# Delete it
my $query = "SELECT bond_operational FROM bonds WHERE bond_uuid = ".$anvil->Database->quote($bond_uuid).";";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
my $results = $anvil->Database->query({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
my $count = @{$results};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
results => $results,
count => $count,
}});
if ($count)
{
my $old_bond_operational = $results->[0]->[0];
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { old_bond_operational => $old_bond_operational }});
if ($old_bond_operational ne "DELETED")
{
my $query = "UPDATE bonds SET bond_operational = 'DELETED' WHERE bond_uuid = ".$anvil->Database->quote($bond_uuid).";";
$anvil->Database->write({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
}
return($bond_uuid);
}
else
{
# Not found.
return("");
}
}
}
# If I still don't have an bond_uuid, we're INSERT'ing . # If I still don't have an bond_uuid, we're INSERT'ing .
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { bond_uuid => $bond_uuid }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { bond_uuid => $bond_uuid }});
if (not $bond_uuid) if (not $bond_uuid)
@ -3520,12 +3616,6 @@ If there is an error, an empty string is returned.
Parameters; Parameters;
=head3 delete (optional, default '0')
When set to C<< 1 >>, the C<< ip_address_note >> is set to C<< DELETED >>, and nothing else is changed.
B<< Note >>: When set to C<< 1 >>, only the C<< ip_address_uuid >> is needed.
=head3 uuid (optional) =head3 uuid (optional)
If set, only the corresponding database will be written to. If set, only the corresponding database will be written to.
@ -3538,6 +3628,14 @@ If set, this is the file name logged as the source of any INSERTs or UPDATEs.
If set, this is the file line number logged as the source of any INSERTs or UPDATEs. If set, this is the file line number logged as the source of any INSERTs or UPDATEs.
=head3 delete (optional, default '0')
When set to C<< 1 >>, the C<< ip_address_note >> is set to C<< DELETED >>, and nothing else is changed. If set, only C<< ip_address_uuid >> or C<< ip_address_address >> are required.
=head2 ip_address_address (required)
This is the acual IP address. It's tested with IPv4 addresses in dotted-decimal format, though it can also store IPv6 addresses. If this is set to C<< 0 >>, it will be treated as deleted and will be ignored (unless a new IP is assigned to the same interface in the future).
=head2 ip_address_uuid (optional) =head2 ip_address_uuid (optional)
If not passed, a check will be made to see if an existing entry is found for C<< ip_address_address >>. If found, that entry will be updated. If not found, a new record will be inserted. If not passed, a check will be made to see if an existing entry is found for C<< ip_address_address >>. If found, that entry will be updated. If not found, a new record will be inserted.
@ -3554,10 +3652,6 @@ This indicates what type of interface the IP address is on. This must be either
This is the UUID of the bridge, bond or interface that this IP address is on. This is the UUID of the bridge, bond or interface that this IP address is on.
=head2 ip_address_address (required)
This is the acual IP address. It's tested with IPv4 addresses in dotted-decimal format, though it can also store IPv6 addresses. If this is set to C<< 0 >>, it will be treated as deleted and will be ignored (unless a new IP is assigned to the same interface in the future).
=head2 ip_address_subnet_mask (required) =head2 ip_address_subnet_mask (required)
This is the subnet mask for the IP address. It is tested with IPv4 in dotted decimal format, though it can also store IPv6 format subnet masks. This is the subnet mask for the IP address. It is tested with IPv4 in dotted decimal format, though it can also store IPv6 format subnet masks.
@ -3605,6 +3699,7 @@ sub insert_or_update_ip_addresses
uuid => $uuid, uuid => $uuid,
file => $file, file => $file,
line => $line, line => $line,
'delete' => $delete,
ip_address_uuid => $ip_address_uuid, ip_address_uuid => $ip_address_uuid,
ip_address_host_uuid => $ip_address_host_uuid, ip_address_host_uuid => $ip_address_host_uuid,
ip_address_on_type => $ip_address_on_type, ip_address_on_type => $ip_address_on_type,
@ -3617,16 +3712,7 @@ sub insert_or_update_ip_addresses
ip_address_note => $ip_address_note, ip_address_note => $ip_address_note,
}}); }});
if ($delete) if (not $delete)
{
# We need the ip_address_uuid _or_ the ip_address_address
if (not $ip_address_uuid)
{
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_ip_addresses()", parameter => "ip_address_uuid" }});
return("");
}
}
else
{ {
# Not deleting, verify we have what we need. # Not deleting, verify we have what we need.
if (not $ip_address_on_type) if (not $ip_address_on_type)
@ -3668,7 +3754,7 @@ FROM
WHERE WHERE
ip_address_address = ".$anvil->Database->quote($ip_address_address)." ip_address_address = ".$anvil->Database->quote($ip_address_address)."
AND AND
ip_address_host_uuid = ".$anvil->Database->quote($anvil->data->{sys}{host_uuid})." ip_address_host_uuid = ".$anvil->Database->quote($ip_address_host_uuid)."
;"; ;";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
@ -3687,9 +3773,41 @@ AND
if ($delete) if ($delete)
{ {
my $query = "UPDATE ip_addresses SET ip_address_note = 'DELETED' WHERE ip_address_uuid = ".$anvil->Database->quote($ip_address_uuid).";"; # We need the ip_address_uuid _or_ the ip_address_address
$anvil->Database->write({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__}); if (not $ip_address_uuid)
return($ip_address_uuid); {
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_ip_addresses()", parameter => "ip_address_uuid" }});
return("");
}
else
{
my $query = "SELECT ip_address_note FROM ip_addresses WHERE ip_address_uuid = ".$anvil->Database->quote($ip_address_uuid).";";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
my $results = $anvil->Database->query({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
my $count = @{$results};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
results => $results,
count => $count,
}});
if ($count)
{
my $old_ip_address_note = $results->[0]->[0];
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { old_ip_address_note => $old_ip_address_note }});
if ($old_ip_address_note ne "DELETED")
{
my $query = "UPDATE ip_addresses SET ip_address_note = 'DELETED' WHERE ip_address_uuid = ".$anvil->Database->quote($ip_address_uuid).";";
$anvil->Database->write({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
}
return($ip_address_uuid);
}
else
{
# Not found.
return("");
}
}
} }
# If I still don't have an ip_address_uuid, we're INSERT'ing . # If I still don't have an ip_address_uuid, we're INSERT'ing .
@ -4351,6 +4469,10 @@ B<< NOTE >>: This only works for existing records. If this is passed for an inte
If set, this is the file line number logged as the source of any INSERTs or UPDATEs. If set, this is the file line number logged as the source of any INSERTs or UPDATEs.
=head3 delete (optional, default '0')
When set to C<< 1 >>, the C<< network_interface_operational >> is set to C<< DELETED >>, and nothing else is changed. When set, either C<< network_interface_uuid >> or C<< network_interface_mac_address >> and C<< network_interface_name >> are needed.
=head3 network_interface_bond_uuid (optional) =head3 network_interface_bond_uuid (optional)
If this interface is part of a bond, this UUID will be the C<< bonds >> -> C<< bond_uuid >> that this interface is slaved to. If this interface is part of a bond, this UUID will be the C<< bonds >> -> C<< bond_uuid >> that this interface is slaved to.
@ -4412,6 +4534,7 @@ sub insert_or_update_network_interfaces
my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0125", variables => { method => "Database->insert_or_update_network_interfaces()" }}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0125", variables => { method => "Database->insert_or_update_network_interfaces()" }});
my $delete = defined $parameter->{'delete'} ? $parameter->{'delete'} : 0;
my $uuid = defined $parameter->{uuid} ? $parameter->{uuid} : ""; my $uuid = defined $parameter->{uuid} ? $parameter->{uuid} : "";
my $file = defined $parameter->{file} ? $parameter->{file} : ""; my $file = defined $parameter->{file} ? $parameter->{file} : "";
my $line = defined $parameter->{line} ? $parameter->{line} : ""; my $line = defined $parameter->{line} ? $parameter->{line} : "";
@ -4430,6 +4553,7 @@ sub insert_or_update_network_interfaces
my $network_interface_uuid = defined $parameter->{network_interface_uuid} ? $parameter->{interface_uuid} : ""; my $network_interface_uuid = defined $parameter->{network_interface_uuid} ? $parameter->{interface_uuid} : "";
my $timestamp = defined $parameter->{timestamp} ? $parameter->{timestamp} : $anvil->data->{sys}{database}{timestamp}; my $timestamp = defined $parameter->{timestamp} ? $parameter->{timestamp} : $anvil->data->{sys}{database}{timestamp};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
'delete' => $delete,
uuid => $uuid, uuid => $uuid,
file => $file, file => $file,
line => $line, line => $line,
@ -4449,32 +4573,35 @@ sub insert_or_update_network_interfaces
}}); }});
# INSERT, but make sure we have enough data first. # INSERT, but make sure we have enough data first.
if (not $network_interface_mac_address) if (not $delete)
{
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_network_interfaces()", parameter => "network_interface_mac_address" }});
return("");
}
else
{
# Always lower-case the MAC address.
$network_interface_mac_address = lc($network_interface_mac_address);
}
if (not $network_interface_name)
{
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_network_interfaces()", parameter => "network_interface_name" }});
return("");
}
if (($network_interface_bond_uuid ne 'NULL') && (not $anvil->Validate->is_uuid({uuid => $network_interface_bond_uuid})))
{
# Bad UUID.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0130", variables => { method => "Database->insert_or_update_network_interfaces()", parameter => "network_interface_bond_uuid", uuid => $network_interface_bond_uuid }});
return("");
}
if (($network_interface_bridge_uuid ne 'NULL') && (not $anvil->Validate->is_uuid({uuid => $network_interface_bridge_uuid})))
{ {
# Bad UUID. if (not $network_interface_mac_address)
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0130", variables => { method => "Database->insert_or_update_network_interfaces()", parameter => "network_interface_bridge_uuid", uuid => $network_interface_bridge_uuid }}); {
return(""); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_network_interfaces()", parameter => "network_interface_mac_address" }});
return("");
}
else
{
# Always lower-case the MAC address.
$network_interface_mac_address = lc($network_interface_mac_address);
}
if (not $network_interface_name)
{
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_network_interfaces()", parameter => "network_interface_name" }});
return("");
}
if (($network_interface_bond_uuid ne 'NULL') && (not $anvil->Validate->is_uuid({uuid => $network_interface_bond_uuid})))
{
# Bad UUID.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0130", variables => { method => "Database->insert_or_update_network_interfaces()", parameter => "network_interface_bond_uuid", uuid => $network_interface_bond_uuid }});
return("");
}
if (($network_interface_bridge_uuid ne 'NULL') && (not $anvil->Validate->is_uuid({uuid => $network_interface_bridge_uuid})))
{
# Bad UUID.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0130", variables => { method => "Database->insert_or_update_network_interfaces()", parameter => "network_interface_bridge_uuid", uuid => $network_interface_bridge_uuid }});
return("");
}
} }
# If we don't have a network interface UUID, try to look one up using the MAC address # If we don't have a network interface UUID, try to look one up using the MAC address
@ -4491,6 +4618,8 @@ WHERE
network_interface_mac_address = ".$anvil->Database->quote($network_interface_mac_address)." network_interface_mac_address = ".$anvil->Database->quote($network_interface_mac_address)."
AND AND
network_interface_name = ".$anvil->Database->quote($network_interface_name)." network_interface_name = ".$anvil->Database->quote($network_interface_name)."
AND
network_interface_host_uuid = ".$anvil->Database->quote($network_interface_host_uuid)."
;"; ;";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
@ -4505,6 +4634,45 @@ AND
} }
} }
if ($delete)
{
if (not $network_interface_uuid)
{
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_network_interfaces()", parameter => "network_interface_uuid" }});
return("");
}
else
{
# Delete it
my $query = "SELECT network_interface_operational FROM network_interfaces WHERE network_interface_uuid = ".$anvil->Database->quote($network_interface_uuid).";";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
my $results = $anvil->Database->query({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
my $count = @{$results};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
results => $results,
count => $count,
}});
if ($count)
{
my $old_network_interface_operational = $results->[0]->[0];
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { old_network_interface_operational => $old_network_interface_operational }});
if ($old_network_interface_operational ne "DELETED")
{
my $query = "UPDATE network_interfaces SET network_interface_operational = 'DELETED' WHERE network_interface_uuid = ".$anvil->Database->quote($network_interface_uuid).";";
$anvil->Database->write({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
}
return($network_interface_uuid);
}
else
{
# Not found.
return("");
}
}
}
# Now, if we're inserting or updating, we'll need to require different bits. # Now, if we're inserting or updating, we'll need to require different bits.
if ($network_interface_uuid) if ($network_interface_uuid)
{ {

@ -1184,7 +1184,9 @@ SELECT
FROM FROM
ip_addresses ip_addresses
WHERE WHERE
ip_address_host_uuid = ".$anvil->Database->quote($host_uuid)." ip_address_host_uuid = ".$anvil->Database->quote($host_uuid)."
AND
ip_address_note != 'DELETED'
;"; ;";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__});
@ -1223,7 +1225,9 @@ SELECT
FROM FROM
network_interfaces network_interfaces
WHERE WHERE
network_interface_uuid = ".$anvil->Database->quote($ip_address_on_uuid)." network_interface_uuid = ".$anvil->Database->quote($ip_address_on_uuid)."
AND
network_interface_operational != 'DELETED'
;"; ;";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__});
@ -1266,7 +1270,9 @@ SELECT
FROM FROM
bonds bonds
WHERE WHERE
bond_uuid = ".$anvil->Database->quote($ip_address_on_uuid)." bond_uuid = ".$anvil->Database->quote($ip_address_on_uuid)."
AND
bond_operational != 'DELETED'
;"; ;";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__});
@ -1309,7 +1315,9 @@ SELECT
FROM FROM
bridges bridges
WHERE WHERE
bridge_uuid = ".$anvil->Database->quote($ip_address_on_uuid)." bridge_uuid = ".$anvil->Database->quote($ip_address_on_uuid)."
AND
bridge_id != 'DELETED'
;"; ;";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__});

@ -806,93 +806,68 @@ CREATE TRIGGER trigger_jobs
AFTER INSERT OR UPDATE ON jobs AFTER INSERT OR UPDATE ON jobs
FOR EACH ROW EXECUTE PROCEDURE history_jobs(); FOR EACH ROW EXECUTE PROCEDURE history_jobs();
-- NOTE: network_interfaces, network_bonds and network_bridges are all used by scan-network (which doesn't
-- exist yet).
-- This stores information about network interfaces on hosts. It is mainly used to match a MAC address to a -- This stores information about network bridges.
-- host. Given that it is possible that network devices can move, the linkage to the host_uuid can change. CREATE TABLE bridges (
CREATE TABLE network_interfaces ( bridge_uuid uuid not null primary key,
network_interface_uuid uuid not null primary key, bridge_host_uuid uuid not null,
network_interface_host_uuid uuid not null, bridge_name text not null,
network_interface_mac_address text not null, bridge_id text not null,
network_interface_name text not null, -- This is the current name of the interface. bridge_mac_address text not null,
network_interface_speed bigint not null, -- This is the speed, in bits-per-second, of the interface. bridge_mtu text not null,
network_interface_mtu bigint not null, -- This is the MTU (Maximum Transmitable Size), in bytes, for this interface. bridge_stp_enabled text not null, -- 0 = disabled, 1 = kernel STP, 2 = user STP
network_interface_link_state text not null, -- 0 or 1 modified_date timestamp with time zone not null,
network_interface_operational text not null, -- This is 'up', 'down' or 'unknown'
network_interface_duplex text not null, -- This is 'full', 'half' or 'unknown'
network_interface_medium text not null, -- This is 'tp' (twisted pair), 'fiber' or whatever they invent in the future.
network_interface_bond_uuid uuid, -- If this iface is in a bond, this will contain the 'bonds -> bond_uuid' that it is slaved to.
network_interface_bridge_uuid uuid, -- If this iface is attached to a bridge, this will contain the 'bridgess -> bridge_uuid' that it is connected to.
modified_date timestamp with time zone not null
FOREIGN KEY(network_interface_bridge_uuid) REFERENCES bridges(bridge_uuid), FOREIGN KEY(bridge_host_uuid) REFERENCES hosts(host_uuid)
FOREIGN KEY(network_interface_bond_uuid) REFERENCES bonds(bond_uuid),
FOREIGN KEY(network_interface_host_uuid) REFERENCES hosts(host_uuid)
); );
ALTER TABLE network_interfaces OWNER TO admin; ALTER TABLE bridges OWNER TO admin;
CREATE TABLE history.network_interfaces ( CREATE TABLE history.bridges (
history_id bigserial, history_id bigserial,
network_interface_uuid uuid not null, bridge_uuid uuid,
network_interface_host_uuid uuid, bridge_host_uuid uuid,
network_interface_mac_address text, bridge_name text,
network_interface_name text, bridge_id text,
network_interface_speed bigint, bridge_mac_address text,
network_interface_mtu bigint, bridge_mtu text,
network_interface_link_state text, bridge_stp_enabled text,
network_interface_operational text, modified_date timestamp with time zone not null
network_interface_duplex text,
network_interface_medium text,
network_interface_bond_uuid uuid,
network_interface_bridge_uuid uuid,
modified_date timestamp with time zone not null
); );
ALTER TABLE history.network_interfaces OWNER TO admin; ALTER TABLE history.bridges OWNER TO admin;
CREATE FUNCTION history_network_interfaces() RETURNS trigger CREATE FUNCTION history_bridges() RETURNS trigger
AS $$ AS $$
DECLARE DECLARE
history_network_interfaces RECORD; history_bridges RECORD;
BEGIN BEGIN
SELECT INTO history_network_interfaces * FROM network_interfaces WHERE network_interface_uuid = new.network_interface_uuid; SELECT INTO history_bridges * FROM bridges WHERE bridge_uuid = new.bridge_uuid;
INSERT INTO history.network_interfaces INSERT INTO history.bridges
(network_interface_uuid, (bridge_uuid,
network_interface_host_uuid, bridge_host_uuid,
network_interface_mac_address, bridge_name,
network_interface_name, bridge_id,
network_interface_speed, bridge_mac_address,
network_interface_mtu, bridge_mtu,
network_interface_link_state, bridge_stp_enabled,
network_interface_operational,
network_interface_duplex,
network_interface_medium,
network_interface_bond_uuid,
network_interface_bridge_uuid,
modified_date) modified_date)
VALUES VALUES
(history_network_interfaces.network_interface_uuid, (history_bridges.bridge_uuid,
history_network_interfaces.network_interface_host_uuid, history_bridges.bridge_host_uuid,
history_network_interfaces.network_interface_mac_address, history_bridges.bridge_name,
history_network_interfaces.network_interface_name, history_bridges.bridge_id,
history_network_interfaces.network_interface_speed, history_bridges.bridge_mac_address,
history_network_interfaces.network_interface_mtu, history_bridges.bridge_mtu,
history_network_interfaces.network_interface_link_state, history_bridges.bridge_stp_enabled,
history_network_interfaces.network_interface_operational, history_bridges.modified_date);
history_network_interfaces.network_interface_duplex,
history_network_interfaces.network_interface_medium,
history_network_interfaces.network_interface_bond_uuid,
history_network_interfaces.network_interface_bridge_uuid,
history_network_interfaces.modified_date);
RETURN NULL; RETURN NULL;
END; END;
$$ $$
LANGUAGE plpgsql; LANGUAGE plpgsql;
ALTER FUNCTION history_network_interfaces() OWNER TO admin; ALTER FUNCTION history_bridges() OWNER TO admin;
CREATE TRIGGER trigger_network_interfaces CREATE TRIGGER trigger_bridges
AFTER INSERT OR UPDATE ON network_interfaces AFTER INSERT OR UPDATE ON bridges
FOR EACH ROW EXECUTE PROCEDURE history_network_interfaces(); FOR EACH ROW EXECUTE PROCEDURE history_bridges();
-- This stores information about network bonds (mode=1) on a hosts. -- This stores information about network bonds (mode=1) on a hosts.
@ -987,67 +962,90 @@ CREATE TRIGGER trigger_bonds
FOR EACH ROW EXECUTE PROCEDURE history_bonds(); FOR EACH ROW EXECUTE PROCEDURE history_bonds();
-- This stores information about network bridges. -- This stores information about network interfaces on hosts. It is mainly used to match a MAC address to a
CREATE TABLE bridges ( -- host. Given that it is possible that network devices can move, the linkage to the host_uuid can change.
bridge_uuid uuid not null primary key, CREATE TABLE network_interfaces (
bridge_host_uuid uuid not null, network_interface_uuid uuid not null primary key,
bridge_name text not null, network_interface_host_uuid uuid not null,
bridge_id text not null, network_interface_mac_address text not null,
bridge_mac_address text not null, network_interface_name text not null, -- This is the current name of the interface.
bridge_mtu text not null, network_interface_speed bigint not null, -- This is the speed, in bits-per-second, of the interface.
bridge_stp_enabled text not null, -- 0 = disabled, 1 = kernel STP, 2 = user STP network_interface_mtu bigint not null, -- This is the MTU (Maximum Transmitable Size), in bytes, for this interface.
modified_date timestamp with time zone not null, network_interface_link_state text not null, -- 0 or 1
network_interface_operational text not null, -- This is 'up', 'down' or 'unknown'
network_interface_duplex text not null, -- This is 'full', 'half' or 'unknown'
network_interface_medium text not null, -- This is 'tp' (twisted pair), 'fiber' or whatever they invent in the future.
network_interface_bond_uuid uuid, -- If this iface is in a bond, this will contain the 'bonds -> bond_uuid' that it is slaved to.
network_interface_bridge_uuid uuid, -- If this iface is attached to a bridge, this will contain the 'bridgess -> bridge_uuid' that it is connected to.
modified_date timestamp with time zone not null,
FOREIGN KEY(bridge_host_uuid) REFERENCES hosts(host_uuid) FOREIGN KEY(network_interface_bridge_uuid) REFERENCES bridges(bridge_uuid),
FOREIGN KEY(network_interface_bond_uuid) REFERENCES bonds(bond_uuid),
FOREIGN KEY(network_interface_host_uuid) REFERENCES hosts(host_uuid)
); );
ALTER TABLE bridges OWNER TO admin; ALTER TABLE network_interfaces OWNER TO admin;
CREATE TABLE history.bridges ( CREATE TABLE history.network_interfaces (
history_id bigserial, history_id bigserial,
bridge_uuid uuid, network_interface_uuid uuid not null,
bridge_host_uuid uuid, network_interface_host_uuid uuid,
bridge_name text, network_interface_mac_address text,
bridge_id text, network_interface_name text,
bridge_mac_address text, network_interface_speed bigint,
bridge_mtu text, network_interface_mtu bigint,
bridge_stp_enabled text, network_interface_link_state text,
modified_date timestamp with time zone not null network_interface_operational text,
network_interface_duplex text,
network_interface_medium text,
network_interface_bond_uuid uuid,
network_interface_bridge_uuid uuid,
modified_date timestamp with time zone not null
); );
ALTER TABLE history.bridges OWNER TO admin; ALTER TABLE history.network_interfaces OWNER TO admin;
CREATE FUNCTION history_bridges() RETURNS trigger CREATE FUNCTION history_network_interfaces() RETURNS trigger
AS $$ AS $$
DECLARE DECLARE
history_bridges RECORD; history_network_interfaces RECORD;
BEGIN BEGIN
SELECT INTO history_bridges * FROM bridges WHERE bridge_uuid = new.bridge_uuid; SELECT INTO history_network_interfaces * FROM network_interfaces WHERE network_interface_uuid = new.network_interface_uuid;
INSERT INTO history.bridges INSERT INTO history.network_interfaces
(bridge_uuid, (network_interface_uuid,
bridge_host_uuid, network_interface_host_uuid,
bridge_name, network_interface_mac_address,
bridge_id, network_interface_name,
bridge_mac_address, network_interface_speed,
bridge_mtu, network_interface_mtu,
bridge_stp_enabled, network_interface_link_state,
network_interface_operational,
network_interface_duplex,
network_interface_medium,
network_interface_bond_uuid,
network_interface_bridge_uuid,
modified_date) modified_date)
VALUES VALUES
(history_bridges.bridge_uuid, (history_network_interfaces.network_interface_uuid,
history_bridges.bridge_host_uuid, history_network_interfaces.network_interface_host_uuid,
history_bridges.bridge_name, history_network_interfaces.network_interface_mac_address,
history_bridges.bridge_id, history_network_interfaces.network_interface_name,
history_bridges.bridge_mac_address, history_network_interfaces.network_interface_speed,
history_bridges.bridge_mtu, history_network_interfaces.network_interface_mtu,
history_bridges.bridge_stp_enabled, history_network_interfaces.network_interface_link_state,
history_bridges.modified_date); history_network_interfaces.network_interface_operational,
history_network_interfaces.network_interface_duplex,
history_network_interfaces.network_interface_medium,
history_network_interfaces.network_interface_bond_uuid,
history_network_interfaces.network_interface_bridge_uuid,
history_network_interfaces.modified_date);
RETURN NULL; RETURN NULL;
END; END;
$$ $$
LANGUAGE plpgsql; LANGUAGE plpgsql;
ALTER FUNCTION history_bridges() OWNER TO admin; ALTER FUNCTION history_network_interfaces() OWNER TO admin;
CREATE TRIGGER trigger_bridges CREATE TRIGGER trigger_network_interfaces
AFTER INSERT OR UPDATE ON bridges AFTER INSERT OR UPDATE ON network_interfaces
FOR EACH ROW EXECUTE PROCEDURE history_bridges(); FOR EACH ROW EXECUTE PROCEDURE history_network_interfaces();
-- This stores information about network ip addresss. -- This stores information about network ip addresss.

@ -25,9 +25,9 @@ my $anvil = Anvil::Tools->new({debug => 3});
$anvil->Log->secure({set => 1}); $anvil->Log->secure({set => 1});
$anvil->Log->level({set => 2}); $anvil->Log->level({set => 2});
$anvil->Database->connect({debug => 2}); $anvil->Database->connect({debug => 3});
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"});
print "DB Connections: [".$anvil->data->{sys}{database}{connections}."]\n"; print "DB Connections: [".$anvil->data->{sys}{database}{connections}."]\n";
#$anvil->Network->load_interfces({debug => 2}); #$anvil->Network->load_interfces({debug => 2});
#$anvil->System->generate_state_json({debug => 2}); $anvil->System->generate_state_json({debug => 2});

Loading…
Cancel
Save