]> git.proxmox.com Git - pmg-api.git/commitdiff
add new parameters cidr(6) to the network api
authorDominik Csapak <d.csapak@proxmox.com>
Fri, 3 May 2019 07:28:53 +0000 (09:28 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 8 May 2019 15:38:55 +0000 (17:38 +0200)
to also allow to set it directly and not only via address/netmask

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
PMG/API2/Network.pm

index 66365535b39c43c34fb8d14e5a8647cf7fe0eb1f..b6a58da73a4752c2cba2398028a14e366343de39 100644 (file)
@@ -124,6 +124,11 @@ my $confdesc = {
        optional => 1,
        requires => 'netmask',
     },
+    cidr => {
+       description => 'IPv4 CIDR.',
+       type => 'string', format => 'CIDRv4',
+       optional => 1,
+    },
     gateway6 => {
        description => 'Default ipv6 gateway address.',
        type => 'string', format => 'ipv6',
@@ -140,7 +145,12 @@ my $confdesc = {
        type => 'string', format => 'ipv6',
        optional => 1,
        requires => 'netmask6',
-    }
+    },
+    cidr6 => {
+       description => 'IPv6 CIDR.',
+       type => 'string', format => 'CIDRv6',
+       optional => 1,
+    },
 };
 
 sub json_config_properties {
@@ -272,6 +282,34 @@ my $check_ipv6_settings = sub {
           (defined($type) && $type !~ /^(?:(?:GLOBAL|(?:UNIQUE|LINK)-LOCAL)-UNICAST)$/);
 };
 
+my $map_cidr_to_address_netmask = sub {
+    my ($param) = @_;
+
+    if ($param->{cidr}) {
+       raise_param_exc({ address => "address conflicts with cidr" })
+           if $param->{address};
+       raise_param_exc({ netmask => "netmask conflicts with cidr" })
+           if $param->{netmask};
+
+       my ($address, $netmask) = $param->{cidr} =~ m!^(.*)/(\d+)$!;
+       $param->{address} = $address;
+       $param->{netmask} = $netmask;
+       delete $param->{cidr};
+    }
+
+    if ($param->{cidr6}) {
+       raise_param_exc({ address6 => "address6 conflicts with cidr6" })
+           if $param->{address6};
+       raise_param_exc({ netmask6 => "netmask6 conflicts with cidr6" })
+           if $param->{netmask6};
+
+       my ($address, $netmask) = $param->{cidr6} =~ m!^(.*)/(\d+)$!;
+       $param->{address6} = $address;
+       $param->{netmask6} = $netmask;
+       delete $param->{cidr6};
+    }
+};
+
 __PACKAGE__->register_method({
     name => 'create_network',
     path => '',
@@ -304,6 +342,8 @@ __PACKAGE__->register_method({
            &$check_duplicate_gateway6($ifaces, $iface)
                if $param->{gateway6};
 
+           $map_cidr_to_address_netmask->($param);
+
            &$check_ipv6_settings($param->{address6}, int($param->{netmask6}))
                if $param->{address6};
 
@@ -386,6 +426,8 @@ __PACKAGE__->register_method({
                @$families = grep(!/^inet6$/, @$families) if $k eq 'address6';
            }
 
+           $map_cidr_to_address_netmask->($param);
+
            &$check_duplicate_gateway($ifaces, $iface)
                if $param->{gateway};
            &$check_duplicate_gateway6($ifaces, $iface)