]>
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) = @_;
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: $@";
57 my ($class, $plugin_config, $subnetid, $subnet) = @_;
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: $@";
80 my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway) = @_;
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);
96 die "error add subnet ip to ipam: ip already exist: $@";
101 my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway) = @_;
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;
110 my $params = { address
=> "$ip/$mask", dns_name
=> $hostname, description
=> $description };
112 my $ip_id = get_ip_id
($url, $ip, $headers);
113 die "can't find ip $ip in ipam" if !$ip_id;
116 PVE
::Network
::SDN
::api_request
("PATCH", "$url/ipam/ip-addresses/$ip_id/", $headers, $params);
119 die "error update ip $ip : $@";
123 sub add_next_freeip
{
124 my ($class, $plugin_config, $subnetid, $subnet, $hostname, $mac, $description) = @_;
126 my $cidr = $subnet->{cidr
};
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"];
132 my $internalid = get_prefix_id
($url, $cidr, $headers);
133 $description .= " mac:$mac" if $mac && $description;
135 my $params = { dns_name
=> $hostname, description
=> $description };
139 my $result = PVE
::Network
::SDN
::api_request
("POST", "$url/ipam/prefixes/$internalid/available-ips/", $headers, $params);
140 $ip = $result->{address
};
144 die "can't find free ip in subnet $cidr: $@";
151 my ($class, $plugin_config, $subnetid, $subnet, $ip) = @_;
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"];
159 my $ip_id = get_ip_id
($url, $ip, $headers);
160 die "can't find ip $ip in ipam" if !$ip_id;
163 PVE
::Network
::SDN
::api_request
("DELETE", "$url/ipam/ip-addresses/$ip_id/", $headers);
166 die "error delete ip $ip : $@";
171 my ($class, $plugin_config) = @_;
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"];
179 PVE
::Network
::SDN
::api_request
("GET", "$url/ipam/aggregates/", $headers);
182 die "Can't connect to netbox api: $@";
187 my ($class, $plugin_config) = @_;
189 PVE
::Network
::SDN
::Ipams
::NetboxPlugin
::verify_api
($class, $plugin_config);
195 my ($url, $cidr, $headers) = @_;
197 my $result = PVE
::Network
::SDN
::api_request
("GET", "$url/ipam/prefixes/?q=$cidr", $headers);
198 my $data = @{$result->{results
}}[0];
199 my $internalid = $data->{id
};
204 my ($url, $ip, $headers) = @_;
205 my $result = PVE
::Network
::SDN
::api_request
("GET", "$url/ipam/ip-addresses/?q=$ip", $headers);
206 my $data = @{$result->{results
}}[0];
207 my $ip_id = $data->{id
};