grafana 0.9.0 → 0.10.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- 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 +65 -65
- data/doc/top-level-namespace.html +1 -1
- data/lib/grafana/admin.rb +2 -1
- data/lib/grafana/alerts.rb +334 -14
- data/lib/grafana/annotations.rb +10 -8
- data/lib/grafana/client.rb +36 -0
- data/lib/grafana/dashboard.rb +96 -6
- data/lib/grafana/dashboard_permissions.rb +132 -0
- data/lib/grafana/dashboard_versions.rb +101 -5
- data/lib/grafana/datasource.rb +34 -38
- data/lib/grafana/folder.rb +198 -0
- data/lib/grafana/folder_and_dashboard_search.rb +57 -0
- data/lib/grafana/folder_permissions.rb +155 -0
- data/lib/grafana/login.rb +40 -34
- data/lib/grafana/network.rb +48 -92
- data/lib/grafana/organization.rb +2 -1
- data/lib/grafana/organizations.rb +13 -4
- data/lib/grafana/preferences.rb +122 -0
- data/lib/grafana/teams.rb +364 -0
- data/lib/grafana/tools.rb +7 -7
- data/lib/grafana/user.rb +6 -2
- data/lib/grafana/users.rb +19 -11
- data/lib/grafana/validator.rb +33 -1
- data/lib/grafana/version.rb +2 -2
- metadata +9 -17
data/lib/grafana/network.rb
CHANGED
@@ -60,55 +60,52 @@ module Grafana
|
|
60
60
|
# @return [Hash]
|
61
61
|
#
|
62
62
|
def request( method_type = 'GET', endpoint = '/', data = {} )
|
63
|
+
# logger.debug( "request( #{method_type}, #{endpoint}, data )" )
|
64
|
+
raise 'try first login()' if @api_instance.nil?
|
63
65
|
|
64
|
-
|
66
|
+
login( username: @username, password: @password )
|
65
67
|
|
66
68
|
response = nil
|
67
69
|
response_code = 404
|
68
70
|
response_body = ''
|
69
71
|
|
70
72
|
begin
|
71
|
-
|
72
73
|
case method_type.upcase
|
73
74
|
when 'GET'
|
74
|
-
response = @api_instance[endpoint].get(
|
75
|
+
response = @api_instance[endpoint].get( headers )
|
75
76
|
when 'POST'
|
76
|
-
response = @api_instance[endpoint].post( data,
|
77
|
+
response = @api_instance[endpoint].post( data, headers )
|
77
78
|
when 'PATCH'
|
78
|
-
response = @api_instance[endpoint].patch( data,
|
79
|
+
response = @api_instance[endpoint].patch( data, headers )
|
79
80
|
when 'PUT'
|
80
|
-
# response = @api_instance[endpoint].put( data,
|
81
|
-
@api_instance[endpoint].put( data,
|
81
|
+
# response = @api_instance[endpoint].put( data, headers )
|
82
|
+
@api_instance[endpoint].put( data, headers ) do |resp, _request, _result|
|
83
|
+
response_body = resp.body
|
84
|
+
response_code = resp.code.to_i
|
85
|
+
response_body = JSON.parse(response_body) if response_body.is_a?(String)
|
82
86
|
|
83
|
-
case
|
87
|
+
case response_code
|
84
88
|
when 200
|
85
|
-
response_body
|
86
|
-
response_code = response.code.to_i
|
87
|
-
response_body = JSON.parse(response_body) if response_body.is_a?(String)
|
88
|
-
|
89
|
-
return {
|
90
|
-
'status' => response_code,
|
91
|
-
'message' => response_body.dig('message').nil? ? 'Successful' : response_body.dig('message')
|
92
|
-
}
|
89
|
+
return { 'status' => response_code, 'message' => response_body.dig('message').nil? ? 'Successful' : response_body.dig('message') }
|
93
90
|
when 400
|
94
|
-
response_body = response.body
|
95
|
-
response_code = response.code.to_i
|
96
91
|
raise RestClient::BadRequest
|
92
|
+
when 412
|
93
|
+
status = response_body.dig('status')
|
94
|
+
message = response_body.dig('message')
|
95
|
+
message += " (#{status})" unless(status.nil?)
|
96
|
+
return { 'status' => response_code, 'message' => message }
|
97
|
+
when 422
|
98
|
+
raise RestClient::UnprocessableEntity
|
97
99
|
else
|
98
|
-
# logger.error(
|
99
|
-
# logger.error(
|
100
|
-
|
101
|
-
body = JSON.parse(response.body) if(response_body.is_a?(String))
|
102
|
-
return {
|
103
|
-
'status' => response_code,
|
104
|
-
'message' => body.dig('message')
|
105
|
-
}
|
100
|
+
# logger.error( response_code )
|
101
|
+
# logger.error( response_body )
|
102
|
+
return { 'status' => response_code, 'message' => response_body.dig('message') }
|
106
103
|
# response.return! # (request, result)
|
107
104
|
end
|
108
105
|
end
|
109
106
|
|
110
107
|
when 'DELETE'
|
111
|
-
response = @api_instance[endpoint].delete(
|
108
|
+
response = @api_instance[endpoint].delete( headers )
|
112
109
|
else
|
113
110
|
@logger.error( "Error: #{__method__} is not a valid request method." )
|
114
111
|
return false
|
@@ -118,95 +115,54 @@ module Grafana
|
|
118
115
|
response_body = response.body
|
119
116
|
response_headers = response.headers
|
120
117
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
118
|
+
# if( @debug )
|
119
|
+
# logger.debug("response_code : #{response_code}" )
|
120
|
+
# logger.debug("response_body : #{response_body}" )
|
121
|
+
# logger.debug("response_headers : #{response_headers}" )
|
122
|
+
# end
|
126
123
|
|
127
124
|
if( ( response_code >= 200 && response_code <= 299 ) || ( response_code >= 400 && response_code <= 499 ) )
|
128
125
|
|
129
126
|
result = JSON.parse( response_body )
|
127
|
+
return { 'status' => response_code, 'message' => result } if( result.is_a?(Array) )
|
130
128
|
|
131
|
-
if( result.is_a?(
|
132
|
-
r_result= {
|
133
|
-
'status' => response_code,
|
134
|
-
'message' => result
|
135
|
-
}
|
136
|
-
return r_result
|
137
|
-
end
|
138
|
-
|
139
|
-
result_status = result.dig('status') if( result.is_a?( Hash ) )
|
140
|
-
|
129
|
+
result_status = result.dig('status') if( result.is_a?( Hash ) )
|
141
130
|
result['message'] = result_status unless( result_status.nil? )
|
142
131
|
result['status'] = response_code
|
143
132
|
|
144
133
|
return result
|
145
134
|
else
|
146
|
-
|
147
135
|
@logger.error( "#{__method__} #{method_type.upcase} on #{endpoint} failed: HTTP #{response.code} - #{response_body}" )
|
148
|
-
@logger.error(
|
136
|
+
@logger.error( headers )
|
149
137
|
@logger.error( JSON.pretty_generate( response_headers ) )
|
150
138
|
|
151
139
|
return JSON.parse( response_body )
|
152
140
|
end
|
153
141
|
|
154
142
|
rescue RestClient::BadRequest
|
155
|
-
|
156
143
|
response_body = JSON.parse(response_body) if response_body.is_a?(String)
|
157
|
-
|
158
|
-
return {
|
159
|
-
'status' => 400,
|
160
|
-
'message' => response_body.dig('message').nil? ? 'Bad Request' : response_body.dig('message')
|
161
|
-
}
|
162
|
-
|
144
|
+
return { 'status' => 400, 'message' => response_body.dig('message').nil? ? 'Bad Request' : response_body.dig('message') }
|
163
145
|
rescue RestClient::Unauthorized
|
164
|
-
|
165
|
-
return {
|
166
|
-
'status' => 401,
|
167
|
-
'message' => format('Not authorized to connect \'%s/%s\' - wrong username or password?', @url, endpoint)
|
168
|
-
}
|
146
|
+
return { 'status' => 401, 'message' => format('Not authorized to connect \'%s/%s\' - wrong username or password?', @url, endpoint) }
|
169
147
|
rescue RestClient::Forbidden
|
170
|
-
|
171
|
-
return {
|
172
|
-
'status' => 403,
|
173
|
-
'message' => format('The operation is forbidden \'%s/%s\'', @url, endpoint)
|
174
|
-
}
|
175
|
-
|
148
|
+
return { 'status' => 403, 'message' => format('The operation is forbidden \'%s/%s\'', @url, endpoint) }
|
176
149
|
rescue RestClient::NotFound
|
177
|
-
|
178
|
-
return {
|
179
|
-
'status' => 404,
|
180
|
-
'message' => 'Not Found'
|
181
|
-
}
|
182
|
-
|
150
|
+
return { 'status' => 404, 'message' => 'Not Found' }
|
183
151
|
rescue RestClient::Conflict
|
184
|
-
|
185
|
-
return {
|
186
|
-
'status' => 409,
|
187
|
-
'message' => 'Conflict with the current state of the target resource'
|
188
|
-
}
|
189
|
-
|
152
|
+
return { 'status' => 409, 'message' => 'Conflict with the current state of the target resource' }
|
190
153
|
rescue RestClient::PreconditionFailed
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
}
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
logger.error( "Error: #{__method__} #{method_type.upcase} on #{endpoint} error: '#{
|
200
|
-
logger.error( data )
|
201
|
-
|
202
|
-
logger.error( JSON.pretty_generate( response_headers ) )
|
203
|
-
|
204
|
-
return false
|
205
|
-
|
154
|
+
return { 'status' => 412, 'message' => 'Precondition failed. The Object probably already exists.' }
|
155
|
+
rescue RestClient::PreconditionFailed
|
156
|
+
return { 'status' => 412, 'message' => 'Precondition failed. The Object probably already exists.' }
|
157
|
+
rescue RestClient::ExceptionWithResponse => error
|
158
|
+
# logger.error( "Error: #{__method__} #{method_type.upcase} on #{endpoint} error: '#{error}'" )
|
159
|
+
# logger.error( "query: #{data}" )
|
160
|
+
return { 'status' => 500, 'message' => "Internal Server Error: #{error}" }
|
161
|
+
rescue => error
|
162
|
+
# logger.error( "Error: #{__method__} #{method_type.upcase} on #{endpoint} error: '#{error}'" )
|
163
|
+
# logger.error( "query: #{data}" )
|
164
|
+
return { 'status' => 500, 'message' => "Internal Server Error: #{error}" }
|
206
165
|
end
|
207
|
-
|
208
166
|
end
|
209
|
-
|
210
167
|
end
|
211
|
-
|
212
168
|
end
|
data/lib/grafana/organization.rb
CHANGED
@@ -103,7 +103,8 @@ module Grafana
|
|
103
103
|
|
104
104
|
if( org.is_a?(Hash) && org.dig('status').to_i == 200 )
|
105
105
|
org = org.dig('message')
|
106
|
-
return { 'status' => 404, 'message' => format('User \'%s\' are already in the organisation', login_or_email) }
|
106
|
+
return { 'status' => 404, 'message' => format('User \'%s\' are already in the organisation', login_or_email) } \
|
107
|
+
if( org.select { |x| x.dig('email') == login_or_email || x.dig('login') == login_or_email }.count >= 1 )
|
107
108
|
end
|
108
109
|
|
109
110
|
endpoint = '/api/org/users'
|
@@ -23,7 +23,10 @@ module Grafana
|
|
23
23
|
#
|
24
24
|
def organization( organisation_id )
|
25
25
|
|
26
|
-
|
26
|
+
if( organisation_id.is_a?(String) && organisation_id.is_a?(Integer))
|
27
|
+
raise ArgumentError.new(format('wrong type. \'organisation_id\' must be an String (for an Datasource name) ' \
|
28
|
+
'or an Integer (for an Datasource Id), given \'%s\'', organisation_id.class.to_s))
|
29
|
+
end
|
27
30
|
raise ArgumentError.new('missing \'organisation_id\'') if( organisation_id.size.zero? )
|
28
31
|
|
29
32
|
endpoint = format( '/api/orgs/%d', organisation_id ) if(organisation_id.is_a?(Integer))
|
@@ -83,7 +86,10 @@ module Grafana
|
|
83
86
|
#
|
84
87
|
def organization_users( organization_id )
|
85
88
|
|
86
|
-
|
89
|
+
if( organization_id.is_a?(String) && organization_id.is_a?(Integer))
|
90
|
+
raise ArgumentError.new(format('wrong type. \'organization_id\' must be an String (for an Organisation name) '\
|
91
|
+
'or an Integer (for an Organisation Id), given \'%s\'', organization_id.class.to_s))
|
92
|
+
end
|
87
93
|
raise ArgumentError.new('missing \'organization_id\'') if( organization_id.size.zero? )
|
88
94
|
|
89
95
|
if(organization_id.is_a?(String))
|
@@ -266,7 +272,10 @@ module Grafana
|
|
266
272
|
#
|
267
273
|
def delete_organisation( organisation_id )
|
268
274
|
|
269
|
-
|
275
|
+
if( organisation_id.is_a?(String) && organisation_id.is_a?(Integer) )
|
276
|
+
raise ArgumentError.new(format('wrong type. \'organisation_id\' must be an String (for an Organisation name) ' \
|
277
|
+
'or an Integer (for an Organisation Id), given \'%s\'', organisation_id.class.to_s))
|
278
|
+
end
|
270
279
|
raise ArgumentError.new('missing \'organisation_id\'') if( organisation_id.size.zero? )
|
271
280
|
|
272
281
|
if(organisation_id.is_a?(String))
|
@@ -275,7 +284,7 @@ module Grafana
|
|
275
284
|
data.each do |d|
|
276
285
|
organisation_map[d.dig('id')] = d.dig('name')
|
277
286
|
end
|
278
|
-
organisation_id = organisation_map.select { |
|
287
|
+
organisation_id = organisation_map.select { |_,y| y == organisation_id }.keys.first if( organisation_map )
|
279
288
|
end
|
280
289
|
|
281
290
|
return { 'status' => 404, 'message' => format( 'No Organisation \'%s\' found', organisation_id) } if( organisation_id.nil? )
|
@@ -0,0 +1,122 @@
|
|
1
|
+
module Grafana
|
2
|
+
|
3
|
+
#
|
4
|
+
#
|
5
|
+
#
|
6
|
+
#
|
7
|
+
#
|
8
|
+
# original API Documentation can be found under: http://docs.grafana.org/http_api/preferences/#user-and-org-preferences-api
|
9
|
+
#
|
10
|
+
module Preferences
|
11
|
+
|
12
|
+
# Get Current User Prefs
|
13
|
+
# GET /api/user/preferences
|
14
|
+
def user_preferences()
|
15
|
+
|
16
|
+
v, mv = version.values
|
17
|
+
return { 'status' => 404, 'message' => format( 'only Grafana 5 has folder support. you use version %s', v) } if(mv != 5)
|
18
|
+
|
19
|
+
endpoint = '/api/user/preferences'
|
20
|
+
@logger.debug("Getting current user preferences (GET #{endpoint})") if @debug
|
21
|
+
get(endpoint)
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
# Update Current User Prefs
|
26
|
+
# PUT /api/user/preferences
|
27
|
+
#
|
28
|
+
# theme - One of: light, dark, or an empty string for the default theme
|
29
|
+
# homeDashboardId - The numerical :id of a favorited dashboard, default: 0
|
30
|
+
# timezone - One of: utc, browser, or an empty string for the default
|
31
|
+
#
|
32
|
+
def update_user_preferences(params)
|
33
|
+
|
34
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
35
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
36
|
+
|
37
|
+
endpoint = '/api/user/preferences'
|
38
|
+
@logger.debug("update current user preferences (GET #{endpoint})") if @debug
|
39
|
+
|
40
|
+
update_preferences( endpoint, params )
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
# Get Current Org Prefs
|
45
|
+
# GET /api/org/preferences
|
46
|
+
def org_preferences()
|
47
|
+
|
48
|
+
v, mv = version.values
|
49
|
+
return { 'status' => 404, 'message' => format( 'only Grafana 5 has folder support. you use version %s', v) } if(mv != 5)
|
50
|
+
|
51
|
+
endpoint = '/api/org/preferences'
|
52
|
+
@logger.debug("Getting current organisation preferences (GET #{endpoint})") if @debug
|
53
|
+
get(endpoint)
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
# Update Current Org Prefs
|
59
|
+
# PUT /api/org/preferences
|
60
|
+
#
|
61
|
+
# theme - One of: light, dark, or an empty string for the default theme
|
62
|
+
# homeDashboardId - The numerical :id of a favorited dashboard, default: 0
|
63
|
+
# timezone - One of: utc, browser, or an empty string for the default
|
64
|
+
#
|
65
|
+
def update_org_preferences(params)
|
66
|
+
|
67
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
68
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
69
|
+
|
70
|
+
endpoint = '/api/org/preferences'
|
71
|
+
@logger.debug("update current organisation preferences (GET #{endpoint})") if @debug
|
72
|
+
|
73
|
+
update_preferences( endpoint, params )
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
private
|
78
|
+
def update_preferences( endpoint, params )
|
79
|
+
|
80
|
+
theme = validate( params, required: false, var: 'theme' , type: String )
|
81
|
+
timezone = validate( params, required: false, var: 'timezone' , type: String )
|
82
|
+
home_dashboard = validate( params, required: false, var: 'home_dashboard')
|
83
|
+
|
84
|
+
valid_theme = %w[light dark]
|
85
|
+
valid_timezone = %w[utc browser]
|
86
|
+
|
87
|
+
v, mv = version.values
|
88
|
+
return { 'status' => 404, 'message' => format( 'only Grafana 5 has folder support. you use version %s', v) } if(mv != 5)
|
89
|
+
|
90
|
+
unless(theme.nil?)
|
91
|
+
v_theme = validate_hash( theme, valid_theme )
|
92
|
+
return v_theme unless( v_theme == true )
|
93
|
+
end
|
94
|
+
|
95
|
+
unless(timezone.nil?)
|
96
|
+
v_timez = validate_hash( timezone, valid_timezone )
|
97
|
+
return v_timez unless( v_timez == true )
|
98
|
+
end
|
99
|
+
|
100
|
+
unless(home_dashboard.nil?)
|
101
|
+
v_dashboard = dashboard(home_dashboard)
|
102
|
+
return { 'status' => 404, 'message' => format('dashboard \'%s\' not found',home_dashboard) }\
|
103
|
+
unless(v_dashboard.dig('status') == 200)
|
104
|
+
|
105
|
+
dashboard_id = v_dashboard.dig('dashboard','id')
|
106
|
+
end
|
107
|
+
|
108
|
+
payload = {
|
109
|
+
theme: theme,
|
110
|
+
homeDashboardId: dashboard_id,
|
111
|
+
timezone: timezone
|
112
|
+
}
|
113
|
+
payload.reject!{ |_, y| y.nil? }
|
114
|
+
|
115
|
+
# endpoint = '/api/user/preferences'
|
116
|
+
# @logger.debug("update current preferences (GET #{endpoint})") if @debug
|
117
|
+
|
118
|
+
put(endpoint, payload.to_json)
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,364 @@
|
|
1
|
+
|
2
|
+
module Grafana
|
3
|
+
|
4
|
+
# http://docs.grafana.org/http_api/team/
|
5
|
+
#
|
6
|
+
# This API can be used to create/update/delete Teams and to add/remove users to Teams.
|
7
|
+
# All actions require that the user has the Admin role for the organization.
|
8
|
+
#
|
9
|
+
module Teams
|
10
|
+
|
11
|
+
# http://docs.grafana.org/http_api/team/#team-search-with-paging
|
12
|
+
#
|
13
|
+
# GET /api/teams/search?perpage=50&page=1&query=myteam
|
14
|
+
# or
|
15
|
+
# GET /api/teams/search?name=myteam
|
16
|
+
#
|
17
|
+
# Default value for the perpage parameter is 1000 and for the page parameter is 1.
|
18
|
+
#
|
19
|
+
# The totalCount field in the response can be used for pagination of the teams list
|
20
|
+
# E.g. if totalCount is equal to 100 teams and the perpage parameter is set to 10 then there are 10 pages of teams.
|
21
|
+
#
|
22
|
+
# The query parameter is optional and it will return results where the query value is contained in the name field.
|
23
|
+
# Query values with spaces need to be url encoded e.g. query=my%20team.
|
24
|
+
#
|
25
|
+
# The name parameter returns a single team if the parameter matches the name field.
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
#
|
30
|
+
# Status Codes:
|
31
|
+
#
|
32
|
+
# 200 - Ok
|
33
|
+
# 401 - Unauthorized
|
34
|
+
# 403 - Permission denied
|
35
|
+
# 404 - Team not found (if searching by name)
|
36
|
+
#
|
37
|
+
def search_team( params )
|
38
|
+
|
39
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
40
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
41
|
+
|
42
|
+
v, mv = version.values
|
43
|
+
return { 'status' => 404, 'message' => format( 'only Grafana 5 has team support. you use version %s', v) } if(mv != 5)
|
44
|
+
|
45
|
+
perpage = validate( params, required: false, var: 'perpage', type: Integer ) || 1000
|
46
|
+
page = validate( params, required: false, var: 'page' , type: Integer ) || 1
|
47
|
+
query = validate( params, required: false, var: 'query' , type: String )
|
48
|
+
name = validate( params, required: false, var: 'name' , type: String )
|
49
|
+
|
50
|
+
if(name.nil?)
|
51
|
+
api = []
|
52
|
+
api << format( 'perpage=%s', perpage ) unless( perpage.nil? )
|
53
|
+
api << format( 'page=%s', page ) unless( page.nil? )
|
54
|
+
api << format( 'query=%s', CGI.escape( query ) ) unless( query.nil? )
|
55
|
+
|
56
|
+
api = api.join( '&' )
|
57
|
+
|
58
|
+
endpoint = format('/api/teams/search?%s', api)
|
59
|
+
else
|
60
|
+
endpoint = format('/api/teams/search?name=%s',CGI.escape(name))
|
61
|
+
end
|
62
|
+
|
63
|
+
@logger.debug("Attempting to search for alerts (GET #{endpoint})") if @debug
|
64
|
+
|
65
|
+
r = get(endpoint)
|
66
|
+
r['status'] = 404 if( r.dig('totalCount').zero? )
|
67
|
+
r
|
68
|
+
end
|
69
|
+
|
70
|
+
# http://docs.grafana.org/http_api/team/#get-team-by-id
|
71
|
+
#
|
72
|
+
# Get Team By Id
|
73
|
+
# GET /api/teams/:id
|
74
|
+
#
|
75
|
+
#
|
76
|
+
def team( team_id )
|
77
|
+
|
78
|
+
if( team_id.is_a?(String) && team_id.is_a?(Integer) )
|
79
|
+
raise ArgumentError.new(format('wrong type. user \'team_id\' must be an String (for an Team name) or an Integer (for an Team Id), given \'%s\'', team_id.class.to_s))
|
80
|
+
end
|
81
|
+
raise ArgumentError.new('missing \'team_id\'') if( team_id.size.zero? )
|
82
|
+
|
83
|
+
v, mv = version.values
|
84
|
+
return { 'status' => 404, 'message' => format( 'only Grafana 5 has team support. you use version %s', v) } if(mv != 5)
|
85
|
+
|
86
|
+
if(team_id.is_a?(String))
|
87
|
+
o_team = search_team(name: team_id)
|
88
|
+
status = o_team.dig('status')
|
89
|
+
total_count = o_team.dig('totalCount')
|
90
|
+
|
91
|
+
return { 'status' => 404, 'message' => format( 'No Team \'%s\' found', team_id) } unless(status == 200 && total_count > 0)
|
92
|
+
|
93
|
+
teams = o_team.dig('teams')
|
94
|
+
team = teams.detect { |x| x['name'] == team_id }
|
95
|
+
team_id = team.dig('id')
|
96
|
+
end
|
97
|
+
|
98
|
+
endpoint = format( '/api/teams/%s', team_id )
|
99
|
+
|
100
|
+
@logger.debug("Getting team by Id #{team_id} (GET #{endpoint})") if @debug
|
101
|
+
get(endpoint)
|
102
|
+
end
|
103
|
+
|
104
|
+
# http://docs.grafana.org/http_api/team/#add-team
|
105
|
+
#
|
106
|
+
# The Team name needs to be unique. name is required and email is optional.
|
107
|
+
# POST /api/teams
|
108
|
+
#
|
109
|
+
#
|
110
|
+
def add_team( params )
|
111
|
+
|
112
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
113
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
114
|
+
|
115
|
+
v, mv = version.values
|
116
|
+
return { 'status' => 404, 'message' => format( 'only Grafana 5 has team support. you use version %s', v) } if(mv != 5)
|
117
|
+
|
118
|
+
name = validate( params, required: true, var: 'name' , type: String )
|
119
|
+
email = validate( params, required: false, var: 'email', type: String )
|
120
|
+
|
121
|
+
o_team = search_team(name: name)
|
122
|
+
|
123
|
+
status = o_team.dig('status')
|
124
|
+
total_count = o_team.dig('totalCount')
|
125
|
+
# teams = o_team.dig('teams')
|
126
|
+
# team = teams.detect { |x| x['name'] == name }
|
127
|
+
|
128
|
+
return { 'status' => 404, 'message' => format('team \'%s\' alread exists', name) } if(status == 200 && total_count > 0)
|
129
|
+
|
130
|
+
endpoint = '/api/teams'
|
131
|
+
|
132
|
+
payload = {
|
133
|
+
name: name,
|
134
|
+
email: email
|
135
|
+
}
|
136
|
+
payload.reject!{ |_, y| y.nil? }
|
137
|
+
|
138
|
+
@logger.debug("Creating team: #{name} (POST #{endpoint})") if @debug
|
139
|
+
|
140
|
+
post( endpoint, payload.to_json )
|
141
|
+
end
|
142
|
+
|
143
|
+
# http://docs.grafana.org/http_api/team/#update-team
|
144
|
+
#
|
145
|
+
# There are two fields that can be updated for a team: name and email.
|
146
|
+
# PUT /api/teams/:id
|
147
|
+
#
|
148
|
+
def update_team( params )
|
149
|
+
|
150
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
151
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
152
|
+
|
153
|
+
v, mv = version.values
|
154
|
+
return { 'status' => 404, 'message' => format( 'only Grafana 5 has team support. you use version %s', v) } if(mv != 5)
|
155
|
+
|
156
|
+
team_id = validate( params, required: true , var: 'team_id' )
|
157
|
+
name = validate( params, required: false, var: 'name' , type: String )
|
158
|
+
email = validate( params, required: true , var: 'email', type: String )
|
159
|
+
|
160
|
+
if(team_id.is_a?(String))
|
161
|
+
o_team = search_team(name: team_id)
|
162
|
+
status = o_team.dig('status')
|
163
|
+
total_count = o_team.dig('totalCount')
|
164
|
+
|
165
|
+
return { 'status' => 404, 'message' => format( 'No Team \'%s\' found', team_id) } unless(status == 200 && total_count > 0)
|
166
|
+
|
167
|
+
teams = o_team.dig('teams')
|
168
|
+
team = teams.detect { |x| x['name'] == team_id }
|
169
|
+
|
170
|
+
team_id = team.dig('id')
|
171
|
+
end
|
172
|
+
|
173
|
+
payload = {
|
174
|
+
email: email,
|
175
|
+
name: name
|
176
|
+
}
|
177
|
+
payload.reject!{ |_, y| y.nil? }
|
178
|
+
|
179
|
+
endpoint = format( '/api/teams/%d', team_id )
|
180
|
+
@logger.debug("Updating team with Id #{team_id} (PUT #{endpoint})") if @debug
|
181
|
+
|
182
|
+
put( endpoint, payload.to_json )
|
183
|
+
end
|
184
|
+
|
185
|
+
# http://docs.grafana.org/http_api/team/#delete-team-by-id
|
186
|
+
#
|
187
|
+
# DELETE /api/teams/:id
|
188
|
+
#
|
189
|
+
#
|
190
|
+
#
|
191
|
+
#
|
192
|
+
#
|
193
|
+
#
|
194
|
+
def delete_team(team_id)
|
195
|
+
|
196
|
+
if( team_id.is_a?(String) && team_id.is_a?(Integer) )
|
197
|
+
raise ArgumentError.new(format('wrong type. user \'team_id\' must be an String (for an Team name) or an Integer (for an Team Id), given \'%s\'', team_id.class.to_s))
|
198
|
+
end
|
199
|
+
raise ArgumentError.new('missing \'team_id\'') if( team_id.size.zero? )
|
200
|
+
|
201
|
+
v, mv = version.values
|
202
|
+
return { 'status' => 404, 'message' => format( 'only Grafana 5 has team support. you use version %s', v) } if(mv != 5)
|
203
|
+
|
204
|
+
if(team_id.is_a?(String))
|
205
|
+
o_team = search_team(name: team_id)
|
206
|
+
|
207
|
+
status = o_team.dig('status')
|
208
|
+
total_count = o_team.dig('totalCount')
|
209
|
+
|
210
|
+
return { 'status' => 404, 'message' => format( 'No Team \'%s\' found', team_id) } unless(status == 200 && total_count > 0)
|
211
|
+
|
212
|
+
teams = o_team.dig('teams')
|
213
|
+
team = teams.detect { |x| x['name'] == team_id }
|
214
|
+
|
215
|
+
team_id = team.dig('id')
|
216
|
+
end
|
217
|
+
|
218
|
+
endpoint = format( '/api/teams/%s', team_id )
|
219
|
+
|
220
|
+
@logger.debug("delete team Id #{team_id} (GET #{endpoint})") if @debug
|
221
|
+
delete(endpoint)
|
222
|
+
end
|
223
|
+
|
224
|
+
# http://docs.grafana.org/http_api/team/#get-team-members
|
225
|
+
#
|
226
|
+
# GET /api/teams/:teamId/members
|
227
|
+
#
|
228
|
+
#
|
229
|
+
#
|
230
|
+
#
|
231
|
+
def team_members(team_id)
|
232
|
+
|
233
|
+
if( team_id.is_a?(String) && team_id.is_a?(Integer) )
|
234
|
+
raise ArgumentError.new(format('wrong type. user \'team_id\' must be an String (for an Team name) or an Integer (for an Team Id), given \'%s\'', team_id.class.to_s))
|
235
|
+
end
|
236
|
+
raise ArgumentError.new('missing \'team_id\'') if( team_id.size.zero? )
|
237
|
+
|
238
|
+
v, mv = version.values
|
239
|
+
return { 'status' => 404, 'message' => format( 'only Grafana 5 has team support. you use version %s', v) } if(mv != 5)
|
240
|
+
|
241
|
+
if(team_id.is_a?(String))
|
242
|
+
o_team = search_team(name: team_id)
|
243
|
+
|
244
|
+
status = o_team.dig('status')
|
245
|
+
total_count = o_team.dig('totalCount')
|
246
|
+
|
247
|
+
return { 'status' => 404, 'message' => format( 'No Team \'%s\' found', team_id) } unless(status == 200 && total_count > 0)
|
248
|
+
|
249
|
+
teams = o_team.dig('teams')
|
250
|
+
team = teams.detect { |x| x['name'] == team_id }
|
251
|
+
|
252
|
+
team_id = team.dig('id')
|
253
|
+
end
|
254
|
+
|
255
|
+
endpoint = format( '/api/teams/%s/members', team_id )
|
256
|
+
|
257
|
+
@logger.debug("Getting team by Id #{team_id} (GET #{endpoint})") if @debug
|
258
|
+
|
259
|
+
get(endpoint)
|
260
|
+
end
|
261
|
+
|
262
|
+
# http://docs.grafana.org/http_api/team/#add-team-member
|
263
|
+
#
|
264
|
+
# POST /api/teams/:teamId/members
|
265
|
+
#
|
266
|
+
#
|
267
|
+
#
|
268
|
+
def add_team_member(params)
|
269
|
+
|
270
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
271
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
272
|
+
|
273
|
+
v, mv = version.values
|
274
|
+
return { 'status' => 404, 'message' => format( 'only Grafana 5 has team support. you use version %s', v) } if(mv != 5)
|
275
|
+
|
276
|
+
team_id = validate( params, required: true , var: 'team_id' )
|
277
|
+
user_id = validate( params, required: false, var: 'user_id' )
|
278
|
+
|
279
|
+
if( user_id.is_a?(String) && user_id.is_a?(Integer) )
|
280
|
+
raise ArgumentError.new(format('wrong type. user \'user_id\' must be an String (for an User name) or an Integer (for an User Id), given \'%s\'', user_id.class.to_s))
|
281
|
+
end
|
282
|
+
raise ArgumentError.new('missing \'user_id\'') if( user_id.size.zero? )
|
283
|
+
|
284
|
+
if(team_id.is_a?(String))
|
285
|
+
o_team = search_team(name: team_id)
|
286
|
+
status = o_team.dig('status')
|
287
|
+
total_count = o_team.dig('totalCount')
|
288
|
+
|
289
|
+
return { 'status' => 404, 'message' => format( 'No Team \'%s\' found', team_id) } unless(status == 200 && total_count > 0)
|
290
|
+
|
291
|
+
teams = o_team.dig('teams')
|
292
|
+
team = teams.detect { |x| x['name'] == team_id }
|
293
|
+
|
294
|
+
team_id = team.dig('id')
|
295
|
+
end
|
296
|
+
|
297
|
+
if(user_id.is_a?(String))
|
298
|
+
usr = user(user_id)
|
299
|
+
status = usr.dig('status')
|
300
|
+
user_id = usr.dig('id')
|
301
|
+
|
302
|
+
return { 'status' => 404, 'message' => format( 'No User \'%s\' found', user_id) } unless(status == 200)
|
303
|
+
end
|
304
|
+
|
305
|
+
payload = {
|
306
|
+
userId: user_id
|
307
|
+
}
|
308
|
+
payload.reject!{ |_, y| y.nil? }
|
309
|
+
|
310
|
+
endpoint = format( '/api/teams/%d/members', team_id )
|
311
|
+
|
312
|
+
post( endpoint, payload.to_json )
|
313
|
+
end
|
314
|
+
|
315
|
+
# http://docs.grafana.org/http_api/team/#remove-member-from-team
|
316
|
+
#
|
317
|
+
# DELETE /api/teams/:teamId/members/:userId
|
318
|
+
#
|
319
|
+
#
|
320
|
+
#
|
321
|
+
def remove_team_member(params)
|
322
|
+
|
323
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
324
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
325
|
+
|
326
|
+
v, mv = version.values
|
327
|
+
return { 'status' => 404, 'message' => format( 'only Grafana 5 has team support. you use version %s', v) } if(mv != 5)
|
328
|
+
|
329
|
+
team_id = validate( params, required: true , var: 'team_id' )
|
330
|
+
user_id = validate( params, required: false, var: 'user_id' )
|
331
|
+
|
332
|
+
if( user_id.is_a?(String) && user_id.is_a?(Integer) )
|
333
|
+
raise ArgumentError.new(format('wrong type. user \'user_id\' must be an String (for an User name) or an Integer (for an User Id), given \'%s\'', user_id.class.to_s))
|
334
|
+
end
|
335
|
+
raise ArgumentError.new('missing \'user_id\'') if( user_id.size.zero? )
|
336
|
+
|
337
|
+
if(team_id.is_a?(String))
|
338
|
+
o_team = search_team(name: team_id)
|
339
|
+
status = o_team.dig('status')
|
340
|
+
total_count = o_team.dig('totalCount')
|
341
|
+
|
342
|
+
return { 'status' => 404, 'message' => format( 'No Team \'%s\' found', team_id) } unless(status == 200 && total_count > 0)
|
343
|
+
|
344
|
+
teams = o_team.dig('teams')
|
345
|
+
team = teams.detect { |x| x['name'] == team_id }
|
346
|
+
|
347
|
+
team_id = team.dig('id')
|
348
|
+
end
|
349
|
+
|
350
|
+
if(user_id.is_a?(String))
|
351
|
+
usr = user(user_id)
|
352
|
+
status = usr.dig('status')
|
353
|
+
usr_id = usr.dig('id')
|
354
|
+
|
355
|
+
return { 'status' => 404, 'message' => format( 'No User \'%s\' found', user_id) } unless(status == 200)
|
356
|
+
end
|
357
|
+
|
358
|
+
endpoint = format( '/api/teams/%d/members/%d', team_id, usr_id )
|
359
|
+
|
360
|
+
delete(endpoint)
|
361
|
+
end
|
362
|
+
|
363
|
+
end
|
364
|
+
end
|