blib/lib/Zabbix7/API/HostInterface.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 105 CC 5 SCAR 15.2
CC
5
Cov
0%
_prefix
12.0
id
6.0
name
6.0
CRAP 4.6
1 package Zabbix7::API::HostInterface;
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 extends qw/Exporter Zabbix7::API::CRUDE/;
12
13 use constant {
14 INTERFACE_TYPE_UNKNOWN => 0,
15 INTERFACE_TYPE_AGENT => 1,
16 INTERFACE_TYPE_SNMP => 2,
17 INTERFACE_TYPE_IPMI => 3,
18 INTERFACE_TYPE_JMX => 4,
19 INTERFACE_TYPE_ANY => 255,
20 };
21
22 our @EXPORT_OK = qw/
23 INTERFACE_TYPE_UNKNOWN
24 INTERFACE_TYPE_AGENT
25 INTERFACE_TYPE_SNMP
26 INTERFACE_TYPE_IPMI
27 INTERFACE_TYPE_JMX
28 INTERFACE_TYPE_ANY
29 /;
30
31 our %EXPORT_TAGS = (
32 interface_types => [
33 qw/INTERFACE_TYPE_UNKNOWN
34 INTERFACE_TYPE_AGENT
35 INTERFACE_TYPE_SNMP
36 INTERFACE_TYPE_IPMI
37 INTERFACE_TYPE_JMX
38 INTERFACE_TYPE_ANY/
39 ],
40 );
41
42 has 'host' => (is => 'ro', lazy => 1, builder => '_fetch_host');
43
44 sub id {
45 my ($self, $value) = @_;
46 if (defined $value) {
47 $self->data->{interfaceid} = $value;
48 return $self->data->{interfaceid};
49 }
50 return $self->data->{interfaceid};
51 }
52
53 sub _readonly_properties {
54 return {
55 interfaceid => 1,
56 hostid => 1, # Read-only in Zabbix 7.0
57 flags => 1, # Added for Zabbix 7.0 (read-only)
58 };
59 }
60
61 sub _prefix {
62 my (undef, $suffix) = @_;
63 if ($suffix and $suffix =~ m/ids?/) {
64 return 'interface' . $suffix;
65 } elsif ($suffix) {
66 return 'hostinterface' . $suffix;
67 }
68 return 'hostinterface';
69 }
70
71 sub _extension {
72 return (
73 output => 'extend',
74 selectHosts => ['hostid', 'host'], # Added for Zabbix 7.0
75 );
76 }
77
78 sub name {
79 my $self = shift;
80 my $name = $self->data->{useip} == 0 ? $self->data->{dns} : $self->data->{ip};
81 Log::Any->get_logger->debug("Generated name for interface ID: " . ($self->id // 'new') . ": $name");
82 return $name || '';
83 }
84
85 sub _fetch_host {
86 my $self = shift;
87 my $host = $self->{root}->fetch_single('Host', params => { hostids => [ $self->data->{hostid} ] });
88 Log::Any->get_logger->debug("Fetched host for interface ID: " . ($self->id // 'new') . ", hostid: " . ($self->data->{hostid} // 'unknown'));
89 return $host;
90 }
91
92 before 'update' => sub {
93 my ($self) = @_;
94 delete $self->data->{hostid}; # hostid is read-only
95 delete $self->data->{flags}; # flags is read-only
96 Log::Any->get_logger->debug("Preparing to update interface ID: " . ($self->id // 'new'));
97 };
98
99 before 'create' => sub {
100 my ($self) = @_;
101 delete $self->data->{interfaceid}; # Ensure interfaceid is not sent
102 delete $self->data->{flags}; # flags is read-only
103 Log::Any->get_logger->debug("Preparing to create interface for hostid: " . ($self->data->{hostid} // 'unknown'));
104 };
105
106 1;
107 __END__