public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lou Lecrivain via pve-devel <pve-devel@lists.proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: Lou Lecrivain <lou.lecrivain@wdz.de>
Subject: [pve-devel] SPAM: [PATCH pve-network 08/16] ipam: nautobot: 1st working "add subnet" and "add ip"
Date: Wed, 27 Nov 2024 17:17:55 +0100	[thread overview]
Message-ID: <mailman.805.1732724875.391.pve-devel@lists.proxmox.com> (raw)
In-Reply-To: <20241127161803.8866-1-lou.lecrivain@wdz.de>

[-- Attachment #1: Type: message/rfc822, Size: 7430 bytes --]

From: Lou Lecrivain <lou.lecrivain@wdz.de>
To: pve-devel@lists.proxmox.com
Subject: SPAM: [PATCH pve-network 08/16] ipam: nautobot: 1st working "add subnet" and "add ip"
Date: Wed, 27 Nov 2024 17:17:55 +0100
Message-ID: <20241127161803.8866-9-lou.lecrivain@wdz.de>

Signed-off-by: lou lecrivain <lou.lecrivain@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



[-- Attachment #2: Type: text/plain, Size: 160 bytes --]

_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

  parent reply	other threads:[~2024-11-27 16:28 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20241127161803.8866-1-lou.lecrivain@wdz.de>
2024-11-27 16:17 ` [pve-devel] SPAM: [PATCH pve-network 01/16] ipam: nautobot support initial commit Lou Lecrivain via pve-devel
2024-11-27 16:17 ` [pve-devel] SPAM: [PATCH pve-network 02/16] ipam: add Nautobot plugin sources to Makefile Lou Lecrivain via pve-devel
2024-11-27 16:17 ` [pve-devel] SPAM: [PATCH pve-network 03/16] ipam: change verify URL (now common to Nautobot and Netbox) Lou Lecrivain via pve-devel
2024-11-27 16:17 ` [pve-devel] SPAM: [PATCH pve-network 04/16] ipam: nautobot: add verification for ipam namespace plugin parameter Lou Lecrivain via pve-devel
2024-11-27 16:17 ` [pve-devel] SPAM: [PATCH pve-network 05/16] ipam: nautobot: fix on_update_hook for NautobotPlugin Lou Lecrivain via pve-devel
2024-11-27 16:17 ` [pve-devel] SPAM: [PATCH pve-network 06/16] ipam: nautobot: add default active status check Lou Lecrivain via pve-devel
2024-11-27 16:17 ` [pve-devel] SPAM: [PATCH pve-network 07/16] ipam: nautobot: fix typo Lou Lecrivain via pve-devel
2024-11-27 16:17 ` Lou Lecrivain via pve-devel [this message]
2024-11-27 16:17 ` [pve-devel] SPAM: [PATCH pve-network 09/16] ipam: nautobot: simplify query Lou Lecrivain via pve-devel
2024-11-27 16:17 ` [pve-devel] SPAM: [PATCH pve-network 10/16] ipam: nautobot: api endpoint change no longer needed Lou Lecrivain via pve-devel
2024-11-27 16:17 ` [pve-devel] SPAM: [PATCH pve-network 11/16] ipam: nautobot: add update_ip sub Lou Lecrivain via pve-devel
2024-11-27 16:17 ` [pve-devel] SPAM: [PATCH pve-network 12/16] ipam: nautobot: add get ips for mac function Lou Lecrivain via pve-devel
2024-11-27 16:18 ` [pve-devel] SPAM: [PATCH pve-network 13/16] ipam: nautobot: 1st draft for allocating ip in dhcp range Lou Lecrivain via pve-devel
2024-11-27 16:18 ` [pve-devel] SPAM: [PATCH pve-network 14/16] ipam: nautobot: hacky ip range support Lou Lecrivain via pve-devel
2024-11-27 16:18 ` [pve-devel] SPAM: [PATCH pve-network 15/16] ipam: nautobot: implement plain prefix allocation (without ranges) Lou Lecrivain via pve-devel
2024-11-27 16:18 ` [pve-devel] SPAM: [PATCH pve-network 16/16] ipam: nautobot: add word of warning for dhcp range support Lou Lecrivain via pve-devel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=mailman.805.1732724875.391.pve-devel@lists.proxmox.com \
    --to=pve-devel@lists.proxmox.com \
    --cc=lou.lecrivain@wdz.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal