]> git.proxmox.com Git - pve-ha-manager.git/commitdiff
complete ha group api
authorDietmar Maurer <dietmar@proxmox.com>
Fri, 3 Apr 2015 09:08:23 +0000 (11:08 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Fri, 3 Apr 2015 09:08:23 +0000 (11:08 +0200)
src/PVE/API2/HA/Groups.pm
src/PVE/HA/Config.pm

index e204d5305cefb6ccdf18bb85b8e962a483c181d9..67f317e83dfcd86151b3a2164f240ef41bd441cd 100644 (file)
@@ -2,6 +2,7 @@ package PVE::API2::HA::Groups;
 
 use strict;
 use warnings;
+use Data::Dumper;
 
 use PVE::SafeSyslog;
 use PVE::Tools qw(extract_param);
@@ -16,20 +17,18 @@ 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 ($cfg, $group) = @_;
 
-    my $scfg = dclone($cfg->{ids}->{$sid});
-    $scfg->{group} = $sid;
-    $scfg->{digest} = $cfg->{digest};
+    die "no such ha group '$group'\n" if !$cfg->{ids}->{$group};
+    
+    my $group_cfg = dclone($cfg->{ids}->{$group});
+    $group_cfg->{group} = $group;
+    $group_cfg->{digest} = $cfg->{digest};
 
-    return $scfg;
+    return $group_cfg;
 };
 
 __PACKAGE__->register_method ({
@@ -52,16 +51,11 @@ __PACKAGE__->register_method ({
     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 $cfg = PVE::HA::Config::read_group_config();
 
        my $res = [];
-       foreach my $sid (keys %{$cfg->{ids}}) {
-           my $scfg = &$api_copy_config($cfg, $sid);
+       foreach my $group (keys %{$cfg->{ids}}) {
+           my $scfg = &$api_copy_config($cfg, $group);
            next if $scfg->{type} ne 'group'; # should not happen
            push @$res, $scfg;
        }
@@ -70,4 +64,160 @@ __PACKAGE__->register_method ({
     }});
 
 
+__PACKAGE__->register_method ({
+    name => 'read',
+    path => '{group}',
+    method => 'GET',
+    description => "Read ha group configuration.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           group => get_standard_option('pve-ha-group-id'),
+       },
+    },
+    returns => {},
+    code => sub {
+       my ($param) = @_;
+
+       my $cfg = PVE::HA::Config::read_group_config();
+
+       return &$api_copy_config($cfg, $param->{group});
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'create',
+    protected => 1,
+    path => '',
+    method => 'POST',
+    description => "Create a new HA group.",
+    parameters => PVE::HA::Groups->createSchema(),
+    returns => { type => 'null' },
+    code => sub {
+       my ($param) = @_;
+
+       # create /etc/pve/ha directory
+       PVE::Cluster::check_cfs_quorum();
+       mkdir("/etc/pve/ha");
+       
+       my $group = extract_param($param, 'group');
+       my $type = 'group';
+       
+       if (my $param_type = extract_param($param, 'type')) {
+           # useless, but do it anyway
+           die "types does not match\n" if $param_type ne $type;
+       }
+
+       my $plugin = PVE::HA::Groups->lookup($type);
+
+       my $opts = $plugin->check_config($group, $param, 1, 1);
+
+       PVE::HA::Config::lock_ha_config(
+           sub {
+
+               my $cfg = PVE::HA::Config::read_group_config();
+
+               if ($cfg->{ids}->{$group}) {
+                   die "ha group ID '$group' already defined\n";
+               }
+
+               $cfg->{ids}->{$group} = $opts;
+
+               PVE::HA::Env::PVE2::write_group_config($cfg)
+
+           }, "create ha group failed");
+
+        return undef;
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'update',
+    protected => 1,
+    path => '{group}',
+    method => 'PUT',
+    description => "Update ha group configuration.",
+    parameters => PVE::HA::Groups->updateSchema(),
+    returns => { type => 'null' },
+    code => sub {
+       my ($param) = @_;
+
+       my $digest = extract_param($param, 'digest');
+       my $delete = extract_param($param, 'delete');
+
+       my $group = extract_param($param, 'group');
+       my $type = 'group';
+
+       if (my $param_type = extract_param($param, 'type')) {
+           # useless, but do it anyway
+           die "types does not match\n" if $param_type ne $type;
+       }
+
+       PVE::HA::Config::lock_ha_config(
+           sub {
+
+               my $cfg = PVE::HA::Config::read_group_config();
+
+               PVE::SectionConfig::assert_if_modified($cfg, $digest);
+
+               my $group_cfg = $cfg->{ids}->{$group} ||
+                   die "no such ha group '$group'\n";
+
+               my $plugin = PVE::HA::Groups->lookup($group_cfg->{type});
+               my $opts = $plugin->check_config($group, $param, 0, 1);
+
+               foreach my $k (%$opts) {
+                   $group_cfg->{$k} = $opts->{$k};
+               }
+
+               if ($delete) {
+                   my $options = $plugin->private()->{options}->{$type};
+                   foreach my $k (PVE::Tools::split_list($delete)) {
+                       my $d = $options->{$k} ||
+                           die "no such option '$k'\n";
+                       die "unable to delete required option '$k'\n"
+                           if !$d->{optional};
+                       die "unable to delete fixed option '$k'\n"
+                           if $d->{fixed};
+                       delete $group_cfg->{$k};
+                   }
+               }
+
+               PVE::HA::Config::write_group_config($cfg)
+
+           }, "update ha group failed");
+
+       return undef;
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'delete',
+    protected => 1,
+    path => '{group}',
+    method => 'DELETE',
+    description => "Delete ha group configuration.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           group => get_standard_option('pve-ha-group-id'),
+       },
+    },
+    returns => { type => 'null' },
+    code => sub {
+       my ($param) = @_;
+
+       my $group = extract_param($param, 'group');
+
+       PVE::HA::Config::lock_ha_config(
+           sub {
+
+               my $cfg = PVE::HA::Config::read_group_config();
+
+               delete $cfg->{ids}->{$group};
+
+               PVE::HA::Config::write_group_config($cfg)
+
+           }, "delete ha group failed");
+
+       return undef;
+    }});
+
 1;
index 0f025e52f6868acc62f3f7b2299524bcdecad183..bf32c765b25ed6cb50199f413d924b6652d1ca1c 100644 (file)
@@ -86,11 +86,16 @@ sub read_resources_config {
 }
 
 sub read_group_config {
-    my () = @_;
 
     return cfs_read_file($ha_groups_config);
 }
 
+sub write_group_config {
+    my ($cfg) = @_;
+
+    cfs_write_file($ha_groups_config, $cfg);
+}
+
 sub write_resources_config {
     my ($cfg) = @_;