|
|
@@ -1,10 +1,15 @@
|
|
|
package api
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "net/http"
|
|
|
"time"
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
+ "github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
|
|
@@ -12,6 +17,11 @@ import (
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
)
|
|
|
|
|
|
+var client = &http.Client{
|
|
|
+ Timeout: time.Second * 5,
|
|
|
+ Transport: &http.Transport{Proxy: http.ProxyFromEnvironment},
|
|
|
+}
|
|
|
+
|
|
|
func GetSharingOptions(c *m.ReqContext) {
|
|
|
c.JSON(200, util.DynMap{
|
|
|
"externalSnapshotURL": setting.ExternalSnapshotUrl,
|
|
|
@@ -20,26 +30,82 @@ func GetSharingOptions(c *m.ReqContext) {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+type CreateExternalSnapshotResponse struct {
|
|
|
+ Key string `json:"key"`
|
|
|
+ DeleteKey string `json:"deleteKey"`
|
|
|
+ Url string `json:"url"`
|
|
|
+ DeleteUrl string `json:"deleteUrl"`
|
|
|
+}
|
|
|
+
|
|
|
+func createExternalDashboardSnapshot(cmd m.CreateDashboardSnapshotCommand) (*CreateExternalSnapshotResponse, error) {
|
|
|
+ var createSnapshotResponse CreateExternalSnapshotResponse
|
|
|
+ message := map[string]interface{}{
|
|
|
+ "name": cmd.Name,
|
|
|
+ "expires": cmd.Expires,
|
|
|
+ "dashboard": cmd.Dashboard,
|
|
|
+ }
|
|
|
+
|
|
|
+ messageBytes, err := simplejson.NewFromAny(message).Encode()
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ response, err := client.Post(setting.ExternalSnapshotUrl+"/api/snapshots", "application/json", bytes.NewBuffer(messageBytes))
|
|
|
+ if response != nil {
|
|
|
+ defer response.Body.Close()
|
|
|
+ }
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if response.StatusCode != 200 {
|
|
|
+ return nil, fmt.Errorf("Create external snapshot response status code %d", response.StatusCode)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := json.NewDecoder(response.Body).Decode(&createSnapshotResponse); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return &createSnapshotResponse, nil
|
|
|
+}
|
|
|
+
|
|
|
+// POST /api/snapshots
|
|
|
func CreateDashboardSnapshot(c *m.ReqContext, cmd m.CreateDashboardSnapshotCommand) {
|
|
|
if cmd.Name == "" {
|
|
|
cmd.Name = "Unnamed snapshot"
|
|
|
}
|
|
|
|
|
|
+ var url string
|
|
|
+ cmd.ExternalUrl = ""
|
|
|
+ cmd.OrgId = c.OrgId
|
|
|
+ cmd.UserId = c.UserId
|
|
|
+
|
|
|
if cmd.External {
|
|
|
- // external snapshot ref requires key and delete key
|
|
|
- if cmd.Key == "" || cmd.DeleteKey == "" {
|
|
|
- c.JsonApiErr(400, "Missing key and delete key for external snapshot", nil)
|
|
|
+ if !setting.ExternalEnabled {
|
|
|
+ c.JsonApiErr(403, "External dashboard creation is disabled", nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ response, err := createExternalDashboardSnapshot(cmd)
|
|
|
+ if err != nil {
|
|
|
+ c.JsonApiErr(500, "Failed to create external snaphost", err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- cmd.OrgId = -1
|
|
|
- cmd.UserId = -1
|
|
|
+ url = response.Url
|
|
|
+ cmd.Key = response.Key
|
|
|
+ cmd.DeleteKey = response.DeleteKey
|
|
|
+ cmd.ExternalUrl = response.Url
|
|
|
+ cmd.ExternalDeleteUrl = response.DeleteUrl
|
|
|
+ cmd.Dashboard = simplejson.New()
|
|
|
+
|
|
|
metrics.M_Api_Dashboard_Snapshot_External.Inc()
|
|
|
} else {
|
|
|
cmd.Key = util.GetRandomString(32)
|
|
|
cmd.DeleteKey = util.GetRandomString(32)
|
|
|
- cmd.OrgId = c.OrgId
|
|
|
- cmd.UserId = c.UserId
|
|
|
+ url = setting.ToAbsUrl("dashboard/snapshot/" + cmd.Key)
|
|
|
+
|
|
|
metrics.M_Api_Dashboard_Snapshot_Create.Inc()
|
|
|
}
|
|
|
|
|
|
@@ -51,7 +117,7 @@ func CreateDashboardSnapshot(c *m.ReqContext, cmd m.CreateDashboardSnapshotComma
|
|
|
c.JSON(200, util.DynMap{
|
|
|
"key": cmd.Key,
|
|
|
"deleteKey": cmd.DeleteKey,
|
|
|
- "url": setting.ToAbsUrl("dashboard/snapshot/" + cmd.Key),
|
|
|
+ "url": url,
|
|
|
"deleteUrl": setting.ToAbsUrl("api/snapshots-delete/" + cmd.DeleteKey),
|
|
|
})
|
|
|
}
|