grafana 0.8.2 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,7 +13,6 @@ module Grafana
13
13
  get( endpoint )
14
14
  end
15
15
 
16
-
17
16
  # Get a single data sources by Id or Name
18
17
  #
19
18
  # @example
@@ -35,28 +34,6 @@ module Grafana
35
34
  get(endpoint)
36
35
  end
37
36
 
38
- # # Get Organisation by Id
39
- # # GET /api/orgs/:orgId
40
- # def organization_by_id( id )
41
- #
42
- # raise ArgumentError.new('id must be an Integer') unless( id.is_a?(Integer) )
43
- #
44
- # endpoint = format( '/api/orgs/%d', id )
45
- # @logger.debug("Get Organisation by Id (GET #{endpoint}})") if @debug
46
- # get( endpoint )
47
- # end
48
- #
49
- # # Get Organisation by Name
50
- # # GET /api/orgs/name/:orgName
51
- # def organization_by_name( name )
52
- #
53
- # raise ArgumentError.new('name must be an String') unless( name.is_a?(String) )
54
- #
55
- # endpoint = format( '/api/orgs/name/%s', URI.escape( name ) )
56
- # @logger.debug("Get Organisation by Name (GET #{endpoint})") if @debug
57
- # get( endpoint )
58
- # end
59
-
60
37
  # Update Organisation
61
38
  #
62
39
  # fields Adress 1, Adress 2, City are not implemented yet.
@@ -78,30 +55,25 @@ module Grafana
78
55
  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
79
56
  raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
80
57
 
81
- organization = validate( params, required: true, var: 'organization', type: String )
82
- name = validate( params, required: true, var: 'name', type: String )
58
+ organization = validate( params, required: true, var: 'organization', type: String )
59
+ name = validate( params, required: true, var: 'name', type: String )
60
+ org = organization( organization )
83
61
 
84
- org = organization( organization )
62
+ return { 'status' => 404, 'message' => format('Organization \'%s\' not found', organization) } if( org.nil? || org.dig('status').to_i != 200 )
85
63
 
86
- if( org.nil? || org.dig('status').to_i != 200 )
87
- return {
88
- 'status' => 404,
89
- 'message' => format('Organization \'%s\' not found', organization)
90
- }
91
- end
92
- org_id = org.dig('id')
64
+ organization_id = org.dig('id')
93
65
 
66
+ endpoint = format( '/api/orgs/%s', organization_id )
94
67
  payload = { name: name }
95
68
 
96
- endpoint = format( '/api/orgs/%s', org_id )
97
- @logger.debug("Update Organisation id #{org_id} (PUT #{endpoint})") if @debug
69
+ @logger.debug("Update Organisation id #{organization_id} (PUT #{endpoint})") if @debug
98
70
 
99
71
  put( endpoint, payload.to_json )
100
72
  end
101
73
 
102
74
  # Get Users in Organisation
103
75
  #
104
- # @param [Mixed] user_id Username (String) or Userid (Integer) for delete User
76
+ # @param [Mixed] organization_id Organistaion Name (String) or Organistaion Id (Integer)
105
77
  #
106
78
  # @example
107
79
  # organization_users( 1 )
@@ -109,36 +81,32 @@ module Grafana
109
81
  #
110
82
  # @return [Hash]
111
83
  #
112
- def organization_users( org_id )
84
+ def organization_users( organization_id )
113
85
 
114
- raise ArgumentError.new(format('wrong type. user \'org_id\' must be an String (for an Organisation name) or an Integer (for an Organisation Id), given \'%s\'', org_id.class.to_s)) if( org_id.is_a?(String) && org_id.is_a?(Integer) )
115
- raise ArgumentError.new('missing \'org_id\'') if( org_id.size.zero? )
86
+ raise ArgumentError.new(format('wrong type. user \'organization_id\' must be an String (for an Organisation name) or an Integer (for an Organisation Id), given \'%s\'', organization_id.class.to_s)) if( organization_id.is_a?(String) && organization_id.is_a?(Integer) )
87
+ raise ArgumentError.new('missing \'organization_id\'') if( organization_id.size.zero? )
116
88
 
