Module: Grafana::User

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

Overview

Instance Method Summary collapse

Instance Method Details

#add_dashboard_star(dashboard_id) ⇒ Hash

Star a dashboard

Examples:

add_dashboard_star( 1 )
add_dashboard_star( 'QA Graphite Carbon Metrics' )

Parameters:

  • dashboard_id (Mixed)

    Dashboard Name (String) or Dashboard Id (Integer) for add a star

Returns:

Raises:

  • (ArgumentError)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/grafana/user.rb', line 99

def add_dashboard_star( dashboard_id )

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

  dashboard_id = dashboard if(dashboard_id.is_a?(Integer))

  if(dashboard_id.is_a?(String))
    r = search_dashboards( query: dashboard_id )
    message = r.dig('message')
    dashboard_id = message.first.dig('id')
  end

  raise format('Dashboard Id can not be 0') if( dashboard_id.zero? )

  endpoint = format( '/api/user/stars/dashboard/%d', dashboard_id )
  @logger.debug("Adding star to dashboard id #{dashboard_id} (GET #{endpoint})") if @debug
  post( endpoint, {}.to_json )
end

#current_userHash

Actual User

Examples:

current_user

Returns:



15
16
17
18
19
# File 'lib/grafana/user.rb', line 15

def current_user
  endpoint = '/api/user'
  @logger.debug("Getting user current user (GET #{endpoint})") if @debug
  get(endpoint)
end

#current_user_oganizationsHash

Organisations of the actual User

Examples:

current_user_oganizations

Returns:



83
84
85
86
87
# File 'lib/grafana/user.rb', line 83

def current_user_oganizations
  endpoint = '/api/user/orgs'
  @logger.debug("Getting current user organizations (GET #{endpoint})") if @debug
  get(endpoint)
end

#remove_dashboard_star(dashboard_id) ⇒ Hash

Unstar a dashboard

Examples:

remove_dashboard_star( 1 )
remove_dashboard_star( 'QA Graphite Carbon Metrics' )

Parameters:

  • dashboard_id (Mixed)

    Dashboard Name (String) or Dashboard Id (Integer) for delete a star

Returns:

Raises:

  • (ArgumentError)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/grafana/user.rb', line 129

def remove_dashboard_star( dashboard_id )

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

  dashboard_id = dashboard( dashboard_id ) if(dashboard_id.is_a?(Integer))

  if(dashboard_id.is_a?(String))
    r = search_dashboards( query: dashboard_id )
    message = r.dig('message')
    dashboard_id = message.first.dig('id')
  end

  raise format('Dashboard Id can not be 0') if  dashboard_id.zero?

  endpoint = format( '/api/user/stars/dashboard/%d', dashboard_id )
  @logger.debug("Deleting star on dashboard id #{dashboard_id} (GET #{endpoint})") if @debug
  delete( endpoint )
end

#switch_current_user_organization(organization) ⇒ Hash

Switch user context for signed in user

Examples:

switch_current_user_organization( 'Main. Org' )

Parameters:

  • organization (String)

    organization

Returns:

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/grafana/user.rb', line 59

def switch_current_user_organization( organization )

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

  org = organization_by_name( organization )

  return { 'status' => 404, 'message' => format('Organization \'%s\' not found', organization) } if( org.nil? || org.dig('status').to_i != 200 )

  org_id = org.dig('id')

  endpoint = format( '/api/user/using/%d', org_id )
  @logger.debug("Switching current user to Organization #{organization} (GET #{endpoint})") if @debug

  post( endpoint, {} )
end

#update_current_user_password(params) ⇒ Hash

Change Password

Examples:

update_current_user_password( old_password: 'foo', new_password: 'FooBar' )

Parameters:

Options Hash (params):

  • old_password (String)

    the old password

  • new_password (String)

    the new password

Returns:

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/grafana/user.rb', line 32

def update_current_user_password( 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? )

  old_password   = validate( params, required: true, var: 'old_password', type: String )
  new_password   = validate( params, required: true, var: 'new_password', type: String )

  endpoint = '/api/user/password'
  payload = {
    oldPassword: old_password,
    newPassword: new_password,
    confirmNew: new_password
  }
  @logger.debug("Updating current user password (PUT #{endpoint})") if @debug
  put( endpoint, payload.to_json )
end