]>
Commit | Line | Data |
---|---|---|
70b03506 AD |
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) = @_; | |
32 | ||
e8736dac | 33 | my $cidr = $subnet->{cidr}; |
70b03506 AD |
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) { | |
70b03506 AD |
43 | |
44 | my $params = { prefix => $cidr }; | |
45 | ||
46 | eval { | |
167dc03f | 47 | my $result = PVE::Network::SDN::api_request("POST", "$url/ipam/prefixes/", $headers, $params); |
70b03506 AD |
48 | }; |
49 | if ($@) { | |
50 | die "error add subnet to ipam: $@"; | |
51 | } | |
52 | } | |
53 | ||
54 | } | |
55 | ||
56 | sub del_subnet { | |
57 | my ($class, $plugin_config, $subnetid, $subnet) = @_; | |
58 | ||
e8736dac | 59 | my $cidr = $subnet->{cidr}; |
70b03506 AD |
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; | |
70b03506 | 67 | |
e8736dac | 68 | return; #fixme: check that prefix is empty exluding gateway, before delete |
70b03506 AD |
69 | |
70 | eval { | |
167dc03f | 71 | PVE::Network::SDN::api_request("DELETE", "$url/ipam/prefixes/$internalid/", $headers); |
70b03506 AD |
72 | }; |
73 | if ($@) { | |
74 | die "error deleting subnet from ipam: $@"; | |
75 | } | |
76 | ||
77 | } | |
78 | ||
79 | sub add_ip { | |
e9365ab0 | 80 | my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway) = @_; |
70b03506 | 81 | |
e8736dac | 82 | my $mask = $subnet->{mask}; |
70b03506 AD |
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"]; | |
e9365ab0 | 87 | $description .= " mac:$mac" if $mac && $description; |
70b03506 | 88 | |
ceb972a9 | 89 | my $params = { address => "$ip/$mask", dns_name => $hostname, description => $description }; |
70b03506 AD |
90 | |
91 | eval { | |
167dc03f | 92 | PVE::Network::SDN::api_request("POST", "$url/ipam/ip-addresses/", $headers, $params); |
70b03506 AD |
93 | }; |
94 | ||
95 | if ($@) { | |
96 | die "error add subnet ip to ipam: ip already exist: $@"; | |
97 | } | |
98 | } | |
99 | ||
dd54b5a3 AD |
100 | sub update_ip { |
101 | my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway) = @_; | |
102 | ||
103 | my $mask = $subnet->{mask}; | |
104 | my $url = $plugin_config->{url}; | |
105 | my $token = $plugin_config->{token}; | |
106 | my $section = $plugin_config->{section}; | |
107 | my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"]; | |
108 | $description .= " mac:$mac" if $mac && $description; | |
109 | ||
110 | my $params = { address => "$ip/$mask", dns_name => $hostname, description => $description }; | |
111 | ||
112 | my $ip_id = get_ip_id($url, $ip, $headers); | |
113 | die "can't find ip $ip in ipam" if !$ip_id; | |
114 | ||
115 | eval { | |
167dc03f | 116 | PVE::Network::SDN::api_request("PATCH", "$url/ipam/ip-addresses/$ip_id/", $headers, $params); |
dd54b5a3 AD |
117 | }; |
118 | if ($@) { | |
119 | die "error update ip $ip : $@"; | |
120 | } | |
121 | } | |
122 | ||
70b03506 | 123 | sub add_next_freeip { |
e9365ab0 | 124 | my ($class, $plugin_config, $subnetid, $subnet, $hostname, $mac, $description) = @_; |
70b03506 | 125 | |
e8736dac AD |
126 | my $cidr = $subnet->{cidr}; |
127 | ||
70b03506 AD |
128 | my $url = $plugin_config->{url}; |
129 | my $token = $plugin_config->{token}; | |
130 | my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"]; | |
131 | ||
132 | my $internalid = get_prefix_id($url, $cidr, $headers); | |
e9365ab0 | 133 | $description .= " mac:$mac" if $mac && $description; |
70b03506 | 134 | |
ceb972a9 | 135 | my $params = { dns_name => $hostname, description => $description }; |
70b03506 AD |
136 | |
137 | my $ip = undef; | |
138 | eval { | |
167dc03f | 139 | my $result = PVE::Network::SDN::api_request("POST", "$url/ipam/prefixes/$internalid/available-ips/", $headers, $params); |
70b03506 AD |
140 | $ip = $result->{address}; |
141 | }; | |
142 | ||
143 | if ($@) { | |
144 | die "can't find free ip in subnet $cidr: $@"; | |
145 | } | |
146 | ||
147 | return $ip; | |
148 | } | |
149 | ||
150 | sub del_ip { | |
e8736dac | 151 | my ($class, $plugin_config, $subnetid, $subnet, $ip) = @_; |
70b03506 AD |
152 | |
153 | return if !$ip; | |
154 | ||
155 | my $url = $plugin_config->{url}; | |
156 | my $token = $plugin_config->{token}; | |
157 | my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"]; | |
158 | ||
159 | my $ip_id = get_ip_id($url, $ip, $headers); | |
160 | die "can't find ip $ip in ipam" if !$ip_id; | |
161 | ||
162 | eval { | |
167dc03f | 163 | PVE::Network::SDN::api_request("DELETE", "$url/ipam/ip-addresses/$ip_id/", $headers); |
70b03506 AD |
164 | }; |
165 | if ($@) { | |
166 | die "error delete ip $ip : $@"; | |
167 | } | |
168 | } | |
169 | ||
170 | sub verify_api { | |
171 | my ($class, $plugin_config) = @_; | |
172 | ||
173 | my $url = $plugin_config->{url}; | |
174 | my $token = $plugin_config->{token}; | |
175 | my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"]; | |
176 | ||
177 | ||
178 | eval { | |
167dc03f | 179 | PVE::Network::SDN::api_request("GET", "$url/ipam/aggregates/", $headers); |
70b03506 AD |
180 | }; |
181 | if ($@) { | |
182 | die "Can't connect to netbox api: $@"; | |
183 | } | |
184 | } | |
185 | ||
186 | sub on_update_hook { | |
187 | my ($class, $plugin_config) = @_; | |
188 | ||
189 | PVE::Network::SDN::Ipams::NetboxPlugin::verify_api($class, $plugin_config); | |
190 | } | |
191 | ||
192 | #helpers | |
193 | ||
194 | sub get_prefix_id { | |
195 | my ($url, $cidr, $headers) = @_; | |
196 | ||
167dc03f | 197 | my $result = PVE::Network::SDN::api_request("GET", "$url/ipam/prefixes/?q=$cidr", $headers); |
70b03506 AD |
198 | my $data = @{$result->{results}}[0]; |
199 | my $internalid = $data->{id}; | |
200 | return $internalid; | |
201 | } | |
202 | ||
203 | sub get_ip_id { | |
204 | my ($url, $ip, $headers) = @_; | |
167dc03f | 205 | my $result = PVE::Network::SDN::api_request("GET", "$url/ipam/ip-addresses/?q=$ip", $headers); |
70b03506 AD |
206 | my $data = @{$result->{results}}[0]; |
207 | my $ip_id = $data->{id}; | |
208 | return $ip_id; | |
209 | } | |
210 | ||
211 | ||
212 | 1; | |
213 | ||
214 |