Module: Icinga2::Usergroups

Included in:
Client
Defined in:
lib/icinga2/usergroups.rb

Overview

namespace for usergroup handling

Instance Method Summary collapse

Instance Method Details

#add_usergroup(params) ⇒ Hash

add a usergroup

Examples:

add_usergroup(user_group: 'foo', display_name: 'FOO')

Parameters:

Options Hash (params):

  • user_group (String)

    usergroup to create

  • display_name (String)

    the displayed name

Returns:

Raises:

  • (ArgumentError)


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
# File 'lib/icinga2/usergroups.rb', line 20

def add_usergroup( params )

  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
  raise ArgumentError.new('missing \'params\'') if( params.size.zero? )

  user_group   = validate( params, required: true, var: 'user_group', type: String )
  display_name = validate( params, required: false, var: 'display_name', type: String )

  payload = {
    attrs: {
      display_name: display_name
    }
  }

  # remove all empty attrs
  payload.reject!{ |_k, v| v.nil? }
  payload[:attrs].reject!{ |_k, v| v.nil? }

  put(
    url: format( '%s/objects/usergroups/%s', @icinga_api_url_base, user_group ),
    headers: @headers,
    options: @options,
    payload: payload
  )
end

#delete_usergroup(user_group) ⇒ Hash

delete a usergroup

Examples:

delete_usergroup('foo')

Parameters:

  • user_group (String)

    usergroup to delete

Returns:

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/icinga2/usergroups.rb', line 55

def delete_usergroup( user_group )

  raise ArgumentError.new(format('wrong type. \'user_group\' must be an String, given \'%s\'', user_group.class.to_s)) unless( user_group.is_a?(String) )
  raise ArgumentError.new('missing \'user_group\'') if( user_group.size.zero? )

  delete(
    url: format( '%s/objects/usergroups/%s?cascade=1', @icinga_api_url_base, user_group ),
    headers: @headers,
    options: @options
  )
end

#exists_usergroup?(user_group) ⇒ Bool

returns true if the usergroup exists

Examples:

exists_usergroup?('icingaadmins')

Parameters:

  • user_group (String)

    the name of the usergroups

Returns:

  • (Bool)

    returns true if the usergroup exists

Raises:

  • (ArgumentError)


100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/icinga2/usergroups.rb', line 100

def exists_usergroup?( user_group )

  raise ArgumentError.new(format('wrong type. \'user_group\' must be an String, given \'%s\'', user_group.class.to_s)) unless( user_group.is_a?(String) )
  raise ArgumentError.new('Missing \'user_group\'') if( user_group.size.zero? )

  result = usergroups( user_group )
  result = JSON.parse( result ) if result.is_a?( String )
  result = result.first if( result.is_a?(Array) )

  return false if( result.is_a?(Hash) && result.dig('code') == 404 )

  true
end

#usergroups(user_group = nil) ⇒ Hash

returns all usersgroups

Examples:

to get all users

usergroups

to get one user

usergroups('icingaadmins')

Parameters:

  • user_group (String) (defaults to: nil)

    (nil) optional for a single usergroup

Returns:

  • (Hash)

    returns a hash with all usergroups



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/icinga2/usergroups.rb', line 79

def usergroups( user_group = nil )

  url = format( '%s/objects/usergroups'   , @icinga_api_url_base )
  url = format( '%s/objects/usergroups/%s', @icinga_api_url_base, user_group ) unless( user_group.nil? )

  api_data(
    url: url,
    headers: @headers,
    options: @options
  )
end