aboutsummaryrefslogtreecommitdiffstats
path: root/web/api/write/network-update
blob: f6684c5b2daace5190d16cd0d19229ce71731eed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#! /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();