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