117
- if(org_id.is_a?(String))
118
- org = organization(org_id)
119
- if( org.nil? || org.dig('status').to_i != 200 )
120
- return {
121
- 'status' => 404,
122
- 'message' => format('Organization \'%s\' not found', organization)
123
- }
124
- end
125
- org_id = org.dig('id')
89
+ if(organization_id.is_a?(String))
90
+ org = organization(organization_id)
91
+ return { 'status' => 404, 'message' => format('Organization \'%s\' not found', organization) } if( org.nil? || org.dig('status').to_i != 200 )
92
+
93
+ organization_id = org.dig('id')
126
94
  end
127
95
 
128
- endpoint = format( '/api/orgs/%s/users', org_id )
96
+ endpoint = format( '/api/orgs/%s/users', organization_id )
129
97
 
130
- @logger.debug("Getting users in Organisation id #{org_id} (GET #{endpoint})") if @debug
98
+ @logger.debug("Getting users in Organisation id #{organization_id} (GET #{endpoint})") if @debug
131
99
  get(endpoint)
132
100
  end
133
101
 
134
102
  # Add User in Organisation
135
103
  #
136
- # @param
104
+ # @param [Hash] params
137
105
  # @option params [String] organization Organisation name
138
106
  # @option params [String] login_or_email Login or email
139
107
  # @option params [String] role Name of the Role - only 'Viewer', 'Editor', 'Read Only Editor' or 'Admin' allowed
140
108
  #
141
- # @examle
109
+ # @example
142
110
  # params = {
143
111
  # organization: 'Foo',
144
112
  # login_or_email: 'foo@foo-bar.tld',
@@ -150,54 +118,25 @@ module Grafana
150
118
  #
151
119
  def add_user_to_organization( params )
152
120
 
153
- raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
154
- raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
155
-
156
- organization = validate( params, required: true, var: 'organization', type: String )
157
- login_or_email = validate( params, required: true, var: 'login_or_email', type: String )
158
- role = validate( params, required: true, var: 'role', type: String )
159
- valid_roles = ['Viewer', 'Editor', 'Read Only Editor', 'Admin']
160
-
161
- # https://stackoverflow.com/questions/9333952/case-insensitive-arrayinclude?answertab=votes#tab-top
162
- # Do this once, or each time the array changes
163
- downcased = Set.new valid_roles.map(&:downcase)
164
- unless( downcased.include?( role.downcase ) )
121
+ data = validate_organisation_user( params )
122
+ status = data.dig('status')
165
123
 
166
- message = format( 'wrong role. Role must be one of %s, given \'%s\'', valid_roles.join(', '), role )
124
+ return data if( status.nil? || status.to_i == 404 )
167
125
 
168
- return {
169
- 'status' => 404,
170
- 'login_or_email' => login_or_email,
171
- 'role' => role,
172
- 'message' => message
173
- }
174
- end
126
+ org = data.dig('organisation')
127
+ usr = data.dig('user')
175
128
 
176
- org = organization( organization )
177
- usr = user_by_name( login_or_email )
178
-
179
- if( org.nil? || org.dig('status').to_i != 200 )
180
- return {
181
- 'status' => 404,
182
- 'message' => format('Organization \'%s\' not found', organization)
183
- }
184
- end
185
-
186
- if( usr.nil? || usr.dig('status').to_i != 200 )
187
- return {
188
- 'status' => 404,
189
- 'message' => format('User \'%s\' not found', login_or_email)
190
- }
191
- end
192
-
193
- org_id = org.dig('id')
129
+ organization_id = org.dig('id')
130
+ organization = org.dig('name')
131
+ login_or_email = usr.dig('email')
132
+ role = data.dig('role')
194
133
 
134
+ endpoint = format( '/api/orgs/%d/users', organization_id )
195
135
  payload = {
196
136
  loginOrEmail: login_or_email,
197
137
  role: role
198
138
  }
