feat(cli): add grafana version header to all request against grafana.net

This commit is contained in:
bergquist
2016-09-15 16:01:06 +02:00
parent 3c966caa23
commit 3c92f78ee7
9 changed files with 74 additions and 1074 deletions

View File

@@ -1,35 +1,59 @@
package services
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
"time"
"github.com/franela/goreq"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
)
var IoHelper m.IoUtil = IoUtilImp{}
var (
IoHelper m.IoUtil = IoUtilImp{}
HttpClient http.Client
grafanaVersion string
)
func Init(version string) {
grafanaVersion = version
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
}
HttpClient = http.Client{
Timeout: time.Duration(10 * time.Second),
Transport: tr,
}
}
func ListAllPlugins(repoUrl string) (m.PluginRepo, error) {
fullUrl := repoUrl + "/repo"
res, err := goreq.Request{Uri: fullUrl, MaxRedirects: 3}.Do()
body, err := createRequest(repoUrl, "repo")
if err != nil {
logger.Info("Failed to create request", "error", err)
return m.PluginRepo{}, fmt.Errorf("Failed to create request. error: %v", err)
}
if err != nil {
return m.PluginRepo{}, err
}
if res.StatusCode != 200 {
return m.PluginRepo{}, fmt.Errorf("Could not access %s statuscode %v", fullUrl, res.StatusCode)
}
var resp m.PluginRepo
err = res.Body.FromJsonTo(&resp)
var data m.PluginRepo
err = json.Unmarshal(body, &data)
if err != nil {
return m.PluginRepo{}, errors.New("Could not load plugin data")
logger.Info("Failed to unmarshal graphite response error: %v", err)
return m.PluginRepo{}, err
}
return resp, nil
return data, nil
}
func ReadPlugin(pluginDir, pluginName string) (m.InstalledPlugin, error) {
@@ -88,21 +112,48 @@ func RemoveInstalledPlugin(pluginPath, pluginName string) error {
}
func GetPlugin(pluginId, repoUrl string) (m.Plugin, error) {
fullUrl := repoUrl + "/repo/" + pluginId
body, err := createRequest(repoUrl, "repo", pluginId)
if err != nil {
logger.Info("Failed to create request", "error", err)
return m.Plugin{}, fmt.Errorf("Failed to create request. error: %v", err)
}
res, err := goreq.Request{Uri: fullUrl, MaxRedirects: 3}.Do()
if err != nil {
return m.Plugin{}, err
}
if res.StatusCode != 200 {
return m.Plugin{}, fmt.Errorf("Could not access %s statuscode %v", fullUrl, res.StatusCode)
}
var resp m.Plugin
err = res.Body.FromJsonTo(&resp)
var data m.Plugin
err = json.Unmarshal(body, &data)
if err != nil {
return m.Plugin{}, errors.New("Could not load plugin data")
logger.Info("Failed to unmarshal graphite response error: %v", err)
return m.Plugin{}, err
}
return resp, nil
return data, nil
}
func createRequest(repoUrl string, subPaths ...string) ([]byte, error) {
u, _ := url.Parse(repoUrl)
for _, v := range subPaths {
u.Path = path.Join(u.Path, v)
}
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
logger.Info("grafanaVersion ", grafanaVersion)
req.Header.Set("grafana-version", grafanaVersion)
req.Header.Set("User-Agent", "grafana "+grafanaVersion)
if err != nil {
return []byte{}, err
}
res, err := HttpClient.Do(req)
body, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
return body, err
}