Module: Grafana::Datasource

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

Overview

Instance Method Summary collapse

Instance Method Details

#create_datasource(params) ⇒ Hash

Create data source

Examples:

params = {
  name: 'graphite',
  type: 'graphite',
  url: 'http://localhost:8080'
}
create_datasource(params)

params = {
  name: 'graphite',
  type: 'graphite',
  default: true,
  url: 'http://localhost:8080',
  json_data: { graphiteVersion: '1.1' }
}
create_datasource(params)

params = {
  name: 'test_datasource',
  type: 'cloudwatch',
  url: 'http://monitoring.us-west-1.amazonaws.com',
  json_data: {
    authType: 'keys',
    defaultRegion: 'us-west-1'
  },
  json_secure: {
    accessKey: 'Ol4pIDpeKSA6XikgOl4p',
    secretKey: 'dGVzdCBrZXkgYmxlYXNlIGRvbid0IHN0ZWFs'
  }
}
create_datasource(params)

Parameters:

Options Hash (params):

  • type (String)

    Datasource Type - (required) (grafana graphite cloudwatch elasticsearch prometheus influxdb mysql opentsdb postgres)

  • name (String)

    Datasource Name - (required)

  • access (String) — default: proxy

    Acess Type - (required) (proxy or direct)

  • default (Boolean) — default: false
  • url (String)

    Datasource URL - (required)

  • json_data (Hash)
  • json_secure (Hash)
  • basic_user (String)
  • basic_password (String)

Returns:

Raises:

  • (ArgumentError)


231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/grafana/datasource.rb', line 231

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

  type        = validate( params, required: true , var: 'type', type: String )
  name        = validate( params, required: true , var: 'name', type: String )
  access      = validate( params, required: false, var: 'access', type: String ) || 'proxy'
  default     = validate( params, required: false, var: 'default', type: Boolean ) || false
  url         = validate( params, required: true , var: 'url', type: String )
  json_data   = validate( params, required: false, var: 'json_data', type: Hash )
  json_secure = validate( params, required: false, var: 'json_secure', type: Hash )
  ba_user     = validate( params, required: false, var: 'basic_user', type: String )
  ba_password = validate( params, required: false, var: 'basic_password', type: String )

  basic_auth  = false
  basic_auth  = true unless( ba_user.nil? && ba_password.nil? )

  valid_types = %w[grafana graphite cloudwatch elasticsearch prometheus influxdb mysql opentsdb postgres]

  raise ArgumentError.new(format('wrong datasource type. only %s allowed, given \%s\'', valid_types.join(', '), type)) if( valid_types.include?(type.downcase) == false )

  payload = {
    isDefault: default,
    basicAuth: basic_auth,
    basicAuthUser: ba_user,
    basicAuthPassword: ba_password,
    name: name,
    type: type,
    url: url,
    access: access,
    jsonData: json_data,
    secureJsonData: json_secure
  }
  payload.reject!{ |_k, v| v.nil? }

  endpoint = '/api/datasources'
  post(endpoint, payload.to_json)
end

#datasource(datasource_id) ⇒ Hash

Get a single datasources by Id or Name

Examples:

datasource( 1 )
datasource( 'foo' )

Parameters:

  • datasource_id (Mixed)

    Datasource Name (String) or Datasource Id (Integer)

Returns:

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/grafana/datasource.rb', line 45

def datasource( datasource_id )

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

  if(datasource_id.is_a?(String))
    data = datasources.select { |_k,v| v['name'] == datasource_id }
    datasource_id = data.keys.first if( data )
  end

  return { 'status' => 404, 'message' => format( 'No Datasource \'%s\' found', datasource_id) } if( datasource_id.nil? )

  raise format('DataSource Id can not be 0') if( datasource_id.zero? )

  endpoint = format('/api/datasources/%d', datasource_id )

  @logger.debug("Attempting to get existing data source Id #{datasource_id} (GET #{endpoint})") if  @debug

  get(endpoint)
end

#datasourcesHash

Get all datasources

Examples:

datasources

Returns:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/grafana/datasource.rb', line 15

def datasources

  endpoint = '/api/datasources'

  @logger.debug("Attempting to get all existing data sources (GET #{endpoint})") if @debug

  datasources = get( endpoint )

  return { 'status' => 404, 'message' => 'No Datasources found' } if( datasources.nil? || datasources == false || datasources.dig('status').to_i != 200 )

  datasources = datasources.dig('message')

  datasource_map = {}
  datasources.each do |ds|
    datasource_map[ds['id']] = ds
  end

  datasource_map
end

#delete_datasource(datasource_id) ⇒ Hash

Delete an existing data source by id

Examples:

delete_datasource( 1 )
delete_datasource( 'foo' )

Parameters:

  • datasource_id (Mixed)

    Datasource Name (String) or Datasource Id (Integer) for delete Datasource

