SPAM: [PATCH pve-network 11/16] ipam: nautobot: add update_ip sub
Lou Lecrivain
lou.lecrivain at wdz.de
Wed Nov 27 17:17:58 CET 2024
+ use default_headers to avoid code repetition
Signed-off-by: lou lecrivain <lou.lecrivain at wdz.de>
---
src/PVE/Network/SDN/Ipams/NautobotPlugin.pm | 45 ++++++++++++++++++---
1 file changed, 39 insertions(+), 6 deletions(-)
diff --git a/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm b/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm
index c6581f9..e328c9f 100644
--- a/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm
+++ b/src/PVE/Network/SDN/Ipams/NautobotPlugin.pm
@@ -32,6 +32,13 @@ sub default_ip_status {
return 'Active';
}
+sub default_headers {
+ my ($plugin_config) = @_;
+ my $token = $plugin_config->{token};
+
+ return ['Content-Type' => "application/json", 'Authorization' => "token $token", 'Accept' => "application/json"];
+}
+
# implem
sub add_subnet {
@@ -40,9 +47,8 @@ sub add_subnet {
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 $headers = default_headers($plugin_config);
my $internalid = PVE::Network::SDN::Ipams::NetboxPlugin::get_prefix_id($url, $cidr, $headers);
@@ -64,9 +70,8 @@ sub add_ip {
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 $headers = default_headers($plugin_config);
my $description = undef;
if ($is_gateway) {
@@ -91,13 +96,41 @@ sub add_ip {
}
+sub update_ip {
+ my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $vmid, $is_gateway, $noerr) = @_;
+
+ my $mask = $subnet->{mask};
+ my $url = $plugin_config->{url};
+ my $namespace = $plugin_config->{namespace};
+ my $headers = default_headers($plugin_config);
+
+ 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 => $namespace, status => default_ip_status()};
+
+ my $ip_id = PVE::Network::SDN::Ipams::NetboxPlugin::get_ip_id($url, $ip, $headers);
+ die "can't find ip $ip in ipam" if !$ip_id;
+
+ eval {
+ PVE::Network::SDN::api_request("PATCH", "$url/ipam/ip-addresses/$ip_id/", $headers, $params);
+ };
+ if ($@) {
+ die "error updating ip $ip: $@" 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 = ['Content-Type' => "application/json", 'Authorization' => "token $token", 'Accept' => "application/json"];
+ my $headers = default_headers($plugin_config);
# check that the namespace exists AND that default IP active status
# exists AND that we have indeed API access
--
2.39.5
More information about the pve-devel
mailing list