From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <pve-devel-bounces@lists.proxmox.com> Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id DB6651FF195 for <inbox@lore.proxmox.com>; Fri, 7 Mar 2025 18:44:06 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 1ACEE30A18; Fri, 7 Mar 2025 18:43:57 +0100 (CET) From: Stefan Hanreich <s.hanreich@proxmox.com> To: pve-devel@lists.proxmox.com Date: Fri, 7 Mar 2025 18:43:46 +0100 Message-Id: <20250307174352.337597-2-s.hanreich@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250307174352.337597-1-s.hanreich@proxmox.com> References: <20250307174352.337597-1-s.hanreich@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.276 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods PROLO_LEO1 0.1 Meta Catches all Leo drug variations so far RDNS_NONE 0.793 Delivered to internal network by a host with no rDNS SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record Subject: [pve-devel] [PATCH pve-network 2/8] ipam: netbox: implement deleting subnets X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com> List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe> List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/> List-Post: <mailto:pve-devel@lists.proxmox.com> List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help> List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe> Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com> Deleting a subnet did not delete any created entities in Netbox. Implement deletion of a subnet by deleting all entities that are created in Netbox upon creation of a subnet. We are checking for any leftover IP assignments before deleting the prefix, so we do not accidentally delete any manually created IP assignments. This method tries to check for any possible errors before editing the entities. There is still a small window where external changes can occur that lead to errors. We are touching multiple entities here, so in case of errors users have to fix their Netbox instance manually. Signed-off-by: Stefan Hanreich <s.hanreich@proxmox.com> --- src/PVE/Network/SDN/Ipams/NetboxPlugin.pm | 34 ++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm b/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm index 9fef3dc..ea0fd0c 100644 --- a/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm +++ b/src/PVE/Network/SDN/Ipams/NetboxPlugin.pm @@ -71,7 +71,22 @@ sub del_subnet { my $internalid = get_prefix_id($plugin_config, $cidr, $noerr); - return; #fixme: check that prefix is empty exluding gateway, before delete + # definedness check, because ID could be 0 + if (!defined($internalid)) { + warn "could not find id for ip prefix $cidr"; + return; + } + + if (!is_prefix_empty($plugin_config, $cidr, $noerr)) { + return if $noerr; + die "not deleting prefix $cidr because it still contains entries"; + } + + # last IP is assumed to be the gateway, delete it + if (!$class->del_ip($plugin_config, $subnetid, $subnet, $subnet->{gateway}, $noerr)) { + return if $noerr; + die "could not delete gateway ip from subnet $subnetid"; + } eval { netbox_api_request($plugin_config, "DELETE", "/ipam/prefixes/$internalid/"); @@ -217,6 +232,8 @@ sub del_ip { if ($@) { die "error delete ip $ip : $@" if !$noerr; } + + return 1; } sub get_ips_from_mac { @@ -324,6 +341,21 @@ sub is_ip_gateway { return $is_gateway; } +sub is_prefix_empty { + my ($config, $cidr, $noerr) = @_; + + # everywhere else we canonicalize to trailing slashes, but this endpoint + # DOES NOT WORK with trailing slashes... + my $result = eval { netbox_api_request($config, "GET", "/ipam/ip-addresses/?parent=$cidr") }; + if ($@) { + return if $noerr; + die "could not query children for prefix $cidr: $@"; + } + + # checking against 1, because we do not count the gateway + return scalar(@{$result->{results}}) <= 1; +} + 1; -- 2.39.5 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel