Class: Grafana::Client

Inherits:
Object
  • Object
show all
Includes:
Admin, Dashboard, DashboardTemplate, Datasource, Frontend, HttpRequest, Login, Organization, Organizations, Snapshot, User, Users
Defined in:
lib/grafana/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Admin

#create_user, #delete_user, #get_admin_settings, #update_user_pass, #update_user_permissions

Methods included from Login

#ping_session

Methods included from Frontend

#get_frontend_settings

Methods included from Snapshot

#create_snapshot, #delete_snapshot, #get_snapshot

Methods included from DashboardTemplate

#build_panel, #build_target, #build_template

Methods included from Dashboard

#create_dashboard, #create_slug, #delete_dashboard, #get_dashboard, #get_dashboard_tags, #get_home_dashboard, #search_dashboards

Methods included from Organizations

#add_user_to_org, #delete_user_from_org, #get_all_orgs, #get_org_users, #update_org, #update_org_user

Methods included from Organization

#add_user_to_current_org, #get_current_org, #get_current_org_users, #update_current_org

Methods included from Datasource

#create_data_source, #delete_data_source, #get_available_data_source_types, #get_cw_namespaces, #get_data_source, #get_data_sources, #update_data_source

Methods included from Users

#get_all_users, #get_user_by_id, #get_user_orgs, #search_for_users_by, #update_user_info

Methods included from User

#add_dashboard_star, #get_current_user, #get_current_user_orgs, #remove_dashboard_star, #switch_current_user_org, #update_current_user_pass

Methods included from HttpRequest

#_issue_request, #delete_request, #get_request, #patch_request, #post_request, #put_request

Constructor Details

#initialize(host = "localhost", port = 3000, user = 'admin', pass = '', settings = {}) ⇒ Client

Returns a new instance of Client.



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
# File 'lib/grafana/client.rb', line 39

def initialize(host="localhost", port=3000, user='admin', pass='', settings={})

  if settings.has_key?('timeout') && settings['timeout'].to_i <= 0
    settings['timeout'] = 5
  end
  
  if settings.has_key?('open_timeout') && settings['open_timeout'].to_i <= 0
    settings['open_timeout'] = 5
  end

  if settings.has_key?('headers') && settings['headers'].class.to_s != 'Hash'
    settings['headers'] = {}
  end

  proto = ( settings.has_key?('ssl') && settings['ssl'] == true ? 'https' : 'http')
  
  @logger.info("Initializing API client #{proto}://#{host}:#{port}") if @debug
  @logger.info("Options: #{options}") if @debug
  
  @api_instance = RestClient::Resource.new(
    "#{proto}://#{host}:#{port}", 
    :timeout => settings['timeout'],
    :open_timeout => settings['open_timeout'],
    :headers => settings['headers']
  )
  @debug = (settings['debug'] ? true : false)
  @logger = Logger.new(STDOUT)
  @headers = nil

  self.(user, pass)
  return self
end

Instance Attribute Details

#api_instanceObject (readonly)

Returns the value of attribute api_instance.



24
25
26
# File 'lib/grafana/client.rb', line 24

def api_instance
  @api_instance
end

#debugObject (readonly)

Returns the value of attribute debug.



24
25
26
# File 'lib/grafana/client.rb', line 24

def debug
  @debug
end

#headersObject (readonly)

Returns the value of attribute headers.



24
25
26
# File 'lib/grafana/client.rb', line 24

def headers
  @headers
end

#loggerObject (readonly)

Returns the value of attribute logger.



24
25
26
# File 'lib/grafana/client.rb', line 24

def logger
  @logger
end

#session_cookiesObject (readonly)

Returns the value of attribute session_cookies.



24
25
26
# File 'lib/grafana/client.rb', line 24

def session_cookies
  @session_cookies
end

Instance Method Details

#login(user = 'admin', pass = 'admin') ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/grafana/client.rb', line 72

def (user='admin',pass='admin')
  @logger.info("Attempting to establish user session") if @debug
  request_data = {'User' => user, 'Password' => pass}
  begin
    resp = @api_instance['/login'].post(
      request_data.to_json, 
      {:content_type => 'application/json; charset=UTF-8'}
    )
    @session_cookies = resp.cookies
    if resp.code.to_i == 200
      @headers = {
        :content_type => 'application/json; charset=UTF-8',
        :cookies => @session_cookies
      }
      return true
    else
      return false
    end
  rescue => e
    @logger.error("Error running POST request on /login: #{e}") if @debug
    @logger.error("Request data: #{request_data.to_json}") if @debug
    return false
  end
  @logger.info("User session initiated") if @debug
end