[pve-devel] r5113 - pve-common/trunk

svn-commits at proxmox.com svn-commits at proxmox.com
Fri Sep 10 14:54:03 CEST 2010


Author: dietmar
Date: 2010-09-10 12:54:03 +0000 (Fri, 10 Sep 2010)
New Revision: 5113

Modified:
   pve-common/trunk/ChangeLog
   pve-common/trunk/JSONSchema.pm
Log:
	* JSONSchema.pm (register_standard_option, get_standard_option): a
	way to register/get commom schemas by name.



Modified: pve-common/trunk/ChangeLog
===================================================================
--- pve-common/trunk/ChangeLog	2010-09-10 12:09:56 UTC (rev 5112)
+++ pve-common/trunk/ChangeLog	2010-09-10 12:54:03 UTC (rev 5113)
@@ -1,5 +1,8 @@
 2010-09-10  Proxmox Support Team  <support at proxmox.com>
 
+	* JSONSchema.pm (register_standard_option, get_standard_option): a
+	way to register/get commom schemas by name.
+
 	* Tools.pm (extract_param): new helper
 
 	* CLIHandler.pm: new verbose option for help.

Modified: pve-common/trunk/JSONSchema.pm
===================================================================
--- pve-common/trunk/JSONSchema.pm	2010-09-10 12:09:56 UTC (rev 5112)
+++ pve-common/trunk/JSONSchema.pm	2010-09-10 12:54:03 UTC (rev 5113)
@@ -10,31 +10,94 @@
 use HTTP::Status qw(:constants);
 use Data::Dumper; # fixme: remove
 
+use base 'Exporter';
+
+our @EXPORT_OK = qw(
+register_standard_option 
+get_standard_option
+);
+
 # Note: This class implements something similar to JSON schema, but it is not 100% complete. 
 # see: http://tools.ietf.org/html/draft-zyp-json-schema-02
 # see: http://json-schema.org/
 
 # the code is similar to the javascript parser from http://code.google.com/p/jsonschema/
 
+my $standard_options = {};
+sub register_standard_option {
+    my ($name, $schema) = @_;
+
+    die "standard option '$name' already registered\n" 
+	if $standard_options->{$name};
+
+    $standard_options->{$name} = $schema;
+}
+
+sub get_standard_option {
+    my ($name) = @_;
+
+    my $res =  $standard_options->{$name};
+    die "no such standard option\n" if !$res;
+    return $res;
+};
+
+register_standard_option('vmid', {
+    description => "The (unique) ID of the VM.",
+    type => 'integer', format => 'pve-vmid',
+    minimum => 1
+});
+
+register_standard_option('node', {
+    description => "The cluster node name.",
+    type => 'string', format => 'pve-node',
+});
+
 my $format_list = {};
 
 sub register_format {
     my ($format, $code) = @_;
 
-    die "JSON schema format '$format' already registered\n" if $format_list->{$format};
+    die "JSON schema format '$format' already registered\n" 
+	if $format_list->{$format};
 
     $format_list->{$format} = $code;
 }
 
 # register some common type for pve
-register_format('pve-configid', sub {
-    my $id = shift;
-
-    die "invalid cofiguration ID '$id'\n" if $id !~ m/^[a-z][a-z0-9_]+$/i;
-    
+register_format('pve-configid', \&pve_verify_configid);
+sub pve_verify_configid {
+    my ($id, $noerr) = @_;
+ 
+    if ($id !~ m/^[a-z][a-z0-9_]+$/i) {
+	return undef if $noerr;
+	die "invalid cofiguration ID '$id'\n"; 
+    }
     return $id;
-});
+}
 
+register_format('pve-vmid', \&pve_verify_vmid);
+sub pve_verify_vmid {
+    my ($vmid, $noerr) = @_;
+
+    if ($vmid !~ m/^[1-9][0-9]+$/) {
+	return undef if $noerr;
+	die "value does not look like a valid VM ID\n";
+    }
+    return $vmid;
+}
+
+register_format('pve-node', \&pve_verify_node_name);
+sub pve_verify_node_name {
+    my ($node, $noerr) = @_;
+
+    # fixme: use better regex ?
+    if ($node !~ m/^[[:alnum:]\-]+$/) {
+	return undef if $noerr;
+	die "value does not look like a valid node name\n";
+    }
+    return $node;
+}
+
 sub check_format {
     my ($format, $value) = @_;
 



More information about the pve-devel mailing list