]>
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 { | |
47 | my $result = PVE::Network::SDN::Ipams::Plugin::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 { | |
71 | PVE::Network::SDN::Ipams::Plugin::api_request("DELETE", "$url/ipam/prefixes/$internalid/", $headers); | |
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 { | |
92 | PVE::Network::SDN::Ipams::Plugin::api_request("POST", "$url/ipam/ip-addresses/", $headers, $params); | |
93 | }; | |
94 | ||
95 | if ($@) { | |
96 | die "error add subnet ip to ipam: ip already exist: $@"; | |
97 | } | |
98 | } | |
99 | ||
100 | sub add_next_freeip { | |
e9365ab0 | 101 | my ($class, $plugin_config, $subnetid, $subnet, $hostname, $mac, $description) = @_; |
70b03506 | 102 | |
e8736dac AD |
103 | my $cidr = $subnet->{cidr}; |
104 | ||
70b03506 AD |
105 | my $url = $plugin_config->{url}; |
106 | my $token = $plugin_config->{token}; | |
107 | my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"]; | |
108 | ||
109 | my $internalid = get_prefix_id($url, $cidr, $headers); | |
e9365ab0 | 110 | $description .= " mac:$mac" if $mac && $description; |
70b03506 | 111 | |
ceb972a9 | 112 | my $params = { dns_name => $hostname, description => $description }; |
70b03506 AD |
113 | |
114 | my $ip = undef; | |
115 | eval { | |
116 | my $result = PVE::Network::SDN::Ipams::Plugin::api_request("POST", "$url/ipam/prefixes/$internalid/available-ips/", $headers, $params); | |
117 | $ip = $result->{address}; | |
118 | }; | |
119 | ||
120 | if ($@) { | |
121 | die "can't find free ip in subnet $cidr: $@"; | |
122 | } | |
123 | ||
124 | return $ip; | |
125 | } | |
126 | ||
127 | sub del_ip { | |
e8736dac | 128 | my ($class, $plugin_config, $subnetid, $subnet, $ip) = @_; |
70b03506 AD |
129 | |
130 | return if !$ip; | |
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 $ip_id = get_ip_id($url, $ip, $headers); | |
137 | die "can't find ip $ip in ipam" if !$ip_id; | |
138 | ||
139 | eval { | |
140 | PVE::Network::SDN::Ipams::Plugin::api_request("DELETE", "$url/ipam/ip-addresses/$ip_id/", $headers); | |
141 | }; | |
142 | if ($@) { | |
143 | die "error delete ip $ip : $@"; | |
144 | } | |
145 | } | |
146 | ||
147 | sub verify_api { | |
148 | my ($class, $plugin_config) = @_; | |
149 | ||
150 | my $url = $plugin_config->{url}; | |
151 | my $token = $plugin_config->{token}; | |
152 | my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"]; | |
153 | ||
154 | ||
155 | eval { | |
156 | PVE::Network::SDN::Ipams::Plugin::api_request("GET", "$url/ipam/aggregates/", $headers); | |
157 | }; | |
158 | if ($@) { | |
159 | die "Can't connect to netbox api: $@"; | |
160 | } | |
161 | } | |
162 | ||
163 | sub on_update_hook { | |
164 | my ($class, $plugin_config) = @_; | |
165 | ||
166 | PVE::Network::SDN::Ipams::NetboxPlugin::verify_api($class, $plugin_config); | |
167 | } | |
168 | ||
169 | #helpers | |
170 | ||
171 | sub get_prefix_id { | |
172 | my ($url, $cidr, $headers) = @_; | |
173 | ||
174 | my $result = PVE::Network::SDN::Ipams::Plugin::api_request("GET", "$url/ipam/prefixes/?q=$cidr", $headers); | |
175 | my $data = @{$result->{results}}[0]; | |
176 | my $internalid = $data->{id}; | |
177 | return $internalid; | |
178 | } | |
179 | ||
180 | sub get_ip_id { | |
181 | my ($url, $ip, $headers) = @_; | |
182 | my $result = PVE::Network::SDN::Ipams::Plugin::api_request("GET", "$url/ipam/ip-addresses/?q=$ip", $headers); | |
183 | my $data = @{$result->{results}}[0]; | |
184 | my $ip_id = $data->{id}; | |
185 | return $ip_id; | |
186 | } | |
187 | ||
188 | ||
189 | 1; | |
190 | ||
191 |