[pve-devel] r4908 - in pve-manager/pve2: lib/PVE lib/PVE/API2 www/manager www/manager/form

svn-commits at proxmox.com svn-commits at proxmox.com
Fri Jul 16 17:47:53 CEST 2010


Author: dietmar
Date: 2010-07-16 15:47:53 +0000 (Fri, 16 Jul 2010)
New Revision: 4908

Modified:
   pve-manager/pve2/lib/PVE/API2/Storage.pm
   pve-manager/pve2/lib/PVE/APIDaemon.pm
   pve-manager/pve2/lib/PVE/JSONSchema.pm
   pve-manager/pve2/lib/PVE/REST.pm
   pve-manager/pve2/lib/PVE/RESTHandler.pm
   pve-manager/pve2/www/manager/PVEUtils.js
   pve-manager/pve2/www/manager/StorageBrowser.js
   pve-manager/pve2/www/manager/form/ModifyDirStorage.js
   pve-manager/pve2/www/manager/form/StdForm.js
Log:
make the storage form functional


Modified: pve-manager/pve2/lib/PVE/API2/Storage.pm
===================================================================
--- pve-manager/pve2/lib/PVE/API2/Storage.pm	2010-07-16 12:01:11 UTC (rev 4907)
+++ pve-manager/pve2/lib/PVE/API2/Storage.pm	2010-07-16 15:47:53 UTC (rev 4908)
@@ -95,10 +95,66 @@
     $scfg->{digest} = $cfg->{digest};
     $scfg->{time} = time(); # fixme: remove
 
