]>
git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Ipams/NetboxPlugin.pm
1 package PVE
::Network
::SDN
::Ipams
::NetboxPlugin
;
9 use base
('PVE::Network::SDN::Ipams::Plugin');
23 url
=> { optional
=> 0},
24 token
=> { optional
=> 0 },
28 # Plugin implementation
31 my ($class, $plugin_config, $subnetid, $subnet, $noerr) = @_;
33 my $cidr = $subnet->{cidr
};
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"];
39 my $internalid = get_prefix_id
($url, $cidr, $headers);
44 my $params = { prefix
=> $cidr };
47 my $result = PVE
::Network
::SDN
::api_request
("POST", "$url/ipam/prefixes/", $headers, $params);
50 die "error add subnet to ipam: $@" if !$noerr;
57 my ($class, $plugin_config, $subnetid, $subnet, $noerr) = @_;
59 my $cidr = $subnet->{cidr
};
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"];
65 my $internalid = get_prefix_id
($url, $cidr, $headers);
66 return if !$internalid;
68 return; #fixme: check that prefix is empty exluding gateway, before delete
71 PVE
::Network
::SDN
::api_request
("DELETE", "$url/ipam/prefixes/$internalid/", $headers);
74 die "error deleting subnet from ipam: $@" if !$noerr;
80 my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway, $noerr) = @_;
82 my $mask = $subnet->{mask
};
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 $description .= " mac:$mac" if $mac && $description;
89 my $params = { address
=> "$ip/$mask", dns_name
=> $hostname, description
=> $description };
92 PVE
::Network
::SDN
::api_request
("POST", "$url/ipam/ip-addresses/", $headers, $params);
97 die "error add subnet ip to ipam: ip $ip already exist: $@" if !is_ip_gateway
($url, $ip, $headers) && !$noerr;
99 die "error add subnet ip to ipam: ip already exist: $@" if !$noerr;
105 my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway, $noerr) = @_;
107 my $mask = $subnet->{mask
};
108 my $url = $plugin_config->{url
};
109 my $token = $plugin_config->{token
};
110 my $section = $plugin_config->{section
};
111 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
112 $description .= " mac:$mac" if $mac && $description;
114 my $params = { address
=> "$ip/$mask", dns_name
=> $hostname, description
=> $description };
116 my $ip_id = get_ip_id
($url, $ip, $headers);
117 die "can't find ip $ip in ipam" if !$ip_id;
120 PVE
::Network
::SDN
::api_request
("PATCH", "$url/ipam/ip-addresses/$ip_id/", $headers, $params);
123 die "error update ip $ip : $@" if !$noerr;
127 sub add_next_freeip
{
128 my ($class, $plugin_config, $subnetid, $subnet, $hostname, $mac, $description, $noerr) = @_;
130 my $cidr = $subnet->{cidr
};
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"];
136 my $internalid = get_prefix_id
($url, $cidr, $headers);
137 $description .= " mac:$mac" if $mac && $description;
139 my $params = { dns_name
=> $hostname, description
=> $description };
143 my $result = PVE
::Network
::SDN
::api_request
("POST", "$url/ipam/prefixes/$internalid/available-ips/", $headers, $params);
144 $ip = $result->{address
};
148 die "can't find free ip in subnet $cidr: $@" if !$noerr;
155 my ($class, $plugin_config, $subnetid, $subnet, $ip, $noerr) = @_;
159 my $url = $plugin_config->{url
};
160 my $token = $plugin_config->{token
};
161 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
163 my $ip_id = get_ip_id
($url, $ip, $headers);
164 die "can't find ip $ip in ipam" if !$ip_id;
167 PVE
::Network
::SDN
::api_request
("DELETE", "$url/ipam/ip-addresses/$ip_id/", $headers);
170 die "error delete ip $ip : $@" if !$noerr;
175 my ($class, $plugin_config) = @_;
177 my $url = $plugin_config->{url
};
178 my $token = $plugin_config->{token
};
179 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
183 PVE
::Network
::SDN
::api_request
("GET", "$url/ipam/aggregates/", $headers);
186 die "Can't connect to netbox api: $@";
191 my ($class, $plugin_config) = @_;
193 PVE
::Network
::SDN
::Ipams
::NetboxPlugin
::verify_api
($class, $plugin_config);
199 my ($url, $cidr, $headers) = @_;
201 my $result = PVE
::Network
::SDN
::api_request
("GET", "$url/ipam/prefixes/?q=$cidr", $headers);
202 my $data = @{$result->{results
}}[0];
203 my $internalid = $data->{id
};
208 my ($url, $ip, $headers) = @_;
209 my $result = PVE
::Network
::SDN
::api_request
("GET", "$url/ipam/ip-addresses/?q=$ip", $headers);
210 my $data = @{$result->{results
}}[0];
211 my $ip_id = $data->{id
};
216 my ($url, $ip, $headers) = @_;
217 my $result = PVE
::Network
::SDN
::api_request
("GET", "$url/addresses/search/$ip", $headers);
218 my $data = @{$result->{data
}}[0];
219 my $description = $data->{description
};
220 my $is_gateway = 1 if $description eq 'gateway';