199
139
 
200
- endpoint = format( '/api/orgs/%d/users', org_id )
201
140
  @logger.debug("Adding user '#{login_or_email}' to organisation '#{organization}' (POST #{endpoint})") if @debug
202
141
 
203
142
  post( endpoint, payload.to_json )
@@ -205,12 +144,12 @@ module Grafana
205
144
 
206
145
  # Update Users in Organisation
207
146
  #
208
- # @param
147
+ # @param [Hash] params
209
148
  # @option params [String] organization Organisation name
210
149
  # @option params [String] login_or_email Login or email
211
150
  # @option params [String] role Name of the Role - only 'Viewer', 'Editor', 'Read Only Editor' or 'Admin' allowed
212
151
  #
213
- # @examle
152
+ # @example
214
153
  # params = {
215
154
  # organization: 'Foo',
216
155
  # login_or_email: 'foo@foo-bar.tld',
@@ -222,67 +161,36 @@ module Grafana
222
161
  #
223
162
  def update_organization_user( params )
224
163
 
225
- raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
226
- raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
164
+ data = validate_organisation_user( params )
165
+ status = data.dig('status')
227
166
 
228
- organization = validate( params, required: true, var: 'organization', type: String )
229
- login_or_email = validate( params, required: true, var: 'login_or_email', type: String )
230
- role = validate( params, required: true, var: 'role', type: String )
231
- valid_roles = ['Viewer', 'Editor', 'Read Only Editor', 'Admin']
167
+ return data if( status.nil? || status.to_i == 404 )
232
168
 
233
- # https://stackoverflow.com/questions/9333952/case-insensitive-arrayinclude?answertab=votes#tab-top
234
- # Do this once, or each time the array changes
235
- downcased = Set.new valid_roles.map(&:downcase)
236
- unless( downcased.include?( role.downcase ) )
237
-
238
- message = format( 'wrong role. Role must be one of %s, given \'%s\'', valid_roles.join(', '), role )
239
-
240
- return {
241
- 'status' => 404,
242
- 'login_or_email' => login_or_email,
243
- 'role' => role,
244
- 'message' => message
245
- }
246
- end
247
-
248
- org = organization( organization )
249
- usr = user_by_name( login_or_email )
169
+ org = data.dig('organisation')
170
+ usr = data.dig('user')
250
171
 
251
- if( org.nil? || org.dig('status').to_i != 200 )
252
- return {
253
- 'status' => 404,
254
- 'message' => format('Organization \'%s\' not found', organization)
255
- }
256
- end
257
-
258
- if( usr.nil? || usr.dig('status').to_i != 200 )
259
- return {
260
- 'status' => 404,
261
- 'message' => format('User \'%s\' not found', login_or_email)
262
- }
263
- end
264
-
265
- org_id = org.dig('id')
172
+ organization_id = org.dig('id')
173
+ organization = org.dig('name')
266
174
  usr_id = usr.dig('id')
175
+ login_or_email = usr.dig('name')
176
+ role = data.dig(:role)
267
177
 
178
+ endpoint = format( '/api/orgs/%d/users/%d', organization_id, usr_id )
268
179
  payload = {
269
180
  role: role
270
181
  }
271
182
 
272
- endpoint = format( '/api/orgs/%d/users/%d', org_id, usr_id )
273
-
274
183
  @logger.debug("Updating user '#{login_or_email}' in organization '#{organization}' (PATCH #{endpoint})") if @debug
275
184
  patch( endpoint, payload.to_json )
276
185
  end
277
186
 
278
187
  # Delete User in Organisation
279
188
  #
280
- # @param
189
+ # @param [Hash] params
281
190
  # @option params [String] organization Organisation name
282
191
  # @option params [String] login_or_email Login or email
283
- # @option params [String] role Name of the Role - only 'Viewer', 'Editor', 'Read Only Editor' or 'Admin' allowed
284
192
  #
