/usr/share/perl5/Zabbix/API/UserGroup.pm is in libzabbix-api-perl 0.009-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | package Zabbix::API::UserGroup;
use strict;
use warnings;
use 5.010;
use Carp;
use parent qw/Zabbix::API::CRUDE/;
use Zabbix::API::User;
sub id {
## mutator for id
my ($self, $value) = @_;
if (defined $value) {
$self->data->{usrgrpid} = $value;
return $self->data->{usrgrpid};
} else {
return $self->data->{usrgrpid};
}
}
sub prefix {
my (undef, $suffix) = @_;
if ($suffix and $suffix =~ m/ids?/) {
return 'usrgrp'.$suffix;
} elsif ($suffix) {
return 'usergroup'.$suffix;
} else {
return 'usergroup';
}
}
sub extension {
return ( output => 'extend' );
}
sub collides {
my $self = shift;
return @{$self->{root}->query(method => $self->prefix('.get'),
params => { filter => { name => $self->data->{name} },
$self->extension })};
}
sub name {
my $self = shift;
return $self->data->{name} || '[no user group name?]';
}
sub users {
my ($self, $value) = @_;
if (defined $value) {
$self->data->{users} = $value;
return $self->data->{users};
} else {
my $users = $self->{root}->fetch('User', params => { usrgrpids => [ $self->id ] });
$self->{users} = $users;
return $self->{users};
}
}
sub push {
# override CRUDE's push()
my ($self, $data) = @_;
$data //= $self->data;
foreach my $user (@{$data->{users}}) {
if (exists $user->{user}) {
if (eval { $user->{user}->isa('Zabbix::API::User') }) {
$user->{user}->push;
$user->{userid} = $user->{user}->id;
} else {
croak 'Type mismatch: user attribute should be an instance of Zabbix::API::User';
}
}
}
# copying the anonymous hashes so we can delete stuff without touching the
# originals
my $users_copy = [ map { { %{$_} } } @{$data->{users}} ];
foreach my $user (@{$users_copy}) {
delete $user->{user};
}
# copying the data hashref so we can replace its users with the fake
my $data_copy = { %{$data} };
# the old switcheroo
$data_copy->{users} = $users_copy;
return $self->SUPER::push($data_copy);
}
sub pull {
# override CRUDE's pull()
my ($self, $data) = @_;
if (defined $data) {
$self->{data} = $data;
} else {
my %stash = map { $_->id => $_ } grep { eval { $_->isa('Zabbix::API::User') } } @{$self->users};
$self->SUPER::pull;
## no critic (ProhibitCommaSeparatedStatements)
# restore stashed items that have been removed by pulling
$self->users(
[map {
{ %{$_},
user =>
$stash{$_->{userid}} // Zabbix::API::User->new(root => $self->{root},
data => { userid => $_->{userid} })->pull
}
}
@{$self->users}]
);
## use critic
}
return $self;
}
1;
__END__
=pod
=head1 NAME
Zabbix::API::UserGroup -- Zabbix usergroup objects
=head1 SYNOPSIS
use Zabbix::API::UserGroup;
my $group = $zabbix->fetch(...);
$group->delete;
=head1 DESCRIPTION
Handles CRUD for Zabbix usergroup objects.
This is a very simple subclass of C<Zabbix::API::CRUDE>. Only the
required methods are implemented (and in a very simple fashion on top
of that).
=head1 METHODS
=over 4
=item name()
Accessor for the usergroup's name (the "name" attribute); returns the
empty string if no name is set, for instance if the usergroup has not
been created on the server yet.
=item users()
Mutator for the usergroup's users.
=item push()
This method handles extraneous C<< user => Zabbix::API::User >>
attributes in the users array, transforming them into C<userid>
attributes, and pushing the users to the server if they don't exist
already. The original user attributes are kept but hidden from the
C<CRUDE> C<push> method, and restored after the C<pull> method is
called.
This means you can put C<Zabbix::API::User> objects in your data and
the module will Do The Right Thing (assuming you agree with my
definition of the Right Thing). Users that have been created this way
will not be removed from the server if they are removed from the
graph, however.
Overriden from C<Zabbix::API::CRUDE>.
=back
=head1 SEE ALSO
L<Zabbix::API::CRUDE>.
=head1 AUTHOR
Fabrice Gabolde <fabrice.gabolde@uperto.com>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2013 SFR
This library is free software; you can redistribute it and/or modify it under
the terms of the GPLv3.
=cut
|