From c3708b3096fa88cc8c6b9fdec8bdec671bca9787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 27 May 2016 16:11:05 +0200 Subject: [PATCH] feat(import): import directly from grafana.net id/url now works --- pkg/api/gnetproxy.go | 13 ++++-- public/app/features/dashboard/all.js | 2 +- .../dashboard/export/export_modal.html | 10 ---- .../dashboard/import/dash_import.html | 46 +++++++++++++------ .../features/dashboard/import/dash_import.ts | 42 ++++++++++++++++- .../features/dashboard/partials/settings.html | 2 +- .../dashboard/specs/dash_import_ctrl_specs.ts | 38 ++++++++++++++- public/sass/components/_gf-form.scss | 4 ++ 8 files changed, 123 insertions(+), 34 deletions(-) diff --git a/pkg/api/gnetproxy.go b/pkg/api/gnetproxy.go index 344cd3fe926..8425f0e41b7 100644 --- a/pkg/api/gnetproxy.go +++ b/pkg/api/gnetproxy.go @@ -5,8 +5,10 @@ import ( "net" "net/http" "net/http/httputil" + "net/url" "time" + "github.com/Unknwon/log" "github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/util" @@ -23,12 +25,15 @@ var gNetProxyTransport = &http.Transport{ } func ReverseProxyGnetReq(proxyPath string) *httputil.ReverseProxy { + url, _ := url.Parse(setting.GrafanaNetUrl) + director := func(req *http.Request) { - req.URL.Scheme = "https" - req.URL.Host = "grafana.net" - req.Host = "grafana.net" + req.URL.Scheme = url.Scheme + req.URL.Host = url.Host + req.Host = url.Host - req.URL.Path = util.JoinUrlFragments(setting.GrafanaNetUrl+"/api", proxyPath) + req.URL.Path = util.JoinUrlFragments(url.Path+"/api", proxyPath) + log.Info("Url: %v", req.URL.Path) // clear cookie headers req.Header.Del("Cookie") diff --git a/public/app/features/dashboard/all.js b/public/app/features/dashboard/all.js index 15c765f2281..6aea2efa9f1 100644 --- a/public/app/features/dashboard/all.js +++ b/public/app/features/dashboard/all.js @@ -17,7 +17,7 @@ define([ './importCtrl', './impression_store', './upload', - './import/import', + './import/dash_import', './export/export_modal', './dash_list_ctrl', ], function () {}); diff --git a/public/app/features/dashboard/export/export_modal.html b/public/app/features/dashboard/export/export_modal.html index 7241a210bf5..e890e405020 100644 --- a/public/app/features/dashboard/export/export_modal.html +++ b/public/app/features/dashboard/export/export_modal.html @@ -15,16 +15,6 @@ You can share dashboards on Grafana.net

-
-
- - - -
-
-
+ Cancel + Back
diff --git a/public/app/features/dashboard/import/dash_import.ts b/public/app/features/dashboard/import/dash_import.ts index 2bb6508ff01..7a3e5b3ae3d 100644 --- a/public/app/features/dashboard/import/dash_import.ts +++ b/public/app/features/dashboard/import/dash_import.ts @@ -14,11 +14,20 @@ export class DashImportCtrl { dash: any; inputs: any[]; inputsValid: boolean; + gnetUrl: string; + gnetError: string; + gnetInfo: any; /** @ngInject */ - constructor(private backendSrv, private $location, private $scope) { + constructor(private backendSrv, private $location, private $scope, private $routeParams) { this.step = 1; this.nameExists = false; + + // check gnetId in url + if ($routeParams.gnetId) { + this.gnetUrl = $routeParams.gnetId ; + this.checkGnetDashboard(); + } } onUpload(dash) { @@ -121,6 +130,37 @@ export class DashImportCtrl { } } + checkGnetDashboard() { + this.gnetError = ''; + + var match = /(^\d+$)|dashboards\/(\d+)/.exec(this.gnetUrl); + var dashboardId; + + if (match && match[1]) { + dashboardId = match[1]; + } else if (match && match[2]) { + dashboardId = match[2]; + } else { + this.gnetError = 'Could not find dashboard'; + } + + return this.backendSrv.get('api/gnet/dashboards/' + dashboardId).then(res => { + this.gnetInfo = res; + // store reference to grafana.net + res.json.gnetId = dashboardId; + this.onUpload(res.json); + }).catch(err => { + this.gnetError = err.message || err; + }); + } + + back() { + this.gnetUrl = ''; + this.step = 1; + this.gnetError = ''; + this.gnetInfo = ''; + } + } export function dashImportDirective() { diff --git a/public/app/features/dashboard/partials/settings.html b/public/app/features/dashboard/partials/settings.html index bd198fffa38..3aa4638aea4 100644 --- a/public/app/features/dashboard/partials/settings.html +++ b/public/app/features/dashboard/partials/settings.html @@ -22,7 +22,7 @@
Details
- +
diff --git a/public/app/features/dashboard/specs/dash_import_ctrl_specs.ts b/public/app/features/dashboard/specs/dash_import_ctrl_specs.ts index 77fb3b30978..9e56a5081e3 100644 --- a/public/app/features/dashboard/specs/dash_import_ctrl_specs.ts +++ b/public/app/features/dashboard/specs/dash_import_ctrl_specs.ts @@ -7,6 +7,7 @@ describe('DashImportCtrl', function() { var ctx: any = {}; var backendSrv = { search: sinon.stub().returns(Promise.resolve([])), + get: sinon.stub() }; beforeEach(angularMocks.module('grafana.core')); @@ -20,7 +21,7 @@ describe('DashImportCtrl', function() { }); })); - describe('when upload json', function() { + describe('when uploading json', function() { beforeEach(function() { config.datasources = { ds: { @@ -37,14 +38,47 @@ describe('DashImportCtrl', function() { it('should build input model', function() { expect(ctx.ctrl.inputs.length).to.eql(1); - expect(ctx.ctrl.inputs[0].label).to.eql(1); + expect(ctx.ctrl.inputs[0].name).to.eql('ds'); + expect(ctx.ctrl.inputs[0].info).to.eql('Select a Test DB data source'); }); it('should set inputValid to false', function() { expect(ctx.ctrl.inputsValid).to.eql(false); }); + }); + + describe('when specifing grafana.net url', function() { + beforeEach(function() { + ctx.ctrl.gnetUrl = 'http://grafana.net/dashboards/123'; + // setup api mock + backendSrv.get = sinon.spy(() => { + return Promise.resolve({ + }); + }); + ctx.ctrl.checkGnetDashboard(); + }); + + it('should call gnet api with correct dashboard id', function() { + expect(backendSrv.get.getCall(0).args[0]).to.eql('api/gnet/dashboards/123'); + }); + }); + describe('when specifing dashbord id', function() { + beforeEach(function() { + ctx.ctrl.gnetUrl = '2342'; + // setup api mock + backendSrv.get = sinon.spy(() => { + return Promise.resolve({ + }); + }); + ctx.ctrl.checkGnetDashboard(); + }); + + it('should call gnet api with correct dashboard id', function() { + expect(backendSrv.get.getCall(0).args[0]).to.eql('api/gnet/dashboards/2342'); + }); }); + }); diff --git a/public/sass/components/_gf-form.scss b/public/sass/components/_gf-form.scss index 0ae9f7ebb71..602ea9ef5c1 100644 --- a/public/sass/components/_gf-form.scss +++ b/public/sass/components/_gf-form.scss @@ -158,6 +158,10 @@ $gf-form-margin: 0.25rem; color: transparent; text-shadow: 0 0 0 $text-color; } + + &.ng-empty { + color: $text-color-weak; + } } &:after {