rename start.pm to GuestStatus.pm, implement stop command
authorDietmar Maurer <dietmar@proxmox.com>
Wed, 13 Jun 2018 07:24:37 +0000 (09:24 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 13 Jun 2018 07:24:37 +0000 (09:24 +0200)
Please note that we group similar commands inside a single command class.
Our CLIHandler then nicely groups this commands inside help.

Makefile
PVE/APIClient/Commands/GuestStatus.pm [new file with mode: 0644]
PVE/APIClient/Commands/start.pm [deleted file]
pveclient

index 6aa2d4c..7daa428 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -36,6 +36,7 @@ install:  pve-api-definition.dat
        install -D -m 0644 PVE/APIClient/Config.pm ${LIB_DIR}/PVE/APIClient/Config.pm
        install -D -m 0644 PVE/APIClient/Commands/remote.pm ${LIB_DIR}/PVE/APIClient/Commands/remote.pm
        install -D -m 0644 PVE/APIClient/Commands/lxc.pm ${LIB_DIR}/PVE/APIClient/Commands/lxc.pm
+       install -D -m 0644 PVE/APIClient/Commands/GuestStatus.pm ${LIB_DIR}/PVE/APIClient/Commands/GuestStatus.pm
        install -D -m 0644 pve-api-definition.dat ${LIB_DIR}/pve-api-definition.dat
        install -D -m 0755 pveclient ${DESTDIR}/usr/bin/pveclient
        install -D -m 0644 pveclient.bash-completion ${BASHCOMPLDIR}/pveclient
diff --git a/PVE/APIClient/Commands/GuestStatus.pm b/PVE/APIClient/Commands/GuestStatus.pm
new file mode 100644 (file)
index 0000000..7903a2b
--- /dev/null
@@ -0,0 +1,76 @@
+package PVE::APIClient::Commands::GuestStatus;
+
+use strict;
+use warnings;
+
+use PVE::APIClient::Helpers;
+use PVE::APIClient::Config;
+
+use PVE::JSONSchema qw(get_standard_option);
+
+use PVE::CLIHandler;
+
+use base qw(PVE::CLIHandler);
+
+my $guest_status_command = sub {
+    my ($remote, $vmid, $cmd, $param) = @_,
+
+    my $config = PVE::APIClient::Config->load();
+    my $conn = PVE::APIClient::Config->remote_conn($config, $remote);
+
+    my $resource = PVE::APIClient::Helpers::get_vmid_resource($conn, $vmid);
+
+    my $upid = $conn->post("api2/json/nodes/$resource->{node}/$resource->{type}/$resource->{vmid}/status/$cmd", $param);
+
+    print PVE::APIClient::Helpers::poll_task($conn, $resource->{node}, $upid) . "\n";
+};
+
+__PACKAGE__->register_method ({
+    name => 'start',
+    path => 'start',
+    method => 'POST',
+    description => "Start a  guest (VM/Container).",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           remote => get_standard_option('pveclient-remote-name'),
+           vmid => get_standard_option('pve-vmid'),
+       },
+    },
+    returns => { type => 'null'},
+    code => sub {
+       my ($param) = @_;
+
+       my $remote = PVE::Tools::extract_param($param, 'remote');
+       my $vmid = PVE::Tools::extract_param($param, 'vmid');
+
+       $guest_status_command->($remote, $vmid, 'start', $param);
+
+       return undef;
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'stop',
+    path => 'stop',
+    method => 'POST',
+    description => "Stop a guest (VM/Container).",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           remote => get_standard_option('pveclient-remote-name'),
+           vmid => get_standard_option('pve-vmid'),
+       },
+    },
+    returns => { type => 'null'},
+    code => sub {
+       my ($param) = @_;
+
+       my $remote = PVE::Tools::extract_param($param, 'remote');
+       my $vmid = PVE::Tools::extract_param($param, 'vmid');
+
+       $guest_status_command->($remote, $vmid, 'stop', $param);
+
+       return undef;
+    }});
+
+1;
diff --git a/PVE/APIClient/Commands/start.pm b/PVE/APIClient/Commands/start.pm
deleted file mode 100644 (file)
index 2b217a5..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-package PVE::APIClient::Commands::start;
-
-use strict;
-use warnings;
-
-use PVE::APIClient::Helpers;
-use PVE::JSONSchema qw(get_standard_option);
-
-use PVE::CLIHandler;
-
-use base qw(PVE::CLIHandler);
-
-__PACKAGE__->register_method ({
-    name => 'start',
-    path => 'start',
-    method => 'POST',
-    description => "Start a Qemu VM/LinuX Container.",
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           remote => get_standard_option('pveclient-remote-name'),
-           vmid => get_standard_option('pve-vmid'),
-       },
-    },
-    returns => { type => 'null'},
-    code => sub {
-       my ($param) = @_;
-
-       my $config = PVE::APIClient::Config->load();
-       my $conn = PVE::APIClient::Config->remote_conn($config, $param->{remote});
-
-       my $resource = PVE::APIClient::Helpers::get_vmid_resource($conn, $param->{vmid});
-
-       my $upid = $conn->post("api2/json/nodes/$resource->{node}/$resource->{type}/$resource->{vmid}/status/start", {});
-
-       print PVE::APIClient::Helpers::poll_task($conn, $resource->{node}, $upid) . "\n";
-
-       return undef;
-    }});
-
-our $cmddef = [ __PACKAGE__, 'start', ['remote', 'vmid']];
-
-1;
index 8b07853..4abe7b7 100755 (executable)
--- a/pveclient
+++ b/pveclient
@@ -18,7 +18,7 @@ use PVE::APIClient::Commands::config;
 use PVE::APIClient::Commands::remote;
 use PVE::APIClient::Commands::list;
 use PVE::APIClient::Commands::lxc;
-use PVE::APIClient::Commands::start;
+use PVE::APIClient::Commands::GuestStatus;
 
 use JSON;
 
@@ -162,7 +162,8 @@ our $cmddef = {
     lxc => $PVE::APIClient::Commands::lxc::cmddef,
     remote => $PVE::APIClient::Commands::remote::cmddef,
     
-    start => $PVE::APIClient::Commands::start::cmddef,
+    start => [ 'PVE::APIClient::Commands::GuestStatus', 'start', ['remote', 'vmid']],
+    stop => [ 'PVE::APIClient::Commands::GuestStatus', 'stop', ['remote', 'vmid']],
 
     api => {
        get => [ __PACKAGE__, 'pveclient_get', ['remote', 'api_path']],