aboutsummaryrefslogtreecommitdiffstats
path: root/web/api/write/networks
diff options
context:
space:
mode:
Diffstat (limited to 'web/api/write/networks')
-rwxr-xr-xweb/api/write/networks82
1 files changed, 82 insertions, 0 deletions
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();