Library elements: Remove ability to set as "library variable" (#106594)

This commit is contained in:
Stephanie Hingtgen
2025-06-12 08:41:18 -05:00
committed by GitHub
parent a1f2693fd8
commit f4e4ba6f19
15 changed files with 22 additions and 233 deletions
@@ -35,7 +35,7 @@ Returns a list of all library elements the authenticated user has permission to
Query parameters:
- `searchString`: Part of the name or description searched for.
- `kind`: Kind of element to search for. Use `1` for library panels or `2` for library variables.
- `kind`: Kind of element to search for. Use `1` for library panels.
- `sortDirection`: Sort order of elements. Use `alpha-asc` for ascending and `alpha-desc` for descending sort order.
- `typeFilter`: A comma separated list of types to filter the elements by.
- `excludeUid`: Element UID to exclude from search results.
@@ -285,7 +285,7 @@ JSON Body schema:
- `folderUid`: Optional, the UID of the folder where the library element is stored, empty string when it is at the root level.
- `name`: Optional, the name of the library element.
- `model`: The JSON model for the library element.
- `kind`: Kind of element to create, Use `1` for library panels or `2` for library variables.
- `kind`: Kind of element to create, Use `1` for library panels.
- `uid`: Optional, the [unique identifier](#identifier-id-vs-unique-identifier-uid).
**Example Request**:
@@ -364,7 +364,7 @@ JSON Body schema:
- `folderUid`: UID of the folder where the library element is stored, empty string when it is at the root level.
- `name`: Name of the library element.
- `model`: The JSON model for the library element.
- `kind`: Kind of element to create. Use `1` for library panels or `2` for library variables.
- `kind`: Kind of element to create. Use `1` for library panels.
- `version`: Version of the library element you are updating.
- `uid`: Optional, the [unique identifier](#identifier-id-vs-unique-identifier-uid).
-10
View File
@@ -205,9 +205,6 @@ var (
// StatsTotalLibraryPanels is a metric of total number of library panels stored in Grafana.
StatsTotalLibraryPanels prometheus.Gauge
// StatsTotalLibraryVariables is a metric of total number of library variables stored in Grafana.
StatsTotalLibraryVariables prometheus.Gauge
// StatsTotalDataKeys is a metric of total number of data keys stored in Grafana.
StatsTotalDataKeys *prometheus.GaugeVec
@@ -644,12 +641,6 @@ func init() {
Namespace: ExporterName,
})
StatsTotalLibraryVariables = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "stat_totals_library_variables",
Help: "total amount of library variables in the database",
Namespace: ExporterName,
})
StatsTotalDataKeys = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "stat_totals_data_keys",
Help: "total amount of data keys in the database",
@@ -782,7 +773,6 @@ func initMetricVars(reg prometheus.Registerer) {
StatsTotalAlertRules,
StatsTotalRuleGroups,
StatsTotalLibraryPanels,
StatsTotalLibraryVariables,
StatsTotalDataKeys,
MStatTotalPublicDashboards,
MPublicDashboardRequestCount,
@@ -343,7 +343,6 @@ func (s *Service) updateTotalStats(ctx context.Context) bool {
metrics.StatsTotalAlertRules.Set(float64(statsResult.AlertRules))
metrics.StatsTotalRuleGroups.Set(float64(statsResult.RuleGroups))
metrics.StatsTotalLibraryPanels.Set(float64(statsResult.LibraryPanels))
metrics.StatsTotalLibraryVariables.Set(float64(statsResult.LibraryVariables))
metrics.StatsTotalDataKeys.With(prometheus.Labels{"active": "true"}).Set(float64(statsResult.ActiveDataKeys))
inactiveDataKeys := statsResult.DataKeys - statsResult.ActiveDataKeys
+1 -2
View File
@@ -405,8 +405,7 @@ type GetLibraryElementsParams struct {
// required:false
// Description:
// * 1 - library panels
// * 2 - library variables
// enum: 1,2
// enum: 1
Kind int `json:"kind"`
// Sort order of elements.
// in:query
-3
View File
@@ -57,9 +57,6 @@ func syncFieldsWithModel(libraryElement *model.LibraryElement) error {
modelLibraryElement = make(map[string]any)
}
if model.LibraryElementKind(libraryElement.Kind) == model.VariableElement {
modelLibraryElement["name"] = libraryElement.Name
}
if modelLibraryElement["type"] != nil {
libraryElement.Type = modelLibraryElement["type"].(string)
} else {
-2
View File
@@ -24,8 +24,6 @@ func (l *LibraryElementService) requireSupportedElementKind(kindAsInt int64) err
switch kind {
case model.PanelElement:
return nil
case model.VariableElement:
return nil
default:
return model.ErrLibraryElementUnSupportedElementKind
}
@@ -2,7 +2,6 @@ package libraryelements
import (
"encoding/json"
"strconv"
"testing"
"github.com/google/go-cmp/cmp"
@@ -36,141 +35,6 @@ func TestIntegration_GetAllLibraryElements(t *testing.T) {
}
})
scenarioWithPanel(t, "When an admin tries to get all panel elements and both panels and variables exist, it should only return panels",
func(t *testing.T, sc scenarioContext) {
// nolint:staticcheck
command := getCreateVariableCommand(sc.folder.ID, sc.folder.UID, "query0")
sc.reqContext.Req.Body = mockRequestBody(command)
resp := sc.service.createHandler(sc.reqContext)
require.Equal(t, 200, resp.Status())
err := sc.reqContext.Req.ParseForm()
require.NoError(t, err)
sc.reqContext.Req.Form.Add("kind", strconv.FormatInt(int64(model.PanelElement), 10))
resp = sc.service.getAllHandler(sc.reqContext)
require.Equal(t, 200, resp.Status())
var result libraryElementsSearch
err = json.Unmarshal(resp.Body(), &result)
require.NoError(t, err)
var expected = libraryElementsSearch{
Result: libraryElementsSearchResult{
TotalCount: 1,
Page: 1,
PerPage: 100,
Elements: []libraryElement{
{
ID: 1,
OrgID: 1,
FolderID: 1, // nolint:staticcheck
FolderUID: sc.folder.UID,
UID: result.Result.Elements[0].UID,
Name: "Text - Library Panel",
Kind: int64(model.PanelElement),
Type: "text",
Description: "A description",
Model: map[string]interface{}{
"datasource": "${DS_GDEV-TESTDATA}",
"description": "A description",
"id": float64(1),
"title": "Text - Library Panel",
"type": "text",
},
Version: 1,
Meta: model.LibraryElementDTOMeta{
FolderName: "ScenarioFolder",
FolderUID: sc.folder.UID,
ConnectedDashboards: 0,
Created: result.Result.Elements[0].Meta.Created,
Updated: result.Result.Elements[0].Meta.Updated,
CreatedBy: librarypanel.LibraryElementDTOMetaUser{
Id: 1,
Name: userInDbName,
AvatarUrl: userInDbAvatar,
},
UpdatedBy: librarypanel.LibraryElementDTOMetaUser{
Id: 1,
Name: userInDbName,
AvatarUrl: userInDbAvatar,
},
},
},
},
},
}
if diff := cmp.Diff(expected, result, getCompareOptions()...); diff != "" {
t.Fatalf("Result mismatch (-want +got):\n%s", diff)
}
})
scenarioWithPanel(t, "When an admin tries to get all variable elements and both panels and variables exist, it should only return panels",
func(t *testing.T, sc scenarioContext) {
// nolint:staticcheck
command := getCreateVariableCommand(sc.folder.ID, sc.folder.UID, "query0")
sc.reqContext.Req.Body = mockRequestBody(command)
resp := sc.service.createHandler(sc.reqContext)
require.Equal(t, 200, resp.Status())
err := sc.reqContext.Req.ParseForm()
require.NoError(t, err)
sc.reqContext.Req.Form.Add("kind", strconv.FormatInt(int64(model.VariableElement), 10))
resp = sc.service.getAllHandler(sc.reqContext)
require.Equal(t, 200, resp.Status())
var result libraryElementsSearch
err = json.Unmarshal(resp.Body(), &result)
require.NoError(t, err)
var expected = libraryElementsSearch{
Result: libraryElementsSearchResult{
TotalCount: 1,
Page: 1,
PerPage: 100,
Elements: []libraryElement{
{
ID: 2,
OrgID: 1,
FolderID: 1, // nolint:staticcheck
FolderUID: sc.folder.UID,
UID: result.Result.Elements[0].UID,
Name: "query0",
Kind: int64(model.VariableElement),
Type: "query",
Description: "A description",
Model: map[string]interface{}{
"datasource": "${DS_GDEV-TESTDATA}",
"name": "query0",
"type": "query",
"description": "A description",
},
Version: 1,
Meta: model.LibraryElementDTOMeta{
FolderName: "ScenarioFolder",
FolderUID: sc.folder.UID,
ConnectedDashboards: 0,
Created: result.Result.Elements[0].Meta.Created,
Updated: result.Result.Elements[0].Meta.Updated,
CreatedBy: librarypanel.LibraryElementDTOMetaUser{
Id: 1,
Name: userInDbName,
AvatarUrl: userInDbAvatar,
},
UpdatedBy: librarypanel.LibraryElementDTOMetaUser{
Id: 1,
Name: userInDbName,
AvatarUrl: userInDbAvatar,
},
},
},
},
},
}
if diff := cmp.Diff(expected, result, getCompareOptions()...); diff != "" {
t.Fatalf("Result mismatch (-want +got):\n%s", diff)
}
})
scenarioWithPanel(t, "When an admin tries to get all library panels and two exist, it should succeed",
func(t *testing.T, sc scenarioContext) {
// nolint:staticcheck
@@ -386,36 +386,4 @@ func TestIntegration_PatchLibraryElement(t *testing.T) {
resp = sc.service.patchHandler(sc.reqContext)
require.Equal(t, 412, resp.Status())
})
scenarioWithPanel(t, "When an admin tries to patch a library panel with an other kind, it should succeed but panel should not change",
func(t *testing.T, sc scenarioContext) {
cmd := model.PatchLibraryElementCommand{
FolderID: sc.folder.ID, // nolint:staticcheck
FolderUID: &sc.folder.UID,
Version: 1,
Kind: int64(model.VariableElement),
}
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID})
sc.ctx.Req.Body = mockRequestBody(cmd)
resp := sc.service.patchHandler(sc.reqContext)
require.Equal(t, 200, resp.Status())
var result = validateAndUnMarshalResponse(t, resp)
sc.initialResult.Result.Type = "text"
sc.initialResult.Result.Kind = int64(model.PanelElement)
sc.initialResult.Result.Description = "A description"
sc.initialResult.Result.Model = map[string]any{
"datasource": "${DS_GDEV-TESTDATA}",
"id": float64(1),
"title": "Text - Library Panel",
"type": "text",
"description": "A description",
}
sc.initialResult.Result.Meta.CreatedBy.Name = userInDbName
sc.initialResult.Result.Meta.CreatedBy.AvatarUrl = userInDbAvatar
sc.initialResult.Result.Meta.Updated = result.Result.Meta.Updated
sc.initialResult.Result.Version = 2
if diff := cmp.Diff(sc.initialResult.Result, result.Result, getCompareOptions()...); diff != "" {
t.Fatalf("Result mismatch (-want +got):\n%s", diff)
}
})
}
@@ -114,7 +114,7 @@ func TestIntegration_DeleteLibraryPanelsInFolder(t *testing.T) {
scenarioWithPanel(t, "When an admin tries to delete a folder that contains disconnected elements, it should delete all disconnected elements too",
func(t *testing.T, sc scenarioContext) {
// nolint:staticcheck
command := getCreateVariableCommand(sc.folder.ID, sc.folder.UID, "query0")
command := getCreatePanelCommand(sc.folder.ID, sc.folder.UID, "query0")
sc.reqContext.Req.Body = mockRequestBody(command)
resp := sc.service.createHandler(sc.reqContext)
require.Equal(t, 200, resp.Status())
@@ -292,19 +292,6 @@ func getCreatePanelCommand(folderID int64, folderUID string, name string) model.
return command
}
func getCreateVariableCommand(folderID int64, folderUID, name string) model.CreateLibraryElementCommand {
command := getCreateCommandWithModel(folderID, folderUID, name, model.VariableElement, []byte(`
{
"datasource": "${DS_GDEV-TESTDATA}",
"name": "query0",
"type": "query",
"description": "A description"
}
`))
return command
}
func getCreateCommandWithModel(folderID int64, folderUID, name string, kind model.LibraryElementKind, byteModel []byte) model.CreateLibraryElementCommand {
command := model.CreateLibraryElementCommand{
FolderUID: &folderUID,
+2 -6
View File
@@ -177,8 +177,7 @@ type CreateLibraryElementCommand struct {
// Kind of element to create, Use 1 for library panels or 2 for c.
// Description:
// * 1 - library panels
// * 2 - library variables
// Enum: 1,2
// Enum: 1
Kind int64 `json:"kind" binding:"Required"`
// required: false
UID string `json:"uid"`
@@ -199,8 +198,7 @@ type PatchLibraryElementCommand struct {
// Kind of element to create, Use 1 for library panels or 2 for c.
// Description:
// * 1 - library panels
// * 2 - library variables
// Enum: 1,2
// Enum: 1
Kind int64 `json:"kind" binding:"Required"`
// Version of the library element you are updating.
Version int64 `json:"version" binding:"Required"`
@@ -263,8 +261,6 @@ type LibraryElementKind int
const (
// PanelElement is used for library elements that are of the Panel kind
PanelElement LibraryElementKind = iota + 1
// VariableElement is used for library elements that are of the Variable kind
VariableElement
)
const LibraryElementConnectionTableName = "library_element_connection"
+1 -1
View File
@@ -41,7 +41,7 @@ func writePerPageSQL(query model.SearchLibraryElementsQuery, sqlStore db.DB, bui
}
func writeKindSQL(query model.SearchLibraryElementsQuery, builder *db.SQLBuilder) {
if model.LibraryElementKind(query.Kind) == model.PanelElement || model.LibraryElementKind(query.Kind) == model.VariableElement {
if model.LibraryElementKind(query.Kind) == model.PanelElement {
builder.Write(" AND le.kind = ?", query.Kind)
}
}
-1
View File
@@ -151,7 +151,6 @@ func (ss *sqlStatsService) GetSystemStats(ctx context.Context, query *stats.GetS
sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("alert_rule") + `) AS alert_rules,`)
sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("api_key") + `WHERE service_account_id IS NULL) AS api_keys,`)
sb.Write(`(SELECT COUNT(id) FROM `+dialect.Quote("library_element")+` WHERE kind = ?) AS library_panels,`, model.PanelElement)
sb.Write(`(SELECT COUNT(id) FROM `+dialect.Quote("library_element")+` WHERE kind = ?) AS library_variables,`, model.VariableElement)
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("data_keys") + `) AS data_keys,`)
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("data_keys") + `WHERE active = true) AS active_data_keys,`)
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("dashboard_public") + `) AS public_dashboards,`)
+4 -6
View File
@@ -3788,12 +3788,11 @@
"type": "string"
},
"kind": {
"description": "Kind of element to create, Use 1 for library panels or 2 for c.\nDescription:\n1 - library panels\n2 - library variables",
"description": "Kind of element to create, Use 1 for library panels or 2 for c.\nDescription:\n1 - library panels",
"type": "integer",
"format": "int64",
"enum": [
1,
2
1
]
},
"model": {
@@ -6079,12 +6078,11 @@
"type": "string"
},
"kind": {
"description": "Kind of element to create, Use 1 for library panels or 2 for c.\nDescription:\n1 - library panels\n2 - library variables",
"description": "Kind of element to create, Use 1 for library panels or 2 for c.\nDescription:\n1 - library panels",
"type": "integer",
"format": "int64",
"enum": [
1,
2
1
]
},
"model": {
+5 -8
View File
@@ -5529,8 +5529,7 @@
},
{
"enum": [
1,
2
1
],
"type": "integer",
"format": "int64",
@@ -14209,12 +14208,11 @@
"type": "string"
},
"kind": {
"description": "Kind of element to create, Use 1 for library panels or 2 for c.\nDescription:\n1 - library panels\n2 - library variables",
"description": "Kind of element to create, Use 1 for library panels or 2 for c.\nDescription:\n1 - library panels",
"type": "integer",
"format": "int64",
"enum": [
1,
2
1
]
},
"model": {
@@ -18004,12 +18002,11 @@
"type": "string"
},
"kind": {
"description": "Kind of element to create, Use 1 for library panels or 2 for c.\nDescription:\n1 - library panels\n2 - library variables",
"description": "Kind of element to create, Use 1 for library panels or 2 for c.\nDescription:\n1 - library panels",
"type": "integer",
"format": "int64",
"enum": [
1,
2
1
]
},
"model": {
+5 -8
View File
@@ -4258,10 +4258,9 @@
"type": "string"
},
"kind": {
"description": "Kind of element to create, Use 1 for library panels or 2 for c.\nDescription:\n1 - library panels\n2 - library variables",
"description": "Kind of element to create, Use 1 for library panels or 2 for c.\nDescription:\n1 - library panels",
"enum": [
1,
2
1
],
"format": "int64",
"type": "integer"
@@ -8053,10 +8052,9 @@
"type": "string"
},
"kind": {
"description": "Kind of element to create, Use 1 for library panels or 2 for c.\nDescription:\n1 - library panels\n2 - library variables",
"description": "Kind of element to create, Use 1 for library panels or 2 for c.\nDescription:\n1 - library panels",
"enum": [
1,
2
1
],
"format": "int64",
"type": "integer"
@@ -19667,8 +19665,7 @@
"name": "kind",
"schema": {
"enum": [
1,
2
1
],
"format": "int64",
"type": "integer"