aboutsummaryrefslogtreecommitdiffstats
path: root/web/api/write
diff options
context:
space:
mode:
Diffstat (limited to 'web/api/write')
-rwxr-xr-xweb/api/write/linknet-add32
-rw-r--r--web/api/write/network-add49
-rw-r--r--web/api/write/network-update114
-rwxr-xr-xweb/api/write/networks82
-rwxr-xr-xweb/api/write/switch-add94
-rwxr-xr-xweb/api/write/switches (renamed from web/api/write/switch-update)4
6 files changed, 85 insertions, 290 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 100644
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/network-update
deleted file mode 100644
index f6684c5..0000000
--- a/web/api/write/network-update
+++ /dev/null
@@ -1,114 +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;
-
-$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=?");
-
-
-my @fields = ('name','last_updated','placement','subnet4','subnet6','gw4','gw6','routing_point','vlan','tags');
-
-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 %network = %{$tmp2};
- my $affected = 0;
- my %template = ();
- map { $template{$_} = 'DEFAULT' } @fields;
- if (not defined($network{'sysname'})) {
- next;
- }
-
- $sth->execute( $network{'sysname'});
- while ( my @row = $sth->fetchrow_array ) {
- $affected += 1;
- }
-
- if ($affected == 0) {
- my %placement;
- if (not defined ($network{'placement'})) {
- %placement = guess_placement($network{'sysname'});
- } else {
- %placement = %{convertplace($network{'sysname'})};
- }
- my ($x1,$x2,$y1,$y2);
- $x1 = $placement{'x1'};
- $y1 = $placement{'y1'};
- $x2 = $placement{'xx'};
- $y2 = $placement{'yy'};
- $network{'placement'} = "(($x1,$y1),($x2,$y2))";
-
- map {
- if (defined ($template{$_})) {
- $template{$_} = $dbh->quote($network{$_});
- }
- } keys %network;
-
-
- $nms::web::dbh->do("INSERT INTO NETWORKS (name, last_updated, placement, subnet4, subnet6, routing_point, gw4, gw6, vlan, tags) VALUES ($template{'sysname'}, $template{'last_updated'}, $template{'placement'}, $template{'subnet4'}, $template{'subnet6'}, $template{'routing_point'}, $template{'gw4'}, $template{'gw6'}, $template{'vlan'}, $template{'tags'});");
- push @added, $network{'sysname'};
- } else {
- if (defined($network{'placement'})) {
- my %placement;
- if ($network{'placement'} eq "reset") {
- %placement = guess_placement($network{'sysname'});
- } else {
- %placement = %{convertplace($network{'placement'})};
- }
- my ($x1,$x2,$y1,$y2);
- $x1 = $placement{'x1'};
- $y1 = $placement{'y1'};
- $x2 = $placement{'xx'};
- $y2 = $placement{'yy'};
- $network{'placement'} = "(($x1,$y1),($x2,$y2))";
- push @dups, "not really, but: " . $network{'placement'};
- }
- if (defined($network{'tags'})) {
- $network{'tags'} =~ s/'/"/g;
- }
- my @set;
- map {
- if (defined($template{$_})) {
- push @set, "$_=" . $dbh->quote($network{$_});
- }
- } keys %network;
- $nms::web::dbh->do("UPDATE NETWORKS SET " . join(", ", @set) . "WHERE name=" . $dbh->quote($network{'sysname'}) . ";");
- push @dups, $network{'sysname'};
- }
-}
-$json{'networks_addded'} = \@added;
-$json{'networks_updated'} = \@dups;
-
-print "X-ban: /api/.*networks.*\n";
-finalize_output();
diff --git a/web/api/write/networks b/web/api/write/networks
new file mode 100755
index 0000000..460a7ae
--- /dev/null
+++ b/web/api/write/networks
@@ -0,0 +1,82 @@
+#! /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 strict;
+use warnings;
+use JSON;
+use Data::Dumper;
+
+$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=?");
+
+
+my @fields = ('name','subnet4','subnet6','gw4','gw6','router','vlan','tags');
+
+foreach my $tmp2 (@tmp) {
+ my %network = %{$tmp2};
+ my $affected = 0;
+ my %template = ();
+ map { $template{$_} = 'DEFAULT' } @fields;
+ if (not defined($network{'name'})) {
+ next;
+ }
+
+ $sth->execute( $network{'name'});
+ while ( my @row = $sth->fetchrow_array ) {
+ $affected += 1;
+ }
+
+ if ($affected == 0) {
+
+ map {
+ if (defined ($template{$_})) {
+ $template{$_} = $dbh->quote($network{$_});
+ }
+ } keys %network;
+
+ 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'})) {
+ $network{'tags'} =~ s/'/"/g;
+ }
+ my @set;
+ map {
+ if (defined($template{$_})) {
+ 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'}) . ";");
+ push @dups, $network{'name'};
+ }
+}
+$json{'networks_addded'} = \@added;
+$json{'networks_updated'} = \@dups;
+
+print "X-ban: /api/.*networks.*\n";
+finalize_output();
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 b71548b..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";
@@ -23,7 +24,7 @@ my @dups;
my $sth = $nms::web::dbh->prepare("SELECT sysname FROM switches WHERE sysname=?");
-my @fields = ( 'community', 'tags', 'distro_name', 'distro_phy_port', 'mgmt_v4_addr', 'mgmt_v4_gw', 'mgmt_v4_netsize', 'mgmt_v6_addr', 'mgmt_v6_gw', 'mgmt_v6_netsize', 'mgmt_vlan', 'placement', 'poll_frequency', 'subnet4', 'subnet6', 'sysname', 'traffic_vlan', 'deleted');
+my @fields = ( 'community', 'tags', 'distro_name', 'distro_phy_port', 'mgmt_v4_addr', 'mgmt_v6_addr', 'mgmt_vlan', 'placement', 'poll_frequency', 'sysname', 'traffic_vlan', 'deleted');
sub convertplace
{
@@ -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;