]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/SDN/Ipams/NetboxPlugin.pm
sdn: pending_config: initialize empty pending key
[pve-network.git] / PVE / Network / SDN / Ipams / NetboxPlugin.pm
CommitLineData
70b03506
AD
1package PVE::Network::SDN::Ipams::NetboxPlugin;
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 'netbox';
13}
14
15sub properties {
16 return {
17 };
18}
19
20sub options {
21
22 return {
23 url => { optional => 0},
24 token => { optional => 0 },
25 };
26}
27
28# Plugin implementation
29
30sub 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
56sub 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
79sub add_ip {
ceb972a9 80 my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $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"];
87
ceb972a9 88 my $params = { address => "$ip/$mask", dns_name => $hostname, description => $description };
70b03506
AD
89
90 eval {
91 PVE::Network::SDN::Ipams::Plugin::api_request("POST", "$url/ipam/ip-addresses/", $headers, $params);
92 };
93
94 if ($@) {
95 die "error add subnet ip to ipam: ip already exist: $@";
96 }
97}
98
99sub add_next_freeip {
ceb972a9 100 my ($class, $plugin_config, $subnetid, $subnet, $hostname, $description) = @_;
70b03506 101
e8736dac
AD
102 my $cidr = $subnet->{cidr};
103
70b03506
AD
104 my $url = $plugin_config->{url};
105 my $token = $plugin_config->{token};
106 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
107
108 my $internalid = get_prefix_id($url, $cidr, $headers);
109
ceb972a9 110 my $params = { dns_name => $hostname, description => $description };
70b03506
AD
111
112 my $ip = undef;
113 eval {
114 my $result = PVE::Network::SDN::Ipams::Plugin::api_request("POST", "$url/ipam/prefixes/$internalid/available-ips/", $headers, $params);
115 $ip = $result->{address};
116 };
117
118 if ($@) {
119 die "can't find free ip in subnet $cidr: $@";
120 }
121
122 return $ip;
123}
124
125sub del_ip {
e8736dac 126 my ($class, $plugin_config, $subnetid, $subnet, $ip) = @_;
70b03506
AD
127
128 return if !$ip;
129
130 my $url = $plugin_config->{url};
131 my $token = $plugin_config->{token};
132 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
133
134 my $ip_id = get_ip_id($url, $ip, $headers);
135 die "can't find ip $ip in ipam" if !$ip_id;
136
137 eval {
138 PVE::Network::SDN::Ipams::Plugin::api_request("DELETE", "$url/ipam/ip-addresses/$ip_id/", $headers);
139 };
140 if ($@) {
141 die "error delete ip $ip : $@";
142 }
143}
144
145sub verify_api {
146 my ($class, $plugin_config) = @_;
147
148 my $url = $plugin_config->{url};
149 my $token = $plugin_config->{token};
150 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
151
152
153 eval {
154 PVE::Network::SDN::Ipams::Plugin::api_request("GET", "$url/ipam/aggregates/", $headers);
155 };
156 if ($@) {
157 die "Can't connect to netbox api: $@";
158 }
159}
160
161sub on_update_hook {
162 my ($class, $plugin_config) = @_;
163
164 PVE::Network::SDN::Ipams::NetboxPlugin::verify_api($class, $plugin_config);
165}
166
167#helpers
168
169sub get_prefix_id {
170 my ($url, $cidr, $headers) = @_;
171
172 my $result = PVE::Network::SDN::Ipams::Plugin::api_request("GET", "$url/ipam/prefixes/?q=$cidr", $headers);
173 my $data = @{$result->{results}}[0];
174 my $internalid = $data->{id};
175 return $internalid;
176}
177
178sub get_ip_id {
179 my ($url, $ip, $headers) = @_;
180 my $result = PVE::Network::SDN::Ipams::Plugin::api_request("GET", "$url/ipam/ip-addresses/?q=$ip", $headers);
181 my $data = @{$result->{results}}[0];
182 my $ip_id = $data->{id};
183 return $ip_id;
184}
185
186
1871;
188
189