[pve-devel] [PATCH] pve-storage autocompletion

Alen Grizonic a.grizonic at proxmox.com
Wed Sep 30 13:04:53 CEST 2015


---
 PVE/API2/Storage/Config.pm  |  4 ++-
 PVE/API2/Storage/Content.pm | 43 ++++++++++++++++++++++---------
 PVE/API2/Storage/Status.pm  | 29 ++++++++++++++-------
 PVE/CLI/pvesm.pm            |  3 +++
 PVE/Storage.pm              | 62 +++++++++++++++++++++++++++++++++++++--------
 5 files changed, 109 insertions(+), 32 deletions(-)

diff --git a/PVE/API2/Storage/Config.pm b/PVE/API2/Storage/Config.pm
index e5c57ee..b9fdc0e 100755
--- a/PVE/API2/Storage/Config.pm
+++ b/PVE/API2/Storage/Config.pm
@@ -229,7 +229,9 @@ __PACKAGE__->register_method ({
     parameters => {
     	additionalProperties => 0,
 	properties => { 
-	    storage => get_standard_option('pve-storage-id'),
+	    storage => get_standard_option('pve-storage-id', {
+                completion => \&PVE::Storage::complete_storage,
+            }),
 	},
     },
     returns => { type => 'null' },
diff --git a/PVE/API2/Storage/Content.pm b/PVE/API2/Storage/Content.pm
index 63dc0a4..792d491 100644
--- a/PVE/API2/Storage/Content.pm
+++ b/PVE/API2/Storage/Content.pm
@@ -31,17 +31,20 @@ __PACKAGE__->register_method ({
     	additionalProperties => 0,
 	properties => { 
 	    node => get_standard_option('pve-node'),
-	    storage => get_standard_option('pve-storage-id'),
+	    storage => get_standard_option('pve-storage-id', {
+		completion => \&PVE::Storage::complete_storage_enabled,
+	    }),
 	    content => { 
 		description => "Only list content of this type.",
+		completion => \&PVE::Storage::complete_content,
 		type => 'string', format => 'pve-storage-content',
 		optional => 1,
 	    },
-	    vmid => get_standard_option
-		('pve-vmid', { 
-		    description => "Only list images for this VM",
-		    optional => 1,		
-		 }),
+	    vmid => get_standard_option('pve-vmid', {
+		description => "Only list images for this VM",
+		completion => \&PVE::Cluster::complete_vmid,
+		optional => 1,
+	    }),
 	},
     },
     returns => {
@@ -225,10 +228,16 @@ __PACKAGE__->register_method ({
     	additionalProperties => 0,
 	properties => { 
 	    node => get_standard_option('pve-node'),
-	    storage => get_standard_option('pve-storage-id', { optional => 1 }),
+            storage => get_standard_option('pve-storage-id', {
+		completion => \&PVE::Storage::complete_storage_enabled,
+		type => 'string',
+		optional => 1,
+            }),
 	    volume => {
 		description => "Volume identifier",
-		type => 'string', 
+		completion => \&PVE::Storage::complete_volume,
+		type => 'string',
+		maxLength => 128,
 	    },
 	},
     },
@@ -273,10 +282,15 @@ __PACKAGE__->register_method ({
     	additionalProperties => 0,
 	properties => { 
 	    node => get_standard_option('pve-node'),
-	    storage => get_standard_option('pve-storage-id', { optional => 1}),
+	    storage => get_standard_option('pve-storage-id', {
+		completion => \&PVE::Storage::complete_storage_enabled,
+		optional => 1,
+	    }),
 	    volume => {
 		description => "Volume identifier",
-		type => 'string', 
+		completion => \&PVE::Storage::complete_volume,
+		type => 'string',
+		maxLength => 128,
 	    },
 	},
     },
@@ -315,10 +329,15 @@ __PACKAGE__->register_method ({
     	additionalProperties => 0,
 	properties => { 
 	    node => get_standard_option('pve-node'),
-	    storage => get_standard_option('pve-storage-id', { optional => 1}),
+	    storage => get_standard_option('pve-storage-id', {
+		completion => \&PVE::Storage::complete_storage_enabled,
+		optional => 1,
+	    }),
 	    volume => {
 		description => "Source volume identifier",
-		type => 'string', 
+		completion => \&PVE::Storage::complete_volume,
+		type => 'string',
+		maxLength => 128,
 	    },
 	    target => {
 		description => "Target volume identifier",
diff --git a/PVE/API2/Storage/Status.pm b/PVE/API2/Storage/Status.pm
index e04e184..bd363b1 100644
--- a/PVE/API2/Storage/Status.pm
+++ b/PVE/API2/Storage/Status.pm
@@ -40,14 +40,15 @@ __PACKAGE__->register_method ({
     	additionalProperties => 0,
 	properties => {
 	    node => get_standard_option('pve-node'),
-	    storage => get_standard_option
-		('pve-storage-id', {
-		    description => "Only list status for  specified storage",
-		    optional => 1,
-		 }),
+	    storage => get_standard_option('pve-storage-id', {
+		description => "Only list status for  specified storage",
+		completion => \&PVE::Storage::complete_storage_enabled,
+		optional => 1,
+	    }),
 	    content => { 
 		description => "Only list stores which support this content type.",
 		type => 'string', format => 'pve-storage-content-list',
+		completion => \&PVE::Storage::complete_content,
 		optional => 1,
 	    },
 	    enabled => {
@@ -59,6 +60,7 @@ __PACKAGE__->register_method ({
 	    target => get_standard_option('pve-node', {
 		description => "If target is different to 'node', we only lists shared storages which " .
 		    "content is accessible on this 'node' and the specified 'target' node.",
+		completion => \&PVE::Cluster::get_nodelist,
 		optional => 1,
 	    }),
 	},
@@ -129,7 +131,10 @@ __PACKAGE__->register_method ({
     	additionalProperties => 0,
 	properties => {
 	    node => get_standard_option('pve-node'),
-	    storage => get_standard_option('pve-storage-id'),
+            storage => get_standard_option('pve-storage-id', {
+                description => "Only list status forspecified storage",
+                completion => \&PVE::Storage::complete_storage_enabled,
+	     }),
 	},
     },
     returns => {
@@ -206,7 +211,9 @@ __PACKAGE__->register_method ({
     	additionalProperties => 0,
 	properties => {
 	    node => get_standard_option('pve-node'),
-	    storage => get_standard_option('pve-storage-id'),
+	    storage => get_standard_option('pve-storage-id', {
+		completion => \&PVE::Storage::complete_storage_enabled,
+	    }),
 	    timeframe => {
 		description => "Specify the time frame you are interested in.",
 		type => 'string',
@@ -253,7 +260,9 @@ __PACKAGE__->register_method ({
     	additionalProperties => 0,
 	properties => {
 	    node => get_standard_option('pve-node'),
-	    storage => get_standard_option('pve-storage-id'),
+	    storage => get_standard_option('pve-storage-id', {
+		completion => \&PVE::Storage::complete_storage_enabled,
+            }),
 	    timeframe => {
 		description => "Specify the time frame you are interested in.",
 		type => 'string',
@@ -297,7 +306,9 @@ __PACKAGE__->register_method ({
     	additionalProperties => 0,
 	properties => {
 	    node => get_standard_option('pve-node'),
-	    storage => get_standard_option('pve-storage-id'),
+	    storage => get_standard_option('pve-storage-id', {
+                completion => \&PVE::Storage::complete_storage_enabled,
+            }),
 	    content => { 
 		description => "Content type.",
 		type => 'string', format => 'pve-storage-content',
diff --git a/PVE/CLI/pvesm.pm b/PVE/CLI/pvesm.pm
index bb122ce..2522742 100755
--- a/PVE/CLI/pvesm.pm
+++ b/PVE/CLI/pvesm.pm
@@ -34,6 +34,9 @@ __PACKAGE__->register_method ({
 	    volume => {
 		description => "Volume identifier",
 		type => 'string', format => 'pve-volume-id',
+		completion => \&PVE::Storage::complete_volume,
+                type => 'string',
+                maxLength => 128,
 	    },
 	},
     },
diff --git a/PVE/Storage.pm b/PVE/Storage.pm
index c27e9cf..d75189b 100755
--- a/PVE/Storage.pm
+++ b/PVE/Storage.pm
@@ -1169,23 +1169,65 @@ sub foreach_volid {
 
 # bash completion helper
 
+sub complete_content {
+    my ($cmdname, $pname, $cvalue) = @_;
+
+    my @ctypes = qw(images vztmpl iso backup);
+
+    push(@ctypes, 'rootdir') if ($cmdname eq 'add' || $cmdname eq 'status');
+
+    return [@ctypes];
+}
+
+sub complete_volume {
+    my ($cmdname, $pname, $cvalue) = @_;
+
+    my $data;
+    my $vollist = [];
+
+    my $cfg = PVE::Storage::config();
+    my $storage = complete_storage_enabled();
+
+    my $foreach_volume = sub {
+	my $sid = shift;
+	foreach my $volume (@{$data->{$sid}}) {
+	    push @$vollist, $volume->{volid};
+	}
+    };
+
+    foreach my $sid (@$storage) {
+	$data = PVE::Storage::vdisk_list ($cfg, $sid);
+	&$foreach_volume($sid);
+	$data = PVE::Storage::template_list ($cfg, $sid, 'iso');
+	&$foreach_volume($sid);
+	$data = PVE::Storage::template_list ($cfg, $sid, 'vztmpl');
+	&$foreach_volume($sid);
+	$data = PVE::Storage::template_list ($cfg, $sid, 'vztmpl');
+	&$foreach_volume($sid);
+    }
+
+    return $vollist;
+}
+
 sub complete_storage {
-   my ($cmdname, $pname, $cvalue) = @_;
+    my ($cmdname, $pname, $cvalue) = @_;
+
+    my $cfg = PVE::Storage::config();
 
-   return  $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids() ];
+    return  $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids($cfg) ];
 }
 
 sub complete_storage_enabled {
-   my ($cmdname, $pname, $cvalue) = @_;
+    my ($cmdname, $pname, $cvalue) = @_;
 
-   my $res = [];
+    my $res = [];
 
-   my $cfg = PVE::Storage::config();
-   foreach my $sid (keys %{$cfg->{ids}}) {
-       next if !storage_check_enabled($cfg, $sid, undef, 1);
-       push @$res, $sid;
-   }
-   return $res;
+    my $cfg = PVE::Storage::config();
+    foreach my $sid (keys %{$cfg->{ids}}) {
+	next if !storage_check_enabled($cfg, $sid, undef, 1);
+	push @$res, $sid;
+    }
+    return $res;
 }
 
 1;
-- 
2.1.4





More information about the pve-devel mailing list