SPAM: [PATCH pve-network 08/16] ipam: nautobot: 1st working "add subnet" and "add ip"
Lou Lecrivain
lou.lecrivain at wdz.de
Wed Nov 27 17:17:55 CET 2024
Signed-off-by: lou lecrivain <lou.lecrivain at wdz.de>
---
src/PVE/Network/SDN/Ipams/NautobotPlugin.pm | 69 ++++++++++++++++++++-
1 file changed, 66 insertions(+), 3 deletions(-)
diff --git a/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm b/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm
index ee0ad49..6675ba4 100644
--- a/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm
+++ b/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm
@@ -34,19 +34,82 @@ sub default_ip_status {
# implem
+sub add_subnet {
+ my ($class, $plugin_config, $subnetid, $subnet, $noerr) = @_;
+
+ my $cidr = $subnet->{cidr};
+ my $gateway = $subnet->{gateway};
+ my $url = $plugin_config->{url};
+ my $token = $plugin_config->{token};
+ my $namespace = $plugin_config->{namespace};
+ my $headers = ['Content-Type' => "application/json", 'Authorization' => "token $token", 'Accept' => "application/json"];
+
+ my $internalid = PVE::Network::SDN::Ipams::NetboxPlugin::get_prefix_id($url, $cidr, $headers);
+
+ #create subnet
+ if (!$internalid) {
+ my $namespace_id = get_namespace_id($url, $namespace, $headers);
+ my $status_id = get_status_id($url, default_ip_status(), $headers);
+
+ my $params = { prefix => $cidr, namespace => { id => $namespace_id}, status => { id => $status_id}};
+
+ eval {
+ my $result = PVE::Network::SDN::api_request("POST", "$url/ipam/prefixes/", $headers, $params);
+ };
+ if ($@) {
+ die "error adding subnet to ipam: $@" if !$noerr;
+ }
+ }
+}
+
+sub add_ip {
+ my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $vmid, $is_gateway, $noerr) = @_;
+
+ my $mask = $subnet->{mask};
+ my $url = $plugin_config->{url};
+ my $token = $plugin_config->{token};
+ my $namespace = $plugin_config->{namespace};
+ my $headers = ['Content-Type' => "application/json", 'Authorization' => "token $token", 'Accept' => "application/json"];
+
+ my $namespace_id = get_namespace_id($url, $namespace, $headers);
+ my $status_id = get_status_id($url, default_ip_status(), $headers);
+
+ my $description = undef;
+ if ($is_gateway) {
+ $description = 'gateway'
+ } elsif ($mac) {
+ $description = "mac:$mac";
+ }
+
+ my $params = { address => "$ip/$mask", type => "dhcp", dns_name => $hostname, description => $description, namespace => { id => $namespace_id }, status => { id => $status_id }};
+
+ eval {
+ PVE::Network::SDN::api_request("POST", "$url/ipam/ip-addresses/", $headers, $params);
+ };
+
+ if ($@) {
+ if($is_gateway) {
+ die "error adding subnet ip to ipam: ip $ip already exists: $@" if !PVE::Network::SDN::Ipams::NetboxPlugin::is_ip_gateway($url, $ip, $headers) && !$noerr;
+ } else {
+ die "error adding subnet ip to ipam: ip $ip already exists: $@" if !$noerr;
+ }
+ }
+}
+
+
sub verify_api {
my ($class, $plugin_config) = @_;
my $url = $plugin_config->{url};
my $token = $plugin_config->{token};
my $namespace = $plugin_config->{namespace};
- my $headers = [ 'Authorization' => "token $token", 'Accept' => "application/json; indent=4" ];
+ my $headers = ['Content-Type' => "application/json", 'Authorization' => "token $token", 'Accept' => "application/json"];
# check that the namespace exists AND that default IP active status
# exists AND that we have indeed API access
eval {
- PVE::Network::SDN::Ipams::NautobotPlugin::get_namespace_id($url, $namespace, $headers) // die "namespace $namespace does not exist";
- PVE::Network::SDN::Ipams::NautobotPlugin::get_status_id($url, default_ip_status(), $headers) // die "default IP status ". default_ip_status() . " not found";
+ get_namespace_id($url, $namespace, $headers) // die "namespace $namespace does not exist";
+ get_status_id($url, default_ip_status(), $headers) // die "default IP status ". default_ip_status() . " not found";
};
if ($@) {
die "Can't use nautobot api: $@";
--
2.39.5
More information about the pve-devel
mailing list