]>
git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Ipams/NetboxPlugin.pm
01f82f2201f576547285e6984dabc33d69ad4831
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 = $subnetid =~ s/-/\//r
;
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);
43 my ($network, $mask) = split(/-/, $subnetid);
45 my $params = { prefix
=> $cidr };
48 my $result = PVE
::Network
::SDN
::Ipams
::Plugin
::api_request
("POST", "$url/ipam/prefixes/", $headers, $params);
49 $subnet->{ipamid
} = $result->{id
} if defined($result->{id
});
52 die "error add subnet to ipam: $@";
59 my ($class, $plugin_config, $subnetid, $subnet) = @_;
61 my $cidr = $subnetid =~ s/-/\//r
;
62 my $url = $plugin_config->{url
};
63 my $token = $plugin_config->{token
};
64 my $gateway = $subnet->{gateway
};
65 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
67 my $internalid = get_prefix_id
($url, $cidr, $headers);
68 return if !$internalid;
69 #fixme: check that prefix is empty exluding gateway, before delete
71 PVE
::Network
::SDN
::Ipams
::NetboxPlugin
::del_ip
($class, $plugin_config, $subnetid, $gateway) if $gateway;
74 PVE
::Network
::SDN
::Ipams
::Plugin
::api_request
("DELETE", "$url/ipam/prefixes/$internalid/", $headers);
77 die "error deleting subnet from ipam: $@";
83 my ($class, $plugin_config, $subnetid, $ip, $is_gateway) = @_;
85 my ($network, $mask) = split(/-/, $subnetid);
86 my $url = $plugin_config->{url
};
87 my $token = $plugin_config->{token
};
88 my $section = $plugin_config->{section
};
89 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
91 my $params = { address
=> "$ip/$mask" };
94 PVE
::Network
::SDN
::Ipams
::Plugin
::api_request
("POST", "$url/ipam/ip-addresses/", $headers, $params);
98 die "error add subnet ip to ipam: ip already exist: $@";
102 sub add_next_freeip
{
103 my ($class, $plugin_config, $subnetid, $subnet) = @_;
105 my $cidr = $subnetid =~ s/-/\//r
;
106 my $url = $plugin_config->{url
};
107 my $token = $plugin_config->{token
};
108 my $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => "token $token"];
110 my $internalid = get_prefix_id
($url, $cidr, $headers);
116 my $result = PVE
::Network
::SDN
::Ipams
::Plugin
::api_request
("POST", "$url/ipam/prefixes/$internalid/available-ips/", $headers, $params);
117 $ip = $result->{address
};
121 die "can't find free ip in subnet $cidr: $@";
128 my ($class, $plugin_config, $subnetid, $ip) = @_;
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 $ip_id = get_ip_id
($url, $ip, $headers);
137 die "can't find ip $ip in ipam" if !$ip_id;
140 PVE
::Network
::SDN
::Ipams
::Plugin
::api_request
("DELETE", "$url/ipam/ip-addresses/$ip_id/", $headers);
143 die "error delete ip $ip : $@";
148 my ($class, $plugin_config) = @_;
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"];
156 PVE
::Network
::SDN
::Ipams
::Plugin
::api_request
("GET", "$url/ipam/aggregates/", $headers);
159 die "Can't connect to netbox api: $@";
164 my ($class, $plugin_config) = @_;
166 PVE
::Network
::SDN
::Ipams
::NetboxPlugin
::verify_api
($class, $plugin_config);
172 my ($url, $cidr, $headers) = @_;
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
};
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
};