line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Zabbix::API::CRUDE; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
2048
|
use strict; |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
157
|
|
4
|
5
|
|
|
5
|
|
23
|
use warnings; |
|
5
|
|
|
|
|
6
|
|
|
5
|
|
|
|
|
129
|
|
5
|
5
|
|
|
5
|
|
86
|
use 5.010; |
|
5
|
|
|
|
|
16
|
|
6
|
5
|
|
|
5
|
|
30
|
use Carp; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
5214
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub new { |
9
|
|
|
|
|
|
|
|
10
|
0
|
|
|
0
|
1
|
|
my ($class, %args) = @_; |
11
|
|
|
|
|
|
|
|
12
|
0
|
|
|
|
|
|
my $self = \%args; |
13
|
|
|
|
|
|
|
|
14
|
0
|
|
|
|
|
|
bless $self, $class; |
15
|
|
|
|
|
|
|
|
16
|
0
|
|
|
|
|
|
return $self; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub id { |
21
|
|
|
|
|
|
|
|
22
|
0
|
|
|
0
|
1
|
|
croak 'Class '.(ref shift).' does not implement required mutator id()'; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub prefix { |
27
|
|
|
|
|
|
|
|
28
|
0
|
|
|
0
|
1
|
|
croak 'Class '.(ref shift).' does not implement required method prefix()'; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub extension { |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
0
|
1
|
|
croak 'Class '.(ref shift).' does not implement required method extension()'; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub name { |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
0
|
1
|
|
croak 'Class '.(ref shift).' does not implement required method name()'; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub data { |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
## accessor for data |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
0
|
1
|
|
my ($self, $value) = @_; |
49
|
|
|
|
|
|
|
|
50
|
0
|
0
|
|
|
|
|
if (defined $value) { |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
croak 'Accessor data() called as mutator'; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
} else { |
55
|
|
|
|
|
|
|
|
56
|
0
|
0
|
|
|
|
|
$self->{data} = {} unless exists $self->{data}; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
return $self->{data}; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub pull { |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
0
|
1
|
|
my ($self, $data) = @_; |
67
|
|
|
|
|
|
|
|
68
|
0
|
0
|
|
|
|
|
if (defined $data) { |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
$self->{data} = $data; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
} else { |
73
|
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
|
croak sprintf('Cannot pull data from server into a %s without ID', $self->prefix) |
75
|
|
|
|
|
|
|
unless $self->id; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
$self->{data} = $self->{root}->query(method => $self->prefix('.get'), |
78
|
|
|
|
|
|
|
params => { |
79
|
|
|
|
|
|
|
$self->prefix('ids') => [ $self->id ], |
80
|
|
|
|
|
|
|
$self->extension |
81
|
|
|
|
|
|
|
})->[0]; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# uniquify objects |
84
|
0
|
0
|
|
|
|
|
if (my $replacement = $self->{root}->refof($self)) { |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
$self = $replacement; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
} else { |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
$self->{root}->reference($self); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
return $self; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub created { |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
103
|
|
|
|
|
|
|
|
104
|
0
|
0
|
|
|
|
|
return $self->id if $self->{root}->{lazy}; |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
return @{$self->{root}->query(method => $self->prefix('.get'), |
|
0
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
params => { |
108
|
|
|
|
|
|
|
$self->prefix('ids') => [$self->id], |
109
|
|
|
|
|
|
|
$self->extension |
110
|
|
|
|
|
|
|
})}; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub collides { |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
0
|
1
|
|
croak 'Class '.(ref shift).' does not implement required method collides()'; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub push { |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
0
|
1
|
|
my ($self, $data) = @_; |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
0
|
|
|
|
$data //= $self->data; |
125
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
my @colliders; |
127
|
|
|
|
|
|
|
|
128
|
0
|
0
|
0
|
|
|
|
if ($self->id and $self->created) { |
|
|
0
|
0
|
|
|
|
|
|
|
0
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
say sprintf('Updating %s %s', $self->prefix, $self->id) |
131
|
0
|
0
|
|
|
|
|
if $self->{root}->{verbosity}; |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
$self->{root}->query(method => $self->prefix('.update'), |
134
|
|
|
|
|
|
|
params => $data); |
135
|
|
|
|
|
|
|
|
136
|
0
|
0
|
|
|
|
|
$self->pull unless $self->{root}->{lazy}; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
} elsif ($self->id) { |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
croak sprintf('%s has a %s but does not exist on server', $self->id, $self->prefix('id')); |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
} elsif (@colliders = $self->collides and $colliders[0]) { |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
say sprintf('Updating %s (match by collisions)', $self->prefix) |
145
|
0
|
0
|
|
|
|
|
if $self->{root}->{verbosity}; |
146
|
|
|
|
|
|
|
|
147
|
0
|
0
|
|
|
|
|
if (@colliders > 1) { |
148
|
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
|
croak sprintf('Cannot push %s: too many possible targets', $self->prefix); |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
my $class = ref $self; |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# not referenced! and that's the way we want it |
156
|
0
|
|
|
|
|
|
my $collider = $class->new(root => $self->{root}, data => $colliders[0]); |
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
$self->id($collider->id); |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
$self->push; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
} else { |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
say 'Creating '.$self->prefix |
165
|
0
|
0
|
|
|
|
|
if $self->{root}->{verbosity}; |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
my $id = $self->{root}->query(method => $self->prefix('.create'), |
168
|
0
|
|
|
|
|
|
params => $data)->{$self->prefix('ids')}->[0]; |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
$self->id($id); |
171
|
|
|
|
|
|
|
|
172
|
0
|
0
|
|
|
|
|
$self->pull unless $self->{root}->{lazy}; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
return $self; |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub delete { |
181
|
|
|
|
|
|
|
|
182
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
183
|
|
|
|
|
|
|
|
184
|
0
|
0
|
|
|
|
|
if ($self->id) { |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
say sprintf('Deleting %s %s', $self->prefix, $self->id) |
187
|
0
|
0
|
|
|
|
|
if $self->{root}->{verbosity}; |
188
|
|
|
|
|
|
|
|
189
|
0
|
|
|
|
|
|
$self->{root}->query(method => $self->prefix('.delete'), |
190
|
|
|
|
|
|
|
params => [ $self->id ]); |
191
|
|
|
|
|
|
|
|
192
|
0
|
|
|
|
|
|
delete $self->{root}->{stash}->{$self->prefix('s')}->{$self->id}; |
193
|
|
|
|
|
|
|
|
194
|
0
|
|
|
|
|
|
$self->{root}->dereference($self); |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
} else { |
197
|
|
|
|
|
|
|
|
198
|
0
|
|
|
|
|
|
carp sprintf(q{Useless call of delete() on a %s that does not have a %s}, $self->prefix, $self->prefix('id')); |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
0
|
|
|
|
|
|
return $self; |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
1; |
207
|
|
|
|
|
|
|
__END__ |