summaryrefslogtreecommitdiff
path: root/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm
blob: 6595cbc3e73e7a5c64d38bd94fbab687cb591250 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package PVE::Network::SDN::Ipams::NetboxPlugin;

use strict;
use warnings;
use PVE::INotify;
use PVE::Cluster;
use PVE::Tools;

use base('PVE::Network::SDN::Ipams::Plugin');

sub type {
    return 'netbox';
}

sub properties {
    return {
    };
}

sub options {
    return {
        url => { optional => 0},
        token => { optional => 0 },
        fingerprint => { optional => 1 },
    };
}

sub netbox_api_request {
    my ($config, $method, $path, $params) = @_;

    return PVE::Network::SDN::api_request(
	$method,
	"$config->{url}${path}",
	[
	    'Content-Type' => 'application/json; charset=UTF-8',
	    'Authorization' => "token $config->{token}"
	],
	$params,
	$config->{fingerprint},
    );
}

# Plugin implementation

sub add_subnet {
    my ($class, $plugin_config, $subnetid, $subnet, $noerr) = @_;

    my $cidr = $subnet->{cidr};
    my $gateway = $subnet->{gateway};

    if (get_prefix_id($plugin_config, $cidr, $noerr)) {
	return if $noerr;
	die "prefix $cidr already exists in netbox";
    }

    eval {
	netbox_api_request($plugin_config, "POST", "/ipam/prefixes/", {
	    prefix => $cidr
	});
    };
    if ($@) {
	return if $noerr;
	die "error adding subnet to ipam: $@";
    }
}

sub del_subnet {
    my ($class, $plugin_config, $subnetid, $subnet, $noerr) = @_;

    my $cidr = $subnet->{cidr};

    my $internalid = get_prefix_id($plugin_config, $cidr, $noerr);

    # definedness check, because ID could be 0
    if (!defined($internalid)) {
	warn "could not find id for ip prefix $cidr";
	return;
    }

    if (!is_prefix_empty($plugin_config, $cidr, $noerr)) {
	return if $noerr;
	die "not deleting prefix $cidr because it still contains entries";
    }

    # last IP is assumed to be the gateway, delete it
    if (!$class->del_ip($plugin_config, $subnetid, $subnet, $subnet->{gateway}, $noerr)) {
	return if $noerr;
	die "could not delete gateway ip from subnet $subnetid";
    }

    eval {
	netbox_api_request($plugin_config, "DELETE", "/ipam/prefixes/$internalid/");
    };
    die "error deleting subnet from ipam: $@" if $@ && !$noerr;
}

sub add_ip {
    my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $vmid, $is_gateway, $noerr) = @_;

    my $mask = $subnet->{mask};

    my $description = undef;
    if ($is_gateway) {
	$description = 'gateway'
    } elsif ($mac) {
	$description = "mac:$mac";
    }

    eval {
	netbox_api_request($plugin_config, "POST", "/ipam/ip-addresses/", {
	    address => "$ip/$mask",
	    dns_name => $hostname,
	    description => $description,
	});
    };

    if ($@) {
	if ($is_gateway) {
	    die "error add subnet ip to ipam: ip $ip already exist: $@"
		if !is_ip_gateway($plugin_config, $ip, $noerr);
	} elsif (!$noerr) {
	    die "error add subnet ip to ipam: ip already exist: $@";
	}
    }
}

sub update_ip {
    my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $vmid, $is_gateway, $noerr) = @_;

    my $mask = $subnet->{mask};

    my $description = undef;
    if ($is_gateway) {
	$description = 'gateway'
    } elsif ($mac) {
	$description = "mac:$mac";
    }

    my $ip_id = get_ip_id($plugin_config, $ip, $noerr);

    # definedness check, because ID could be 0
    if (!defined($ip_id)) {
	return if $noerr;
	die "could not find id for ip address $ip";
    }

    eval {
	netbox_api_request($plugin_config, "PATCH", "/ipam/ip-addresses/$ip_id/", {
	    address => "$ip/$mask",
	    dns_name => $hostname,
	    description => $description,
	});
    };
    if ($@) {
	die "error update ip $ip : $@" if !$noerr;
    }
}