285
- # @examle
193
+ # @example
286
194
  # params = {
287
195
  # organization: 'Foo',
288
196
  # login_or_email: 'foo@foo-bar.tld'
@@ -291,7 +199,6 @@ module Grafana
291
199
  #
292
200
  # @return [Hash]
293
201
  #
294
- # DELETE /api/orgs/:orgId/users/:userId
295
202
  def delete_user_from_organization( params )
296
203
 
297
204
  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
@@ -301,26 +208,15 @@ module Grafana
301
208
  login_or_email = validate( params, required: true, var: 'login_or_email', type: String )
302
209
 
303
210
  org = organization( organization )
304
- usr = user_by_name( login_or_email )
211
+ usr = user( login_or_email )
305
212
 
306
- if( org.nil? || org.dig('status').to_i != 200 )
307
- return {
308
- 'status' => 404,
309
- 'message' => format('Organization \'%s\' not found', organization)
310
- }
311
- end
213
+ return { 'status' => 404, 'message' => format('Organization \'%s\' not found', organization) } if( org.nil? || org.dig('status').to_i != 200 )
214
+ return { 'status' => 404, 'message' => format('User \'%s\' not found', login_or_email) } if( usr.nil? || usr.dig('status').to_i != 200 )
312
215
 
313
- if( usr.nil? || usr.dig('status').to_i != 200 )
314
- return {
315
- 'status' => 404,
316
- 'message' => format('User \'%s\' not found', login_or_email)
317
- }
318
- end
319
-
320
- org_id = org.dig('id')
216
+ organization_id = org.dig('id')
321
217
  usr_id = usr.dig('id')
322
218
 
323
- endpoint = format( '/api/orgs/%d/users/%d', org_id, usr_id )
219
+ endpoint = format( '/api/orgs/%d/users/%d', organization_id, usr_id )
324
220
 
325
221
  @logger.debug("Deleting user '#{login_or_email}' in organization '#{organization}' (DELETE #{endpoint})") if @debug
326
222
  delete(endpoint)
@@ -328,10 +224,10 @@ module Grafana
328
224
 
329
225
  # Create Organisation
330
226
  #
331
- # @param
227
+ # @param [Hash] params
332
228
  # @option params [String] organization Organisation name
333
229
  #
334
- # @examle
230
+ # @example
335
231
  # params = {
336
232
  # name: 'Foo'
337
233
  # }
@@ -345,15 +241,9 @@ module Grafana
345
241
  raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
346
242
 
347
243
  name = validate( params, required: true, var: 'name', type: String )
244
+ org = organization( name )
348
245
 
349
- org = organization( name )
350
-
351
- if( org.nil? || org.dig('status').to_i == 200 )
352
- return {
353
- 'status' => 409,
354
- 'message' => format('Organisation \'%s\' already exists', name )
355
- }
356
- end
246
+ return { 'status' => 409, 'message' => format('Organisation \'%s\' already exists', name ) } if( org.nil? || org.dig('status').to_i == 200 )
357
247
 
358
248
  endpoint = '/api/orgs'
