| 1 | package Zabbix7::API::CRUDE; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use 5.010; | ||
| 5 | use Carp; | ||
| 6 | use Scalar::Util qw/blessed/; | ||
| 7 | use Moo; | ||
| 8 | use Log::Any; # Added for better debugging | ||
| 9 | |||
| 10 | has 'data' => (is => 'ro', writer => '_set_data', default => sub { {} }); | ||
| 11 | has 'root' => (is => 'ro', required => 1); | ||
| 12 | |||
| 13 | sub short_class { | ||
| 14 | my $self = shift; | ||
| 15 | my $class = blessed($self) // $self; | ||
| 16 | $class =~ s/^Zabbix7::API:://; | ||
| 17 | return $class; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub id { | ||
| 21 | my ($self, $value) = @_; | ||
| 22 | if (defined $value) { | ||
| 23 | $self->data->{$self->_prefix('id')} = $value; | ||
| 24 | } | ||
| 25 | return $self->data->{$self->_prefix('id')}; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub node_id { | ||
| 29 | my $self = shift; | ||
| 30 | croak(sprintf(q{%s class object does not have a local ID, can't tell the node ID}, | ||
| 31 | $self->short_class)) unless $self->id; | ||
| 32 | return unless $self->id > 100_000_000_000_000; | ||
| 33 | return int($self->id/100_000_000_000_000); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _prefix { | ||
| 37 | croak 'Class '.(ref shift).' does not implement required method _prefix()'; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _extension { | ||
| 41 | croak 'Class '.(ref shift).' does not implement required method _extension()'; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub name { | ||
| 45 | croak 'Class '.(ref shift).' does not implement required method name()'; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub pull { | ||
| 49 | my $self = shift; | ||
| 50 | croak(sprintf(q{Cannot pull data from server into a %s without ID}, $self->short_class)) | ||
| 51 | unless $self->id; | ||
| 52 | my $data = $self->root->query( | ||
| 53 | method => $self->_prefix('.get'), | ||
| 54 | params => { | ||
| 55 | $self->_prefix('ids') => [$self->id], | ||
| 56 | $self->_extension | ||
| 57 | } | ||
| 58 | )->[0]; | ||
| 59 | croak(sprintf(q{%s class object has a local ID that does not appear to exist on the server}, | ||
| 60 | $self->short_class)) unless $data; | ||
| 61 | $self->_set_data($data); | ||
| 62 | Log::Any->get_logger->debug("Pulled data for $self->short_class ID: " . $self->id); | ||
| 63 | return $self; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub exists { | ||
| 67 | my $self = shift; | ||
| 68 | if (my $id = $self->id) { | ||
| 69 | my $response = $self->root->query( | ||
| 70 | method => $self->_prefix('.get'), | ||
| 71 | params => { | ||
| 72 | $self->_prefix('ids') => [$id], | ||
| 73 | countOutput => 1 | ||
| 74 | } | ||
| 75 | ); | ||
| 76 | Log::Any->get_logger->debug("Checked existence of $self->short_class ID: $id, exists: $response"); | ||
| 77 | return !!$response; | ||
| 78 | } else { | ||
| 79 | croak(sprintf(q{Cannot tell if a '%s' object exists on the server without the %s property}, | ||
| 80 | $self->short_class, $self->_prefix('id'))); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | sub create { | ||
| 85 | my $self = shift; | ||
| 86 | my %data = %{$self->data}; | ||
| 87 | if ($self->can('_readonly_properties')) { | ||
| 88 | delete @data{keys(%{$self->_readonly_properties})}; | ||
| 89 | } | ||
| 90 | |||
| 91 | my $results = eval { | ||
| 92 | $self->root->query( | ||
| 93 | method => $self->_prefix('.create'), | ||
| 94 | params => \%data | ||
| 95 | ) | ||
| 96 | }; | ||
| 97 | if (my $error = $@) { | ||
| 98 | Log::Any->get_logger->error("Failed to create $self->short_class: $error"); | ||
| 99 | croak "Failed to create $self->short_class: $error"; | ||
| 100 | } | ||
| 101 | |||
| 102 | my $id = $results->{$self->_prefix('ids')}->[0]; | ||
| 103 | $self->id($id); | ||
| 104 | Log::Any->get_logger->debug("Created $self->short_class with ID: $id"); | ||
| 105 | |||
| 106 | if ($self->root->pull_after_push_mode) { | ||
| 107 | $self->pull; | ||
| 108 | } | ||
| 109 | return $self; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub update { | ||
| 113 | my $self = shift; | ||
| 114 | if (my $id = $self->id) { | ||
| 115 | my %data = %{$self->data}; | ||
| 116 | if ($self->can('_readonly_properties')) { | ||
| 117 | delete @data{keys(%{$self->_readonly_properties})}; | ||
| 118 | } | ||
| 119 | $data{$self->_prefix('id')} = $id; # Ensure ID is included | ||
| 120 | |||
| 121 | my $results = eval { | ||
| 122 | $self->root->query( | ||
| 123 | method => $self->_prefix('.update'), | ||
| 124 | params => \%data | ||
| 125 | ) | ||
| 126 | }; | ||
| 127 | if (my $error = $@) { | ||
| 128 | Log::Any->get_logger->error("Failed to update $self->short_class ID: $id: $error"); | ||
| 129 | croak "Failed to update $self->short_class ID: $id: $error"; | ||
| 130 | } | ||
| 131 | |||
| 132 | Log::Any->get_logger->debug("Updated $self->short_class ID: $id"); | ||
| 133 | if ($self->root->pull_after_push_mode) { | ||
| 134 | $self->pull; | ||
| 135 | } | ||
| 136 | } else { | ||
| 137 | croak(sprintf(q{Cannot update new %s, need to create it or fetch it first}, | ||
| 138 | $self->short_class)); | ||
| 139 | } | ||
| 140 | return $self; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub delete { | ||
| 144 | my $self = shift; | ||
| 145 | if (my $id = $self->id) { | ||
| 146 | my $response = eval { | ||
| 147 | $self->root->query( | ||
| 148 | method => $self->_prefix('.delete'), | ||
| 149 | params => [{ $self->_prefix('id') => $id }] # Zabbix 7.0 expects array of objects | ||
| 150 | ) | ||
| 151 | }; | ||
| 152 | if (my $error = $@) { | ||
| 153 | Log::Any->get_logger->error("Failed to delete $self->short_class ID: $id: $error"); | ||
| 154 | croak "Failed to delete $self->short_class ID: $id: $error"; | ||
| 155 | } | ||
| 156 | Log::Any->get_logger->debug("Deleted $self->short_class ID: $id"); | ||
| 157 | } else { | ||
| 158 | croak(sprintf(q{Useless call of delete() on a %s that does not have a %s}, | ||
| 159 | $self->short_class, $self->_prefix('id'))); | ||
| 160 | } | ||
| 161 | return $self; | ||
| 162 | } | ||
| 163 | |||
| 164 | 1; | ||
| 165 | __END__ |