]> git.proxmox.com Git - pmg-api.git/blob - src/PMG/RuleDB/Counter.pm
dkim: add QID in warnings
[pmg-api.git] / src / PMG / RuleDB / Counter.pm
1 package PMG::RuleDB::Counter;
2
3 # FIXME: remove with PMG 8.0
4
5 use strict;
6 use warnings;
7 use DBI;
8 use Digest::SHA;
9
10 use PVE::SafeSyslog;
11
12 use PMG::Utils;
13 use PMG::RuleDB::Object;
14
15 use base qw(PMG::RuleDB::Object);
16
17 sub otype {
18 return 4999;
19 }
20
21 sub oclass {
22 return 'action';
23 }
24
25 sub otype_text {
26 return 'Counter';
27 }
28
29 sub new {
30 my ($type, $count, $ogroup) = @_;
31
32 my $class = ref($type) || $type;
33
34 my $self = $class->SUPER::new($class->otype(), $ogroup);
35
36 $count = 0 if !defined ($count);
37
38 $self->{count} = $count;
39
40 return $self;
41 }
42
43 sub load_attr {
44 my ($type, $ruledb, $id, $ogroup, $value) = @_;
45
46 my $class = ref($type) || $type;
47
48 defined($value) || die "undefined value: ERROR";
49
50 my $obj = $class->new($value, $ogroup);
51 $obj->{id} = $id;
52
53 $obj->{digest} = Digest::SHA::sha1_hex($id, $value, $ogroup);
54
55 return $obj;
56 }
57
58 sub save {
59 my ($self, $ruledb) = @_;
60
61 my $adr;
62
63 defined($self->{ogroup}) || die "undefined ogroup: ERROR";
64 defined($self->{count}) || die "undefined count: ERROR";
65
66 if (defined ($self->{id})) {
67 # update
68
69 $ruledb->{dbh}->do(
70 "UPDATE Object SET Value = ? WHERE ID = ?",
71 undef, $self->{count}, $self->{id});
72
73 } else {
74 # insert
75
76 my $sth = $ruledb->{dbh}->prepare(
77 "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
78 "VALUES (?, ?, ?);");
79
80 $sth->execute($self->{ogroup}, $self->otype, $self->{count});
81
82 $self->{id} = PMG::Utils::lastid($ruledb->{dbh}, 'object_id_seq');
83 }
84
85 return $self->{id};
86 }
87
88 sub execute {
89 my ($self, $queue, $ruledb, $mod_group, $targets,
90 $msginfo, $vars, $marks) = @_;
91
92 syslog('warning', "%s: deprecated action 'Counter' will be removed with PMG 8.0.",
93 $queue->{logid},);
94
95 eval {
96 $ruledb->{dbh}->begin_work;
97
98 $ruledb->{dbh}->do("LOCK TABLE Object IN SHARE MODE");
99
100 my $sth = $ruledb->{dbh}->prepare(
101 "SELECT Value FROM Object where ID = ?");
102 $sth->execute($self->{id});
103
104 my $ref = $sth->fetchrow_hashref();
105
106 $sth->finish();
107
108 defined($ref->{'value'}) || die "undefined value: ERROR";
109
110 my $value = int($ref->{'value'});
111
112 $ruledb->{dbh}->do(
113 "UPDATE Object SET Value = ? WHERE ID = ?",
114 undef, $value + 1, $self->{id});
115
116 $ruledb->{dbh}->commit;
117
118 if ($msginfo->{testmode}) {
119 print ("counter increased\n");
120 }
121 };
122 if (my $err = $@) {
123 $ruledb->{dbh}->rollback;
124 syslog('err', $err);
125 return undef;
126 }
127 }
128
129 sub count {
130 my ($self, $count) = @_;
131
132 if (defined ($count)) {
133 $self->{count} = $count;
134 }
135
136 $self->{count};
137 }
138
139
140 sub short_desc {
141 my $self = shift;
142
143 return "Increase Counter";
144 }
145
146 1;
147
148 __END__
149
150 =head1 PMG::RuleDB::Counter
151
152 Counter Object
153
154 =head2 Attributes
155
156 =head3 count
157
158 Unique Name of the Counter
159
160 =head2 Examples
161
162 $obj = PMG::RuleDB::Counter->new (0);
163