Commit 9413ce5e by Daniel Lee Committed by GitHub

Merge pull request #8096 from ryantxu/influx-db-query2

let the influxdb API set a database
parents 511b34eb a04c4ba4
...@@ -189,12 +189,6 @@ func (proxy *DataSourceProxy) getDirector() func(req *http.Request) { ...@@ -189,12 +189,6 @@ func (proxy *DataSourceProxy) getDirector() func(req *http.Request) {
} }
func (proxy *DataSourceProxy) validateRequest() error { func (proxy *DataSourceProxy) validateRequest() error {
if proxy.ds.Type == m.DS_INFLUXDB {
if proxy.ctx.Query("db") != proxy.ds.Database {
return errors.New("Datasource is not configured to allow this database")
}
}
if !checkWhiteList(proxy.ctx, proxy.targetUrl.Host) { if !checkWhiteList(proxy.ctx, proxy.targetUrl.Host) {
return errors.New("Target url is not a valid target") return errors.New("Target url is not a valid target")
} }
......
...@@ -82,7 +82,7 @@ export default class InfluxDatasource { ...@@ -82,7 +82,7 @@ export default class InfluxDatasource {
// replace templated variables // replace templated variables
allQueries = this.templateSrv.replace(allQueries, scopedVars); allQueries = this.templateSrv.replace(allQueries, scopedVars);
return this._seriesQuery(allQueries).then((data): any => { return this._seriesQuery(allQueries, options).then((data): any => {
if (!data || !data.results) { if (!data || !data.results) {
return []; return [];
} }
...@@ -135,7 +135,7 @@ export default class InfluxDatasource { ...@@ -135,7 +135,7 @@ export default class InfluxDatasource {
var query = options.annotation.query.replace('$timeFilter', timeFilter); var query = options.annotation.query.replace('$timeFilter', timeFilter);
query = this.templateSrv.replace(query, null, 'regex'); query = this.templateSrv.replace(query, null, 'regex');
return this._seriesQuery(query).then(data => { return this._seriesQuery(query, options).then(data => {
if (!data || !data.results || !data.results[0]) { if (!data || !data.results || !data.results[0]) {
throw { message: 'No results in response from InfluxDB' }; throw { message: 'No results in response from InfluxDB' };
} }
...@@ -164,30 +164,30 @@ export default class InfluxDatasource { ...@@ -164,30 +164,30 @@ export default class InfluxDatasource {
return false; return false;
} }
metricFindQuery(query) { metricFindQuery(query: string, options?: any) {
var interpolated = this.templateSrv.replace(query, null, 'regex'); var interpolated = this.templateSrv.replace(query, null, 'regex');
return this._seriesQuery(interpolated).then(_.curry(this.responseParser.parse)(query)); return this._seriesQuery(interpolated, options).then(_.curry(this.responseParser.parse)(query));
} }
getTagKeys(options) { getTagKeys(options) {
var queryBuilder = new InfluxQueryBuilder({ measurement: '', tags: [] }, this.database); var queryBuilder = new InfluxQueryBuilder({ measurement: '', tags: [] }, this.database);
var query = queryBuilder.buildExploreQuery('TAG_KEYS'); var query = queryBuilder.buildExploreQuery('TAG_KEYS');
return this.metricFindQuery(query); return this.metricFindQuery(query, options);
} }
getTagValues(options) { getTagValues(options) {
var queryBuilder = new InfluxQueryBuilder({ measurement: '', tags: [] }, this.database); var queryBuilder = new InfluxQueryBuilder({ measurement: '', tags: [] }, this.database);
var query = queryBuilder.buildExploreQuery('TAG_VALUES', options.key); var query = queryBuilder.buildExploreQuery('TAG_VALUES', options.key);
return this.metricFindQuery(query); return this.metricFindQuery(query, options);
} }
_seriesQuery(query) { _seriesQuery(query: string, options?: any) {
if (!query) { if (!query) {
return this.$q.when({ results: [] }); return this.$q.when({ results: [] });
} }
return this._influxRequest('GET', '/query', { q: query, epoch: 'ms' }); return this._influxRequest('GET', '/query', { q: query, epoch: 'ms' }, options);
} }
serializeParams(params) { serializeParams(params) {
...@@ -225,21 +225,21 @@ export default class InfluxDatasource { ...@@ -225,21 +225,21 @@ export default class InfluxDatasource {
}); });
} }
_influxRequest(method, url, data) { _influxRequest(method: string, url: string, data: any, options?: any) {
var self = this; const currentUrl = this.urls.shift();
this.urls.push(currentUrl);
var currentUrl = self.urls.shift(); let params: any = {};
self.urls.push(currentUrl);
var params: any = {}; if (this.username) {
params.u = this.username;
if (self.username) { params.p = this.password;
params.u = self.username;
params.p = self.password;
} }
if (self.database) { if (options && options.database) {
params.db = self.database; params.db = options.database;
} else if (this.database) {
params.db = this.database;
} }
if (method === 'GET') { if (method === 'GET') {
...@@ -247,7 +247,7 @@ export default class InfluxDatasource { ...@@ -247,7 +247,7 @@ export default class InfluxDatasource {
data = null; data = null;
} }
var options: any = { let req: any = {
method: method, method: method,
url: currentUrl + url, url: currentUrl + url,
params: params, params: params,
...@@ -257,15 +257,15 @@ export default class InfluxDatasource { ...@@ -257,15 +257,15 @@ export default class InfluxDatasource {
paramSerializer: this.serializeParams, paramSerializer: this.serializeParams,
}; };
options.headers = options.headers || {}; req.headers = req.headers || {};
if (this.basicAuth || this.withCredentials) { if (this.basicAuth || this.withCredentials) {
options.withCredentials = true; req.withCredentials = true;
} }
if (self.basicAuth) { if (this.basicAuth) {
options.headers.Authorization = self.basicAuth; req.headers.Authorization = this.basicAuth;
} }
return this.backendSrv.datasourceRequest(options).then( return this.backendSrv.datasourceRequest(req).then(
result => { result => {
return result.data; return result.data;
}, },
......
...@@ -23,6 +23,20 @@ ...@@ -23,6 +23,20 @@
</div> </div>
</div> </div>
<div class="gf-form-group">
<div class="grafana-info-box">
<h5>Database Access</h5>
<p>
Setting the database for this datasource does not deny access to other databases. The InfluxDB query syntax allows
switching the database in the query. For example:
<code>SHOW MEASUREMENTS ON _internal</code> or <code>SELECT * FROM "_internal".."database" LIMIT 10</code>
<br/><br/>
To support data isolation and security, make sure appropriate permissions are configured in InfluxDB.
</p>
</div>
</div>
<div class="gf-form-group"> <div class="gf-form-group">
<div class="gf-form-inline"> <div class="gf-form-inline">
<div class="gf-form"> <div class="gf-form">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment