From 7252f28d633b46fa8b485a7a071698917a99b5fa Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 12 Mar 2015 12:17:34 +0100 Subject: [PATCH] start API for groups --- src/PVE/API2/HA/Groups.pm | 73 +++++++++++++++++++++++++++++++++++++++ src/PVE/API2/HA/Makefile | 2 +- src/PVE/HA/Config.pm | 2 +- src/ha-manager | 16 +++++++-- 4 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 src/PVE/API2/HA/Groups.pm diff --git a/src/PVE/API2/HA/Groups.pm b/src/PVE/API2/HA/Groups.pm new file mode 100644 index 0000000..e204d53 --- /dev/null +++ b/src/PVE/API2/HA/Groups.pm @@ -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; diff --git a/src/PVE/API2/HA/Makefile b/src/PVE/API2/HA/Makefile index 125a3cb..ceed9b7 100644 --- a/src/PVE/API2/HA/Makefile +++ b/src/PVE/API2/HA/Makefile @@ -1,4 +1,4 @@ -SOURCES=Resources.pm +SOURCES=Resources.pm Groups.pm .PHONY: install install: diff --git a/src/PVE/HA/Config.pm b/src/PVE/HA/Config.pm index 25f06cf..be59e99 100644 --- a/src/PVE/HA/Config.pm +++ b/src/PVE/HA/Config.pm @@ -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 { diff --git a/src/ha-manager b/src/ha-manager index 296ddf2..78384aa 100755 --- a/src/ha-manager +++ b/src/ha-manager @@ -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; -- 2.39.2