[pve-devel] [PATCH common] Add mac-addr standard option for MAC address verification

Thomas Lamprecht t.lamprecht at proxmox.com
Tue Jan 22 10:01:20 CET 2019


On 1/21/19 12:35 PM, Christian Ebner wrote:
> Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
> ---
>  src/PVE/JSONSchema.pm | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
> 
> diff --git a/src/PVE/JSONSchema.pm b/src/PVE/JSONSchema.pm
> index fb58ad3..0408c5d 100644
> --- a/src/PVE/JSONSchema.pm
> +++ b/src/PVE/JSONSchema.pm
> @@ -113,6 +113,11 @@ register_standard_option('pve-output-format', {
>      default => 'text',
>  });
>  
> +register_standard_option('mac-addr', {
> +    description => "Mac address.",
> +    type => 'string', format => 'mac-addr',
> +});
> +
>  my $format_list = {};
>  

discussed this with Wolfgang B. a bit and we both really see no sense in the
standard option, the format is fine and wanted though.

Besides the reasons I mentioned in another thread[0] get_standard_option also
hides stuff a bit to much, so it should be only used where the option identifier
is very clear and enough, e.g., nodename, VMID, and there like.
Just adding standard options globally to save lines can make things even worse,
IMO, as longs it's not logic/imperative programming but a schematic definition
I often rather have the full definition and hand locally, with no need to jump
into rabbit holes. Naturally, there are exemptions, and module local defined
standard options can be just fine. But there's also a reason that in > 7 years
of modern PVE API2 + JSONSchema use there are only a handful of those global, for
all JSONSchema module users, registered standard options. Just to give you, and
other readers some rationale.

[0]: https://pve.proxmox.com/pipermail/pve-devel/2019-January/035358.html

>  sub register_format {
> @@ -204,6 +209,17 @@ sub pve_verify_node_name {
>      return $node;
>  }
>  
> +register_format('mac-addr', \&pve_verify_mac_addr);
> +sub pve_verify_mac_addr {
> +    my ($mac_addr, $noerr) = @_;
> +
> +    if ($mac_addr !~ m/[0-9a-f]{2}(?::[0-9a-f]{2}){5}/i) {
> +	return undef if $noerr;
> +	die "value does not look like a valid MAC address\n";
> +    }
> +    return $mac_addr;
> +}
> +
>  register_format('ipv4', \&pve_verify_ipv4);
>  sub pve_verify_ipv4 {
>      my ($ipv4, $noerr) = @_;
> 





More information about the pve-devel mailing list