Merge branch 'master' of github.com:grafana/grafana

This commit is contained in:
Torkel Ödegaard
2016-03-08 15:32:15 +01:00
16 changed files with 275 additions and 115 deletions
+4
View File
@@ -174,6 +174,10 @@ func Register(r *macaron.Macaron) {
r.Get("/plugins", GetDataSourcePlugins)
}, reqOrgAdmin)
r.Group("/datasources/name/:name", func() {
r.Get("/", wrap(GetDataSourceByName))
}, reqOrgAdmin)
r.Get("/frontend/settings/", GetFrontendSettings)
r.Any("/datasources/proxy/:id/*", reqSignedIn, ProxyDataSourceRequest)
r.Any("/datasources/proxy/:id", reqSignedIn, ProxyDataSourceRequest)
+39 -17
View File
@@ -52,24 +52,9 @@ func GetDataSourceById(c *middleware.Context) Response {
}
ds := query.Result
dtos := convertModelToDtos(ds)
return Json(200, &dtos.DataSource{
Id: ds.Id,
OrgId: ds.OrgId,
Name: ds.Name,
Url: ds.Url,
Type: ds.Type,
Access: ds.Access,
Password: ds.Password,
Database: ds.Database,
User: ds.User,
BasicAuth: ds.BasicAuth,
BasicAuthUser: ds.BasicAuthUser,
BasicAuthPassword: ds.BasicAuthPassword,
WithCredentials: ds.WithCredentials,
IsDefault: ds.IsDefault,
JsonData: ds.JsonData,
})
return Json(200, &dtos)
}
func DeleteDataSource(c *middleware.Context) {
@@ -132,3 +117,40 @@ func GetDataSourcePlugins(c *middleware.Context) {
c.JSON(200, dsList)
}
}
// Get /api/datasources/name/:name
func GetDataSourceByName(c *middleware.Context) Response {
query := m.GetDataSourceByNameQuery{Name: c.Params(":name"), OrgId: c.OrgId}
if err := bus.Dispatch(&query); err != nil {
if err == m.ErrDataSourceNotFound {
return ApiError(404, "Data source not found", nil)
}
return ApiError(500, "Failed to query datasources", err)
}
ds := query.Result
dtos := convertModelToDtos(ds)
return Json(200, &dtos)
}
func convertModelToDtos(ds m.DataSource) dtos.DataSource {
return dtos.DataSource{
Id: ds.Id,
OrgId: ds.OrgId,
Name: ds.Name,
Url: ds.Url,
Type: ds.Type,
Access: ds.Access,
Password: ds.Password,
Database: ds.Database,
User: ds.User,
BasicAuth: ds.BasicAuth,
BasicAuthUser: ds.BasicAuthUser,
BasicAuthPassword: ds.BasicAuthPassword,
WithCredentials: ds.WithCredentials,
IsDefault: ds.IsDefault,
JsonData: ds.JsonData,
}
}
+27 -18
View File
@@ -26,8 +26,8 @@ func validateInput(c CommandLine, pluginFolder string) error {
return errors.New("missing path flag")
}
fileinfo, err := os.Stat(pluginDir)
if err != nil && !fileinfo.IsDir() {
fileInfo, err := os.Stat(pluginDir)
if err != nil && !fileInfo.IsDir() {
return errors.New("path is not a directory")
}
@@ -49,11 +49,12 @@ func installCommand(c CommandLine) error {
log.Infof("version: %v\n", version)
}
return InstallPlugin(pluginToInstall, pluginFolder, version, c.GlobalString("repo"))
return InstallPlugin(pluginToInstall, version, c)
}
func InstallPlugin(pluginName, pluginFolder, version, repoUrl string) error {
plugin, err := s.GetPlugin(pluginName, repoUrl)
func InstallPlugin(pluginName, version string, c CommandLine) error {
plugin, err := s.GetPlugin(pluginName, c.GlobalString("repo"))
pluginFolder := c.GlobalString("path")
if err != nil {
return err
}
@@ -85,7 +86,7 @@ func InstallPlugin(pluginName, pluginFolder, version, repoUrl string) error {
res, _ := s.ReadPlugin(pluginFolder, pluginName)
for _, v := range res.Dependency.Plugins {
InstallPlugin(v.Id, pluginFolder, "", repoUrl)
InstallPlugin(v.Id, version, c)
log.Infof("Installed Dependency: %v ✔\n", v.Id)
}
@@ -106,12 +107,26 @@ func SelectVersion(plugin m.Plugin, version string) (m.Version, error) {
return m.Version{}, errors.New("Could not find the version your looking for")
}
func RemoveGitBuildFromname(pluginname, filename string) string {
func RemoveGitBuildFromName(pluginName, filename string) string {
r := regexp.MustCompile("^[a-zA-Z0-9_.-]*/")
return r.ReplaceAllString(filename, pluginname+"/")
return r.ReplaceAllString(filename, pluginName+"/")
}
func downloadFile(pluginName, filepath, url string) (err error) {
var retryCount = 0
func downloadFile(pluginName, filePath, url string) (err error) {
defer func() {
if r := recover(); r != nil {
retryCount++
if retryCount == 1 {
log.Debug("\nFailed downloading. Will retry once.\n")
downloadFile(pluginName, filePath, url)
} else {
panic(r)
}
}
}()
resp, err := http.Get(url)
if err != nil {
return err
@@ -122,24 +137,18 @@ func downloadFile(pluginName, filepath, url string) (err error) {
if err != nil {
return err
}
log.Infof("Got statuscode %s from %s\n", resp.Status, url)
if resp.StatusCode == 302 || resp.StatusCode == 301 {
str, _ := ioutil.ReadAll(resp.Body)
log.Info("body %s\n\n", string(str))
}
r, err := zip.NewReader(bytes.NewReader(body), resp.ContentLength)
if err != nil {
return err
}
for _, zf := range r.File {
newfile := path.Join(filepath, RemoveGitBuildFromname(pluginName, zf.Name))
newFile := path.Join(filePath, RemoveGitBuildFromName(pluginName, zf.Name))
if zf.FileInfo().IsDir() {
os.Mkdir(newfile, 0777)
os.Mkdir(newFile, 0777)
} else {
dst, err := os.Create(newfile)
dst, err := os.Create(newFile)
if err != nil {
log.Errorf("%v", err)
}
@@ -19,7 +19,7 @@ func TestFoldernameReplacement(t *testing.T) {
Convey("should be replaced with plugin name", func() {
for k, v := range paths {
So(RemoveGitBuildFromname(pluginName, k), ShouldEqual, v)
So(RemoveGitBuildFromName(pluginName, k), ShouldEqual, v)
}
})
})
@@ -32,7 +32,7 @@ func TestFoldernameReplacement(t *testing.T) {
Convey("should be replaced with plugin name", func() {
for k, v := range paths {
So(RemoveGitBuildFromname(pluginName, k), ShouldEqual, v)
So(RemoveGitBuildFromName(pluginName, k), ShouldEqual, v)
}
})
})
@@ -54,7 +54,7 @@ func upgradeAllCommand(c CommandLine) error {
log.Infof("Upgrading %v \n", p.Id)
s.RemoveInstalledPlugin(pluginDir, p.Id)
InstallPlugin(p.Id, pluginDir, "", c.GlobalString("repo"))
InstallPlugin(p.Id, "", c)
}
return nil
@@ -24,7 +24,7 @@ func upgradeCommand(c CommandLine) error {
if localPlugin.Id == v.Id {
if ShouldUpgrade(localPlugin.Info.Version, v) {
s.RemoveInstalledPlugin(pluginDir, pluginName)
return InstallPlugin(localPlugin.Id, pluginDir, "", c.GlobalString("repo"))
return InstallPlugin(localPlugin.Id, "", c)
}
}
}