+   
+    my @cta;
+    foreach my $ct (keys %{$scfg->{content}}) {
+	push @cta, $ct if $scfg->{content}->{$ct};
+    } 
+
+    $scfg->{content} =  join(',', @cta);
+ 
     return $scfg;
 }
 
 __PACKAGE__->register_method ({
+    name => 'update_config',
+    protected => 1,
+    match_re => [ 'config', '\S+' ], 
+    method => 'PUT',
+    description => "Update storage configuration.",
+    parameters => {
+    	additionalProperties => 0,
+	properties => { 
+	    path => {
+		type => 'string',
+		optional => 1,
+	    },
+	    content => {
+		type => 'string',
+		optional => 1,
+	    },
+	    disable => {
+		type => 'boolean',
+		optional => 1,
+	    },
+	    shared => {
+		type => 'boolean',
+		optional => 1,
+	    },
+	    digest => {
+		type => 'string',
+		optional => 1,
+	    }
+	},
+    },
+    returns => { type => 'null' },
+});
+sub update_config {
+    my ($conn, $resp, $param) = @_;
+
+    syslog("info", Dumper($param));
+
+    my ($storeid) = $conn->{rel_uri} =~ m!/([^/]+)$!;
+ 
+    my $digest = $param->{digest};
+    delete($param->{digest});
+
+    PVE::Storage::storage_set($storeid, $param, $digest);
+
+    return undef;
+}
+
+__PACKAGE__->register_method ({
     name => 'list_storage_config', 
     match_re => [ 'config' ], # /storage/config
     method => 'GET',

Modified: pve-manager/pve2/lib/PVE/APIDaemon.pm
===================================================================
--- pve-manager/pve2/lib/PVE/APIDaemon.pm	2010-07-16 12:01:11 UTC (rev 4907)
+++ pve-manager/pve2/lib/PVE/APIDaemon.pm	2010-07-16 15:47:53 UTC (rev 4908)
@@ -261,7 +261,7 @@
 			    PVE::REST::prepare_response_data($format, $res);
 			    my ($raw, $ct) = PVE::REST::format_response_data($format, $res, $uri);
 
-			    my $response = HTTP::Response->new($res->{status});
+			    my $response = HTTP::Response->new($res->{status}, $res->{message});
 			    $response->header("Content-Type" => $ct);
 			    $response->header("Pragma", "no-cache");
 

Modified: pve-manager/pve2/lib/PVE/JSONSchema.pm
===================================================================
--- pve-manager/pve2/lib/PVE/JSONSchema.pm	2010-07-16 12:01:11 UTC (rev 4907)
+++ pve-manager/pve2/lib/PVE/JSONSchema.pm	2010-07-16 15:47:53 UTC (rev 4908)
@@ -103,11 +103,14 @@
 		if ($type eq 'string') {
 		    return 1; # nothing to check ?
 		} elsif ($type eq 'boolean') {
-		    if ($value !~ m/^[01]$/) {
+		    if ($value =~ m/^(1|yes|on)$/i) {
+			return 1;
+		    } elsif ($value =~ m/^(0|no|off)$/i) {
+			return 0;
+		    } else {
 			add_error($errors, $path, "type check ('$type') failed - got '$value'");
 			return undef;
 		    }
-		    return 1;
 		} elsif ($type eq 'integer') {
 		    if (!is_integer($value)) {
 			add_error($errors, $path, "type check ('$type') failed - got '$value'");

Modified: pve-manager/pve2/lib/PVE/REST.pm
===================================================================
--- pve-manager/pve2/lib/PVE/REST.pm	2010-07-16 12:01:11 UTC (rev 4907)
+++ pve-manager/pve2/lib/PVE/REST.pm	2010-07-16 15:47:53 UTC (rev 4908)
@@ -140,6 +140,8 @@
 	# HACK: extjs wants 'success' property instead of useful HTTP status codes
 	if (is_error($res->{status})) {
 	    $success = 0;
+	    $new->{message} = $res->{message} || status_message($res->{status});
+	    $res->{message} = undef;
 	    $res->{status} = HTTP_OK;
 	}
 	$new->{success} = $success;
@@ -148,6 +150,35 @@
     $res->{data} = $new;
 }
 
+sub create_http_request {
+    my ($uri, $method, $params) = @_;
+
+    # NOTE: HTTP::Request::Common::PUT is crap - so we use our own code
+    # borrowed from HTTP::Request::Common::POST
+
+    if  ($method eq 'POST' || $method eq 'PUT') {
+
+	my $req = HTTP::Request->new($method => $uri);
+	$req->header('Content-Type' => 'application/x-www-form-urlencoded'); 
+
+	# We use a temporary URI object to format
+	# the application/x-www-form-urlencoded content.
+	my $url = URI->new('http:');
+	$url->query_form(%$params);
+	my $content = $url->query;
+	if (defined($content)) {
+	    $req->header('Content-Length' => length($content));
+	    $req->content($content);
+	} else {
+	    $req->header('Content-Length' => 0);
+	}
+
+	return $req;
+    }
+
+    die "unknown method '$method'"; 
+}
+
 sub proxy_handler {
     my($r, $host, $method, $abs_uri, $ticket, $params) = @_;
 
@@ -180,10 +211,8 @@
     if ($method eq 'GET') {
 	$uri->query_form($params);
 	$response = $ua->request(HTTP::Request::Common::GET($uri));		    
-    } elsif ($method eq 'POST') {
-	$response = $ua->request(HTTP::Request::Common::POST($uri, Content => $params));
-    } elsif ($method eq 'PUT') {
-	$response = $ua->request(HTTP::Request::Common::PUT($uri, Content => $params));
+    } elsif ($method eq 'POST' || $method eq 'PUT') {
+	$response = $ua->request(create_http_request($uri, $method, $params));
     } elsif ($method eq 'DELETE') {
 	$response = $ua->request(HTTP::Request::Common::DELETE($uri));
     } else {
@@ -192,6 +221,7 @@
 	return $code;
     }
 
+
     if (my $cookie = $response->header("Set-Cookie")) {
 	$r->err_headers_out()->add("Set-Cookie" => $cookie);
     }
@@ -260,7 +290,7 @@
     if (!$handler || !$info) {
 	return {
 	    status => HTTP_NOT_IMPLEMENTED,
-	    message => "Method '$abs_uri' not implemented",
+	    message => "Method '$method $abs_uri' not implemented",
 	};
     }
 
@@ -329,7 +359,8 @@
      $r->status($res->{status} || HTTP_OK);
  
      if ($res->{message}) {
-	 $r->status_line("$res->{status} $res->{message}");
+	 my ($firstline) = $res->{message} =~ m/\A(.*)$/m;
+	 $r->status_line("$res->{status} $firstline");
      }
 
      my ($raw, $ct) = format_response_data($format, $res, $abs_uri);

Modified: pve-manager/pve2/lib/PVE/RESTHandler.pm
===================================================================
--- pve-manager/pve2/lib/PVE/RESTHandler.pm	2010-07-16 12:01:11 UTC (rev 4907)
+++ pve-manager/pve2/lib/PVE/RESTHandler.pm	2010-07-16 15:47:53 UTC (rev 4908)
@@ -99,7 +99,7 @@
     my $func = $info->{name} ? $self->can($info->{name}) : undef;
 
     if (!($info->{name} && $func)) {
-	$resp->{message} = "SERVER ERROR: method lookup failed ('$info->{name}')";
+	$resp->{message} = "Method lookup failed ('$info->{name}')";
 	$resp->{status} = HTTP_INTERNAL_SERVER_ERROR;
 	return $resp->{status};
     }
@@ -109,6 +109,7 @@
 	my $res = PVE::JSONSchema::validate($conn->{params}, $schema);
 	if (!$res->{valid}) {
 	    $resp->{status} = HTTP_BAD_REQUEST;
+	    $resp->{message} = "Parameter verification failed";
 	    $resp->{errors} = $res->{errors},
 	    return $resp->{status};
 	}
@@ -134,7 +135,7 @@
 	my $res = PVE::JSONSchema::validate($resp->{data}, $schema);
 	if (!$res->{valid}) {
 
-	    $resp->{message} = "SERVER ERROR: result verification vailed";
+	    $resp->{message} = "Result verification vailed";
 	    $resp->{status} = HTTP_INTERNAL_SERVER_ERROR;
 	    $resp->{errors} = $res->{errors};
 

Modified: pve-manager/pve2/www/manager/PVEUtils.js
===================================================================
--- pve-manager/pve2/www/manager/PVEUtils.js	2010-07-16 12:01:11 UTC (rev 4907)
+++ pve-manager/pve2/www/manager/PVEUtils.js	2010-07-16 15:47:53 UTC (rev 4908)
@@ -123,14 +123,16 @@
 	format_content_types: function(value) {
 	    var cta = [];
 
-	    if (value.images)
-		cta.push('Images');
-	    if (value.backup)
-		cta.push('Backups');
-	    if (value.vztmpl)
-		cta.push('Templates');
-	    if (value.iso)
-		cta.push('ISO');
+	    Ext.each(value.split(','), function(ct) {
+		if (ct === 'images')
+		    cta.push('Images');
+		if (ct === 'backup')
+		    cta.push('Backups');
+		if (ct === 'vztmpl')
+		    cta.push('Templates');
+		if (ct === 'iso')
+		    cta.push('ISO');
+	    });
 
 	    return cta.join(', ');
 	},

Modified: pve-manager/pve2/www/manager/StorageBrowser.js
===================================================================
--- pve-manager/pve2/www/manager/StorageBrowser.js	2010-07-16 12:01:11 UTC (rev 4907)
+++ pve-manager/pve2/www/manager/StorageBrowser.js	2010-07-16 15:47:53 UTC (rev 4908)
@@ -237,12 +237,14 @@
 	    if (!rec)
 		return;
 
-	    var ct = rec.data.value || {};
+	    var ct = rec.data.value || '';
+	    var cthash = {};
+	    Ext.each(ct.split(','), function(item) { cthash[item] = 1 });
 
-	    cond_view_comp('images', ct.images);
-	    cond_view_comp('iso', ct.iso);
-	    cond_view_comp('vztmpl', ct.vztmpl);
-	    cond_view_comp('backup', ct.backup);
+	    cond_view_comp('images', cthash.images);
+	    cond_view_comp('iso', cthash.iso);
+	    cond_view_comp('vztmpl', cthash.vztmpl);
+	    cond_view_comp('backup', cthash.backup);
 	};
 	
 	if (!store) {
@@ -254,7 +256,7 @@
 		    type: { header: 'Storage Type', renderer: PVE.Utils.format_storage_type },
 		    path: { header: 'Path' },
 		    shared: { header: 'Shared', renderer: PVE.Utils.format_boolean },
-		    disabled: { header: 'Enabled',  renderer: PVE.Utils.format_neg_boolean },
+		    disable: { header: 'Disabled', renderer: PVE.Utils.format_boolean },
 		    time: { header: 'TIME' },
 		    content: { header: 'Content', renderer: PVE.Utils.format_content_types }
 		}

Modified: pve-manager/pve2/www/manager/form/ModifyDirStorage.js
===================================================================
--- pve-manager/pve2/www/manager/form/ModifyDirStorage.js	2010-07-16 12:01:11 UTC (rev 4907)
+++ pve-manager/pve2/www/manager/form/ModifyDirStorage.js	2010-07-16 15:47:53 UTC (rev 4908)
@@ -9,26 +9,38 @@
 
 	var items_col1 = [
 	    {
+		xtype: 'hidden',
+		name: 'digest',
+	    },
+	    {
 		xtype: 'textfield',
 		fieldLabel: 'Storage name',
-		name: 'storage'
-		//disabled: true,
-		//allowBlank: false
+		name: 'storage',
+		value: storeid,
+		disabled: true
 	    },
 	    {
 		xtype: 'textfield',
 		fieldLabel: 'Directory',
 		name: 'path',
-		allowBlank: false
+		disabled: true
 	    }
 	];
 
+	var cts = [['images', 'Images']];
+
+	if (storeid === 'local') {
+	    cts.push(['vztmpl', 'OpenVZ Templates']);
+	    cts.push(['iso', 'ISO']);
+	} else {
+	    cts.push(['backup', 'Backup']);
+	} 
+
 	var items_col2 = [
 	    {
 		xtype: 'checkbox',
 		fieldLabel: 'Disabled',
-		name: 'disabled',
-		value: 'test1'
+		name: 'disable'
 	    },
 	    {
 		xtype: 'checkbox',
@@ -41,19 +53,21 @@
 		editable: false,
 		triggerAction: 'all',
 		fieldLabel: 'Content',
-		name: 'content',
+		name: storeid === 'local' ? 'content' : 'hiddencontent',
+		mode: 'local',
 		style : 'margin-bottom:10px',// avoid scrolbars with Firefox
 		width: 150,
 		height: 'auto',
-		store: [['images', 'Images'], ['backup', 'Backup'], 
-			['vztmpl', 'OpenVZ Templates'], ['iso', 'ISO']]
+		store: cts,
+		hiddenName: 'content'
 	    }
 	];
 
 	// NOTE: If subclassing FormPanel, any configuration options for 
 	// the BasicForm must be applied to initialConfig
 	Ext.apply(self, Ext.apply(self.initialConfig, {
-	    url: "/api2/extjs/test",
+	    url: "/api2/extjs/storage/config/" + storeid,
+	    method: 'PUT',
 
 	    items: {
 		layout:'column',
@@ -78,5 +92,13 @@
 	}));
 
 	PVE.form.ModifyDirStorage.superclass.initComponent.call(self);
+
+	var form = self.getForm();
+
+	form.load({
+	    url: "/api2/extjs/storage/config/" + storeid,
+	    method: 'GET'
+	});
+
     }
 });

Modified: pve-manager/pve2/www/manager/form/StdForm.js
===================================================================
--- pve-manager/pve2/www/manager/form/StdForm.js	2010-07-16 12:01:11 UTC (rev 4907)
+++ pve-manager/pve2/www/manager/form/StdForm.js	2010-07-16 15:47:53 UTC (rev 4908)
@@ -15,16 +15,34 @@
 
 		var form = self.getForm();
 
+		// NOTE: we add parameter for unset checkbox (because html 
+		// does not sent them by default)
+		var params = {};
+		form.items.each(function(f) {
+		    n = f.getName();
+                    val = f.getValue();
+		    xt = f.getXType();
+
+		    if (xt === 'checkbox' && !val) {
+			params[n] = 0;
+		    }
+		});
+ 
 		if(form.isValid()){
 		    self.el.mask('Please wait...', 'x-mask-loading');
 
 		    form.submit({
+			params: params,
 			failure: function(f, resp){
 			    self.el.unmask();
 			    if (Ext.isFunction(options.failure)) {
 				options.failure();
 			    } else {
-				Ext.MessageBox.alert('Failure', "Please try again");
+				var msg = "Please try again";
+				if (resp.result && resp.result.message) {
+				    msg = resp.result.message;
+				} 
+				Ext.MessageBox.alert('Failure', msg);
 			    }
 			},
 			success: function(f, resp){




More information about the pve-devel mailing list