]> git.proxmox.com Git - pve-ha-manager.git/commitdiff
start API for groups
authorDietmar Maurer <dietmar@proxmox.com>
Thu, 12 Mar 2015 11:17:34 +0000 (12:17 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 12 Mar 2015 11:17:34 +0000 (12:17 +0100)
src/PVE/API2/HA/Groups.pm [new file with mode: 0644]
src/PVE/API2/HA/Makefile
src/PVE/HA/Config.pm
src/ha-manager

diff --git a/src/PVE/API2/HA/Groups.pm b/src/PVE/API2/HA/Groups.pm
new file mode 100644 (file)
index 0000000..e204d53
--- /dev/null
@@ -0,0 +1,73 @@
+package PVE::API2::HA::Groups;
+
+use strict;
+use warnings;
+
+use PVE::SafeSyslog;
+use PVE::Tools qw(extract_param);
+use PVE::Cluster qw(cfs_read_file cfs_write_file);
+use PVE::HA::Config;
+use HTTP::Status qw(:constants);
+use Storable qw(dclone);
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::RPCEnvironment;
+
+use PVE::RESTHandler;
+
+use base qw(PVE::RESTHandler);
+
+# fixme: use cfs_read_file
+
+my $ha_groups_config = "/etc/pve/ha/groups.cfg";
+
+# fixme: fix permissions
+
+my $api_copy_config = sub {
+    my ($cfg, $sid) = @_;
+
+    my $scfg = dclone($cfg->{ids}->{$sid});
+    $scfg->{group} = $sid;
+    $scfg->{digest} = $cfg->{digest};
+
+    return $scfg;
+};
+
+__PACKAGE__->register_method ({
+    name => 'index', 
+    path => '',
+    method => 'GET',
+    description => "Get HA groups.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {},
+    },
+    returns => {
+       type => 'array',
+       items => {
+           type => "object",
+           properties => { group => { type => 'string'} },
+       },
+       links => [ { rel => 'child', href => "{group}" } ],
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $raw = '';
+
+       $raw = PVE::Tools::file_get_contents($ha_groups_config)
+           if -f $ha_groups_config;
+
+       my $cfg = PVE::HA::Config::parse_groups_config($ha_groups_config, $raw);
+
+       my $res = [];
+       foreach my $sid (keys %{$cfg->{ids}}) {
+           my $scfg = &$api_copy_config($cfg, $sid);
+           next if $scfg->{type} ne 'group'; # should not happen
+           push @$res, $scfg;
+       }
+
+       return $res;
+    }});
+
+
+1;
index 125a3cb76557b3a9e7aa47f9b0bc4aa5a8d27370..ceed9b7f9e8e7bf8731fceb09627097c14420072 100644 (file)
@@ -1,4 +1,4 @@
-SOURCES=Resources.pm
+SOURCES=Resources.pm Groups.pm
 
 .PHONY: install
 install:
index 25f06cf1c5d7308871204f2f23e53119b7bdccc7..be59e99fc51a81542f6d7f166d9101a32d5bd9fc 100644 (file)
@@ -18,7 +18,7 @@ PVE::HA::Resources->init();
 sub parse_groups_config {
     my ($filename, $raw) = @_;
 
-    return PVE::HA::Groups->parse_config($filename, $raw);
+     return PVE::HA::Groups->parse_config($filename, $raw);
 }
 
 sub parse_resources_config {
index 296ddf2aec2bf8a14780a271eb6b129673faf44d..78384aa22560b93ab86fd043097797af6c441926 100755 (executable)
@@ -9,6 +9,7 @@ use PVE::CLIHandler;
 use PVE::Cluster;
 
 use PVE::API2::HA::Resources;
+use PVE::API2::HA::Groups;
 
 use base qw(PVE::CLIHandler);
 
@@ -42,15 +43,26 @@ my $cmddef = {
     test => [ __PACKAGE__, 'test', []],
     config => [ 'PVE::API2::HA::Resources', 'index', [], {}, sub {
        my $res = shift;
-       foreach my $rec (@$res) {
+       foreach my $rec (sort { $a->{name} cmp $b->{name} } @$res) {
            print "$rec->{type}: $rec->{name}\n";
-           foreach my $k (keys %$rec) {
+           foreach my $k (sort keys %$rec) {
                next if $k eq 'digest' || $k eq 'sid' || 
                    $k eq 'type' || $k eq 'name';
                print "\t$k $rec->{$k}\n";
            }
            print "\n";
        }}],
+    groups => [ 'PVE::API2::HA::Groups', 'index', [], {}, sub {
+       my $res = shift;
+       foreach my $rec (sort { $a->{group} cmp $b->{group} } @$res) {
+           print "group: $rec->{group}\n";
+           foreach my $k (sort keys %$rec) {
+               next if $k eq 'digest' || $k eq 'group' || 
+                   $k eq 'type';
+               print "\t$k $rec->{$k}\n";
+           }
+           print "\n";
+       }}],
 };
 
 my $cmd = shift;