Module: Icinga2::Users
- Included in:
- Client
- Defined in:
- lib/icinga2/users.rb
Overview
namespace for User handling
Instance Method Summary collapse
-
#add_user(params) ⇒ Hash
add a user.
-
#delete_user(params) ⇒ Hash
delete a user.
-
#exists_user?(user_name) ⇒ Bool
checks if the user exists.
-
#users(params = {}) ⇒ Array
returns a named or all users.
Instance Method Details
#add_user(params) ⇒ Hash
add a user
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 |
# File 'lib/icinga2/users.rb', line 24 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 = params.dig(:user_name) display_name = params.dig(:display_name) email = params.dig(:email) pager = params.dig(:pager) notifications = params.dig(:enable_notifications) || false groups = params.dig(:groups) || [] raise ArgumentError.new('Missing user_name') if( user_name.nil? ) raise ArgumentError.new('groups must be an array') unless( groups.is_a?( Array ) ) payload = { 'attrs' => { 'display_name' => display_name, 'email' => email, 'pager' => pager, 'enable_notifications' => notifications } } payload['attrs']['groups'] = groups unless groups.empty? group_validate = [] groups.each do |g| group_validate << g if exists_usergroup?( g ) == false end if( group_validate.count != 0 ) groups = group_validate.join(', ') return { status: 404, message: "these groups are not exists: #{groups}", data: params } end put( url: format( '%s/objects/users/%s', @icinga_api_url_base, user_name ), headers: @headers, options: @options, payload: payload ) end |
#delete_user(params) ⇒ Hash
delete a user
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/icinga2/users.rb', line 85 def delete_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 = params.dig(:user_name) raise ArgumentError.new('Missing user_name') if( user_name.nil? ) 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
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/icinga2/users.rb', line 143 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: 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(params = {}) ⇒ Array
returns a named or all users
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/icinga2/users.rb', line 114 def users( params = {} ) raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) ) user_name = params.dig(:user_name) url = if( user_name.nil? ) format( '%s/objects/users' , @icinga_api_url_base ) else format( '%s/objects/users/%s', @icinga_api_url_base, user_name ) end api_data( url: url, headers: @headers, options: @options ) end |