Returns:

Raises:

  • (ArgumentError)


281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/grafana/datasource.rb', line 281

def delete_datasource( datasource_id )

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

  if(datasource_id.is_a?(String))
    data = datasources.select { |_k,v| v['name'] == datasource_id }
    datasource_id = data.keys.first if( data )
  end

  return { 'status' => 404, 'message' => format( 'No Datasource \'%s\' found', datasource_id) } if( datasource_id.nil? )

  raise format('Data Source Id can not be 0') if( datasource_id.zero? )

  endpoint = format('/api/datasources/%d', datasource_id)
  logger.debug("Deleting data source Id #{datasource_id} (DELETE #{endpoint})") if @debug

  delete(endpoint)
end

#update_datasource(params) ⇒ Hash

Update an existing data source

merge an current existing datasource configuration with the new values

Examples:

params = {
  name: 'graphite',
  new_name: 'influx',
  organisation: 'Main Org.',
  type: 'influxdb',
  url: 'http://localhost:8090'
}
update_datasource( params )

Parameters:

Options Hash (params):

  • name (Mixed)

    Name or Id of the current existing Datasource (required)

  • organisation (Mixed)

    Name or Id of an existing Organisation

  • type (String)

    Datasource Type - (required) (grafana graphite cloudwatch elasticsearch prometheus influxdb mysql opentsdb postgres)

  • new_name (String)

    New Datasource Name

  • database (String)

    Datasource Database

  • access (String) — default: proxy

    Acess Type

  • default (Boolean) — default: false
  • user (String)
  • password (String)
  • url (String)

    Datasource URL

  • json_data (Hash)
  • basic_user (String)
  • basic_password (String)

Returns:

Raises:

  • (ArgumentError)


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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/grafana/datasource.rb', line 105

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

  name         = validate( params, required: true , var: 'name' )
  organisation = validate( params, required: false, var: 'organisation' )
  type         = validate( params, required: false, var: 'type', type: String )
  new_name     = validate( params, required: false, var: 'new_name', type: String )
  database     = validate( params, required: false, var: 'database', type: String )
  access       = validate( params, required: false, var: 'access', type: String ) || 'proxy'
  default      = validate( params, required: false, var: 'default', type: Boolean ) || false
  user         = validate( params, required: false, var: 'user', type: String )
  password     = validate( params, required: false, var: 'password', type: String )
  url          = validate( params, required: false, var: 'url', type: String )
  json_data    = validate( params, required: false, var: 'json_data', type: Hash )
  ba_user      = validate( params, required: false, var: 'basic_user', type: String )
  ba_password  = validate( params, required: false, var: 'basic_password', type: String )
  basic_auth   = false
  basic_auth   = true unless( ba_user.nil? && ba_password.nil? )
  org_id       = nil

  if( name.is_a?(String) && name.is_a?(Integer) )
    raise ArgumentError.new(format('wrong type. \'name\' must be an String (for an Datasource name) or an Integer (for an Datasource Id), given \'%s\'', name.class.to_s))
  end
  if( organisation )
    if( organisation.is_a?(String) && organisation.is_a?(Integer) )
      raise ArgumentError.new(format('wrong type. \'organisation\' must be an String (for an Organisation name) or an Integer (for an Organisation Id), given \'%s\'', organisation.class.to_s))
    end

    org    = organization( organisation )
    org_id = org.dig('id')

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

  existing_ds = datasource(name)
  existing_ds.reject! { |x| x == 'status' }
  existing_ds = existing_ds.deep_string_keys
  datasource_id = existing_ds.dig('id')

  return { 'status' => 404, 'message' => format('No Datasource \'%s\' found', name) } if( datasource_id.nil? )

  raise format('Data Source Id can not be 0') if( datasource_id.zero? )

  unless( type.nil? )
    valid_types = %w[grafana graphite cloudwatch elasticsearch prometheus influxdb mysql opentsdb postgres]
    raise ArgumentError.new(format('wrong datasource type. only %s allowed, given \%s\'', valid_types.join(', '), type)) if( valid_types.include?(type.downcase) == false )
  end

  data = {
    id: datasource_id,
    orgId: org_id,
    name: new_name,
    type: type,
    access: access,
    url: url,
    password: password,
    user: user,
    database: database,
    basicAuth: basic_auth,
    basicAuthUser: ba_user,
    basicAuthPassword: ba_user,
    isDefault: default,
    jsonData: json_data
  }
  data.reject!{ |_k, v| v.nil? }

  payload = data.deep_string_keys
  payload = existing_ds.merge(payload).deep_symbolize_keys

  endpoint = format('/api/datasources/%d', datasource_id )

  @logger.debug("Updating data source Id #{datasource_id} (GET #{endpoint})") if  @debug
#       logger.debug(payload.to_json) if(@debug)

  put( endpoint, payload.to_json )
end