| 1 | package Zabbix7::API::Macro; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use 5.010; | ||
| 6 | use Carp; | ||
| 7 | use autodie; | ||
| 8 | use utf8; | ||
| 9 | |||
| 10 | use Moo; | ||
| 11 | use JSON; | ||
| 12 | extends qw/Zabbix7::API::CRUDE/; | ||
| 13 | |||
| 14 | sub id { | ||
| 15 | my ($self, $value) = @_; | ||
| 16 | if (defined $value) { | ||
| 17 | if ($self->globalp) { | ||
| 18 | $self->data->{globalmacroid} = $value; | ||
| 19 | delete $self->data->{hostmacroid}; | ||
| 20 | Log::Any->get_logger->debug("Set globalmacroid: $value for global macro"); | ||
| 21 | return $self->data->{globalmacroid}; | ||
| 22 | } else { | ||
| 23 | $self->data->{hostmacroid} = $value; | ||
| 24 | delete $self->data->{globalmacroid}; | ||
| 25 | Log::Any->get_logger->debug("Set hostmacroid: $value for host macro"); | ||
| 26 | return $self->data->{hostmacroid}; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | my $id = $self->globalp ? $self->data->{globalmacroid} : $self->data->{hostmacroid}; | ||
| 30 | Log::Any->get_logger->debug("Retrieved ID for macro: " . ($id // 'none') . ", global: " . ($self->globalp ? 'yes' : 'no')); | ||
| 31 | return $id; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _readonly_properties { | ||
| 35 | return { | ||
| 36 | hosts => 1, | ||
| 37 | globalmacroid => 1, # Added for Zabbix 7.0 (read-only for updates) | ||
| 38 | hostmacroid => 1, # Added for Zabbix 7.0 (read-only for updates) | ||
| 39 | }; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub _prefix { | ||
| 43 | my ($self, $suffix) = @_; | ||
| 44 | if ($suffix) { | ||
| 45 | if ($suffix =~ m/ids?/) { | ||
| 46 | return ($self->globalp ? 'globalmacro' : 'hostmacro') . $suffix; | ||
| 47 | } elsif ($suffix eq '.delete') { | ||
| 48 | return 'usermacro.' . ($self->globalp ? 'deleteglobal' : 'delete'); | ||
| 49 | } elsif ($suffix eq '.create') { | ||
| 50 | return 'usermacro.' . ($self->globalp ? 'createglobal' : 'create'); | ||
| 51 | } elsif ($suffix eq '.update') { | ||
| 52 | return 'usermacro.' . ($self->globalp ? 'updateglobal' : 'update'); | ||
| 53 | } | ||
| 54 | return 'usermacro' . $suffix; | ||
| 55 | } | ||
| 56 | return 'usermacro'; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _extension { | ||
| 60 | return ( | ||
| 61 | output => 'extend', | ||
| 62 | selectHosts => ['hostid', 'host'], # Added for Zabbix 7.0 (for host macros) | ||
| 63 | ); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub name { | ||
| 67 | my $self = shift; | ||
| 68 | my $name = $self->data->{macro} || '???'; | ||
| 69 | Log::Any->get_logger->debug("Retrieved name for macro ID: " . ($self->id // 'new') . ": $name"); | ||
| 70 | return $name; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub value { | ||
| 74 | my ($self, $value) = @_; | ||
| 75 | if (defined $value) { | ||
| 76 | $self->data->{value} = $value; | ||
| 77 | Log::Any->get_logger->debug("Set value for macro ID: " . ($self->id // 'new') . ": $value"); | ||
| 78 | } | ||
| 79 | my $val = $self->data->{value}; | ||
| 80 | Log::Any->get_logger->debug("Retrieved value for macro ID: " . ($self->id // 'new') . ": " . ($val // 'none')); | ||
| 81 | return $val; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub globalp { | ||
| 85 | my $self = shift; | ||
| 86 | my $is_global = !exists($self->data->{hostid}); | ||
| 87 | Log::Any->get_logger->debug("Checked globalp for macro ID: " . ($self->id // 'new') . ": " . ($is_global ? 'global' : 'host')); | ||
| 88 | return $is_global; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub pull { | ||
| 92 | my $self = shift; | ||
| 93 | croak(sprintf(q{Cannot pull data from server into a %s without ID}, $self->short_class)) | ||
| 94 | unless $self->id; | ||
| 95 | my $data = $self->root->query( | ||
| 96 | method => $self->_prefix('.get'), | ||
| 97 | params => { | ||
| 98 | $self->_prefix('ids') => [ $self->id ], | ||
| 99 | globalmacro => $self->globalp ? JSON::true : JSON::false, | ||
| 100 | $self->_extension | ||
| 101 | } | ||
| 102 | )->[0]; | ||
| 103 | croak(sprintf(q{%s class object has a local ID that does not appear to exist on the server}, | ||
| 104 | $self->short_class)) unless $data; | ||
| 105 | $self->_set_data($data); | ||
| 106 | Log::Any->get_logger->debug("Pulled data for $self->short_class ID: " . $self->id); | ||
| 107 | return $self; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub exists { | ||
| 111 | my $self = shift; | ||
| 112 | my $response = $self->root->query( | ||
| 113 | method => $self->_prefix('.get'), | ||
| 114 | params => { | ||
| 115 | $self->_prefix('ids') => [ $self->id ], | ||
| 116 | globalmacro => $self->globalp ? JSON::true : JSON::false, | ||
| 117 | countOutput => 1, | ||
| 118 | } | ||
| 119 | ); | ||
| 120 | Log::Any->get_logger->debug("Checked existence of $self->short_class ID: " . ($self->id // 'none') . ", exists: $response"); | ||
| 121 | return !!$response; | ||
| 122 | } | ||
| 123 | |||
| 124 | before 'create' => sub { | ||
| 125 | my ($self) = @_; | ||
| 126 | delete $self->data->{globalmacroid}; # Ensure IDs are not sent | ||
| 127 | delete $self->data->{hostmacroid}; | ||
| 128 | Log::Any->get_logger->debug("Preparing to create macro: " . ($self->data->{macro} // 'unknown') . ", global: " . ($self->globalp ? 'yes' : 'no')); | ||
| 129 | }; | ||
| 130 | |||
| 131 | before 'update' => sub { | ||
| 132 | my ($self) = @_; | ||
| 133 | delete $self->data->{globalmacroid}; # Ensure IDs are not sent | ||
| 134 | delete $self->data->{hostmacroid}; | ||
| 135 | Log::Any->get_logger->debug("Preparing to update macro ID: " . ($self->id // 'new') . ", global: " . ($self->globalp ? 'yes' : 'no')); | ||
| 136 | }; | ||
| 137 | |||
| 138 | 1; | ||
| 139 | __END__ |