| 1 | package Zabbix2::API::CRUDE; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 12 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 12 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 12 | ▶ | use 5.010; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 12 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 12 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 12 | ▶ | use Moo::Lax; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | has 'data' => (is => 'ro', | ||||||||||||||||||||
| 12 | writer => '_set_data', | ||||||||||||||||||||
| 13 | 0 | ▶ | default => sub { {} }); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 14 | 12 | has 'root' => (is => 'ro', | |||||||||||||||||||
| 15 | required => 1); | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | sub short_class { | ||||||||||||||||||||
| 18 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
short_class: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 19 | 0 | ▶ | my $class = blessed($self) // $self; | ||||||||||||||||||
|
Condition0%0/3
builtin::blessed($self) // $self
MC/DC0%0/2
builtin::blessed($self) // $self
builtin::blessed($self)
$self
Truth table0%0/3
A // B
A= builtin::blessed($self)B= $self
| |||||||||||||||||||||
| 20 | 0 | $class =~ s/^Zabbix2::API:://; | |||||||||||||||||||
| 21 | 0 | return $class; | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | sub id { | ||||||||||||||||||||
| 25 | 0 | ▶ | my ($self, $value) = @_; | ||||||||||||||||||
|
Subroutine
id: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 26 | 0 | ▶ | if (defined $value) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | 0 | $self->data->{$self->_prefix('id')} = $value; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | 0 | return $self->data->{$self->_prefix('id')}; | |||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | sub node_id { | ||||||||||||||||||||
| 33 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
node_id: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 34 | 0 | ▶ | croak(sprintf(q{%s class object does not have a local ID, can't tell the node ID}, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | $self->short_class)) unless $self->id; | ||||||||||||||||||||
| 36 | 0 | ▶ | return unless $self->id > 100_000_000_000_000; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | # this is how Zabbix operates, don't blame me | ||||||||||||||||||||
| 38 | 0 | return int($self->id/100_000_000_000_000); | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | sub _prefix { | ||||||||||||||||||||
| 42 | 0 | ▶ | croak 'Class '.(ref shift).' does not implement required method _prefix()'; | ||||||||||||||||||
|
Subroutine
_prefix: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | sub _extension { | ||||||||||||||||||||
| 46 | 0 | ▶ | croak 'Class '.(ref shift).' does not implement required method _extension()'; | ||||||||||||||||||
|
Subroutine
_extension: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | sub name { | ||||||||||||||||||||
| 50 | 0 | ▶ | croak 'Class '.(ref shift).' does not implement required method name()'; | ||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | sub pull { | ||||||||||||||||||||
| 54 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
pull: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 55 | 0 | ▶ | croak(sprintf(q{Cannot pull data from server into a %s without ID}, $self->short_class)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | unless $self->id; | ||||||||||||||||||||
| 57 | 0 | my $data = $self->root->query(method => $self->_prefix('.get'), | |||||||||||||||||||
| 58 | params => { $self->_prefix('ids') => [ $self->id ], | ||||||||||||||||||||
| 59 | $self->_extension })->[0]; | ||||||||||||||||||||
| 60 | 0 | ▶ | croak(sprintf(q{%s class object has a local ID that does not appear to exist on the server}, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | $self->short_class)) unless $data; | ||||||||||||||||||||
| 62 | 0 | $self->_set_data($data); | |||||||||||||||||||
| 63 | 0 | return $self; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | sub exists { | ||||||||||||||||||||
| 67 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
exists: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 68 | 0 | ▶ | if (my $id = $self->id) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 0 | my $response = $self->root->query(method => $self->_prefix('.get'), | |||||||||||||||||||
| 70 | params => { $self->_prefix('ids') => [$id], | ||||||||||||||||||||
| 71 | countOutput => 1 }); | ||||||||||||||||||||
| 72 | 0 | return !!$response; | |||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||
| 74 | 0 | croak(sprintf(q{Cannot tell if a '%s' object exists on the server without the %s property}, | |||||||||||||||||||
| 75 | $self->_prefix, $self->_prefix('id'))); | ||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | sub create { | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
create: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | ## remove the readonly properties | ||||||||||||||||||||
| 84 | 0 | my %data = %{$self->data}; | |||||||||||||||||||
| 85 | 0 | ▶ | if ($self->can('_readonly_properties')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 0 | delete @data{keys(%{$self->_readonly_properties})}; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | my $results = $self->root->query(method => $self->_prefix('.create'), | |||||||||||||||||||
| 90 | params => \%data); | ||||||||||||||||||||
| 91 | 0 | my $id = $results->{$self->_prefix('ids')}->[0]; | |||||||||||||||||||
| 92 | 0 | $self->id($id); | |||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | ## if lazy_push: done, else fetch the new updated data | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 0 | ▶ | if ($self->root->pull_after_push_mode) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | $self->pull; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | return $self; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | sub update { | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
update: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | ## save the ID because it is likely to be one of the readonly | ||||||||||||||||||||
| 109 | ## properties | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | ▶ | if (my $id = $self->id) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | ## remove the readonly properties | ||||||||||||||||||||
| 113 | 0 | my %data = %{$self->data}; | |||||||||||||||||||
| 114 | 0 | ▶ | if ($self->can('_readonly_properties')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 0 | delete @data{keys(%{$self->_readonly_properties})}; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 0 | eval { | |||||||||||||||||||
| 119 | 0 | $self->root->query(method => $self->_prefix('.update'), | |||||||||||||||||||
| 120 | params => { $self->_prefix('id') => $id, | ||||||||||||||||||||
| 121 | %data }); | ||||||||||||||||||||
| 122 | }; | ||||||||||||||||||||
| 123 | 0 | ▶ | croak $@ if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 0 | ▶ | if ($self->root->pull_after_push_mode) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 0 | $self->pull; | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | } else { | ||||||||||||||||||||
| 129 | 0 | croak(sprintf(q{Cannot update new %s, need to create it or fetch it first}, | |||||||||||||||||||
| 130 | $self->short_class)); | ||||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 0 | return $self; | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | sub delete { | ||||||||||||||||||||
| 138 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 139 | 0 | ▶ | if ($self->id) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | 0 | my $response = $self->root->query(method => $self->_prefix('.delete'), | |||||||||||||||||||
| 141 | params => [ $self->id ]); | ||||||||||||||||||||
| 142 | } else { | ||||||||||||||||||||
| 143 | 0 | croak(sprintf(q{Useless call of delete() on a %s that does not have a %s}, | |||||||||||||||||||
| 144 | $self->_prefix, $self->_prefix('id'))); | ||||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | 0 | return $self; | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 12 | 1; | |||||||||||||||||||
| 150 | __END__ | ||||||||||||||||||||