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