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
+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)
}
}