diff options
Diffstat (limited to 'web/api/write')
-rwxr-xr-x | web/api/write/linknet-add | 32 | ||||
-rwxr-xr-x | web/api/write/network-add | 49 | ||||
-rwxr-xr-x | web/api/write/networks (renamed from web/api/write/network-update) | 21 | ||||
-rwxr-xr-x | web/api/write/switch-add | 94 | ||||
-rwxr-xr-x | web/api/write/switches (renamed from web/api/write/switch-update) | 2 |
5 files changed, 18 insertions, 180 deletions
diff --git a/web/api/write/linknet-add b/web/api/write/linknet-add deleted file mode 100755 index 13ccd17..0000000 --- a/web/api/write/linknet-add +++ /dev/null @@ -1,32 +0,0 @@ -#! /usr/bin/perl -# vim:ts=8:sw=8 -use lib '/opt/gondul/include'; -use utf8; -use nms::web qw($dbh db_safe_quote get_input finalize_output); -use strict; -use warnings; - -my $in = get_input(); -my %tmp = %{JSON::XS::decode_json($in)}; - -my $q = $nms::web::dbh->prepare("INSERT INTO linknets (switch1, switch2) VALUES((SELECT switch FROM switches WHERE sysname = ? LIMIT 1), (SELECT switch FROM switches WHERE sysname = ? LIMIT 1));"); -my $sth = $nms::web::dbh->prepare("SELECT linknet FROM linknets WHERE switch1 = (SELECT switch FROM switches WHERE sysname = ? LIMIT 1) and switch2 = (SELECT switch FROM switches WHERE sysname = ? LIMIT 1);"); - -$sth->execute($tmp{'switch1'}, $tmp{'switch2'}); -my $affected = 0; -while ( my @row = $sth->fetchrow_array ) { - $affected += 1; -} - -print "X-affected: $affected\n"; -if ($affected eq 0) { - $q->execute($tmp{'switch1'}, $tmp{'switch2'}); -} - -$dbh->commit; -$nms::web::cc{'max-age'} = '0'; -$nms::web::cc{'stale-while-revalidate'} = '0'; -$nms::web::json{'state'} = 'ok'; - -print "X-ban: /api/public/.*\n"; -finalize_output(); diff --git a/web/api/write/network-add b/web/api/write/network-add deleted file mode 100755 index d1aab4e..0000000 --- a/web/api/write/network-add +++ /dev/null @@ -1,49 +0,0 @@ -#! /usr/bin/perl -# vim:ts=8:sw=8 - -#use CGI qw(fatalsToBrowser); -use DBI; -use lib '/opt/gondul/include'; -use nms; -use nms::web qw(%get_params %json finalize_output get_input $dbh); -use nms::util qw(guess_placement); -use strict; -use warnings; -use JSON; -use Data::Dumper; -use nms::oplog qw(oplog); - -$nms::web::cc{'max-age'} = "0"; - -my $in = get_input(); -my @tmp = @{JSON::XS::decode_json($in)}; - -my @added; -my @dups; - -my $sth = $nms::web::dbh->prepare("SELECT name FROM networks WHERE name=?"); - -foreach my $tmp2 (@tmp) { - my %network = %{$tmp2}; - my $affected = 0; - my %template = (); - if (not defined($network{'name'})) { - next; - } - - $sth->execute( $network{'name'}); - while ( my @row = $sth->fetchrow_array ) { - $affected += 1; - } - - if ($affected == 0) { - $nms::web::dbh->do("INSERT INTO NETWORKS (name) VALUES ('$network{'name'}');"); - push @added, $network{'name'}; - oplog("\"" . $network{'name'} . "\"", "Network added: " . $network{'name'}); - } -} - -$json{'networks_addded'} = \@added; - -print "X-ban: /api/.*\n"; -finalize_output(); diff --git a/web/api/write/network-update b/web/api/write/networks index 6344c37..460a7ae 100755 --- a/web/api/write/network-update +++ b/web/api/write/networks @@ -6,7 +6,6 @@ use DBI; use lib '/opt/gondul/include'; use nms; use nms::web qw(%get_params %json finalize_output get_input $dbh); -use nms::util qw(guess_placement); use strict; use warnings; use JSON; @@ -23,7 +22,7 @@ my @dups; my $sth = $nms::web::dbh->prepare("SELECT name FROM networks WHERE name=?"); -my @fields = ('name','last_updated','subnet4','subnet6','gw4','gw6','routing_point','vlan','tags'); +my @fields = ('name','subnet4','subnet6','gw4','gw6','router','vlan','tags'); foreach my $tmp2 (@tmp) { my %network = %{$tmp2}; @@ -47,8 +46,16 @@ foreach my $tmp2 (@tmp) { } } keys %network; - - $nms::web::dbh->do("INSERT INTO NETWORKS (name, last_updated, subnet4, subnet6, routing_point, gw4, gw6, vlan, tags) VALUES ($template{'name'}, $template{'last_updated'}, $template{'subnet4'}, $template{'subnet6'}, $template{'routing_point'}, $template{'gw4'}, $template{'gw6'}, $template{'vlan'}, $template{'tags'});"); + if ($template{'router'} ne 'DEFAULT') { + $template{'router'} = "(select switch from switches where sysname = $template{'router'})"; + } + if ($template{'gw4'} eq 'DEFAULT' and $template{'subnet4'} ne 'DEFAULT') { + $template{'gw4'} = "host(inet $template{'subnet4'} + 1)"; + } + if ($template{'gw6'} eq 'DEFAULT' and $template{'subnet6'} ne 'DEFAULT') { + $template{'gw6'} = "host(inet $template{'subnet6'} + 1)"; + } + $nms::web::dbh->do("INSERT INTO NETWORKS (name, subnet4, subnet6, router, gw4, gw6, vlan, tags) VALUES ($template{'name'}, $template{'subnet4'}, $template{'subnet6'}, $template{'router'}, $template{'gw4'}, $template{'gw6'}, $template{'vlan'}, $template{'tags'});"); push @added, $network{'name'}; } else { if (defined($network{'tags'})) { @@ -57,7 +64,11 @@ foreach my $tmp2 (@tmp) { my @set; map { if (defined($template{$_})) { - push @set, "$_=" . $dbh->quote($network{$_}); + if ($_ eq "router") { + push @set, "router=(select switch from switches where sysname = " . $dbh->quote($network{$_}) . ")"; + } else { + push @set, "$_=" . $dbh->quote($network{$_}); + } } } keys %network; $nms::web::dbh->do("UPDATE networks SET " . join(", ", @set) . "WHERE name=" . $dbh->quote($network{'name'}) . ";"); diff --git a/web/api/write/switch-add b/web/api/write/switch-add deleted file mode 100755 index c92cf31..0000000 --- a/web/api/write/switch-add +++ /dev/null @@ -1,94 +0,0 @@ -#! /usr/bin/perl -# vim:ts=8:sw=8 - -#use CGI qw(fatalsToBrowser); -use DBI; -use lib '/opt/gondul/include'; -use nms; -use nms::web qw(%get_params %json finalize_output get_input $dbh); -use nms::util qw(guess_placement); -use strict; -use warnings; -use JSON; -use Data::Dumper; -use nms::oplog qw(oplog); - -$nms::web::cc{'max-age'} = "0"; - -my $in = get_input(); -my @tmp = @{JSON::XS::decode_json($in)}; - -my @added; -my @dups; - -my $sth = $nms::web::dbh->prepare("SELECT sysname FROM switches WHERE sysname=?"); - -my @fields = ( 'community', 'distro_name', 'distro_phy_port', 'mgmt_v4_addr', 'mgmt_v6_addr', 'mgmt_vlan', 'placement', 'poll_frequency', 'sysname', 'traffic_vlan'); - -sub convertplace -{ - my %in = %{$_[0]}; - my %out = (); - - if (not defined $in{'x1'} and defined($in{'x'})) { - $out{'x1'} = int($in{'x'}); - $out{'y1'} = int($in{'y'}); - $out{'xx'} = int($in{'x'} + $in{'width'}); - $out{'yy'} = int($in{'y'} + $in{'height'}); - } else { - return \%in; - } - return \%out; -} - -foreach my $tmp2 (@tmp) { - my %switch = %{$tmp2}; - my $affected = 0; - my %template = (); - map { $template{$_} = 'DEFAULT' } @fields; - if (not defined($switch{'sysname'})) { - next; - } - - $sth->execute( $switch{'sysname'}); - while ( my @row = $sth->fetchrow_array ) { - $affected += 1; - } - - if ($affected == 0) { - my %placement; - if (not defined ($switch{'placement'})) { - %placement = guess_placement($switch{'sysname'}); - } else { - %placement = %{convertplace($switch{'placement'})}; - } - if (not defined($switch{'ip'}) and defined($switch{'mgtmt4'})) { - $switch{'ip'} = $switch{'mgtmt4'}; - } - if (not defined($switch{'secondary_ip'}) and defined($switch{'mgtmt6'})) { - $switch{'secondary_ip'} = $switch{'mgtmt6'}; - } - my ($x1,$x2,$y1,$y2); - $x1 = $placement{'x1'}; - $y1 = $placement{'y1'}; - $x2 = $placement{'xx'}; - $y2 = $placement{'yy'}; - $switch{'placement'} = "(($x1,$y1),($x2,$y2))"; - - map { - if (defined ($template{$_})) { - $template{$_} = $dbh->quote($switch{$_}); - } - } keys %switch; - - - $nms::web::dbh->do("INSERT INTO SWITCHES (mgmt_v4_addr, sysname, poll_frequency, community, mgmt_v6_addr, placement, distro_name) VALUES ($template{'mgmt_v4_addr'}, $template{'sysname'}, $template{'poll_frequency'}, $template{'community'}, $template{'mgmt_v6_addr'}, $template{'placement'}, $template{'distro_name'});"); - push @added, $switch{'sysname'}; - oplog("\"" . $switch{'sysname'} . "\"", "Switch added: " . $switch{'sysname'}); - } -} - -$json{'switches_addded'} = \@added; - -print "X-ban: /api/.*\n"; -finalize_output(); diff --git a/web/api/write/switch-update b/web/api/write/switches index bcf69e1..947404d 100755 --- a/web/api/write/switch-update +++ b/web/api/write/switches @@ -11,6 +11,7 @@ use strict; use warnings; use JSON; use Data::Dumper; +use nms::oplog qw(oplog); $nms::web::cc{'max-age'} = "0"; @@ -84,6 +85,7 @@ foreach my $tmp2 (@tmp) { $nms::web::dbh->do("INSERT INTO SWITCHES (ip, sysname, last_updated, locked, poll_frequency, tags, community, secondary_ip, placement,subnet4,subnet6,distro) VALUES ($template{'ip'}, $template{'sysname'}, $template{'last_updated'}, $template{'locked'}, $template{'poll_frequency'}, $template{'tags'}, $template{'community'}, $template{'secondary_ip'}, $template{'placement'},$template{'subnet4'},$template{'subnet6'},$template{'distro'});"); push @added, $switch{'sysname'}; + oplog("\"" . $switch{'sysname'} . "\"", "Switch added: " . $switch{'sysname'}); } else { if (defined($switch{'placement'})) { my %placement; |