Module: Grafana::Users
- Included in:
- Client
- Defined in:
- lib/grafana/users.rb
Overview
Instance Method Summary collapse
-
#search_for_users_by(params) ⇒ Array of Hashes
search users with parameters.
-
#update_user(params) ⇒ Hash
User Update.
-
#user(user_id) ⇒ Hash
Get a single user by Id or Name.
-
#user_organizations(user_id) ⇒ Hash
Get Organisations for user.
-
#users ⇒ Hash
All Users.
Instance Method Details
#search_for_users_by(params) ⇒ Array of Hashes
search users with parameters
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/grafana/users.rb', line 70 def search_for_users_by( 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? ) all_users = users key, value = params.first logger.debug("Searching for users matching '#{key}' = '#{value}'") if @debug users = [] all_users.dig('message').each do |u| users.push(u) if u.select { |_k,v| v == value }.count >= 1 end (users.length >= 1 ? users : nil) end |
#update_user(params) ⇒ Hash
User Update
PUT /api/users/:id
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 |
# File 'lib/grafana/users.rb', line 107 def update_user( 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 = validate( params, required: true , var: 'user_name' , type: String ) email = validate( params, required: true , var: 'email' , type: String ) login_name = validate( params, required: false, var: 'login_name', type: String ) || user_name theme = validate( params, required: false, var: 'theme' , type: String ) usr = user(user_name) return { 'status' => 404, 'message' => format('User \'%s\' not found', user_name) } if( usr.nil? || usr.dig('status').to_i != 200 ) user_id = usr.dig('id') endpoint = format( '/api/users/%d', user_id ) payload = { email: email, name: user_name, login: login_name, theme: theme } payload.reject!{ |_k, v| v.nil? } @logger.debug("Updating user with Id #{user_id}") if @debug usr = usr.deep_string_keys payload = payload.deep_string_keys payload = usr.merge(payload) put( endpoint, payload.to_json ) end |
#user(user_id) ⇒ Hash
Get a single user by Id or Name
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 |
# File 'lib/grafana/users.rb', line 31 def user( user_id ) if( user_id.is_a?(String) && user_id.is_a?(Integer) ) raise ArgumentError.new(format('wrong type. user \'user_id\' must be an String (for an User name) or an Integer (for an User Id), given \'%s\'', user_id.class.to_s)) end raise ArgumentError.new('missing \'user_id\'') if( user_id.size.zero? ) if(user_id.is_a?(String)) usrs = users usrs = JSON.parse(usrs) if(usrs.is_a?(String)) status = usrs.dig('status') return usrs if( status != 200 ) u = usrs.dig('message').detect { |v| v['login'] == user_id || v['email'] == user_id || v['name'] == user_id } return { 'status' => 404, 'message' => format( 'No User \'%s\' found', user_id) } if( u.nil? ) user_id = u.dig('id') unless(u.nil?) end return { 'status' => 404, 'message' => format( 'No User \'%s\' found', user_id) } if( user_id.nil? ) endpoint = format( '/api/users/%s', user_id ) @logger.debug("Getting user by Id #{user_id} (GET #{endpoint})") if @debug data = get(endpoint) data['id'] = user_id data end |
#user_organizations(user_id) ⇒ Hash
Get Organisations for user
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/grafana/users.rb', line 151 def user_organizations( user_id ) if( user_id.is_a?(String) && user_id.is_a?(Integer) ) raise ArgumentError.new(format('wrong type. user \'user_id\' must be an String (for an Username) or an Integer (for an Userid), given \'%s\'', user_id.class.to_s)) end raise ArgumentError.new('missing \'user_id\'') if( user_id.size.zero? ) usr = user(user_id) return { 'status' => 404, 'message' => format('User \'%s\' not found', user_id) } if( usr.nil? || usr.dig('status').to_i != 200 ) user_id = usr.dig('id') endpoint = format('/api/users/%d/orgs', user_id ) @logger.debug("Getting organizations for User #{user_id} (GET #{endpoint})") if @debug get(endpoint) end |
#users ⇒ Hash
All Users
15 16 17 18 19 |
# File 'lib/grafana/users.rb', line 15 def users endpoint = '/api/users' @logger.debug("Getting all users (GET #{endpoint})") if @debug get(endpoint) end |