grafana 0.9.0 → 0.10.1
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.
- 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/dashboard.rb
CHANGED
@@ -2,12 +2,36 @@
|
|
2
2
|
module Grafana
|
3
3
|
|
4
4
|
# http://docs.grafana.org/http_api/dashboard/
|
5
|
+
|
6
|
+
# The identifier (id) of a dashboard is an auto-incrementing numeric value and is only unique per Grafana install.
|
7
|
+
#
|
8
|
+
# The unique identifier (uid) of a dashboard can be used for uniquely identify a dashboard between multiple Grafana installs.
|
9
|
+
# It's automatically generated if not provided when creating a dashboard. The uid allows having consistent URL's for
|
10
|
+
# accessing dashboards and when syncing dashboards between multiple Grafana installs, see dashboard provisioning for
|
11
|
+
# more information. This means that changing the title of a dashboard will not break any bookmarked links to that dashboard.
|
12
|
+
#
|
13
|
+
# The uid can have a maximum length of 40 characters.
|
14
|
+
#
|
15
|
+
# Deprecated resources
|
16
|
+
# Please note that these resource have been deprecated and will be removed in a future release.
|
17
|
+
#
|
18
|
+
# - Get dashboard by slug
|
19
|
+
# - Delete dashboard by slug
|
20
|
+
#
|
21
|
+
#
|
5
22
|
#
|
6
23
|
module Dashboard
|
7
24
|
|
8
|
-
|
25
|
+
# http://docs.grafana.org/http_api/dashboard/#get-dashboard-by-slug
|
26
|
+
# - Deprecated starting from Grafana v5.0.
|
27
|
+
# Please update to use the new Get dashboard by uid resource instead
|
28
|
+
#
|
9
29
|
# Get dashboard
|
10
30
|
#
|
31
|
+
# Will return the dashboard given the dashboard slug.
|
32
|
+
# Slug is the url friendly version of the dashboard title.
|
33
|
+
# If there exists multiple dashboards with the same slug, one of them will be returned in the response.
|
34
|
+
#
|
11
35
|
# @example
|
12
36
|
# dashboard('dashboard for many foo')
|
13
37
|
#
|
@@ -18,23 +42,71 @@ module Grafana
|
|
18
42
|
raise ArgumentError.new(format('wrong type. \'name\' must be an String, given \'%s\'', name.class.to_s)) unless( name.is_a?(String) )
|
19
43
|
raise ArgumentError.new('missing name') if( name.size.zero? )
|
20
44
|
|
45
|
+
# v, mv = version.values
|
46
|
+
#
|
47
|
+
# if( mv == 5)
|
48
|
+
# puts 'DEPRICATION WARNING'
|
49
|
+
# puts 'Grafana v5.0 use a new interal id/uid handling'
|
50
|
+
# puts 'This function works well with Grafana v4.x'
|
51
|
+
# end
|
52
|
+
|
21
53
|
endpoint = format( '/api/dashboards/db/%s', slug(name) )
|
54
|
+
@logger.debug( "Attempting to get dashboard (GET #{endpoint})" ) if @debug
|
55
|
+
|
56
|
+
get( endpoint )
|
57
|
+
end
|
58
|
+
|
59
|
+
# http://docs.grafana.org/http_api/dashboard/#get-dashboard-by-uid
|
60
|
+
#
|
61
|
+
# GET /api/dashboards/uid/:uid
|
62
|
+
# Will return the dashboard given the dashboard unique identifier (uid).
|
63
|
+
#
|
64
|
+
# Get dashboard
|
65
|
+
#
|
66
|
+
# Will return the dashboard given the dashboard unique identifier (uid).
|
67
|
+
#
|
68
|
+
# @example
|
69
|
+
# dashboard('L42r6NWiz')
|
70
|
+
#
|
71
|
+
# @return [String]
|
72
|
+
#
|
73
|
+
def dashboard_by_uid( uid )
|
74
|
+
|
75
|
+
if( uid.is_a?(String) && uid.is_a?(Integer) )
|
76
|
+
raise ArgumentError.new(format('wrong type. dashboard \'uid\' must be an String (for an title name) or an Integer (for an Datasource Id), given \'%s\'', uid.class.to_s))
|
77
|
+
end
|
78
|
+
raise ArgumentError.new('missing \'uid\'') if( uid.size.zero? )
|
22
79
|
|
23
|
-
|
80
|
+
v, mv = version.values
|
81
|
+
return { 'status' => 404, 'message' => format( 'only Grafana 5 has uid support. you use version %s', v) } if(mv != 5)
|
82
|
+
|
83
|
+
return { 'status' => 404, 'message' => format( 'The uid can have a maximum length of 40 characters. \'%s\' given', uid.length) } if( uid.length > 40 )
|
84
|
+
|
85
|
+
endpoint = format( '/api/dashboards/uid/%s', uid )
|
86
|
+
@logger.debug( "Attempting to get dashboard (GET #{endpoint})" ) if @debug
|
24
87
|
|
25
88
|
get( endpoint )
|
26
89
|
end
|
27
90
|
|
91
|
+
|
28
92
|
# Create / Update dashboard
|
29
93
|
#
|
94
|
+
# Creates a new dashboard or updates an existing dashboard.
|
95
|
+
#
|
30
96
|
# @param [Hash] params
|
31
|
-
# @option params [Hash] dashboard
|
97
|
+
# @option params [Hash] dashboard The complete dashboard model
|
98
|
+
# - dashboard.id - id = null to create a new dashboard.
|
99
|
+
# - dashboard.uid - Optional unique identifier when creating a dashboard. uid = null will generate a new uid.
|
100
|
+
# - folderId - The id of the folder to save the dashboard in.
|
101
|
+
# - overwrite - Set to true if you want to overwrite existing dashboard with newer version, same dashboard title in folder or same dashboard uid.
|
102
|
+
# - message - Set a commit message for the version history.
|
32
103
|
# @option params [Boolean] overwrite (true)
|
33
104
|
#
|
34
105
|
# @example
|
35
106
|
# params = {
|
36
107
|
# dashboard: {
|
37
108
|
# id: null,
|
109
|
+
# uid: null,
|
38
110
|
# title: 'Production Overview',
|
39
111
|
# tags: [ 'templated' ],
|
40
112
|
# timezone": 'browser',
|
@@ -45,7 +117,9 @@ module Grafana
|
|
45
117
|
# 'schemaVersion': 6,
|
46
118
|
# 'version': 0
|
47
119
|
# },
|
48
|
-
#
|
120
|
+
# folderId: 0,
|
121
|
+
# overwrite: false,
|
122
|
+
# message: 'created by foo'
|
49
123
|
# }
|
50
124
|
# create_dashboard( params )
|
51
125
|
#
|
@@ -57,11 +131,20 @@ module Grafana
|
|
57
131
|
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
58
132
|
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
59
133
|
|
60
|
-
dashboard = validate( params, required: true, var: 'dashboard', type: Hash )
|
134
|
+
dashboard = validate( params, required: true , var: 'dashboard', type: Hash )
|
61
135
|
overwrite = validate( params, required: false, var: 'overwrite', type: Boolean ) || true
|
136
|
+
folder_id = validate( params, required: false, var: 'folderId' )
|
137
|
+
message = validate( params, required: false, var: 'message', type: String )
|
62
138
|
|
63
139
|
dashboard = regenerate_template_ids( dashboard )
|
64
140
|
|
141
|
+
unless(folder_id.nil?)
|
142
|
+
f_folder = folder(folder_id)
|
143
|
+
return { 'status' => 404, 'message' => format( 'No Folder \'%s\' found', folder_id) } if( f_folder.dig('status') != 200 )
|
144
|
+
|
145
|
+
folder_id = f_folder.dig('id')
|
146
|
+
end
|
147
|
+
|
65
148
|
db = JSON.parse( dashboard ) if( dashboard.is_a?(String) )
|
66
149
|
title = db.dig('dashboard','title')
|
67
150
|
|
@@ -69,7 +152,9 @@ module Grafana
|
|
69
152
|
|
70
153
|
payload = {
|
71
154
|
dashboard: db.dig('dashboard'),
|
72
|
-
overwrite: overwrite
|
155
|
+
overwrite: overwrite,
|
156
|
+
folderId: folder_id,
|
157
|
+
message: message
|
73
158
|
}
|
74
159
|
payload.reject!{ |_k, v| v.nil? }
|
75
160
|
|
@@ -78,7 +163,12 @@ module Grafana
|
|
78
163
|
post( endpoint, payload.to_json )
|
79
164
|
end
|
80
165
|
|
166
|
+
# http://docs.grafana.org/http_api/dashboard/#delete-dashboard-by-slug
|
167
|
+
# - Deprecated starting from Grafana v5.0.
|
168
|
+
# Please update to use the new Get dashboard by uid resource instead
|
169
|
+
#
|
81
170
|
# Delete dashboard
|
171
|
+
# Will delete the dashboard given the specified slug. Slug is the url friendly version of the dashboard title.
|
82
172
|
#
|
83
173
|
# @example
|
84
174
|
# delete_dashboard('dashboard for many foo')
|
@@ -0,0 +1,132 @@
|
|
1
|
+
|
2
|
+
module Grafana
|
3
|
+
|
4
|
+
# http://docs.grafana.org/http_api/dashboard_permissions/#dashboard-permissions-api
|
5
|
+
#
|
6
|
+
# This API can be used to update/get the permissions for a dashboard.
|
7
|
+
# Permissions with dashboardId=-1 are the default permissions for users with the Viewer and Editor roles.
|
8
|
+
# Permissions can be set for a user, a team or a role (Viewer or Editor).
|
9
|
+
# Permissions cannot be set for Admins - they always have access to everything.
|
10
|
+
#
|
11
|
+
# The permission levels for the permission field:
|
12
|
+
#
|
13
|
+
# 1 = View
|
14
|
+
# 2 = Edit
|
15
|
+
# 4 = Admin
|
16
|
+
#
|
17
|
+
module DashboardPermissions
|
18
|
+
|
19
|
+
# http://docs.grafana.org/http_api/dashboard_permissions/#get-permissions-for-a-dashboard
|
20
|
+
#
|
21
|
+
# GET /api/dashboards/id/:dashboardId/permissions
|
22
|
+
#
|
23
|
+
# Gets all existing permissions for the dashboard with the given dashboardId.
|
24
|
+
#
|
25
|
+
def dashboard_permissions(uid)
|
26
|
+
|
27
|
+
if( uid.is_a?(String) && uid.is_a?(Integer) )
|
28
|
+
raise ArgumentError.new(format('wrong type. dashboard \'uid\' must be an String (for an title name) or an Integer (for an Datasource Id), given \'%s\'', uid.class.to_s))
|
29
|
+
end
|
30
|
+
raise ArgumentError.new('missing \'uid\'') if( uid.size.zero? )
|
31
|
+
|
32
|
+
endpoint = format( '/api/dashboards/id/%s/permissions', uid )
|
33
|
+
@logger.debug( "Attempting to get dashboard permissions (GET #{endpoint})" ) if @debug
|
34
|
+
|
35
|
+
r = get( endpoint )
|
36
|
+
r['message'] = format('dashboard \'%s\' not found', uid) if(r.dig('status') == 404)
|
37
|
+
r
|
38
|
+
end
|
39
|
+
|
40
|
+
# http://docs.grafana.org/http_api/dashboard_permissions/#update-permissions-for-a-dashboard
|
41
|
+
#
|
42
|
+
# POST /api/dashboards/id/:dashboardId/permissions
|
43
|
+
#
|
44
|
+
# Updates permissions for a dashboard.
|
45
|
+
# This operation will remove existing permissions if they're not included in the request.
|
46
|
+
#
|
47
|
+
#
|
48
|
+
def update_dashboad_permissions(params)
|
49
|
+
|
50
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
51
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
52
|
+
|
53
|
+
v, mv = version.values
|
54
|
+
return { 'status' => 404, 'message' => format( 'only Grafana 5 has folder support. you use version %s', v) } if(mv != 5)
|
55
|
+
|
56
|
+
dashboard_id = validate( params, required: true, var: 'dashboard_id', type: Integer )
|
57
|
+
permissions = validate( params, required: true, var: 'permissions' , type: Hash )
|
58
|
+
|
59
|
+
return { 'status' => 404, 'message' => 'no permissions given' } if( permissions.size.zero? )
|
60
|
+
|
61
|
+
valid_roles = %w[View Edit Admin]
|
62
|
+
|
63
|
+
c_team = permissions.dig('team')
|
64
|
+
c_user = permissions.dig('user')
|
65
|
+
team = []
|
66
|
+
user = []
|
67
|
+
|
68
|
+
unless(c_team.nil?)
|
69
|
+
check_keys = []
|
70
|
+
|
71
|
+
c_team.uniq.each do |x|
|
72
|
+
k = x.keys.first
|
73
|
+
v = x.values.first
|
74
|
+
r = validate_hash( v, valid_roles )
|
75
|
+
|
76
|
+
f_team = team(k)
|
77
|
+
team_id = f_team.dig('id')
|
78
|
+
|
79
|
+
next unless(( f_team.dig('status') == 200) && !check_keys.include?(team_id) && r == true )
|
80
|
+
|
81
|
+
check_keys << team_id
|
82
|
+
|
83
|
+
role_id = valid_roles.index(v)
|
84
|
+
role_id += 1
|
85
|
+
role_id += 1 if(v == 'Admin')
|
86
|
+
|
87
|
+
team << {
|
88
|
+
teamId: team_id,
|
89
|
+
permission: role_id
|
90
|
+
}
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
unless(c_user.nil?)
|
95
|
+
check_keys = []
|
96
|
+
|
97
|
+
c_user.uniq.each do |x|
|
98
|
+
k = x.keys.first
|
99
|
+
v = x.values.first
|
100
|
+
r = validate_hash( v, valid_roles )
|
101
|
+
|
102
|
+
f_user = user(k)
|
103
|
+
user_id = f_user.dig('id')
|
104
|
+
|
105
|
+
next unless(( f_user.dig('status') == 200) && !check_keys.include?(user_id) && r == true )
|
106
|
+
|
107
|
+
check_keys << user_id
|
108
|
+
|
109
|
+
role_id = valid_roles.index(v)
|
110
|
+
role_id += 1
|
111
|
+
role_id += 1 if(v == 'Admin')
|
112
|
+
|
113
|
+
user << {
|
114
|
+
userId: user_id,
|
115
|
+
permission: role_id
|
116
|
+
}
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
payload = {
|
121
|
+
items: team + user
|
122
|
+
}
|
123
|
+
payload.reject!{ |_, y| y.nil? }
|
124
|
+
|
125
|
+
|
126
|
+
endpoint = format('/api/dashboards/id/%s/permissions', dashboard_id)
|
127
|
+
post(endpoint, payload.to_json)
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
@@ -1,29 +1,125 @@
|
|
1
1
|
|
2
2
|
module Grafana
|
3
3
|
|
4
|
-
# http://docs.grafana.org/http_api/
|
4
|
+
# http://docs.grafana.org/http_api/dashboard_versions
|
5
5
|
#
|
6
6
|
module DashboardVersions
|
7
7
|
|
8
8
|
# Get all dashboard versions
|
9
9
|
# http://docs.grafana.org/http_api/dashboard_versions/#get-all-dashboard-versions
|
10
10
|
# GET /api/dashboards/id/:dashboardId/versions
|
11
|
-
|
11
|
+
#
|
12
|
+
#
|
13
|
+
#
|
14
|
+
#
|
15
|
+
#
|
16
|
+
#
|
17
|
+
def dashboard_all_versions( params )
|
18
|
+
|
19
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
20
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
21
|
+
|
22
|
+
# v, mv = version.values
|
23
|
+
# return { 'status' => 404, 'message' => format( 'only Grafana 5 has dashboard version support. you use version %s', v) } if(mv != 5)
|
24
|
+
|
25
|
+
dashboard_id = validate( params, required: true , var: 'dashboard_id', type: Integer )
|
26
|
+
start = validate( params, required: false, var: 'start' , type: Integer )
|
27
|
+
limit = validate( params, required: false, var: 'limit' , type: Integer )
|
28
|
+
|
29
|
+
api = []
|
30
|
+
api << format( 'start=%s', start ) unless( start.nil? )
|
31
|
+
api << format( 'limit=%s', limit ) unless( limit.nil? )
|
32
|
+
api = api.join( '&' )
|
33
|
+
|
34
|
+
endpoint = format('/api/dashboards/id/%s/versions?%s', dashboard_id, api)
|
35
|
+
get(endpoint)
|
36
|
+
end
|
12
37
|
|
13
38
|
# Get dashboard version
|
14
39
|
# http://docs.grafana.org/http_api/dashboard_versions/#get-dashboard-version
|
15
40
|
# GET /api/dashboards/id/:dashboardId/versions/:id
|
16
|
-
def dashboard_version( params )
|
41
|
+
def dashboard_version( params )
|
42
|
+
|
43
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
44
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
45
|
+
|
46
|
+
v, mv = version.values
|
47
|
+
return { 'status' => 404, 'message' => format( 'only Grafana 5 has folder support. you use version %s', v) } if(mv != 5)
|
48
|
+
|
49
|
+
dashboard_id = validate( params, required: true, var: 'dashboard_id', type: Integer )
|
50
|
+
version = validate( params, required: true, var: 'version' , type: Integer )
|
51
|
+
|
52
|
+
endpoint = format('/api/dashboards/id/%s/versions/%s', dashboard_id, version)
|
53
|
+
|
54
|
+
r = get(endpoint)
|
55
|
+
|
56
|
+
r['message'] = format('no dashboard version \'%s\' for dashboard \'%s\' found', version, dashboard_id) if(r.dig('status') == 404)
|
57
|
+
r['message'] = format('no dashboard version \'%s\' for dashboard \'%s\' found', version, dashboard_id) if(r.dig('status') == 500)
|
58
|
+
r
|
59
|
+
end
|
17
60
|
|
18
61
|
# Restore dashboard
|
19
62
|
# http://docs.grafana.org/http_api/dashboard_versions/#restore-dashboard
|
20
63
|
# POST /api/dashboards/id/:dashboardId/restore
|
21
|
-
def restore_dashboard( params )
|
64
|
+
def restore_dashboard( params )
|
65
|
+
|
66
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
67
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
68
|
+
|
69
|
+
# v, mv = version.values
|
70
|
+
# return { 'status' => 404, 'message' => format( 'only Grafana 5 has folder support. you use version %s', v) } if(mv != 5)
|
71
|
+
|
72
|
+
dashboard_id = validate( params, required: true, var: 'dashboard_id', type: Integer )
|
73
|
+
version = validate( params, required: true, var: 'version' , type: Integer )
|
74
|
+
|
75
|
+
endpoint = format('/api/dashboards/id/%s/restore', dashboard_id)
|
76
|
+
|
77
|
+
payload = {
|
78
|
+
version: version
|
79
|
+
}
|
80
|
+
|
81
|
+
post(endpoint, payload.to_json)
|
82
|
+
end
|
22
83
|
|
23
84
|
# Compare dashboard versions
|
24
85
|
# http://docs.grafana.org/http_api/dashboard_versions/#compare-dashboard-versions
|
25
86
|
# POST /api/dashboards/calculate-diff
|
26
|
-
def compare_dashboard_version( params )
|
87
|
+
def compare_dashboard_version( params )
|
88
|
+
|
89
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
90
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
91
|
+
|
92
|
+
base = validate( params, required: true , var: 'base' , type: Hash )
|
93
|
+
new = validate( params, required: true , var: 'new' , type: Hash )
|
94
|
+
diff_type = validate( params, required: false, var: 'diff_type', type: String ) || 'json'
|
95
|
+
|
96
|
+
base_dashboard_id = validate( base, required: true , var: 'dashboard_id', type: Integer )
|
97
|
+
base_dashboard_version = validate( base, required: true , var: 'version' , type: Integer )
|
98
|
+
new_dashboard_id = validate( new , required: true , var: 'dashboard_id', type: Integer )
|
99
|
+
new_dashboard_version = validate( new , required: true , var: 'version' , type: Integer )
|
100
|
+
|
101
|
+
valid_diff_type = %w[json basic]
|
102
|
+
|
103
|
+
r = validate_hash(diff_type, valid_diff_type)
|
104
|
+
|
105
|
+
return r unless(r == true)
|
106
|
+
|
107
|
+
payload = {
|
108
|
+
base: {
|
109
|
+
dashboardId: base_dashboard_id,
|
110
|
+
version: base_dashboard_version
|
111
|
+
},
|
112
|
+
new: {
|
113
|
+
dashboardId: new_dashboard_id,
|
114
|
+
version: new_dashboard_version
|
115
|
+
},
|
116
|
+
diffType: diff_type
|
117
|
+
}
|
118
|
+
|
119
|
+
endpoint = '/api/dashboards/calculate-diff'
|
120
|
+
|
121
|
+
post(endpoint, payload.to_json)
|
122
|
+
end
|
27
123
|
|
28
124
|
end
|
29
125
|
|
data/lib/grafana/datasource.rb
CHANGED
@@ -44,7 +44,9 @@ module Grafana
|
|
44
44
|
#
|
45
45
|
def datasource( datasource_id )
|
46
46
|
|
47
|
-
|
47
|
+
if( datasource_id.is_a?(String) && datasource_id.is_a?(Integer) )
|
48
|
+
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))
|
49
|
+
end
|
48
50
|
raise ArgumentError.new('missing \'datasource_id\'') if( datasource_id.size.zero? )
|
49
51
|
|
50
52
|
if(datasource_id.is_a?(String))
|
@@ -105,27 +107,31 @@ module Grafana
|
|
105
107
|
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
106
108
|
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
107
109
|
|
108
|
-
name
|
109
|
-
organisation
|
110
|
-
type
|
111
|
-
new_name
|
112
|
-
database
|
113
|
-
access
|
114
|
-
default
|
115
|
-
user
|
116
|
-
password
|
117
|
-
url
|
118
|
-
json_data
|
119
|
-
ba_user
|
120
|
-
ba_password
|
121
|
-
basic_auth
|
122
|
-
basic_auth
|
123
|
-
org_id
|
124
|
-
|
125
|
-
|
126
|
-
|
110
|
+
name = validate( params, required: true , var: 'name' )
|
111
|
+
organisation = validate( params, required: false, var: 'organisation' )
|
112
|
+
type = validate( params, required: false, var: 'type', type: String )
|
113
|
+
new_name = validate( params, required: false, var: 'new_name', type: String )
|
114
|
+
database = validate( params, required: false, var: 'database', type: String )
|
115
|
+
access = validate( params, required: false, var: 'access', type: String ) || 'proxy'
|
116
|
+
default = validate( params, required: false, var: 'default', type: Boolean ) || false
|
117
|
+
user = validate( params, required: false, var: 'user', type: String )
|
118
|
+
password = validate( params, required: false, var: 'password', type: String )
|
119
|
+
url = validate( params, required: false, var: 'url', type: String )
|
120
|
+
json_data = validate( params, required: false, var: 'json_data', type: Hash )
|
121
|
+
ba_user = validate( params, required: false, var: 'basic_user', type: String )
|
122
|
+
ba_password = validate( params, required: false, var: 'basic_password', type: String )
|
123
|
+
basic_auth = false
|
124
|
+
basic_auth = true unless( ba_user.nil? && ba_password.nil? )
|
125
|
+
org_id = nil
|
126
|
+
|
127
|
+
if( name.is_a?(String) && name.is_a?(Integer) )
|
128
|
+
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))
|
129
|
+
end
|
127
130
|
if( organisation )
|
128
|
-
|
131
|
+
if( organisation.is_a?(String) && organisation.is_a?(Integer) )
|
132
|
+
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))
|
133
|
+
end
|
134
|
+
|
129
135
|
org = organization( organisation )
|
130
136
|
org_id = org.dig('id')
|
131
137
|
|
@@ -170,7 +176,7 @@ module Grafana
|
|
170
176
|
endpoint = format('/api/datasources/%d', datasource_id )
|
171
177
|
|
172
178
|
@logger.debug("Updating data source Id #{datasource_id} (GET #{endpoint})") if @debug
|
173
|
-
|
179
|
+
# logger.debug(payload.to_json) if(@debug)
|
174
180
|
|
175
181
|
put( endpoint, payload.to_json )
|
176
182
|
end
|
@@ -180,10 +186,8 @@ module Grafana
|
|
180
186
|
# @param [Hash] params
|
181
187
|
# @option params [String] type Datasource Type - (required) (grafana graphite cloudwatch elasticsearch prometheus influxdb mysql opentsdb postgres)
|
182
188
|
# @option params [String] name Datasource Name - (required)
|
183
|
-
# @option params [String] database Datasource Database - (required)
|
184
189
|
# @option params [String] access (proxy) Acess Type - (required) (proxy or direct)
|
185
190
|
# @option params [Boolean] default (false)
|
186
|
-
# @option params [String] password
|
187
191
|
# @option params [String] url Datasource URL - (required)
|
188
192
|
# @option params [Hash] json_data
|
189
193
|
# @option params [Hash] json_secure
|
@@ -194,7 +198,6 @@ module Grafana
|
|
194
198
|
# params = {
|
195
199
|
# name: 'graphite',
|
196
200
|
# type: 'graphite',
|
197
|
-
# database: 'graphite',
|
198
201
|
# url: 'http://localhost:8080'
|
199
202
|
# }
|
200
203
|
# create_datasource(params)
|
@@ -202,7 +205,6 @@ module Grafana
|
|
202
205
|
# params = {
|
203
206
|
# name: 'graphite',
|
204
207
|
# type: 'graphite',
|
205
|
-
# database: 'graphite',
|
206
208
|
# default: true,
|
207
209
|
# url: 'http://localhost:8080',
|
208
210
|
# json_data: { graphiteVersion: '1.1' }
|
@@ -231,13 +233,11 @@ module Grafana
|
|
231
233
|
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
232
234
|
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
233
235
|
|
234
|
-
type = validate( params, required: true, var: 'type', type: String )
|
235
|
-
name = validate( params, required: true, var: 'name', type: String )
|
236
|
-
database = validate( params, required: true, var: 'database', type: String )
|
236
|
+
type = validate( params, required: true , var: 'type', type: String )
|
237
|
+
name = validate( params, required: true , var: 'name', type: String )
|
237
238
|
access = validate( params, required: false, var: 'access', type: String ) || 'proxy'
|
238
239
|
default = validate( params, required: false, var: 'default', type: Boolean ) || false
|
239
|
-
|
240
|
-
url = validate( params, required: true, var: 'url', type: String )
|
240
|
+
url = validate( params, required: true , var: 'url', type: String )
|
241
241
|
json_data = validate( params, required: false, var: 'json_data', type: Hash )
|
242
242
|
json_secure = validate( params, required: false, var: 'json_secure', type: Hash )
|
243
243
|
ba_user = validate( params, required: false, var: 'basic_user', type: String )
|
@@ -262,14 +262,8 @@ module Grafana
|
|
262
262
|
jsonData: json_data,
|
263
263
|
secureJsonData: json_secure
|
264
264
|
}
|
265
|
-
|
266
265
|
payload.reject!{ |_k, v| v.nil? }
|
267
266
|
|
268
|
-
if( @debug )
|
269
|
-
logger.debug("Creating data source: #{name} (database: #{database})")
|
270
|
-
logger.debug( payload.to_json )
|
271
|
-
end
|
272
|
-
|
273
267
|
endpoint = '/api/datasources'
|
274
268
|
post(endpoint, payload.to_json)
|
275
269
|
end
|
@@ -286,7 +280,9 @@ module Grafana
|
|
286
280
|
#
|
287
281
|
def delete_datasource( datasource_id )
|
288
282
|
|
289
|
-
|
283
|
+
if( datasource_id.is_a?(String) && datasource_id.is_a?(Integer) )
|
284
|
+
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))
|
285
|
+
end
|
290
286
|
raise ArgumentError.new('missing \'datasource_id\'') if( datasource_id.size.zero? )
|
291
287
|
|
292
288
|
if(datasource_id.is_a?(String))
|