use Data::Dumper;
use PVE::JSONSchema qw(get_standard_option);
use PVE::SectionConfig;
+use PVE::HA::Tools;
use base qw(PVE::SectionConfig);
-PVE::JSONSchema::register_format('pve-ha-group-node', \&pve_verify_ha_group_node);
-sub pve_verify_ha_group_node {
- my ($node, $noerr) = @_;
-
- if ($node !~ m/^([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)(:\d+)?$/) {
- return undef if $noerr;
- die "value does not look like a valid ha group node\n";
- }
- return $node;
-}
-
-PVE::JSONSchema::register_standard_option('pve-ha-group-node-list', {
- description => "List of cluster node names with optional priority. We use priority '0' as default. The CRM tries to run services on the node with higest priority (also see option 'nofailback').",
- type => 'string', format => 'pve-ha-group-node-list',
- typetext => '<node>[:<pri>]{,<node>[:<pri>]}*',
-});
-
-PVE::JSONSchema::register_standard_option('pve-ha-group-id', {
- description => "The HA group identifier.",
- type => 'string', format => 'pve-configid',
-});
-
my $defaultData = {
propertyList => {
type => { description => "Section type." },
--- /dev/null
+package PVE::HA::Resources;
+
+use strict;
+use warnings;
+
+use Data::Dumper;
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::SectionConfig;
+use PVE::HA::Tools;
+
+use base qw(PVE::SectionConfig);
+
+my $defaultData = {
+ propertyList => {
+ type => { description => "Resource type." },
+ name => {
+ description => "Resource name.",
+ type => 'string',
+ optional => 1,
+ },
+ group => get_standard_option('pve-ha-group-id'),
+ comment => {
+ description => "Description.",
+ type => 'string',
+ optional => 1,
+ maxLength => 4096,
+ },
+ },
+};
+
+sub verify_name {
+ my ($class, $name) = @_;
+
+ die "implement this in subclass";
+}
+
+sub private {
+ return $defaultData;
+}
+
+sub parse_section_header {
+ my ($class, $line) = @_;
+
+ if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
+ my ($type, $name) = (lc($1), $2);
+ my $errmsg = undef; # set if you want to skip whole section
+ eval {
+ if (my $plugin = $defaultData->{plugins}->{$type}) {
+ $plugin->verify_name($name);
+ }
+ # fixme: ?
+ #PVE::JSONSchema::pve_verify_configid($name);
+ };
+ $errmsg = $@ if $@;
+ my $config = {
+ name => $name,
+ }; # to return additional attributes
+ return ($type, "$type:$name", $errmsg, $config);
+ }
+ return undef;
+}
+
+package PVE::HA::Resources::PVEVM;
+
+use strict;
+use warnings;
+
+use base qw(PVE::HA::Resources);
+
+sub type {
+ return 'pvevm';
+}
+
+sub verify_name {
+ my ($class, $name) = @_;
+
+ die "invalid VMID\n" if $name !~ m/^[1-9][0-0]+$/;
+}
+
+sub options {
+ return {
+ name => {},
+ group => { optional => 1 },
+ comment => { optional => 1 },
+ };
+}
+
+package PVE::HA::Resources::IPAddr;
+
+use strict;
+use warnings;
+use PVE::Tools qw($IPV4RE $IPV6RE);
+
+use base qw(PVE::HA::Resources);
+
+sub type {
+ return 'ipaddr';
+}
+
+sub verify_name {
+ my ($class, $name) = @_;
+
+ die "invalid IP address\n" if $name !~ m!^$IPV6RE|$IPV4RE$!;
+}
+
+sub options {
+ return {
+ name => {},
+ group => { optional => 1 },
+ comment => { optional => 1 },
+ };
+}
+
+1;
use strict;
use warnings;
-use JSON;
+use JSON;
+use PVE::JSONSchema;
use PVE::Tools;
+PVE::JSONSchema::register_format('pve-ha-group-node', \&pve_verify_ha_group_node);
+sub pve_verify_ha_group_node {
+ my ($node, $noerr) = @_;
+
+ if ($node !~ m/^([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)(:\d+)?$/) {
+ return undef if $noerr;
+ die "value does not look like a valid ha group node\n";
+ }
+ return $node;
+}
+
+PVE::JSONSchema::register_standard_option('pve-ha-group-node-list', {
+ description => "List of cluster node names with optional priority. We use priority '0' as default. The CRM tries to run services on the node with higest priority (also see option 'nofailback').",
+ type => 'string', format => 'pve-ha-group-node-list',
+ typetext => '<node>[:<pri>]{,<node>[:<pri>]}*',
+});
+
+PVE::JSONSchema::register_standard_option('pve-ha-group-id', {
+ description => "The HA group identifier.",
+ type => 'string', format => 'pve-configid',
+});
+
sub read_json_from_file {
my ($filename, $default) = @_;
my ($filename, $data) = @_;
my $raw = encode_json($data);
-
+
PVE::Tools::file_set_contents($filename, $raw);
}