[pve-devel] r5037 - in pve-storage/pve2: . PVE/API2 PVE/API2/Storage

svn-commits at proxmox.com svn-commits at proxmox.com
Fri Aug 20 11:24:50 CEST 2010


Author: dietmar
Date: 2010-08-20 09:24:50 +0000 (Fri, 20 Aug 2010)
New Revision: 5037

Modified:
   pve-storage/pve2/PVE/API2/Storage.pm
   pve-storage/pve2/PVE/API2/Storage/Content.pm
   pve-storage/pve2/PVE/API2/Storage/Status.pm
   pve-storage/pve2/pvesm
Log:
split out some helper classes


Modified: pve-storage/pve2/PVE/API2/Storage/Content.pm
===================================================================
--- pve-storage/pve2/PVE/API2/Storage/Content.pm	2010-08-20 08:53:28 UTC (rev 5036)
+++ pve-storage/pve2/PVE/API2/Storage/Content.pm	2010-08-20 09:24:50 UTC (rev 5037)
@@ -16,79 +16,8 @@
 
 __PACKAGE__->register_method ({
     name => 'index', 
-    path => '',  
-    method => 'GET',
-    description => "List cluster nodes.",
-    parameters => {
-    	additionalProperties => 0,
-	properties => {},
-    },
-    returns => {
-	type => 'array',
-	items => {
-	    type => "object",
-	    properties => { node => { type => 'string' } },
-	},
-	links => [ { rel => 'child', href => "{node}" } ],
-    },
-    code => sub {
-	my ($conn, $param) = @_;
-
-	# fixme: use the real list
-	my $nodes = [ 
-	    { node => 'node-0'},
-	    { node => 'node-1'},
-	    { node => 'node-2'},
-	    { node => 'node-3'},
-	    ];
-
-	return $nodes;
-
-    }});
-
-__PACKAGE__->register_method ({
-    name => 'list_ids', 
-    path => '{node}', 
-    method => 'GET',
-    description => "List storage IDs.",
-    parameters => {
-    	additionalProperties => 0,
-	properties => {
-	    node => { type => 'string', format => 'pve-node' },
-	},
-    },
-    returns => {
-	type => 'array',
-	items => {
-	    type => "object",
-	    properties => { storage => { type => 'string' } },
-	},
-	links => [ { rel => 'child', href => "{storage}" } ],
-    },
-    code => sub {
-	my ($conn, $param) = @_;
-
-	my $cfg = read_file ("storagecfg");
-
-	my $node = $param->{node};
-
-	# fixme: verify node (node exists)?
-
-	my @sids =  PVE::Storage::storage_ids ($cfg);
-
-	my $res = [];
-	foreach my $storeid (@sids) {
-	    # fixme: check if storeage exists on node ?
-	    push @$res, { storage => $storeid };
-	}
-
-	return $res;
-    }});
-
-__PACKAGE__->register_method ({
-    name => 'list', 
     protected => 1,
-    path => '{node}/{storage}',
+    path => '',
     method => 'GET',
     description => "List storage content.",
     parameters => {
@@ -159,7 +88,7 @@
 
 __PACKAGE__->register_method ({
     name => 'create', 
-    path => '{node}/{storage}',
+    path => '',
     method => 'POST',
     description => "Allocate or upload content.",
     parameters => {
@@ -277,11 +206,78 @@
 
     }});
 
+# we allow to pass volume names (without storage prefix) if the storage
+# is specified as separate parameter.
+my $real_volume_id = sub {
+    my ($storeid, $volume) = @_;
+
+    my $volid;
+
+    if ($volume =~ m/:/) {
+	eval {
+	    my ($sid, $volname) = PVE::Storage::parse_volume_id ($volume);
+	    raise_param_exc({ storage => "storage ID missmatch" }) 
+		if $storeid && $sid ne $storeid;
+	    $volid = $volume;
+	};
+	raise_param_exc({ volume => $@}) if $@; 
+	   
+    } else {
+	raise_param_exc({ volume => "no storage speficied - incomplete volume ID" }) 
+	    if !$storeid;
+	
+	$volid = "$storeid:$volume";
+    }
+
+    return $volid;
+};
+
 __PACKAGE__->register_method ({
+    name => 'info',
+    protected => 1,
+    path => '{volume}',
+    method => 'GET',
+    description => "Get volume attributes",
+    parameters => {
+    	additionalProperties => 0,
+	properties => { 
+	    node => { type => 'string', format => 'pve-node' },
+	    storage => { 
+		type => 'string', format => 'pve-storage-id',
+		optional => 1,
+	    },
+	    volume => {
+		description => "Volume identifier",
+		type => 'string' 
+	    },
+	},
+    },
+    returns => { type => 'object' },
+    code => sub {
+	my ($conn, $param) = @_;
+
+	# fixme: connect to node
+
+	my $volid = &$real_volume_id($param->{storage}, $param->{volume});
+
+	my $cfg = read_file('storagecfg');
+
+	my $path = PVE::Storage::path($cfg, $volid);
+	my ($size, $format, $used) = PVE::Storage::file_size_info ($path);
+
+	# fixme: return more attributes?
+	return {
+	    path => $path,
+	    size => $size,
+            used => $used,
+	};
+}});
+
+
+__PACKAGE__->register_method ({
     name => 'delete',
     protected => 1,
-    # fixme: volid is allowed to contain '/' - how can we fix that ?
-    path => '{node}/{storage}/{volid}',
+    path => '{volume}',
     method => 'DELETE',
     description => "Delete volume",
     parameters => {
@@ -304,30 +300,121 @@
 
 	# fixme: connect to node
 
-	my $storeid = $param->{storage};
+	my $volid = &$real_volume_id($param->{storage}, $param->{volume});
 
-	my $volid;
-
-	if ($param->{volume} =~ m/:/) {
-	    eval {
-		my ($sid, $volname) = PVE::Storage::parse_volume_id ($param->{volume});
-		raise_param_exc({ storage => "storage ID missmatch" }) 
-		    if $storeid && $sid ne $storeid;
-		$volid = $param->{volume};
-	    };
-	    raise_param_exc({ volume => $@}) if $@; 
-	   
-	} else {
-	    raise_param_exc({ volume => "no storage speficied - incomplete volume ID" }) 
-		if !$storeid;
-	    
-	    $volid = "$storeid:$param->{volume}";
-	}
-
 	my $cfg = read_file('storagecfg');
 
 	PVE::Storage::vdisk_free ($cfg, $volid);
 
 	return undef;
+}});
+
+package PVE::API2::Storage::ContentIDList;
+
+use strict;
+use warnings;
+
+use PVE::SafeSyslog;
+use PVE::INotify qw(read_file);;
+use PVE::Storage;
+
+use PVE::RESTHandler;
+
+use PVE::API2::Storage::Content;
+
+use base qw(PVE::RESTHandler);
+
+__PACKAGE__->register_method ({
+    subclass => "PVE::API2::Storage::Content", 
+    # set fragment delimiter (no subdirs) - we need that, because volume
+    # IDs may contain a slash '/' 
+    fragmentDelimiter => '', 
+    path => '{storage}',
+});
+
+__PACKAGE__->register_method ({
+    name => 'index', 
+    path => '', 
+    method => 'GET',
+    description => "List storage IDs.",
+    parameters => {
+    	additionalProperties => 0,
+	properties => {
+	    node => { type => 'string', format => 'pve-node' },
+	},
+    },
+    returns => {
+	type => 'array',
+	items => {
+	    type => "object",
+	    properties => { storage => { type => 'string' } },
+	},
+	links => [ { rel => 'child', href => "{storage}" } ],
+    },
+    code => sub {
+	my ($conn, $param) = @_;
+
+	my $cfg = read_file ("storagecfg");
+
+	my $node = $param->{node};
+
+	# fixme: verify node (node exists)?
+
+	my @sids =  PVE::Storage::storage_ids ($cfg);
+
+	my $res = [];
+	foreach my $storeid (@sids) {
+	    # fixme: check if storeage exists on node ?
+	    push @$res, { storage => $storeid };
+	}
+
+	return $res;
     }});
+
+package PVE::API2::Storage::ContentNodeList;
+
+use strict;
+use warnings;
+
+use PVE::RESTHandler;
+
+use base qw(PVE::RESTHandler);
+
+__PACKAGE__->register_method ({
+    subclass => "PVE::API2::Storage::ContentIDList",  
+    path => '{node}',
+});
+
+__PACKAGE__->register_method ({
+    name => 'index', 
+    path => '',  
+    method => 'GET',
+    description => "List cluster nodes.",
+    parameters => {
+    	additionalProperties => 0,
+	properties => {},
+    },
+    returns => {
+	type => 'array',
+	items => {
+	    type => "object",
+	    properties => { node => { type => 'string' } },
+	},
+	links => [ { rel => 'child', href => "{node}" } ],
+    },
+    code => sub {
+	my ($conn, $param) = @_;
+
+	# fixme: use the real list
+	my $nodes = [ 
+	    { node => 'node-0'},
+	    { node => 'node-1'},
+	    { node => 'node-2'},
+	    { node => 'node-3'},
+	    ];
+
+	return $nodes;
+
+    }});
+
 1;

Modified: pve-storage/pve2/PVE/API2/Storage/Status.pm
===================================================================
--- pve-storage/pve2/PVE/API2/Storage/Status.pm	2010-08-20 08:53:28 UTC (rev 5036)
+++ pve-storage/pve2/PVE/API2/Storage/Status.pm	2010-08-20 09:24:50 UTC (rev 5037)
@@ -12,42 +12,11 @@
 
 my @ctypes = qw(images vztmpl iso backup);
 
+
 __PACKAGE__->register_method ({
     name => 'index', 
-    path => '',  
-    method => 'GET',
-    description => "List cluster nodes.",
-    parameters => {
-    	additionalProperties => 0,
-	properties => {},
-    },
-    returns => {
-	type => 'array',
-	items => {
-	    type => "object",
-	    properties => { node => { type => 'string' } },
-	},
-	links => [ { rel => 'child', href => "{node}" } ],
-    },
-    code => sub {
-	my ($conn, $param) = @_;
-
-	# fixme: use the real list
-	my $nodes = [ 
-	    { node => 'node-0'},
-	    { node => 'node-1'},
-	    { node => 'node-2'},
-	    { node => 'node-3'},
-	    ];
-
-	return $nodes;
-
-    }});
-
-__PACKAGE__->register_method ({
-    name => 'list_status', 
     protected => 1,
-    path => 'status/{node}',
+    path => '',
     method => 'GET',
     description => "Get status for all datastores.",
     parameters => {
@@ -83,10 +52,9 @@
     }});
 
 __PACKAGE__->register_method ({
-    name => 'get_status', 
+    name => 'status', 
     protected => 1,
-    # /storage/status/{nodeid}/{storeid}
-    path => 'status/{node}/{storage}',
+    path => '{storage}',
     method => 'GET',
     description => "Get status for specific datastore.",
     parameters => {
@@ -116,4 +84,50 @@
 	return $info->{$storeid};
     }});
 
+package PVE::API2::Storage::StatusNodeList;
+
+use strict;
+use warnings;
+
+use PVE::RESTHandler;
+
+use base qw(PVE::RESTHandler);
+
+__PACKAGE__->register_method ({
+    subclass => "PVE::API2::Storage::Status",  
+    path => '{node}',
+});
+
+__PACKAGE__->register_method ({
+    name => 'index', 
+    path => '',  
+    method => 'GET',
+    description => "List cluster nodes.",
+    parameters => {
+    	additionalProperties => 0,
+	properties => {},
+    },
+    returns => {
+	type => 'array',
+	items => {
+	    type => "object",
+	    properties => { node => { type => 'string' } },
+	},
+	links => [ { rel => 'child', href => "{node}" } ],
+    },
+    code => sub {
+	my ($conn, $param) = @_;
+
+	# fixme: use the real list
+	my $nodes = [ 
+	    { node => 'node-0'},
+	    { node => 'node-1'},
+	    { node => 'node-2'},
+	    { node => 'node-3'},
+	    ];
+
+	return $nodes;
+
+    }});
+
 1;

Modified: pve-storage/pve2/PVE/API2/Storage.pm
===================================================================
--- pve-storage/pve2/PVE/API2/Storage.pm	2010-08-20 08:53:28 UTC (rev 5036)
+++ pve-storage/pve2/PVE/API2/Storage.pm	2010-08-20 09:24:50 UTC (rev 5037)
@@ -9,6 +9,11 @@
 use HTTP::Status qw(:constants);
 use Storable qw(dclone);
 
+use PVE::API2::Storage::Config;
+use PVE::API2::Storage::Scan;
+use PVE::API2::Storage::Content;
+use PVE::API2::Storage::Status;
+
 use Data::Dumper; # fixme: remove
 
 use PVE::RESTHandler;
@@ -30,12 +35,12 @@
 });
 
 __PACKAGE__->register_method ({
-    subclass => "PVE::API2::Storage::Content",  
+    subclass => "PVE::API2::Storage::ContentNodeList",  
     path => 'content',
 });
 
 __PACKAGE__->register_method ({
-    subclass => "PVE::API2::Storage::Status",  
+    subclass => "PVE::API2::Storage::StatusNodeList",  
     path => 'status',
 });
 

Modified: pve-storage/pve2/pvesm
===================================================================
--- pve-storage/pve2/pvesm	2010-08-20 08:53:28 UTC (rev 5036)
+++ pve-storage/pve2/pvesm	2010-08-20 09:24:50 UTC (rev 5037)
@@ -113,7 +113,7 @@
 
     $opts->{node} = read_file('hostname');
 
-    my $res = PVE::API2::Storage::Status->cli_handler('list_status', \@ARGV, $opts);
+    my $res = PVE::API2::Storage::Status->cli_handler('index', \@ARGV, $opts);
 
     my $maxlen = 0;
     foreach my $res (@$res) {
@@ -139,7 +139,7 @@
    $opts->{storage} = shift;
    $opts->{node} = read_file('hostname');
 
-   my $res = PVE::API2::Storage::Content->cli_handler('list', \@ARGV, $opts);
+   my $res = PVE::API2::Storage::Content->cli_handler('index', \@ARGV, $opts);
 
    print_content($opts->{storage}, $res);
 




More information about the pve-devel mailing list