Commit 5fc255bd by Carl Bergquist Committed by GitHub

Linting fixes for provisoning (#23600)

parent 6e313e7d
......@@ -731,8 +731,9 @@ jobs:
golangci-lint run -v -j 4 --config scripts/go/configs/ci/.golangci.yml -E unconvert -E unused \
-E varcheck -E goconst -E errcheck -E staticcheck ./pkg/...
./scripts/go/bin/revive -formatter stylish -config ./scripts/go/configs/revive.toml ./pkg/...
./scripts/go/bin/revive -formatter stylish ./pkg/services/alerting/...
./scripts/go/bin/revive ./pkg/services/alerting/...
./scripts/go/bin/revive -formatter stylish ./pkg/services/provisioning/datasources/...
./scripts/go/bin/revive -formatter stylish ./pkg/services/provisioning/dashboards/...
./scripts/go/bin/gosec -quiet -exclude=G104,G107,G108,G201,G202,G204,G301,G304,G401,G402,G501 \
-conf=./scripts/go/configs/gosec.json ./pkg/...
......
......@@ -107,7 +107,7 @@ func (hs *HTTPServer) GetDashboard(c *models.ReqContext) Response {
meta.FolderUrl = query.Result.GetUrl()
}
provisioningData, err := dashboards.NewProvisioningService().GetProvisionedDashboardDataByDashboardId(dash.Id)
provisioningData, err := dashboards.NewProvisioningService().GetProvisionedDashboardDataByDashboardID(dash.Id)
if err != nil {
return Error(500, "Error while checking if dashboard is provisioned", err)
}
......@@ -225,7 +225,7 @@ func (hs *HTTPServer) PostDashboard(c *models.ReqContext, cmd models.SaveDashboa
}
}
provisioningData, err := dashboards.NewProvisioningService().GetProvisionedDashboardDataByDashboardId(dash.Id)
provisioningData, err := dashboards.NewProvisioningService().GetProvisionedDashboardDataByDashboardID(dash.Id)
if err != nil {
return Error(500, "Error while checking if dashboard is provisioned", err)
}
......
......@@ -1196,7 +1196,7 @@ func (m mockDashboardProvisioningService) GetProvisionedDashboardData(name strin
panic("implement me")
}
func (mock mockDashboardProvisioningService) GetProvisionedDashboardDataByDashboardId(dashboardId int64) (*models.DashboardProvisioning, error) {
func (mock mockDashboardProvisioningService) GetProvisionedDashboardDataByDashboardID(dashboardId int64) (*models.DashboardProvisioning, error) {
return &models.DashboardProvisioning{}, nil
}
......
package dashboards
import (
"github.com/grafana/grafana/pkg/components/gtime"
"github.com/grafana/grafana/pkg/setting"
"strings"
"time"
"github.com/grafana/grafana/pkg/components/gtime"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
......@@ -26,7 +27,7 @@ type DashboardProvisioningService interface {
SaveProvisionedDashboard(dto *SaveDashboardDTO, provisioning *models.DashboardProvisioning) (*models.Dashboard, error)
SaveFolderForProvisionedDashboards(*SaveDashboardDTO) (*models.Dashboard, error)
GetProvisionedDashboardData(name string) ([]*models.DashboardProvisioning, error)
GetProvisionedDashboardDataByDashboardId(dashboardId int64) (*models.DashboardProvisioning, error)
GetProvisionedDashboardDataByDashboardID(DashboardId int64) (*models.DashboardProvisioning, error)
UnprovisionDashboard(dashboardId int64) error
DeleteProvisionedDashboard(dashboardId int64, orgId int64) error
}
......@@ -70,7 +71,7 @@ func (dr *dashboardServiceImpl) GetProvisionedDashboardData(name string) ([]*mod
return cmd.Result, nil
}
func (dr *dashboardServiceImpl) GetProvisionedDashboardDataByDashboardId(dashboardId int64) (*models.DashboardProvisioning, error) {
func (dr *dashboardServiceImpl) GetProvisionedDashboardDataByDashboardID(dashboardId int64) (*models.DashboardProvisioning, error) {
cmd := &models.GetProvisionedDashboardDataByIdQuery{DashboardId: dashboardId}
err := bus.Dispatch(cmd)
if err != nil {
......@@ -142,7 +143,7 @@ func (dr *dashboardServiceImpl) buildSaveDashboardCommand(dto *SaveDashboardDTO,
}
if validateProvisionedDashboard {
provisionedData, err := dr.GetProvisionedDashboardDataByDashboardId(dash.Id)
provisionedData, err := dr.GetProvisionedDashboardDataByDashboardID(dash.Id)
if err != nil {
return nil, err
}
......@@ -308,7 +309,7 @@ func (dr *dashboardServiceImpl) DeleteProvisionedDashboard(dashboardId int64, or
func (dr *dashboardServiceImpl) deleteDashboard(dashboardId int64, orgId int64, validateProvisionedDashboard bool) error {
if validateProvisionedDashboard {
provisionedData, err := dr.GetProvisionedDashboardDataByDashboardId(dashboardId)
provisionedData, err := dr.GetProvisionedDashboardDataByDashboardID(dashboardId)
if err != nil {
return errutil.Wrap("failed to check if dashboard is provisioned", err)
}
......
......@@ -16,14 +16,14 @@ type configReader struct {
log log.Logger
}
func (cr *configReader) parseConfigs(file os.FileInfo) ([]*DashboardsAsConfig, error) {
func (cr *configReader) parseConfigs(file os.FileInfo) ([]*config, error) {
filename, _ := filepath.Abs(filepath.Join(cr.path, file.Name()))
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
apiVersion := &ConfigVersion{ApiVersion: 0}
apiVersion := &configVersion{APIVersion: 0}
// We ignore the error here because it errors out for version 0 which does not have apiVersion
// specified (so 0 is default). This can also error in case the apiVersion is not an integer but at the moment
......@@ -32,8 +32,8 @@ func (cr *configReader) parseConfigs(file os.FileInfo) ([]*DashboardsAsConfig, e
// integer > max version?).
_ = yaml.Unmarshal(yamlFile, &apiVersion)
if apiVersion.ApiVersion > 0 {
v1 := &DashboardAsConfigV1{}
if apiVersion.APIVersion > 0 {
v1 := &configV1{}
err := yaml.Unmarshal(yamlFile, &v1)
if err != nil {
return nil, err
......@@ -43,7 +43,7 @@ func (cr *configReader) parseConfigs(file os.FileInfo) ([]*DashboardsAsConfig, e
return v1.mapToDashboardsAsConfig()
}
} else {
var v0 []*DashboardsAsConfigV0
var v0 []*configV0
err := yaml.Unmarshal(yamlFile, &v0)
if err != nil {
return nil, err
......@@ -55,11 +55,11 @@ func (cr *configReader) parseConfigs(file os.FileInfo) ([]*DashboardsAsConfig, e
}
}
return []*DashboardsAsConfig{}, nil
return []*config{}, nil
}
func (cr *configReader) readConfig() ([]*DashboardsAsConfig, error) {
var dashboards []*DashboardsAsConfig
func (cr *configReader) readConfig() ([]*config, error) {
var dashboards []*config
files, err := ioutil.ReadDir(cr.path)
if err != nil {
......@@ -84,15 +84,15 @@ func (cr *configReader) readConfig() ([]*DashboardsAsConfig, error) {
uidUsage := map[string]uint8{}
for _, dashboard := range dashboards {
if dashboard.OrgId == 0 {
dashboard.OrgId = 1
if dashboard.OrgID == 0 {
dashboard.OrgID = 1
}
if dashboard.UpdateIntervalSeconds == 0 {
dashboard.UpdateIntervalSeconds = 10
}
if len(dashboard.FolderUid) > 0 {
uidUsage[dashboard.FolderUid]++
if len(dashboard.FolderUID) > 0 {
uidUsage[dashboard.FolderUID]++
}
}
......
......@@ -59,7 +59,7 @@ func TestDashboardsAsConfig(t *testing.T) {
})
})
}
func validateDashboardAsConfig(t *testing.T, cfg []*DashboardsAsConfig) {
func validateDashboardAsConfig(t *testing.T, cfg []*config) {
t.Helper()
So(len(cfg), ShouldEqual, 2)
......@@ -67,9 +67,9 @@ func validateDashboardAsConfig(t *testing.T, cfg []*DashboardsAsConfig) {
ds := cfg[0]
So(ds.Name, ShouldEqual, "general dashboards")
So(ds.Type, ShouldEqual, "file")
So(ds.OrgId, ShouldEqual, 2)
So(ds.OrgID, ShouldEqual, 2)
So(ds.Folder, ShouldEqual, "developers")
So(ds.FolderUid, ShouldEqual, "xyz")
So(ds.FolderUID, ShouldEqual, "xyz")
So(ds.Editable, ShouldBeTrue)
So(len(ds.Options), ShouldEqual, 1)
So(ds.Options["path"], ShouldEqual, "/var/lib/grafana/dashboards")
......@@ -79,9 +79,9 @@ func validateDashboardAsConfig(t *testing.T, cfg []*DashboardsAsConfig) {
ds2 := cfg[1]
So(ds2.Name, ShouldEqual, "default")
So(ds2.Type, ShouldEqual, "file")
So(ds2.OrgId, ShouldEqual, 1)
So(ds2.OrgID, ShouldEqual, 1)
So(ds2.Folder, ShouldEqual, "")
So(ds2.FolderUid, ShouldEqual, "")
So(ds2.FolderUID, ShouldEqual, "")
So(ds2.Editable, ShouldBeFalse)
So(len(ds2.Options), ShouldEqual, 1)
So(ds2.Options["path"], ShouldEqual, "/var/lib/grafana/dashboards")
......
......@@ -25,7 +25,7 @@ type DashboardProvisionerFactory func(string) (DashboardProvisioner, error)
type Provisioner struct {
log log.Logger
fileReaders []*FileReader
configs []*DashboardsAsConfig
configs []*config
}
// New returns a new DashboardProvisioner
......@@ -94,13 +94,13 @@ func (provider *Provisioner) GetProvisionerResolvedPath(name string) string {
func (provider *Provisioner) GetAllowUIUpdatesFromConfig(name string) bool {
for _, config := range provider.configs {
if config.Name == name {
return config.AllowUiUpdates
return config.AllowUIUpdates
}
}
return false
}
func getFileReaders(configs []*DashboardsAsConfig, logger log.Logger) ([]*FileReader, error) {
func getFileReaders(configs []*config, logger log.Logger) ([]*FileReader, error) {
var readers []*FileReader
for _, config := range configs {
......
......@@ -29,14 +29,14 @@ var (
// insert/update dashboards to the Grafana database using
// `dashboards.DashboardProvisioningService`
type FileReader struct {
Cfg *DashboardsAsConfig
Cfg *config
Path string
log log.Logger
dashboardProvisioningService dashboards.DashboardProvisioningService
}
// NewDashboardFileReader returns a new filereader based on `DashboardsAsConfig`
func NewDashboardFileReader(cfg *DashboardsAsConfig, log log.Logger) (*FileReader, error) {
// NewDashboardFileReader returns a new filereader based on `config`
func NewDashboardFileReader(cfg *config, log log.Logger) (*FileReader, error) {
var path string
path, ok := cfg.Options["path"].(string)
if !ok {
......@@ -139,7 +139,7 @@ func (fr *FileReader) handleMissingDashboardFiles(provisionedDashboardRefs map[s
// delete dashboard that are missing json file
for _, dashboardID := range dashboardToDelete {
fr.log.Debug("deleting provisioned dashboard. missing on disk", "id", dashboardID)
err := fr.dashboardProvisioningService.DeleteProvisionedDashboard(dashboardID, fr.Cfg.OrgId)
err := fr.dashboardProvisioningService.DeleteProvisionedDashboard(dashboardID, fr.Cfg.OrgID)
if err != nil {
fr.log.Error("failed to delete dashboard", "id", dashboardID, "error", err)
}
......@@ -212,12 +212,12 @@ func getProvisionedDashboardByPath(service dashboards.DashboardProvisioningServi
return byPath, nil
}
func getOrCreateFolderID(cfg *DashboardsAsConfig, service dashboards.DashboardProvisioningService) (int64, error) {
func getOrCreateFolderID(cfg *config, service dashboards.DashboardProvisioningService) (int64, error) {
if cfg.Folder == "" {
return 0, ErrFolderNameMissing
}
cmd := &models.GetDashboardQuery{Slug: models.SlugifyTitle(cfg.Folder), OrgId: cfg.OrgId}
cmd := &models.GetDashboardQuery{Slug: models.SlugifyTitle(cfg.Folder), OrgId: cfg.OrgID}
err := bus.Dispatch(cmd)
if err != nil && err != models.ErrDashboardNotFound {
......@@ -230,9 +230,9 @@ func getOrCreateFolderID(cfg *DashboardsAsConfig, service dashboards.DashboardPr
dash.Dashboard = models.NewDashboardFolder(cfg.Folder)
dash.Dashboard.IsFolder = true
dash.Overwrite = true
dash.OrgId = cfg.OrgId
dash.OrgId = cfg.OrgID
// set dashboard folderUid if given
dash.Dashboard.SetUid(cfg.FolderUid)
dash.Dashboard.SetUid(cfg.FolderUID)
dbDash, err := service.SaveFolderForProvisionedDashboards(dash)
if err != nil {
return 0, err
......@@ -321,7 +321,7 @@ func (fr *FileReader) readDashboardFromFile(path string, lastModified time.Time,
return nil, err
}
dash, err := createDashboardJson(data, lastModified, fr.Cfg, folderID)
dash, err := createDashboardJSON(data, lastModified, fr.Cfg, folderID)
if err != nil {
return nil, err
}
......
......@@ -14,10 +14,10 @@ var (
)
func TestProvsionedSymlinkedFolder(t *testing.T) {
cfg := &DashboardsAsConfig{
cfg := &config{
Name: "Default",
Type: "file",
OrgId: 1,
OrgID: 1,
Folder: "",
Options: map[string]interface{}{"path": symlinkedFolder},
}
......
......@@ -30,10 +30,10 @@ var (
func TestCreatingNewDashboardFileReader(t *testing.T) {
Convey("creating new dashboard file reader", t, func() {
cfg := &DashboardsAsConfig{
cfg := &config{
Name: "Default",
Type: "file",
OrgId: 1,
OrgID: 1,
Folder: "",
Options: map[string]interface{}{},
}
......@@ -88,10 +88,10 @@ func TestDashboardFileReader(t *testing.T) {
Convey("Reading dashboards from disk", func() {
cfg := &DashboardsAsConfig{
cfg := &config{
Name: "Default",
Type: "file",
OrgId: 1,
OrgID: 1,
Folder: "",
Options: map[string]interface{}{},
}
......@@ -153,10 +153,10 @@ func TestDashboardFileReader(t *testing.T) {
})
Convey("Invalid configuration should return error", func() {
cfg := &DashboardsAsConfig{
cfg := &config{
Name: "Default",
Type: "file",
OrgId: 1,
OrgID: 1,
Folder: "",
}
......@@ -172,8 +172,8 @@ func TestDashboardFileReader(t *testing.T) {
})
Convey("Two dashboard providers should be able to provisioned the same dashboard without uid", func() {
cfg1 := &DashboardsAsConfig{Name: "1", Type: "file", OrgId: 1, Folder: "f1", Options: map[string]interface{}{"path": containingID}}
cfg2 := &DashboardsAsConfig{Name: "2", Type: "file", OrgId: 1, Folder: "f2", Options: map[string]interface{}{"path": containingID}}
cfg1 := &config{Name: "1", Type: "file", OrgID: 1, Folder: "f1", Options: map[string]interface{}{"path": containingID}}
cfg2 := &config{Name: "2", Type: "file", OrgID: 1, Folder: "f2", Options: map[string]interface{}{"path": containingID}}
reader1, err := NewDashboardFileReader(cfg1, logger)
So(err, ShouldBeNil)
......@@ -203,10 +203,10 @@ func TestDashboardFileReader(t *testing.T) {
})
Convey("Should not create new folder if folder name is missing", func() {
cfg := &DashboardsAsConfig{
cfg := &config{
Name: "Default",
Type: "file",
OrgId: 1,
OrgID: 1,
Folder: "",
Options: map[string]interface{}{
"folder": defaultDashboards,
......@@ -218,10 +218,10 @@ func TestDashboardFileReader(t *testing.T) {
})
Convey("can get or Create dashboard folder", func() {
cfg := &DashboardsAsConfig{
cfg := &config{
Name: "Default",
Type: "file",
OrgId: 1,
OrgID: 1,
Folder: "TEAM A",
Options: map[string]interface{}{
"folder": defaultDashboards,
......@@ -255,10 +255,10 @@ func TestDashboardFileReader(t *testing.T) {
})
Convey("Given missing dashboard file", func() {
cfg := &DashboardsAsConfig{
cfg := &config{
Name: "Default",
Type: "file",
OrgId: 1,
OrgID: 1,
Options: map[string]interface{}{
"folder": unprovision,
},
......@@ -436,7 +436,7 @@ func (s *fakeDashboardProvisioningService) DeleteProvisionedDashboard(dashboardI
return nil
}
func (s *fakeDashboardProvisioningService) GetProvisionedDashboardDataByDashboardId(dashboardID int64) (*models.DashboardProvisioning, error) {
func (s *fakeDashboardProvisioningService) GetProvisionedDashboardDataByDashboardID(dashboardID int64) (*models.DashboardProvisioning, error) {
return nil, nil
}
......
......@@ -10,61 +10,61 @@ import (
"github.com/grafana/grafana/pkg/services/provisioning/values"
)
type DashboardsAsConfig struct {
type config struct {
Name string
Type string
OrgId int64
OrgID int64
Folder string
FolderUid string
FolderUID string
Editable bool
Options map[string]interface{}
DisableDeletion bool
UpdateIntervalSeconds int64
AllowUiUpdates bool
AllowUIUpdates bool
}
type DashboardsAsConfigV0 struct {
type configV0 struct {
Name string `json:"name" yaml:"name"`
Type string `json:"type" yaml:"type"`
OrgId int64 `json:"org_id" yaml:"org_id"`
OrgID int64 `json:"org_id" yaml:"org_id"`
Folder string `json:"folder" yaml:"folder"`
FolderUid string `json:"folderUid" yaml:"folderUid"`
FolderUID string `json:"folderUid" yaml:"folderUid"`
Editable bool `json:"editable" yaml:"editable"`
Options map[string]interface{} `json:"options" yaml:"options"`
DisableDeletion bool `json:"disableDeletion" yaml:"disableDeletion"`
UpdateIntervalSeconds int64 `json:"updateIntervalSeconds" yaml:"updateIntervalSeconds"`
AllowUiUpdates bool `json:"allowUiUpdates" yaml:"allowUiUpdates"`
AllowUIUpdates bool `json:"allowUiUpdates" yaml:"allowUiUpdates"`
}
type ConfigVersion struct {
ApiVersion int64 `json:"apiVersion" yaml:"apiVersion"`
type configVersion struct {
APIVersion int64 `json:"apiVersion" yaml:"apiVersion"`
}
type DashboardAsConfigV1 struct {
Providers []*DashboardProviderConfigs `json:"providers" yaml:"providers"`
type configV1 struct {
Providers []*configs `json:"providers" yaml:"providers"`
}
type DashboardProviderConfigs struct {
type configs struct {
Name values.StringValue `json:"name" yaml:"name"`
Type values.StringValue `json:"type" yaml:"type"`
OrgId values.Int64Value `json:"orgId" yaml:"orgId"`
OrgID values.Int64Value `json:"orgId" yaml:"orgId"`
Folder values.StringValue `json:"folder" yaml:"folder"`
FolderUid values.StringValue `json:"folderUid" yaml:"folderUid"`
FolderUID values.StringValue `json:"folderUid" yaml:"folderUid"`
Editable values.BoolValue `json:"editable" yaml:"editable"`
Options values.JSONValue `json:"options" yaml:"options"`
DisableDeletion values.BoolValue `json:"disableDeletion" yaml:"disableDeletion"`
UpdateIntervalSeconds values.Int64Value `json:"updateIntervalSeconds" yaml:"updateIntervalSeconds"`
AllowUiUpdates values.BoolValue `json:"allowUiUpdates" yaml:"allowUiUpdates"`
AllowUIUpdates values.BoolValue `json:"allowUiUpdates" yaml:"allowUiUpdates"`
}
func createDashboardJson(data *simplejson.Json, lastModified time.Time, cfg *DashboardsAsConfig, folderId int64) (*dashboards.SaveDashboardDTO, error) {
func createDashboardJSON(data *simplejson.Json, lastModified time.Time, cfg *config, folderID int64) (*dashboards.SaveDashboardDTO, error) {
dash := &dashboards.SaveDashboardDTO{}
dash.Dashboard = models.NewDashboardFromJson(data)
dash.UpdatedAt = lastModified
dash.Overwrite = true
dash.OrgId = cfg.OrgId
dash.Dashboard.OrgId = cfg.OrgId
dash.Dashboard.FolderId = folderId
dash.OrgId = cfg.OrgID
dash.Dashboard.OrgId = cfg.OrgID
dash.Dashboard.FolderId = folderID
if dash.Dashboard.Title == "" {
return nil, models.ErrDashboardTitleEmpty
......@@ -73,8 +73,8 @@ func createDashboardJson(data *simplejson.Json, lastModified time.Time, cfg *Das
return dash, nil
}
func mapV0ToDashboardsAsConfig(v0 []*DashboardsAsConfigV0) ([]*DashboardsAsConfig, error) {
var r []*DashboardsAsConfig
func mapV0ToDashboardsAsConfig(v0 []*configV0) ([]*config, error) {
var r []*config
seen := make(map[string]bool)
for _, v := range v0 {
......@@ -83,25 +83,25 @@ func mapV0ToDashboardsAsConfig(v0 []*DashboardsAsConfigV0) ([]*DashboardsAsConfi
}
seen[v.Name] = true
r = append(r, &DashboardsAsConfig{
r = append(r, &config{
Name: v.Name,
Type: v.Type,
OrgId: v.OrgId,
OrgID: v.OrgID,
Folder: v.Folder,
FolderUid: v.FolderUid,
FolderUID: v.FolderUID,
Editable: v.Editable,
Options: v.Options,
DisableDeletion: v.DisableDeletion,
UpdateIntervalSeconds: v.UpdateIntervalSeconds,
AllowUiUpdates: v.AllowUiUpdates,
AllowUIUpdates: v.AllowUIUpdates,
})
}
return r, nil
}
func (dc *DashboardAsConfigV1) mapToDashboardsAsConfig() ([]*DashboardsAsConfig, error) {
var r []*DashboardsAsConfig
func (dc *configV1) mapToDashboardsAsConfig() ([]*config, error) {
var r []*config
seen := make(map[string]bool)
for _, v := range dc.Providers {
......@@ -110,17 +110,17 @@ func (dc *DashboardAsConfigV1) mapToDashboardsAsConfig() ([]*DashboardsAsConfig,
}
seen[v.Name.Value()] = true
r = append(r, &DashboardsAsConfig{
r = append(r, &config{
Name: v.Name.Value(),
Type: v.Type.Value(),
OrgId: v.OrgId.Value(),
OrgID: v.OrgID.Value(),
Folder: v.Folder.Value(),
FolderUid: v.FolderUid.Value(),
FolderUID: v.FolderUID.Value(),
Editable: v.Editable.Value(),
Options: v.Options.Value(),
DisableDeletion: v.DisableDeletion.Value(),
UpdateIntervalSeconds: v.UpdateIntervalSeconds.Value(),
AllowUiUpdates: v.AllowUiUpdates.Value(),
AllowUIUpdates: v.AllowUIUpdates.Value(),
})
}
......
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