summaryrefslogtreecommitdiff
path: root/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm
blob: 97a0c3f10d5dc87bb04c6ed8f1fdb231751ce0a5 (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
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},
    );
}

sub add_dhcp_range {
    my ($config, $dhcp_range, $noerr) = @_;

    my $result = eval {
	netbox_api_request($config, "POST", "/ipam/ip-ranges/", {
	    start_address => $dhcp_range->{'start-address'},
	    end_address => $dhcp_range->{'end-address'},
	});
    };
    if ($@) {
	return if $noerr;
	die "could not create ip range $dhcp_range->{'start-address'}:$dhcp_range->{'end-address'}: $@";
    }

    return $result->{id};
}

sub del_dhcp_range {
    my ($config, $id, $noerr) = @_;

    eval {
	netbox_api_request($config, "DELETE", "/ipam/ip-ranges/$id/");
    };

    die "could not create dhcp range: $@" if $@ && !$noerr;
}

# 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: $@";
    }

    my $dhcp_ranges = PVE::Network::SDN::Subnets::get_dhcp_ranges($subnet);
    for my $dhcp_range (@$dhcp_ranges) {
	add_dhcp_range($plugin_config, $dhcp_range, $noerr);
    }
}

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

    # old subnet in SubnetPlugin hook has already parsed dhcp-ranges
    # new subnet doesn't
    my $old_dhcp_ranges = $old_subnet->{'dhcp-range'};
    my $new_dhcp_ranges = PVE::Network::SDN::Subnets::get_dhcp_ranges($subnet);

    my $hash_range = sub {
	my ($dhcp_range) = @_;
	"$dhcp_range->{'start-address'} - $dhcp_range->{'end-address'}"
    };

    my $old_lookup = {};
    for my $dhcp_range (@$old_dhcp_ranges) {
	my $hash = $hash_range->($dhcp_range);
	$old_lookup->{$hash} = undef;
    }

    my $new_lookup = {};
    for my $dhcp_range (@$new_dhcp_ranges) {
	my $hash = $hash_range->($dhcp_range);
	$new_lookup->{$hash} = undef;
    }

    my $to_delete_ids = ();

    # delete first so we don't get errors with overlapping ranges
    for my $dhcp_range (@$old_dhcp_ranges) {
	my $hash = $hash_range->($dhcp_range);

	if (exists($new_lookup->{$hash})) {
	    next;
	}

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

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

	del_dhcp_range($plugin_config, $internalid, $noerr);
    }

    for my $dhcp_range (@$new_dhcp_ranges) {
	my $hash = $hash_range->($dhcp_range);

	add_dhcp_range($plugin_config, $dhcp_range, $noerr)
	    if !exists($old_lookup->{$hash});
    }
}

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";
    }

    my $dhcp_ranges = PVE::Network::SDN::Subnets::get_dhcp_ranges($subnet);
    for my $dhcp_range (@$dhcp_ranges) {
	my $internalid = get_iprange_id($plugin_config, $dhcp_range, $noerr);

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

	del_dhcp_range($plugin_config, $internalid, $noerr);
    }

    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 {
	my $params = {
	    address => "$ip/$mask",
	    description => $description,
	};

	$params->{dns_name} = $hostname if $hostname;

	netbox_api_request($plugin_config, "POST", "/ipam/ip-addresses/", $params);
    };

    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 {
	my $params = {
	    address => "$ip/$mask",
	    description => $description,
	};

	$params->{dns_name} = $hostname if $hostname;

	netbox_api_request($plugin_config, "PATCH", "/ipam/ip-addresses/$ip_id/", $params);
    };
    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 = undef;
    $description = "mac:$mac" if $mac;

    my $ip = eval {
	my $params = {
	    description => $description,
	};

	$params->{dns_name} = $hostname if $hostname;

	my $result = netbox_api_request($plugin_config, "POST", "/ipam/prefixes/$internalid/available-ips/", $params);

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

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

    return $ip;
}

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 = undef;
    $description = "mac:$data->{mac}" if $data->{mac};

    my $ip = eval {
	my $params = {
	    description => $description,
	};

	$params->{dns_name} = $data->{hostname} if $data->{hostname};

	my $result = netbox_api_request($plugin_config, "POST", "/ipam/ip-ranges/$internalid/available-ips/", $params);

	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;
    }

    return $ip;
}

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");
    };
    if ($@) {
	return if $noerr;
	die "could not query ip address entry for mac $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;