Module: Grafana::Users

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

Overview

Instance Method Summary collapse

Instance Method Details

#search_for_users_by(params) ⇒ Array of Hashes

search users with parameters

Examples:

search_for_users_by( isAdmin: true )
search_for_users_by( login: 'foo' )

Returns:

  • (Array of Hashes)

    or false

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/grafana/users.rb', line 64

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

Examples:

params = {
  email:'[email protected]',
  user_name:'User2',
  login_name:'user',
  theme: 'light'
}
update_user( params )

Parameters:

Options Hash (params):

  • email (String)
  • user_name (String)
  • login_name (String)
  • theme (String)

Returns:

Raises:

  • (ArgumentError)


101
102
103
104
105
106
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
# File 'lib/grafana/users.rb', line 101

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 )
   = 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: ,
    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

Examples:

user( 1 )
user( 'foo' )

Parameters:

  • user_id (Mixed)

    Username (String) or Userid (Integer)

Returns:

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/grafana/users.rb', line 31

def user( user_id )

  raise ArgumentError.new(format('wrong type. user \'user_id\' must be an String (for an Datasource name) or an Integer (for an Datasource Id), given \'%s\'', user_id.class.to_s)) if( user_id.is_a?(String) && user_id.is_a?(Integer) )
  raise ArgumentError.new('missing \'user_id\'') if( user_id.size.zero? )

  if(user_id.is_a?(String))
    user_map = {}
    users.dig('message').each do |d|
      usr_id = d.dig('id').to_i
      user_map[usr_id] = d
    end

    user_id = user_map.select { |_k,v| v['login'] == user_id || v['email'] == user_id || v['name'] == user_id }.keys.first
  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

Examples:

user_organizations( 1 )
user_organizations( 'foo' )

Parameters:

  • user_id (Mixed)

    Username (String) or Userid (Integer)

Returns:

Raises:

  • (ArgumentError)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/grafana/users.rb', line 145

def user_organizations( user_id )

  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)) if( user_id.is_a?(String) && user_id.is_a?(Integer) )
  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

#usersHash

All Users

Examples:

all_users

Returns:



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