Module: Icinga2::Users

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

Overview

namespace for User handling

Instance Method Summary collapse

Instance Method Details

#add_user(params) ⇒ Hash

add a user

Examples:

param = {
  user_name: 'foo',
  display_name: 'FOO',
  email: '[email protected]',
  pager: '0000',
  groups: ['icingaadmins']
}
add_user(param)

Parameters:

Options Hash (params):

  • user_name (String)

    user to create

  • display_name (String) — default: ''

    the displayed name

  • email (String) — default: ''

    the user email

  • pager (String) — default: ''

    optional a pager

  • enable_notifications (Bool) — default: false

    enable notifications for this user

  • groups (Array) — default: []

    a hash with groups

  • period (String)
  • states (Array)
  • types (Array)
  • vars (Hash)

Returns:

  • (Hash)

    result

    • code [Integer]

    • message [String]

    • data (optional)

Raises:

  • (ArgumentError)


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

def add_user( 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_name = validate( params, required: true, var: 'user_name', type: String )
  display_name = validate( params, required: false, var: 'display_name', type: String )
  email = validate( params, required: false, var: 'email', type: String )
  pager = validate( params, required: false, var: 'pager', type: String )
  notifications = validate( params, required: false, var: 'enable_notifications', type: Boolean ) || false
  groups = validate( params, required: false, var: 'groups', type: Array ) || []
  period = validate( params, required: false, var: 'period', type: String )
  states = validate( params, required: false, var: 'states', type: Array )
  types = validate( params, required: false, var: 'types', type: Array )
  vars = validate( params, required: false, var: 'vars', type: Hash ) || {}

  group_validate = []

  groups.each do |g|
    group_validate << g if  exists_usergroup?( g ) == false
  end

  if( group_validate.count != 0 )
    return {
      'code' => 404,
      'message' => format('these groups are not exists: \'%s\'', group_validate.join(', ')),
      'data' => params
    }
  end

  payload = {
    attrs: {
      display_name: display_name,
      email: email,
      pager: pager,
      enable_notifications: notifications,
      groups: groups,
      period: period,
      states: states,
      types: types,
      vars: vars
    }
  }

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

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

#delete_user(user_name) ⇒ Hash

delete a user

Examples:

delete_user('foo')

Parameters:

  • user_name (String)

    user to delete

Returns:

Raises:

  • (ArgumentError)


103
104
105
106
107
108
109
110
111
112
113
# File 'lib/icinga2/users.rb', line 103

def delete_user( user_name )

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

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

#exists_user?(user_name) ⇒ Bool

checks if the user exists

Examples:

exists_user?('icingaadmin')

Parameters:

  • user_name (String)

    the name of the user

Returns:

  • (Bool)

    returns true if the user exists

Raises:

  • (ArgumentError)


148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/icinga2/users.rb', line 148

def exists_user?( user_name )

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

  result = users( user_name )
  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

#users(user_name = nil) ⇒ Array

returns a named or all users

Examples:

to get all users

users

to get one user

users('icingaadmin')

Parameters:

  • user_name (String) (defaults to: nil)

    (”) optional for a single user

Returns:



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/icinga2/users.rb', line 127

def users( user_name = nil )

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

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