-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
DEFERME Bert
committed
Oct 19, 2022
1 parent
61c9216
commit 5b3e391
Showing
2 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../zabbix' | ||
Puppet::Type.type(:zabbix_usergroup).provide(:ruby, parent: Puppet::Provider::Zabbix) do | ||
confine feature: :zabbixapi | ||
|
||
def initialize(value = {}) | ||
super(value) | ||
@property_flush = {} | ||
end | ||
|
||
def get_usrgrp_by_name(grpname) | ||
api_usrgrp = zbx.usergroups.get(name: grpname) | ||
if api_usrgrp.empty? | ||
nil | ||
else | ||
{ | ||
id: api_usrgrp[0]['id'], | ||
name: api_usrgrp[0]['name'], | ||
gui_access: api_usrgrp[0]['gui_access'], | ||
debug_mode: api_usrgrp[0]['debug_mode'] ? 1 : 0, | ||
users_status: api_usrgrp[0]['users_status'], | ||
} | ||
end | ||
end | ||
|
||
def usergroup | ||
@usergroup ||= get_usrgrp_by_name(resource[:name]) | ||
@usergroup | ||
end | ||
|
||
attr_writer :usergroup | ||
|
||
def gui_access | ||
usergroup[:gui_access] | ||
end | ||
|
||
def gui_access=(int) | ||
@property_flush[:gui_access] = int | ||
end | ||
|
||
def debug_mode | ||
usergroup[:debug_mode] | ||
end | ||
|
||
def debug_mode=(int) | ||
@property_flush[:debug_mode] = int | ||
end | ||
|
||
def users_status | ||
usergroup[:users_status] | ||
end | ||
|
||
def users_status=(int) | ||
@property_flush[:users_status] = int | ||
end | ||
|
||
def flush | ||
if @property_flush[:ensure] == :absent | ||
delete_usrgrp | ||
return | ||
end | ||
|
||
return if @property_flush.empty? | ||
|
||
update_usrgrp | ||
end | ||
|
||
def update_usrgrp | ||
zbx.query( | ||
method: 'usergroup.update', | ||
params: { | ||
usrgrpid: zbx.usergroups.get_id(name: @resource[:name]), | ||
}.merge(@property_flush) | ||
) | ||
end | ||
|
||
def delete_usrgrp | ||
zbx.usergroups.delete(zbx.usergroups.get_id(name: @resource[:name])) | ||
end | ||
|
||
def create | ||
params = {} | ||
params[:name] = @resource[:name] | ||
params[:gui_access] = @resource[:gui_access] unless @resource[:gui_access].nil? | ||
params[:debug_mode] = @resource[:debug_mode] unless @resource[:debug_mode].nil? | ||
params[:users_status] = @resource[:users_status] unless @resource[:users_status].nil? | ||
|
||
zbx.usergroups.create(params) | ||
end | ||
|
||
def exists? | ||
usergroup | ||
end | ||
|
||
def destroy | ||
@property_flush[:ensure] = :absent | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
Puppet::Type.newtype(:zabbix_usergroup) do | ||
@doc = %q("Manage zabbix usergroups | ||
zabbix_usergroup{ 'group1': | ||
ensure => present, | ||
gui_access => 2, | ||
debug_mode => true, | ||
users_status => true, | ||
}") | ||
|
||
ensurable do | ||
defaultvalues | ||
defaultto :present | ||
end | ||
|
||
newparam(:name, namevar: true) do | ||
desc 'usergroup name' | ||
end | ||
|
||
newproperty(:gui_access) do | ||
desc 'The type of access for this group.' | ||
newvalues('default', 'internal', 'LDAP', 'none') | ||
defaultto 'default' | ||
|
||
gui_values = { | ||
'default' => 0, | ||
'internal' => 1, | ||
'LDAP' => 2, | ||
'none' => 3 | ||
} | ||
|
||
munge do |value| | ||
gui_values[value] | ||
end | ||
end | ||
|
||
newproperty(:debug_mode, boolean: true) do | ||
desc 'Whether debug mode is enabled or disabled.' | ||
newvalues(true, false) | ||
defaultto false | ||
|
||
munge do |value| | ||
value ? 1 : 0 | ||
end | ||
end | ||
|
||
newproperty(:users_status, boolean: true) do | ||
desc 'Whether the user group is enabled or disabled.' | ||
newvalues(true, false) | ||
defaultto true | ||
|
||
munge do |value| | ||
# 0 = enabled in zabbix api... | ||
value ? 0 : 1 | ||
end | ||
end | ||
|
||
autorequire(:file) { '/etc/zabbix/api.conf' } | ||
end |