[pve-devel] r5005 - in pve-manager/pve2: . bin lib/PVE lib/PVE/API2

svn-commits at proxmox.com svn-commits at proxmox.com
Mon Aug 16 12:13:16 CEST 2010


Author: dietmar
Date: 2010-08-16 10:13:16 +0000 (Mon, 16 Aug 2010)
New Revision: 5005

Modified:
   pve-manager/pve2/ChangeLog
   pve-manager/pve2/bin/pvesh
   pve-manager/pve2/lib/PVE/API2/AccessControl.pm
   pve-manager/pve2/lib/PVE/API2/Cluster.pm
   pve-manager/pve2/lib/PVE/API2/Storage.pm
   pve-manager/pve2/lib/PVE/API2/VM.pm
   pve-manager/pve2/lib/PVE/REST.pm
Log:
	* lib/PVE/API2/*.pm: .pm: remove $resp parameter everywhere.

	* bin/pvesh (call_handler): adjust code for new PVE::Exception

	* lib/PVE/REST.pm (rest_handler): adjust code for new PVE::Exception



Modified: pve-manager/pve2/ChangeLog
===================================================================
--- pve-manager/pve2/ChangeLog	2010-08-16 10:02:33 UTC (rev 5004)
+++ pve-manager/pve2/ChangeLog	2010-08-16 10:13:16 UTC (rev 5005)
@@ -1,3 +1,11 @@
+2010-08-16  Proxmox Support Team  <support at proxmox.com>
+
+	* lib/PVE/API2/*.pm: .pm: remove $resp parameter everywhere.
+
+	* bin/pvesh (call_handler): adjust code for new PVE::Exception
+
+	* lib/PVE/REST.pm (rest_handler): adjust code for new PVE::Exception
+
 2010-08-12  Proxmox Support Team  <support at proxmox.com>
 
 	* lib/PVE/API2/User.pm: moved to pve-access-control

Modified: pve-manager/pve2/bin/pvesh
===================================================================
--- pve-manager/pve2/bin/pvesh	2010-08-16 10:02:33 UTC (rev 5004)
+++ pve-manager/pve2/bin/pvesh	2010-08-16 10:13:16 UTC (rev 5005)
@@ -52,33 +52,8 @@
     return $ret;
 }
 
-sub print_result {
-    my ($info, $res, $nodata) = @_;
-
-    my $status = $res->{status};
-    if (!is_success($status)) {
-	if ($res->{errors}) {
-	    foreach my $e (keys %{$res->{errors}}) {
-		warn "$e: $res->{errors}->{$e}\n";
-	    }
-	}
-    }
-
-    my $msg = $res->{message} || status_message($status);
-
-    chomp($msg);
-
-    warn "$status $msg\n";
-
-    return if $nodata;
-    return if $info && $info->{returns} && 
-	$info->{returns}->{type} && $info->{returns}->{type} eq 'null';
-
-    print to_json($res->{data}, {allow_nonref => 1, canonical => 1, pretty => 1 });
-}
-
 sub call_handler {
-    my ($handler, $info, $dir, $params) = @_;
+    my ($handler, $info, $dir, $params, $nooutput) = @_;
 
     my $conn = {
 	abs_uri => "$basedir/$dir",
@@ -87,10 +62,18 @@
 	params => $params,
     };
 
-    my $resp = {}; 
-    my $status = $handler->handle($info, $conn, $resp);
+    my $data = $handler->handle($info, $conn);
 
-    return $resp;
+    return $data if $nooutput;
+
+    warn "200 OK\n"; # always print OK status if successful
+
+    return if $info && $info->{returns} && 
+	$info->{returns}->{type} && $info->{returns}->{type} eq 'null';
+
+    print to_json($data, {allow_nonref => 1, canonical => 1, pretty => 1 });
+
+    return $data;
 }
 
 my $read_password = sub {
@@ -118,9 +101,7 @@
 
     # print "CREATE $dir " . Dumper($opts) . "\n";
 
-    my $res = call_handler($handler, $info, $dir, $opts);
-
-    print_result($info, $res);
+    call_handler($handler, $info, $dir, $opts);
 }
 
 sub get_entry {
@@ -137,9 +118,7 @@
 
     # print "GET $dir " . Dumper($opts) . "\n";
 
-    my $res = call_handler($handler, $info, $dir, $opts);
-
-    print_result($info, $res);
+    call_handler($handler, $info, $dir, $opts);
 }
 
 sub update_entry {
@@ -156,9 +135,7 @@
 
     # print "PUT $dir " . Dumper($opts) . "\n";
 
-    my $res = call_handler($handler, $info, $dir, $opts);
-
-    print_result($info, $res);
+    call_handler($handler, $info, $dir, $opts);
 }
 
 sub delete_entry {
@@ -173,9 +150,7 @@
     
     # print "DELETE $dir\n";
 
-    my $res = call_handler($handler, $info, $dir, $uri_param);
-    
-    print_result($info, $res);
+    call_handler($handler, $info, $dir, $uri_param);
 }
 
 sub test_dir {
@@ -203,14 +178,8 @@
 
     my $params = PVE::JSONSchema::get_options($info->{parameters}, $args, $uri_param, $read_password);
 
-    my $res = call_handler($handler, $info, $dir, $params);
-    if (!is_success($res->{status})) {
-	print_result($info, $res, 1);
-	return;
-    }
-
-    my $data = $res->{data};
-
+    my $data = call_handler($handler, $info, $dir, $params, 1);
+ 
     my $lnk = PVE::JSONSchema::method_get_child_link($info);
     if ($lnk && $data) {
 	my $href = $lnk->{href};

Modified: pve-manager/pve2/lib/PVE/API2/AccessControl.pm
===================================================================
--- pve-manager/pve2/lib/PVE/API2/AccessControl.pm	2010-08-16 10:02:33 UTC (rev 5004)
+++ pve-manager/pve2/lib/PVE/API2/AccessControl.pm	2010-08-16 10:13:16 UTC (rev 5005)
@@ -54,7 +54,7 @@
 	links => [ { rel => 'child', href => "{subdir}" } ],
     },
     code => sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
     
 	my $res = [];
 

Modified: pve-manager/pve2/lib/PVE/API2/Cluster.pm
===================================================================
--- pve-manager/pve2/lib/PVE/API2/Cluster.pm	2010-08-16 10:02:33 UTC (rev 5004)
+++ pve-manager/pve2/lib/PVE/API2/Cluster.pm	2010-08-16 10:13:16 UTC (rev 5005)
@@ -29,7 +29,7 @@
 	links => [ { rel => 'child', href => "{name}" } ],
     },
     code => sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
     
 	# fixme: this is just some test code
 

Modified: pve-manager/pve2/lib/PVE/API2/Storage.pm
===================================================================
--- pve-manager/pve2/lib/PVE/API2/Storage.pm	2010-08-16 10:02:33 UTC (rev 5004)
+++ pve-manager/pve2/lib/PVE/API2/Storage.pm	2010-08-16 10:13:16 UTC (rev 5005)
@@ -57,7 +57,7 @@
 	links => [ { rel => 'child', href => "{subdir}" } ],
     },
     code => sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
 
 	my $res = [
 	    { subdir => 'config' },
@@ -82,7 +82,7 @@
     },
     returns => {},
     code => sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
 
 	my $cfg = PVE::Config::read_file ("storagecfg");
 
@@ -137,7 +137,7 @@
     },
     returns => { type => 'null' },
     code => sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
 
 	my $storeid = $param->{storage};
 	delete($param->{storage});
@@ -168,7 +168,7 @@
 	links => [ { rel => 'child', href => "{storage}" } ],
     },
     code => sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
 
 	my $cfg = PVE::Config::read_file ("storagecfg");
 
@@ -202,7 +202,7 @@
 	links => [ { rel => 'child', href => "{method}" } ],
     },
     code => sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
 
 	my $res = [ 
 	    { method => 'lvm' },
@@ -229,7 +229,7 @@
     },
     returns => {},
     code => sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
 
 	return [];
     }});
@@ -253,7 +253,7 @@
 #	links => [ { rel => 'child', href => "{storage}" } ],
     },
     code => sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
 
 	my $nodes = [ 'node-0', 'node-1', 'node-2', 'node-3' ]; # fixme: use the real list
 
@@ -306,7 +306,7 @@
 	links => [ { rel => 'child', href => "{node}" } ],
     },
     code => sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
 
 	# fixme: use the real list
 	my $nodes = [ 
@@ -342,7 +342,7 @@
 	links => [ { rel => 'child', href => "{storage}" } ],
     },
     code => sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
 
 	my $cfg = PVE::Config::read_file ("storagecfg");
 
@@ -397,7 +397,7 @@
 	links => [ { rel => 'child', href => "{volname}" } ],
     },
     code => sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
 
 	my $cts = $param->{content} ? [ $param->{content} ] : [ @ctypes ];
 
@@ -455,7 +455,7 @@
     },
     returns => { type => 'null' },
     code => sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
 
 	my $cts = $param->{content} ? [ $param->{content} ] : [ @ctypes ];
 
@@ -514,7 +514,7 @@
 	links => [ { rel => 'child', href => "{storage}" } ],
     },
     code => sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
 
 	my $cts = $param->{content} ? [ $param->{content} ] : [ @ctypes ];
 	
@@ -551,7 +551,7 @@
     },
     returns => {},
     code =>  sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
 
 	my $cts = $param->{content} ? [ $param->{content} ] : [ @ctypes ];
 

Modified: pve-manager/pve2/lib/PVE/API2/VM.pm
===================================================================
--- pve-manager/pve2/lib/PVE/API2/VM.pm	2010-08-16 10:02:33 UTC (rev 5004)
+++ pve-manager/pve2/lib/PVE/API2/VM.pm	2010-08-16 10:13:16 UTC (rev 5005)
@@ -29,7 +29,7 @@
 	links => [ { rel => 'child', href => "{id}" } ],
     },
     code => sub {
-	my ($conn, $resp, $param) = @_;
+	my ($conn, $param) = @_;
 
 	my $ctime = int(time()/3);
 	$ctime = 0;

Modified: pve-manager/pve2/lib/PVE/REST.pm
===================================================================
--- pve-manager/pve2/lib/PVE/REST.pm	2010-08-16 10:02:33 UTC (rev 5004)
+++ pve-manager/pve2/lib/PVE/REST.pm	2010-08-16 10:13:16 UTC (rev 5005)
@@ -312,10 +312,26 @@
     # methods to other hosts?
     return { proxy => 'localhost' } if $info->{protected} && ($euid != 0);
 
-    my $resp = {};
-    $resp->{status} = $handler->handle($info, $conn, $resp);
-    $resp->{info} = $info; # useful to format output
+    my $resp = { 
+	info => $info, # useful to format output
+	status => HTTP_OK,
+    }; 
 
+    eval {
+	$resp->{data} = $handler->handle($info, $conn);
+    };
+    my $err = $@;
+    if ($err) {
+	if (ref($err) eq "PVE::Exception") {
+	    $resp->{status} = $err->{code} || HTTP_INTERNAL_SERVER_ERROR;
+	    $resp->{message} = $err->{msg} || $@;
+	    $resp->{errors} = $err->{errors} if $err->{errors};
+	} else {
+	    $resp->{status} = HTTP_INTERNAL_SERVER_ERROR;
+	    $resp->{message} = $@;
+	}
+    }
+
     # fixme: update ticket if too old
     # $resp->{ticket} = update_ticket($ticket);
 



More information about the pve-devel mailing list