use base qw(PVE::RESTHandler);
+my $id_property = {
+ description => "Action Object ID.",
+ type => 'string',
+ pattern => '\d+_\d+',
+};
+
+my $format_action_object = sub {
+ my ($og, $action) = @_;
+
+ my $data = $action->get_data();
+ $data->{id} = "$data->{ogroup}_$data->{id}";
+ $data->{name} = $og->{name};
+ $data->{info} = $og->{info};
+ delete $data->{ogroup};
+
+ return $data;
+};
+
+my $load_action_with_og = sub {
+ my ($rdb, $id, $exp_otype) = @_;
+
+ die "internal error" if $id !~ m/^(\d+)_(\d+)$/;
+ my ($ogroup, $objid) = ($1, $2);
+
+ my $list = $rdb->load_objectgroups('action', $ogroup);
+ my $og = shift @$list ||
+ die "action group '$ogroup' not found\n";
+
+ my $action = $rdb->load_object_full($objid, $ogroup, $exp_otype);
+
+ return ($og, $action);
+};
__PACKAGE__->register_method ({
name => 'index',
}});
-my $id_property = {
- description => "Action Object ID.",
- type => 'string',
- pattern => '\d+_\d+',
-};
-
-my $format_action_object = sub {
- my ($action) = @_;
-
- my $data = $action->get_data();
- $data->{id} = "$data->{ogroup}_$data->{id}";
- delete $data->{ogroup};
-
- return $data;
-};
-
__PACKAGE__->register_method ({
name => 'list_actions',
path => 'objects',
foreach my $og (@$ogroups) {
my $action = $og->{action};
next if !$action;
- push @$res, $format_action_object->($action);
+ push @$res, $format_action_object->($og, $action);
}
return $res;
my ($param) = @_;
my $rdb = PMG::RuleDB->new();
-
- die "internal error" if $param->{id} !~ m/^(\d+)_(\d+)$/;
- my ($ogroup, $objid) = ($1, $2);
-
# test if object exists
- $rdb->load_object_full($objid, $ogroup);
-
- $rdb->delete_group($ogroup);
+ my ($og, $action) = $load_action_with_og->($rdb, $param->{id});
+ $rdb->delete_group($og->{id});
return undef;
}});
my $otype_text = $class->otype_text();
my $properties = $class->properties();
- my $create_properties = {};
- my $update_properties = { id => $id_property };
+ my $create_properties = {
+ name => {
+ description => "Action name.",
+ type => 'string',
+ maxLength => 255,
+ },
+ info => {
+ description => "Informational comment.",
+ type => 'string',
+ maxLength => 255,
+ optional => 1,
+ },
+ };
+ my $update_properties = {
+ id => $id_property,
+ name => {
+ description => "Action name.",
+ type => 'string',
+ maxLength => 255,
+ optional => 1,
+ },
+ info => {
+ description => "Informational comment.",
+ type => 'string',
+ maxLength => 255,
+ optional => 1,
+ },
+ };
my $read_properties = { id => $id_property };
foreach my $key (keys %$properties) {
my $obj = $rdb->get_object($otype);
$obj->update($param);
- my $og = $rdb->new_action($obj);
+ my $og = $rdb->new_action($obj, $param->{name}, $param->{info});
return "$og->{id}_$obj->{id}";
}});
my $rdb = PMG::RuleDB->new();
- die "internal error" if $param->{id} !~ m/^(\d+)_(\d+)$/;
- my ($ogroup, $objid) = ($1, $2);
-
- my $action = $rdb->load_object_full($objid, $ogroup, $otype);
+ my ($og, $action) = $load_action_with_og->($rdb, $param->{id}, $otype);
- return $format_action_object->($action);
+ return $format_action_object->($og, $action);
}});
__PACKAGE__->register_method ({
my $rdb = PMG::RuleDB->new();
- die "internal error" if $param->{id} !~ m/^(\d+)_(\d+)$/;
- my ($ogroup, $objid) = ($1, $2);
+ my ($og, $action) = $load_action_with_og->($rdb, $param->{id}, $otype);
+
+ my $name = extract_param($param, 'name');
+ my $info = extract_param($param, 'info');
+
+ if (defined($name) || defined($info)) {
+ $og->{name} = $name if defined($name);
+ $og->{info} = $info if defined($info);
+ $rdb->save_group($og);
+
+ return undef if !scalar(keys %$param); # we are done
+ }
- my $action = $rdb->load_object_full($objid, $ogroup, $otype);
+ die "no options specified\n"
+ if !scalar(keys %$param);
$action->update($param);
}
sub new_action {
- my ($self, $obj) = @_;
+ my ($self, $obj, $name, $info) = @_;
my $og;
my $id;
defined($obj) || die "proxmox: undefined object";
+ $name //= '';
+ $info //= '';
+
eval {
$self->{dbh}->begin_work;
- $self->{dbh}->do ("INSERT INTO Objectgroup (Name, Info, Class) " .
- "VALUES (?, ?, ?)", undef,
- decode_entities($obj->otype_text()), '', $obj->oclass());
+ $self->{dbh}->do("INSERT INTO Objectgroup (Name, Info, Class) " .
+ "VALUES (?, ?, ?)", undef,
+ $name, $info, $obj->oclass());
my $lid = PMG::Utils::lastid($self->{dbh}, 'objectgroup_id_seq');
- $og = PMG::RuleDB::Group->new();
+ my $og = PMG::RuleDB::Group->new($name, $info, $obj->oclass());
$og->{id} = $lid;
$obj->{ogroup} = $lid;