sub add_next_freeip {
    my ($class, $plugin_config, $subnetid, $subnet, $hostname, $mac, $vmid, $noerr) = @_;

    my $cidr = $subnet->{cidr};

    my $internalid = get_prefix_id($plugin_config, $cidr, $noerr);

    # definedness check, because ID could be 0
    if (!defined($internalid)) {
	return if $noerr;
	die "could not find id for prefix $cidr";
    }

    my $description = "mac:$mac" if $mac;

    eval {
	my $result = netbox_api_request($plugin_config, "POST", "/ipam/prefixes/$internalid/available-ips/", {
	    dns_name => $hostname,
	    description => $description,
	});

	my ($ip, undef) = split(/\//, $result->{address});
	return $ip;
    };

    if ($@) {
	die "can't find free ip in subnet $cidr: $@" if !$noerr;
    }
}

sub add_range_next_freeip {
    my ($class, $plugin_config, $subnet, $range, $data, $noerr) = @_;

    my $internalid = get_iprange_id($plugin_config, $range, $noerr);

    # definedness check, because ID could be 0
    if (!defined($internalid)) {
	return if $noerr;
	die "could not find id for ip range $range->{'start-address'}:$range->{'end-address'}";
    }

    my $description = "mac:$data->{mac}" if $data->{mac};

    eval {
	my $result = netbox_api_request($plugin_config, "POST", "/ipam/ip-ranges/$internalid/available-ips/", {
	    dns_name => $data->{hostname},
	    description => $description,
	});

	my ($ip, undef) = split(/\//, $result->{address});
	print "found ip free $ip in range $range->{'start-address'}-$range->{'end-address'}\n" if $ip;
	return $ip;
    };

    if ($@) {
	die "can't find free ip in range $range->{'start-address'}-$range->{'end-address'}: $@" if !$noerr;
    }
}

sub del_ip {
    my ($class, $plugin_config, $subnetid, $subnet, $ip, $noerr) = @_;

    return if !$ip;

    my $ip_id = get_ip_id($plugin_config, $ip, $noerr);
    if (!defined($ip_id)) {
	warn "could not find id for ip $ip";
	return;
    }

    eval {
	netbox_api_request($plugin_config, "DELETE", "/ipam/ip-addresses/$ip_id/");
    };
    if ($@) {
	die "error delete ip $ip : $@" if !$noerr;
    }

    return 1;
}

sub get_ips_from_mac {
    my ($class, $plugin_config, $mac, $zoneid, $noerr) = @_;

    my $ip4 = undef;
    my $ip6 = undef;

    my $data = eval {
	netbox_api_request($plugin_config, "GET", "/ipam/ip-addresses/?description__ic=$mac");
    };

    for my $ip (@{$data->{results}}) {
	if ($ip->{family}->{value} == 4 && !$ip4) {
	    ($ip4, undef) = split(/\//, $ip->{address});
	}

	if ($ip->{family}->{value} == 6 && !$ip6) {
	    ($ip6, undef) = split(/\//, $ip->{address});
	}
    }

    return ($ip4, $ip6);
}

sub verify_api {
    my ($class, $plugin_config) = @_;

    eval { netbox_api_request($plugin_config, "GET", "/ipam/aggregates/"); };
    if ($@) {
	die "Can't connect to netbox api: $@";
    }
}

sub on_update_hook {
    my ($class, $plugin_config) = @_;
    verify_api($class, $plugin_config);
}

# helpers
sub get_prefix_id {
    my ($config, $cidr, $noerr) = @_;

    # we need to supply any IP inside the prefix, without supplying the mask, so
    # just take the one from the cidr
    my ($ip, undef) = split(/\//, $cidr);

    my $result = eval { netbox_api_request($config, "GET", "/ipam/prefixes/?q=$ip") };
    if ($@) {
	return if $noerr;
	die "could not obtain ID for prefix $cidr: $@";
    }

    my $data = @{$result->{results}}[0];
    return $data->{id};
}

sub get_iprange_id {
    my ($config, $range, $noerr) = @_;

    my $result = eval {
	netbox_api_request(
	    $config,
	    "GET",
	    "/ipam/ip-ranges/?start_address=$range->{'start-address'}&end_address=$range->{'end-address'}",
	);
    };
    if ($@) {
	return if $noerr;
	die "could not obtain ID for IP range $range->{'start-address'}:$range->{'end-address'}: $@";
    }

    my $data = @{$result->{results}}[0];
    return $data->{id};
}

sub get_ip_id {
    my ($config, $ip, $noerr) = @_;

    my $result = eval { netbox_api_request($config, "GET", "/ipam/ip-addresses/?q=$ip") };
    if ($@) {
	return if $noerr;
	die "could not obtain ID for IP $ip: $@";
    }

    my $data = @{$result->{results}}[0];
    return $data->{id};
}

sub is_ip_gateway {
    my ($config, $ip, $noerr) = @_;

    my $result = eval { netbox_api_request($config, "GET", "/ipam/ip-addresses/?q=$ip") };
    if ($@) {
	return if $noerr;
	die "could not obtain ipam entry for address $ip: $@";
    }

    my $data = @{$result->{data}}[0];
    return $data->{description} eq 'gateway';
}

sub is_prefix_empty {
    my ($config, $cidr, $noerr) = @_;

    my $result = eval { netbox_api_request($config, "GET", "/ipam/ip-addresses/?parent=$cidr") };
    if ($@) {
	return if $noerr;
	die "could not query children for prefix $cidr: $@";
    }

    # checking against 1, because we do not count the gateway
    return scalar(@{$result->{results}}) <= 1;
}

1;