359
249
  payload = {
@@ -382,25 +272,70 @@ module Grafana
382
272
  if(organisation_id.is_a?(String))
383
273
  data = organizations.dig('message')
384
274
  organisation_map = {}
385
- data.each do |ds|
386
- organisation_map[ds['id']] = ds
275
+ data.each do |d|
276
+ organisation_map[d.dig('id')] = d.dig('name')
387
277
  end
388
- organisation_map.select { |_k,v| v['name'] == organisation_id }
389
- organisation_id = organisation_map.keys.first if( data )
278
+ organisation_id = organisation_map.select { |x,y| y == organisation_id }.keys.first if( organisation_map )
390
279
  end
391
280
 
392
- if( organisation_id.nil? )
281
+ return { 'status' => 404, 'message' => format( 'No Organisation \'%s\' found', organisation_id) } if( organisation_id.nil? )
282
+
283
+ endpoint = format( '/api/orgs/%d', organisation_id )
284
+ @logger.debug("Deleting organization #{organisation_id} (DELETE #{endpoint})") if @debug
285
+
286
+ delete(endpoint)
287
+ end
288
+
289
+
290
+ private
291
+ # validate an user for an organisation
292
+ #
293
+ # @example
294
+ # params = {
295
+ # organization: 'Foo',
296
+ # login_or_email: 'foo@foo-bar.tld',
297
+ # role: 'Viewer'
298
+ # }
299
+ # validate_organisation_user( params )
300
+ #
301
+ # @return [Hash]
302
+ #
303
+ def validate_organisation_user( params )
304
+
305
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
306
+ raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
307
+
308
+ organization = validate( params, required: true, var: 'organization', type: String )
309
+ login_or_email = validate( params, required: true, var: 'login_or_email', type: String )
310
+ role = validate( params, required: true, var: 'role', type: String )
311
+ valid_roles = ['Viewer', 'Editor', 'Read Only Editor', 'Admin']
312
+
313
+ # https://stackoverflow.com/questions/9333952/case-insensitive-arrayinclude?answertab=votes#tab-top
314
+ # Do this once, or each time the array changes
315
+ downcased = Set.new valid_roles.map(&:downcase)
316
+ unless( downcased.include?( role.downcase ) )
393
317
  return {
394
318
  'status' => 404,
395
- 'message' => format( 'No Organisation \'%s\' found', organisation_id)
319
+ 'login_or_email' => login_or_email,
320
+ 'role' => role,
321
+ 'message' => format( 'wrong role. Role must be one of %s, given \'%s\'', valid_roles.join(', '), role )
396
322
  }
397
323
  end
398
324
 
399
- endpoint = format( '/api/orgs/%d', organisation_id )
400
- @logger.debug("Deleting organization #{organisation_id} (DELETE #{endpoint})") if @debug
325
+ org = organization( organization )
326
+ usr = user( login_or_email )
401
327
 
402
- delete(endpoint)
328
+ return { 'status' => 404, 'message' => format('Organization \'%s\' not found', organization) } if( org.nil? || org.dig('status').to_i != 200 )
329
+ return { 'status' => 404, 'message' => format('User \'%s\' not found', login_or_email) } if( usr.nil? || usr.dig('status').to_i != 200 )
330
+
331
+ {
332
+ 'status' => 200,
333
+ 'organisation' => org,
334
+ 'user' => usr,
335
+ 'role' => role
336
+ }
403
337
  end
404
338
 
339
+
405
340
  end
406
341
  end
@@ -9,12 +9,10 @@ module Grafana
9
9
  def expand_tags( params )
10
10
 
11
11
  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
12
+ raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
12
13
 
13
- dashboard = params.dig(:dashboard)
14
- additional_tags = params.dig(:additional_tags) || []
15
-
16
- raise ArgumentError.new(format('wrong type. \'dashboard\' must be an Hash, given \'%s\'', dashboard.class.to_s)) unless( dashboard.is_a?(Hash) )
17
- raise ArgumentError.new(format('wrong type. \'additional_tags\' must be an Array, given \'%s\'', additional_tags.class.to_s)) unless( additional_tags.is_a?(Array) )
14
+ dashboard = validate( params, required: true, var: 'dashboard', type: Hash )
15
+ additional_tags = validate( params, required: true, var: 'additional_tags', type: Array )
18
16
 
19
17
  # add tags
20
18
  # dashboard = JSON.parse( dashboard ) if( dashboard.is_a?( String ) )
@@ -24,12 +22,9 @@ module Grafana
24
22
  current_tags = dashboard.dig( 'dashboard', 'tags' )
25
23
 
26
24
  if( !current_tags.nil? && additional_tags.count > 0 )
27
-
28
25
  current_tags << additional_tags
29
-
30
26
  current_tags.flatten!
31
27
  current_tags.sort!
32
-
33
28
  dashboard['dashboard']['tags'] = current_tags
34
29
  end
35
30