File Coverage

blib/lib/Zabbix2/API/Macro.pm
Criterion Covered Total %
statement 23 63 36.5
branch 0 34 0.0
condition n/a
subroutine 8 17 47.0
pod 6 6 100.0
total 37 120 30.8


line stmt bran cond sub pod time code
1             package Zabbix2::API::Macro;
2            
3 1     1   28296 use strict;
  1         3  
  1         31  
4 1     1   5 use warnings;
  1         2  
  1         23  
5 1     1   20 use 5.010;
  1         3  
6 1     1   5 use Carp;
  1         2  
  1         53  
7 1     1   6 use autodie;
  1         2  
  1         4  
8 1     1   5483 use utf8;
  1         2  
  1         5  
9            
10 1     1   25 use Moo;
  1         21  
  1         9  
11 1     1   538 use JSON;
  1         2  
  1         11  
12             extends qw/Zabbix2::API::CRUDE/;
13            
14             sub id {
15             ## mutator for id
16 0     0 1   my ($self, $value) = @_;
17 0 0         if (defined $value) {
18 0 0         if ($self->globalp) {
19 0           $self->data->{globalmacroid} = $value;
20 0           delete $self->data->{hostmacroid};
21 0           return $self->data->{globalmacroid};
22             } else {
23 0           $self->data->{hostmacroid} = $value;
24 0           delete $self->data->{globalmacroid};
25 0           return $self->data->{hostmacroid};
26             }
27             } else {
28 0 0         if ($self->globalp) {
29 0           return $self->data->{globalmacroid};
30             } else {
31 0           return $self->data->{hostmacroid};
32             }
33             }
34             }
35            
36             sub _readonly_properties {
37             # so, for some reason, the server returns a "hosts" property, but
38             # it complains when that property is sent back.
39             return {
40 0     0     hosts => 1,
41             };
42             }
43            
44             sub _prefix {
45 0     0     my ($self, $suffix) = @_;
46 0 0         if ($suffix) {
47 0 0         if ($suffix =~ m/ids?/) {
    0          
    0          
    0          
48 0 0         return ($self->globalp?'globalmacro':'hostmacro').$suffix;
49             } elsif ($suffix eq '.delete') {
50 0 0         return 'usermacro.'.($self->globalp?'deleteglobal':'delete');
51             } elsif ($suffix eq '.create') {
52 0 0         return 'usermacro.'.($self->globalp?'createglobal':'create');
53             } elsif ($suffix eq '.update') {
54 0 0         return 'usermacro.'.($self->globalp?'updateglobal':'update');
55             }
56 0           return 'usermacro'.$suffix;
57             } else {
58 0           return 'usermacro';
59             }
60             }
61            
62             sub _extension {
63 0     0     return (output => 'extend');
64             }
65            
66             sub name {
67 0     0 1   my $self = shift;
68 0           return $self->data->{macro};
69             }
70            
71             sub value {
72 0     0 1   my ($self, $value) = @_;
73 0 0         if (defined $value) {
74 0           $self->data->{value} = $value;
75             }
76 0           return $self->data->{value};
77             }
78            
79             sub globalp {
80 0     0 1   my $self = shift;
81 0           return !exists($self->data->{hostid});
82             }
83            
84             # overridden from CRUDE to set the globalmacro flag
85             sub pull {
86 0     0 1   my $self = shift;
87 0 0         croak(sprintf(q{Cannot pull data from server into a %s without ID}, $self->short_class))
88             unless $self->id;
89 0 0         my $data = $self->root->query(method => $self->_prefix('.get'),
90             params => { $self->_prefix('ids') => [ $self->id ],
91             globalmacro => $self->globalp ? JSON::true : JSON::false,
92             $self->_extension })->[0];
93 0 0         croak(sprintf(q{%s class object has a local ID that does not appear to exist on the server},
94             $self->short_class)) unless $data;
95 0           $self->_set_data($data);
96 0           return $self;
97             }
98            
99             sub exists {
100 0     0 1   my $self = shift;
101 0 0         my $response = $self->root->query(method => $self->_prefix('.get'),
102             params => { $self->_prefix('ids') => [$self->id],
103             globalmacro => $self->globalp ? JSON::true : JSON::false,
104             countOutput => 1 });
105 0           return !!$response;
106             }
107            
108             1;
109             __END__