]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Ipams/NetboxPlugin.pm
subnets/ipam : fix is_gateway
[pve-network.git] / PVE / Network / SDN / Ipams / NetboxPlugin.pm
1 package PVE::Network::SDN::Ipams::NetboxPlugin;
2
3 use strict;
4 use warnings;
5 use PVE::INotify;
6 use PVE::Cluster;
7 use PVE::Tools;
8
9 use base('PVE::Network::SDN::Ipams::Plugin');
10
11 sub type {
12 return 'netbox';
13 }
14
15 sub properties {
16 return {
17 };
18 }
19
20 sub options {
21
22 return {
23 url => { optional => 0},
24 token => { optional => 0 },
25 };
26 }
27
28 # Plugin implementation
29
30 sub add_subnet {
31 my ($class, $plugin_config, $subnetid, $subnet, $noerr) = @_;
32
33 my $cidr = $subnet->{cidr};
34 my $gateway = $subnet->{gateway};
35 my $url = $plugin_config->{url};
36 my $token = $plugin_config->{token};
37 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
38
39 my $internalid = get_prefix_id($url, $cidr, $headers);
40
41 #create subnet
42 if (!$internalid) {
43
44 my $params = { prefix => $cidr };
45
46 eval {
47 my $result = PVE::Network::SDN::api_request("POST", "$url/ipam/prefixes/", $headers, $params);
48 };
49 if ($@) {
50 die "error add subnet to ipam: $@" if !$noerr;
51 }
52 }
53
54 }
55
56 sub del_subnet {
57 my ($class, $plugin_config, $subnetid, $subnet, $noerr) = @_;
58
59 my $cidr = $subnet->{cidr};
60 my $url = $plugin_config->{url};
61 my $token = $plugin_config->{token};
62 my $gateway = $subnet->{gateway};
63 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
64
65 my $internalid = get_prefix_id($url, $cidr, $headers);
66 return if !$internalid;
67
68 return; #fixme: check that prefix is empty exluding gateway, before delete
69
70 eval {
71 PVE::Network::SDN::api_request("DELETE", "$url/ipam/prefixes/$internalid/", $headers);
72 };
73 if ($@) {
74 die "error deleting subnet from ipam: $@" if !$noerr;
75 }
76
77 }
78
79 sub add_ip {
80 my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway, $noerr) = @_;
81
82 my $mask = $subnet->{mask};
83 my $url = $plugin_config->{url};
84 my $token = $plugin_config->{token};
85 my $section = $plugin_config->{section};
86 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
87 $description .= " mac:$mac" if $mac && $description;
88
89 my $params = { address => "$ip/$mask", dns_name => $hostname, description => $description };
90
91 eval {
92 PVE::Network::SDN::api_request("POST", "$url/ipam/ip-addresses/", $headers, $params);
93 };
94
95 if ($@) {
96 if($is_gateway) {
97 die "error add subnet ip to ipam: ip $ip already exist: $@" if !is_ip_gateway($url, $ip, $headers) && !$noerr;
98 } else {
99 die "error add subnet ip to ipam: ip already exist: $@" if !$noerr;
100 }
101 }
102 }
103
104 sub update_ip {
105 my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway, $noerr) = @_;
106
107 my $mask = $subnet->{mask};
108 my $url = $plugin_config->{url};
109 my $token = $plugin_config->{token};
110 my $section = $plugin_config->{section};
111 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
112 $description .= " mac:$mac" if $mac && $description;
113
114 my $params = { address => "$ip/$mask", dns_name => $hostname, description => $description };
115
116 my $ip_id = get_ip_id($url, $ip, $headers);
117 die "can't find ip $ip in ipam" if !$ip_id;
118
119 eval {
120 PVE::Network::SDN::api_request("PATCH", "$url/ipam/ip-addresses/$ip_id/", $headers, $params);
121 };
122 if ($@) {
123 die "error update ip $ip : $@" if !$noerr;
124 }
125 }
126
127 sub add_next_freeip {
128 my ($class, $plugin_config, $subnetid, $subnet, $hostname, $mac, $description, $noerr) = @_;
129
130 my $cidr = $subnet->{cidr};
131
132 my $url = $plugin_config->{url};
133 my $token = $plugin_config->{token};
134 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
135
136 my $internalid = get_prefix_id($url, $cidr, $headers);
137 $description .= " mac:$mac" if $mac && $description;
138
139 my $params = { dns_name => $hostname, description => $description };
140
141 my $ip = undef;
142 eval {
143 my $result = PVE::Network::SDN::api_request("POST", "$url/ipam/prefixes/$internalid/available-ips/", $headers, $params);
144 $ip = $result->{address};
145 };
146
147 if ($@) {
148 die "can't find free ip in subnet $cidr: $@" if !$noerr;
149 }
150
151 return $ip;
152 }
153
154 sub del_ip {
155 my ($class, $plugin_config, $subnetid, $subnet, $ip, $noerr) = @_;
156
157 return if !$ip;
158
159 my $url = $plugin_config->{url};
160 my $token = $plugin_config->{token};
161 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
162
163 my $ip_id = get_ip_id($url, $ip, $headers);
164 die "can't find ip $ip in ipam" if !$ip_id;
165
166 eval {
167 PVE::Network::SDN::api_request("DELETE", "$url/ipam/ip-addresses/$ip_id/", $headers);
168 };
169 if ($@) {
170 die "error delete ip $ip : $@" if !$noerr;
171 }
172 }
173
174 sub verify_api {
175 my ($class, $plugin_config) = @_;
176
177 my $url = $plugin_config->{url};
178 my $token = $plugin_config->{token};
179 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
180
181
182 eval {
183 PVE::Network::SDN::api_request("GET", "$url/ipam/aggregates/", $headers);
184 };
185 if ($@) {
186 die "Can't connect to netbox api: $@";
187 }
188 }
189
190 sub on_update_hook {
191 my ($class, $plugin_config) = @_;
192
193 PVE::Network::SDN::Ipams::NetboxPlugin::verify_api($class, $plugin_config);
194 }
195
196 #helpers
197
198 sub get_prefix_id {
199 my ($url, $cidr, $headers) = @_;
200
201 my $result = PVE::Network::SDN::api_request("GET", "$url/ipam/prefixes/?q=$cidr", $headers);
202 my $data = @{$result->{results}}[0];
203 my $internalid = $data->{id};
204 return $internalid;
205 }
206
207 sub get_ip_id {
208 my ($url, $ip, $headers) = @_;
209 my $result = PVE::Network::SDN::api_request("GET", "$url/ipam/ip-addresses/?q=$ip", $headers);
210 my $data = @{$result->{results}}[0];
211 my $ip_id = $data->{id};
212 return $ip_id;
213 }
214
215 sub is_ip_gateway {
216 my ($url, $ip, $headers) = @_;
217 my $result = PVE::Network::SDN::api_request("GET", "$url/addresses/search/$ip", $headers);
218 my $data = @{$result->{data}}[0];
219 my $description = $data->{description};
220 my $is_gateway = 1 if $description eq 'gateway';
221 return $is_gateway;
222 }
223
224 1;
225
226