grafana 0.8.2 → 0.8.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/doc/Array.html +1 -1
- data/doc/Boolean.html +1 -1
- data/doc/FalseClass.html +1 -1
- data/doc/Grafana.html +1 -1
- data/doc/Hash.html +1 -1
- data/doc/Logging.html +1 -1
- data/doc/Object.html +1 -1
- data/doc/Time.html +1 -1
- data/doc/TrueClass.html +1 -1
- data/doc/_index.html +1 -1
- data/doc/file.README.html +1 -1
- data/doc/index.html +1 -1
- data/doc/method_list.html +33 -57
- data/doc/top-level-namespace.html +1 -1
- data/lib/grafana/admin.rb +37 -64
- data/lib/grafana/client.rb +0 -1
- data/lib/grafana/dashboard.rb +88 -37
- data/lib/grafana/datasource.rb +79 -31
- data/lib/grafana/network.rb +15 -3
- data/lib/grafana/organization.rb +64 -34
- data/lib/grafana/organizations.rb +107 -172
- data/lib/grafana/tags.rb +3 -8
- data/lib/grafana/tools.rb +2 -3
- data/lib/grafana/user.rb +80 -45
- data/lib/grafana/users.rb +97 -54
- data/lib/grafana/version.rb +1 -1
- metadata +1 -1
data/lib/grafana/client.rb
CHANGED
@@ -107,7 +107,6 @@ module Grafana
|
|
107
107
|
raise ArgumentError.new(format('wrong type. \'open_timeout\' must be an Integer, given \'%s\'', @open_timeout.class.to_s)) unless( @open_timeout.is_a?(Integer) )
|
108
108
|
|
109
109
|
protocoll = ssl == true ? 'https' : 'http'
|
110
|
-
raise ArgumentError.new(format('wrong \'protocoll\'. only \'http\' or \'https\' allowed, given \'%s\'', protocoll)) if( %w[http https].include?(protocoll.downcase) == false )
|
111
110
|
|
112
111
|
@url = format( '%s://%s:%d%s', protocoll, host, port, url_path )
|
113
112
|
end
|
data/lib/grafana/dashboard.rb
CHANGED
@@ -7,14 +7,17 @@ module Grafana
|
|
7
7
|
|
8
8
|
|
9
9
|
# Get dashboard
|
10
|
-
#
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# dashboard('dashboard for many foo')
|
13
|
+
#
|
14
|
+
# @return [String]
|
15
|
+
#
|
11
16
|
def dashboard( name )
|
12
17
|
|
13
18
|
raise ArgumentError.new(format('wrong type. \'name\' must be an String, given \'%s\'', name.class.to_s)) unless( name.is_a?(String) )
|
14
19
|
raise ArgumentError.new('missing name') if( name.size.zero? )
|
15
20
|
|
16
|
-
# raise ArgumentError.new('name must be an String') unless( name.is_a?(String) )
|
17
|
-
|
18
21
|
endpoint = format( '/api/dashboards/db/%s', slug(name) )
|
19
22
|
|
20
23
|
@logger.debug( "Attempting to get dashboard (GET /api/dashboards/db/#{name})" ) if @debug
|
@@ -23,38 +26,70 @@ module Grafana
|
|
23
26
|
end
|
24
27
|
|
25
28
|
# Create / Update dashboard
|
29
|
+
#
|
30
|
+
# @param [Hash] params
|
31
|
+
# @option params [Hash] dashboard
|
32
|
+
# @option params [Boolean] overwrite (true)
|
33
|
+
#
|
34
|
+
# @example
|
35
|
+
# params = {
|
36
|
+
# dashboard: {
|
37
|
+
# id: null,
|
38
|
+
# title: 'Production Overview',
|
39
|
+
# tags: [ 'templated' ],
|
40
|
+
# timezone": 'browser',
|
41
|
+
# rows: [
|
42
|
+
# {
|
43
|
+
# }
|
44
|
+
# ],
|
45
|
+
# 'schemaVersion': 6,
|
46
|
+
# 'version': 0
|
47
|
+
# },
|
48
|
+
# overwrite: false
|
49
|
+
# }
|
50
|
+
# create_dashboard( params )
|
51
|
+
#
|
52
|
+
# @return [Hash]
|
53
|
+
#
|
26
54
|
# POST /api/dashboards/db
|
27
55
|
def create_dashboard( params )
|
28
56
|
|
29
|
-
raise ArgumentError.new(format('wrong type. params must be an Hash, given %s', params.class.to_s
|
57
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
58
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
59
|
+
|
60
|
+
dashboard = validate( params, required: true, var: 'dashboard', type: Hash )
|
61
|
+
overwrite = validate( params, required: false, var: 'overwrite', type: Boolean ) || true
|
30
62
|
|
31
|
-
|
32
|
-
dashboard = params.dig(:dashboard)
|
63
|
+
dashboard = regenerate_template_ids( dashboard )
|
33
64
|
|
34
|
-
|
35
|
-
|
36
|
-
raise ArgumentError.new(format('wrong type. dashboard must be an Hash, given %s', dashboard.class.to_s ) ) unless( dashboard.is_a?(Hash) )
|
65
|
+
db = JSON.parse( dashboard ) if( dashboard.is_a?(String) )
|
66
|
+
title = db.dig('dashboard','title')
|
37
67
|
|
38
68
|
endpoint = '/api/dashboards/db'
|
39
|
-
# title = slug(title)
|
40
69
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
title = db.dig('dashboard','title')
|
47
|
-
end
|
70
|
+
payload = {
|
71
|
+
dashboard: db.dig('dashboard'),
|
72
|
+
overwrite: overwrite
|
73
|
+
}
|
74
|
+
payload.reject!{ |_k, v| v.nil? }
|
48
75
|
|
49
76
|
@logger.debug("Creating dashboard: #{title} (POST /api/dashboards/db)") if @debug
|
50
77
|
|
51
|
-
post( endpoint,
|
78
|
+
post( endpoint, payload.to_json )
|
52
79
|
end
|
53
80
|
|
54
81
|
# Delete dashboard
|
55
|
-
#
|
82
|
+
#
|
83
|
+
# @example
|
84
|
+
# delete_dashboard('dashboard for many foo')
|
85
|
+
#
|
86
|
+
# @return [Hash]
|
87
|
+
#
|
56
88
|
def delete_dashboard( name )
|
57
89
|
|
90
|
+
raise ArgumentError.new(format('wrong type. \'name\' must be an String, given \'%s\'', name.class.to_s)) unless( name.is_a?(String) )
|
91
|
+
raise ArgumentError.new('missing name') if( name.size.zero? )
|
92
|
+
|
58
93
|
endpoint = format( '/api/dashboards/db/%s', slug(name) )
|
59
94
|
|
60
95
|
@logger.debug("Deleting dashboard #{slug(name)} (DELETE #{endpoint})") if @debug
|
@@ -63,7 +98,12 @@ module Grafana
|
|
63
98
|
end
|
64
99
|
|
65
100
|
# Gets the home dashboard
|
66
|
-
#
|
101
|
+
#
|
102
|
+
# @example
|
103
|
+
# home_dashboard
|
104
|
+
#
|
105
|
+
# @return [Hash]
|
106
|
+
#
|
67
107
|
def home_dashboard
|
68
108
|
|
69
109
|
endpoint = '/api/dashboards/home'
|
@@ -74,7 +114,12 @@ module Grafana
|
|
74
114
|
end
|
75
115
|
|
76
116
|
# Tags for Dashboard
|
77
|
-
#
|
117
|
+
#
|
118
|
+
# @example
|
119
|
+
# dashboard_tags
|
120
|
+
#
|
121
|
+
# @return [Hash]
|
122
|
+
#
|
78
123
|
def dashboard_tags
|
79
124
|
|
80
125
|
endpoint = '/api/dashboards/tags'
|
@@ -85,29 +130,30 @@ module Grafana
|
|
85
130
|
end
|
86
131
|
|
87
132
|
# Search Dashboards
|
88
|
-
# GET /api/search/
|
89
133
|
#
|
90
|
-
#
|
91
|
-
# searchDashboards(
|
92
|
-
# searchDashboards(
|
93
|
-
# searchDashboards(
|
94
|
-
# searchDashboards(
|
95
|
-
|
134
|
+
# @example
|
135
|
+
# searchDashboards( tags: host )
|
136
|
+
# searchDashboards( tags: [ host, 'tag1' ] )
|
137
|
+
# searchDashboards( tags: [ 'tag2' ] )
|
138
|
+
# searchDashboards( query: title )
|
139
|
+
# searchDashboards( starred: true )
|
140
|
+
#
|
141
|
+
# @return [Hash]
|
142
|
+
#
|
143
|
+
def search_dashboards( params )
|
96
144
|
|
97
145
|
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
98
146
|
|
99
|
-
query = params
|
100
|
-
starred = params
|
101
|
-
tags = params
|
102
|
-
api = []
|
147
|
+
query = validate( params, required: false, var: 'query', type: String )
|
148
|
+
starred = validate( params, required: false, var: 'starred', type: Boolean )
|
149
|
+
tags = validate( params, required: false, var: 'tags' )
|
103
150
|
|
151
|
+
api = []
|
104
152
|
api << format( 'query=%s', CGI.escape( query ) ) unless query.nil?
|
105
153
|
api << format( 'starred=%s', starred ? 'true' : 'false' ) unless( starred.nil? )
|
106
154
|
|
107
155
|
unless( tags.nil? )
|
108
|
-
|
109
156
|
tags = tags.join( '&tag=' ) if( tags.is_a?( Array ) )
|
110
|
-
|
111
157
|
api << format( 'tag=%s', tags )
|
112
158
|
end
|
113
159
|
|
@@ -120,7 +166,13 @@ module Grafana
|
|
120
166
|
get( endpoint )
|
121
167
|
end
|
122
168
|
|
123
|
-
|
169
|
+
# import Dashboards from directory
|
170
|
+
#
|
171
|
+
# @example
|
172
|
+
# import_dashboards_from_directory( '/tmp/dashboards' )
|
173
|
+
#
|
174
|
+
# @return [Hash]
|
175
|
+
#
|
124
176
|
def import_dashboards_from_directory( directory )
|
125
177
|
|
126
178
|
raise ArgumentError.new('directory must be an String') unless( directory.is_a?(String) )
|
@@ -135,10 +187,9 @@ module Grafana
|
|
135
187
|
|
136
188
|
dashboard = File.read( f )
|
137
189
|
dashboard = JSON.parse( dashboard )
|
138
|
-
title = dashboard.dig('dashboard','title') || f
|
139
190
|
|
140
191
|
result[f.to_s] ||= {}
|
141
|
-
result[f.to_s] = create_dashboard(
|
192
|
+
result[f.to_s] = create_dashboard( dashboard: dashboard )
|
142
193
|
end
|
143
194
|
|
144
195
|
result
|
data/lib/grafana/datasource.rb
CHANGED
@@ -20,12 +20,7 @@ module Grafana
|
|
20
20
|
|
21
21
|
datasources = get( endpoint )
|
22
22
|
|
23
|
-
if
|
24
|
-
return {
|
25
|
-
'status' => 404,
|
26
|
-
'message' => 'No Datasources found'
|
27
|
-
}
|
28
|
-
end
|
23
|
+
return { 'status' => 404, 'message' => 'No Datasources found' } if( datasources.nil? || datasources == false || datasources.dig('status').to_i != 200 )
|
29
24
|
|
30
25
|
datasources = datasources.dig('message')
|
31
26
|
|
@@ -39,6 +34,8 @@ module Grafana
|
|
39
34
|
|
40
35
|
# Get a single datasources by Id or Name
|
41
36
|
#
|
37
|
+
# @param [Mixed] datasource_id Datasource Name (String) or Datasource Id (Integer)
|
38
|
+
#
|
42
39
|
# @example
|
43
40
|
# datasource( 1 )
|
44
41
|
# datasource( 'foo' )
|
@@ -55,12 +52,7 @@ module Grafana
|
|
55
52
|
datasource_id = data.keys.first if( data )
|
56
53
|
end
|
57
54
|
|
58
|
-
if( datasource_id.nil? )
|
59
|
-
return {
|
60
|
-
'status' => 404,
|
61
|
-
'message' => format( 'No Datasource \'%s\' found', datasource_id)
|
62
|
-
}
|
63
|
-
end
|
55
|
+
return { 'status' => 404, 'message' => format( 'No Datasource \'%s\' found', datasource_id) } if( datasource_id.nil? )
|
64
56
|
|
65
57
|
raise format('DataSource Id can not be 0') if( datasource_id.zero? )
|
66
58
|
|
@@ -82,14 +74,29 @@ module Grafana
|
|
82
74
|
# merge an current existing datasource configuration with the new values
|
83
75
|
#
|
84
76
|
# @param [Hash] params
|
85
|
-
# @option params [
|
86
|
-
# @option params [Mixed]
|
77
|
+
# @option params [Mixed] name Name or Id of the current existing Datasource (required)
|
78
|
+
# @option params [Mixed] organisation Name or Id of an existing Organisation
|
79
|
+
# @option params [String] type Datasource Type - (required) (grafana graphite cloudwatch elasticsearch prometheus influxdb mysql opentsdb postgres)
|
80
|
+
# @option params [String] new_name New Datasource Name
|
81
|
+
# @option params [String] database Datasource Database
|
82
|
+
# @option params [String] access (proxy) Acess Type
|
83
|
+
# @option params [Boolean] default (false)
|
84
|
+
# @option params [String] user
|
85
|
+
# @option params [String] password
|
86
|
+
# @option params [String] url Datasource URL
|
87
|
+
# @option params [Hash] json_data
|
88
|
+
# @option params [String] basic_user
|
89
|
+
# @option params [String] basic_password
|
87
90
|
#
|
88
91
|
# @example
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
92
|
+
# params = {
|
93
|
+
# name: 'graphite',
|
94
|
+
# new_name: 'influx',
|
95
|
+
# organisation: 'Main Org.',
|
96
|
+
# type: 'influxdb',
|
97
|
+
# url: 'http://localhost:8090'
|
98
|
+
# }
|
99
|
+
# update_datasource( params )
|
93
100
|
#
|
94
101
|
# @return [Hash]
|
95
102
|
#
|
@@ -98,22 +105,70 @@ module Grafana
|
|
98
105
|
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
99
106
|
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
100
107
|
|
101
|
-
|
102
|
-
|
108
|
+
name = validate( params, required: true, var: 'name' )
|
109
|
+
organisation = validate( params, required: false, var: 'organisation' )
|
110
|
+
type = validate( params, required: false, var: 'type', type: String )
|
111
|
+
new_name = validate( params, required: false, var: 'new_name', type: String )
|
112
|
+
database = validate( params, required: false, var: 'database', type: String )
|
113
|
+
access = validate( params, required: false, var: 'access', type: String ) || 'proxy'
|
114
|
+
default = validate( params, required: false, var: 'default', type: Boolean ) || false
|
115
|
+
user = validate( params, required: false, var: 'user', type: String )
|
116
|
+
password = validate( params, required: false, var: 'password', type: String )
|
117
|
+
url = validate( params, required: false, var: 'url', type: String )
|
118
|
+
json_data = validate( params, required: false, var: 'json_data', type: Hash )
|
119
|
+
ba_user = validate( params, required: false, var: 'basic_user', type: String )
|
120
|
+
ba_password = validate( params, required: false, var: 'basic_password', type: String )
|
121
|
+
basic_auth = false
|
122
|
+
basic_auth = true unless( ba_user.nil? && ba_password.nil? )
|
123
|
+
org_id = nil
|
124
|
+
|
125
|
+
raise ArgumentError.new(format('wrong type. user \'name\' must be an String (for an Datasource name) or an Integer (for an Datasource Id), given \'%s\'', name.class.to_s)) if( name.is_a?(String) && name.is_a?(Integer) )
|
103
126
|
|
104
|
-
|
127
|
+
if( organisation )
|
128
|
+
raise ArgumentError.new(format('wrong type. user \'organisation\' must be an String (for an Organisation name) or an Integer (for an Organisation Id), given \'%s\'', organisation.class.to_s)) if( organisation.is_a?(String) && organisation.is_a?(Integer) )
|
129
|
+
org = organization( organisation )
|
130
|
+
org_id = org.dig('id')
|
105
131
|
|
106
|
-
|
132
|
+
return { 'status' => 404, 'message' => format('Organization \'%s\' not found', organization) } if( org.nil? || org.dig('status').to_i != 200 )
|
133
|
+
end
|
107
134
|
|
135
|
+
existing_ds = datasource(name)
|
108
136
|
existing_ds.reject! { |x| x == 'status' }
|
109
137
|
existing_ds = existing_ds.deep_string_keys
|
110
|
-
|
111
138
|
datasource_id = existing_ds.dig('id')
|
112
139
|
|
140
|
+
return { 'status' => 404, 'message' => format('No Datasource \'%s\' found', name) } if( datasource_id.nil? )
|
141
|
+
|
142
|
+
raise format('Data Source Id can not be 0') if( datasource_id.zero? )
|
143
|
+
|
144
|
+
unless( type.nil? )
|
145
|
+
valid_types = %w[grafana graphite cloudwatch elasticsearch prometheus influxdb mysql opentsdb postgres]
|
146
|
+
raise ArgumentError.new(format('wrong datasource type. only %s allowed, given \%s\'', valid_types.join(', '), type)) if( valid_types.include?(type.downcase) == false )
|
147
|
+
end
|
148
|
+
|
149
|
+
data = {
|
150
|
+
id: datasource_id,
|
151
|
+
orgId: org_id,
|
152
|
+
name: new_name,
|
153
|
+
type: type,
|
154
|
+
access: access,
|
155
|
+
url: url,
|
156
|
+
password: password,
|
157
|
+
user: user,
|
158
|
+
database: database,
|
159
|
+
basicAuth: basic_auth,
|
160
|
+
basicAuthUser: ba_user,
|
161
|
+
basicAuthPassword: ba_user,
|
162
|
+
isDefault: default,
|
163
|
+
jsonData: json_data
|
164
|
+
}
|
165
|
+
data.reject!{ |_k, v| v.nil? }
|
166
|
+
|
113
167
|
payload = data.deep_string_keys
|
114
168
|
payload = existing_ds.merge(payload).deep_symbolize_keys
|
115
169
|
|
116
170
|
endpoint = format('/api/datasources/%d', datasource_id )
|
171
|
+
|
117
172
|
@logger.debug("Updating data source Id #{datasource_id} (GET #{endpoint})") if @debug
|
118
173
|
logger.debug(payload.to_json) if(@debug)
|
119
174
|
|
@@ -128,7 +183,6 @@ module Grafana
|
|
128
183
|
# @option params [String] database Datasource Database - (required)
|
129
184
|
# @option params [String] access (proxy) Acess Type - (required) (proxy or direct)
|
130
185
|
# @option params [Boolean] default (false)
|
131
|
-
# @option params [String] user
|
132
186
|
# @option params [String] password
|
133
187
|
# @option params [String] url Datasource URL - (required)
|
134
188
|
# @option params [Hash] json_data
|
@@ -182,7 +236,6 @@ module Grafana
|
|
182
236
|
database = validate( params, required: true, var: 'database', type: String )
|
183
237
|
access = validate( params, required: false, var: 'access', type: String ) || 'proxy'
|
184
238
|
default = validate( params, required: false, var: 'default', type: Boolean ) || false
|
185
|
-
user = validate( params, required: false, var: 'user', type: String )
|
186
239
|
password = validate( params, required: false, var: 'password', type: String )
|
187
240
|
url = validate( params, required: true, var: 'url', type: String )
|
188
241
|
json_data = validate( params, required: false, var: 'json_data', type: Hash )
|
@@ -241,12 +294,7 @@ module Grafana
|
|
241
294
|
datasource_id = data.keys.first if( data )
|
242
295
|
end
|
243
296
|
|
244
|
-
if( datasource_id.nil? )
|
245
|
-
return {
|
246
|
-
'status' => 404,
|
247
|
-
'message' => format( 'No Datasource \'%s\' found', datasource_id)
|
248
|
-
}
|
249
|
-
end
|
297
|
+
return { 'status' => 404, 'message' => format( 'No Datasource \'%s\' found', datasource_id) } if( datasource_id.nil? )
|
250
298
|
|
251
299
|
raise format('Data Source Id can not be 0') if( datasource_id.zero? )
|
252
300
|
|
data/lib/grafana/network.rb
CHANGED
@@ -64,9 +64,15 @@ module Grafana
|
|
64
64
|
response_code = response.code.to_i
|
65
65
|
raise RestClient::BadRequest
|
66
66
|
else
|
67
|
-
|
68
|
-
|
69
|
-
|
67
|
+
# logger.error( response.code )
|
68
|
+
# logger.error( response.body )
|
69
|
+
|
70
|
+
body = JSON.parse(response.body) if(response_body.is_a?(String))
|
71
|
+
return {
|
72
|
+
'status' => response_code,
|
73
|
+
'message' => body.dig('message')
|
74
|
+
}
|
75
|
+
# response.return! # (request, result)
|
70
76
|
end
|
71
77
|
end
|
72
78
|
|
@@ -129,6 +135,12 @@ module Grafana
|
|
129
135
|
'status' => 401,
|
130
136
|
'message' => format('Not authorized to connect \'%s/%s\' - wrong username or password?', @url, endpoint)
|
131
137
|
}
|
138
|
+
rescue RestClient::Forbidden
|
139
|
+
|
140
|
+
return {
|
141
|
+
'status' => 403,
|
142
|
+
'message' => format('The operation is forbidden \'%s/%s\'', @url, endpoint)
|
143
|
+
}
|
132
144
|
|
133
145
|
rescue RestClient::NotFound
|
134
146
|
|
data/lib/grafana/organization.rb
CHANGED
@@ -6,9 +6,11 @@ module Grafana
|
|
6
6
|
module Organization
|
7
7
|
|
8
8
|
# Get current Organisation
|
9
|
-
# GET /api/org
|
10
9
|
#
|
11
|
-
#
|
10
|
+
# @example
|
11
|
+
# current_organization
|
12
|
+
#
|
13
|
+
# @return [Hash]
|
12
14
|
#
|
13
15
|
def current_organization
|
14
16
|
endpoint = '/api/org'
|
@@ -17,25 +19,40 @@ module Grafana
|
|
17
19
|
end
|
18
20
|
|
19
21
|
# Update current Organisation
|
20
|
-
# PUT /api/org
|
21
22
|
#
|
23
|
+
# @param [Hash] params
|
24
|
+
# @option params [String] name new Organisation Name
|
25
|
+
#
|
26
|
+
# @example
|
27
|
+
# params = {
|
28
|
+
# name: 'foo'
|
29
|
+
# }
|
30
|
+
# update_current_organization( params )
|
22
31
|
#
|
32
|
+
# @return [Hash]
|
23
33
|
#
|
24
|
-
def update_current_organization( params
|
34
|
+
def update_current_organization( params )
|
25
35
|
|
26
36
|
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
27
|
-
|
28
|
-
|
37
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
38
|
+
|
39
|
+
name = validate( params, required: true, var: 'name', type: String )
|
29
40
|
|
30
41
|
endpoint = '/api/org'
|
42
|
+
payload = {
|
43
|
+
name: name
|
44
|
+
}
|
45
|
+
|
31
46
|
@logger.debug("Updating current organization (PUT #{endpoint})") if @debug
|
32
|
-
put(endpoint,
|
47
|
+
put(endpoint, payload.to_json)
|
33
48
|
end
|
34
49
|
|
35
50
|
# Get all users within the actual organisation
|
36
|
-
# GET /api/org/users
|
37
51
|
#
|
52
|
+
# @example
|
53
|
+
# current_organization_users
|
38
54
|
#
|
55
|
+
# @return [Hash]
|
39
56
|
#
|
40
57
|
def current_organization_users
|
41
58
|
endpoint = '/api/org/users'
|
@@ -44,46 +61,59 @@ module Grafana
|
|
44
61
|
end
|
45
62
|
|
46
63
|
# Add a new user to the actual organisation
|
47
|
-
# POST /api/org/users
|
48
64
|
#
|
65
|
+
# @param [Hash] params
|
66
|
+
# @option params [String] login_or_email Login or email
|
67
|
+
# @option params [String] role Name of the Role - only 'Viewer', 'Editor', 'Read Only Editor' or 'Admin' allowed
|
68
|
+
#
|
69
|
+
# @example
|
70
|
+
# params = {
|
71
|
+
# login_or_email: 'foo',
|
72
|
+
# role: 'Editor'
|
73
|
+
# }
|
74
|
+
# add_user_to_current_organization( params )
|
49
75
|
#
|
76
|
+
# @return [Hash]
|
50
77
|
#
|
51
|
-
def add_user_to_current_organization( params
|
78
|
+
def add_user_to_current_organization( params )
|
52
79
|
|
53
80
|
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
54
|
-
|
55
|
-
role = params.dig(:role)
|
56
|
-
raise ArgumentError.new('missing loginOrEmail') if( login_or_email.nil? )
|
57
|
-
raise ArgumentError.new('missing role') if( role.nil? )
|
58
|
-
# Defaults to Viewer, other valid options are Admin and Editor and Read Only Editor
|
59
|
-
# valid_perms = ['Viewer','Editor','Read Only Editor','Admin']
|
60
|
-
raise ArgumentError.new( format( 'wrong role. only \'Admin\', \'Viewer\' or \'Editor\' allowed (\'%s\' giving)',role)) if( %w[Admin Viewer Editor].include?(role) == false )
|
61
|
-
|
62
|
-
org = current_organization_users
|
63
|
-
usr = user_by_name( login_or_email )
|
64
|
-
|
65
|
-
if( org )
|
66
|
-
|
67
|
-
org = org.dig('message')
|
81
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
68
82
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
'message' => format('User \'%s\' are already in the organisation', login_or_email)
|
73
|
-
}
|
74
|
-
end
|
75
|
-
end
|
83
|
+
login_or_email = validate( params, required: true, var: 'login_or_email', type: String )
|
84
|
+
role = validate( params, required: true, var: 'role', type: String )
|
85
|
+
valid_roles = ['Viewer', 'Editor', 'Read Only Editor', 'Admin']
|
76
86
|
|
77
|
-
|
87
|
+
# https://stackoverflow.com/questions/9333952/case-insensitive-arrayinclude?answertab=votes#tab-top
|
88
|
+
# Do this once, or each time the array changes
|
89
|
+
downcased = Set.new valid_roles.map(&:downcase)
|
90
|
+
unless( downcased.include?( role.downcase ) )
|
78
91
|
return {
|
79
92
|
'status' => 404,
|
80
|
-
'
|
93
|
+
'login_or_email' => login_or_email,
|
94
|
+
'role' => role,
|
95
|
+
'message' => format( 'wrong role. Role must be one of %s, given \'%s\'', valid_roles.join(', '), role )
|
81
96
|
}
|
82
97
|
end
|
83
98
|
|
99
|
+
org = current_organization_users
|
100
|
+
usr = user( login_or_email )
|
101
|
+
|
102
|
+
return { 'status' => 404, 'message' => format('User \'%s\' not found', login_or_email) } if( usr.nil? || usr.dig('status').to_i != 200 )
|
103
|
+
|
104
|
+
if( org.is_a?(Hash) && org.dig('status').to_i == 200 )
|
105
|
+
org = org.dig('message')
|
106
|
+
return { 'status' => 404, 'message' => format('User \'%s\' are already in the organisation', login_or_email) } if( org.select { |x| x.dig('email') == login_or_email || x.dig('login') == login_or_email }.count >= 1 )
|
107
|
+
end
|
108
|
+
|
84
109
|
endpoint = '/api/org/users'
|
110
|
+
payload = {
|
111
|
+
loginOrEmail: login_or_email,
|
112
|
+
role: role
|
113
|
+
}
|
114
|
+
|
85
115
|
@logger.debug("Adding user to current organization (POST #{endpoint})") if @debug
|
86
|
-
post(endpoint,
|
116
|
+
post(endpoint, payload.to_json)
|
87
117
|
end
|
88
118
|
|
89
119
|
# Updates the given user
|