From ac502e5013781cacbf53cac38b65d130a804f4c3 Mon Sep 17 00:00:00 2001 From: ying-jeanne <74549700+ying-jeanne@users.noreply.github.com> Date: Mon, 25 Jul 2022 17:55:28 -0500 Subject: [PATCH] Chore: add dashboarduid in the connection endpoint response (#52761) * Chore: add dashboarduid in the connection endpoint response * add uid of dashboard in the markdown --- .../developers/http_api/library_element.md | 1 + pkg/services/libraryelements/database.go | 13 ++-- .../libraryelements/libraryelements_test.go | 76 +++++++++++++++++++ pkg/services/libraryelements/models.go | 22 +++--- public/api-merged.json | 3 + public/api-spec.json | 3 + 6 files changed, 102 insertions(+), 16 deletions(-) diff --git a/docs/sources/developers/http_api/library_element.md b/docs/sources/developers/http_api/library_element.md index 2065b468fb4..71eae01aa29 100644 --- a/docs/sources/developers/http_api/library_element.md +++ b/docs/sources/developers/http_api/library_element.md @@ -251,6 +251,7 @@ Content-Type: application/json "kind": 1, "elementId": 25, "connectionId": 527, + "connectionUid": "dHEquNzGz", "created": "2021-09-27T10:00:07+02:00", "createdBy": { "id": 1, diff --git a/pkg/services/libraryelements/database.go b/pkg/services/libraryelements/database.go index 674799c0e4a..6b57841fbbf 100644 --- a/pkg/services/libraryelements/database.go +++ b/pkg/services/libraryelements/database.go @@ -557,7 +557,7 @@ func (l *LibraryElementService) getConnections(c context.Context, signedInUser * } var libraryElementConnections []libraryElementConnectionWithMeta builder := sqlstore.SQLBuilder{} - builder.Write("SELECT lec.*, u1.login AS created_by_name, u1.email AS created_by_email") + builder.Write("SELECT lec.*, u1.login AS created_by_name, u1.email AS created_by_email, dashboard.uid AS connection_uid") builder.Write(" FROM " + models.LibraryElementConnectionTableName + " AS lec") builder.Write(" LEFT JOIN " + l.SQLStore.Dialect.Quote("user") + " AS u1 ON lec.created_by = u1.id") builder.Write(" INNER JOIN dashboard AS dashboard on lec.connection_id = dashboard.id") @@ -571,11 +571,12 @@ func (l *LibraryElementService) getConnections(c context.Context, signedInUser * for _, connection := range libraryElementConnections { connections = append(connections, LibraryElementConnectionDTO{ - ID: connection.ID, - Kind: connection.Kind, - ElementID: connection.ElementID, - ConnectionID: connection.ConnectionID, - Created: connection.Created, + ID: connection.ID, + Kind: connection.Kind, + ElementID: connection.ElementID, + ConnectionID: connection.ConnectionID, + ConnectionUID: connection.ConnectionUID, + Created: connection.Created, CreatedBy: LibraryElementDTOMetaUser{ ID: connection.CreatedBy, Name: connection.CreatedByName, diff --git a/pkg/services/libraryelements/libraryelements_test.go b/pkg/services/libraryelements/libraryelements_test.go index 77068b9a447..71bc9e04c40 100644 --- a/pkg/services/libraryelements/libraryelements_test.go +++ b/pkg/services/libraryelements/libraryelements_test.go @@ -106,6 +106,73 @@ func TestDeleteLibraryPanelsInFolder(t *testing.T) { }) } +func TestGetLibraryPanelConnections(t *testing.T) { + scenarioWithPanel(t, "When an admin tries to get connections of library panel, it should succeed and return correct result", + func(t *testing.T, sc scenarioContext) { + dashJSON := map[string]interface{}{ + "panels": []interface{}{ + map[string]interface{}{ + "id": int64(1), + "gridPos": map[string]interface{}{ + "h": 6, + "w": 6, + "x": 0, + "y": 0, + }, + }, + map[string]interface{}{ + "id": int64(2), + "gridPos": map[string]interface{}{ + "h": 6, + "w": 6, + "x": 6, + "y": 0, + }, + "libraryPanel": map[string]interface{}{ + "uid": sc.initialResult.Result.UID, + "name": sc.initialResult.Result.Name, + }, + }, + }, + } + dash := models.Dashboard{ + Title: "Testing GetLibraryPanelConnections", + Data: simplejson.NewFromAny(dashJSON), + } + dashInDB := createDashboard(t, sc.sqlStore, sc.user, &dash, sc.folder.Id) + err := sc.service.ConnectElementsToDashboard(sc.reqContext.Req.Context(), sc.reqContext.SignedInUser, []string{sc.initialResult.Result.UID}, dashInDB.Id) + require.NoError(t, err) + + var expected = func(res LibraryElementConnectionsResponse) LibraryElementConnectionsResponse { + return LibraryElementConnectionsResponse{ + Result: []LibraryElementConnectionDTO{ + { + ID: sc.initialResult.Result.ID, + Kind: sc.initialResult.Result.Kind, + ElementID: 1, + ConnectionID: dashInDB.Id, + ConnectionUID: dashInDB.Uid, + Created: res.Result[0].Created, + CreatedBy: LibraryElementDTOMetaUser{ + ID: 1, + Name: userInDbName, + AvatarURL: userInDbAvatar, + }, + }, + }, + } + } + + sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID}) + resp := sc.service.getConnectionsHandler(sc.reqContext) + var result = validateAndUnMarshalConnectionResponse(t, resp) + + if diff := cmp.Diff(expected(result), result, getCompareOptions()...); diff != "" { + t.Fatalf("Result mismatch (-want +got):\n%s", diff) + } + }) +} + type libraryElement struct { ID int64 `json:"id"` OrgID int64 `json:"orgId"` @@ -285,6 +352,15 @@ func validateAndUnMarshalResponse(t *testing.T, resp response.Response) libraryE return result } +func validateAndUnMarshalConnectionResponse(t *testing.T, resp response.Response) LibraryElementConnectionsResponse { + t.Helper() + require.Equal(t, 200, resp.Status()) + var result = LibraryElementConnectionsResponse{} + err := json.Unmarshal(resp.Body(), &result) + require.NoError(t, err) + return result +} + func validateAndUnMarshalArrayResponse(t *testing.T, resp response.Response) libraryElementArrayResult { t.Helper() diff --git a/pkg/services/libraryelements/models.go b/pkg/services/libraryelements/models.go index a684f8075fe..d7320dc252f 100644 --- a/pkg/services/libraryelements/models.go +++ b/pkg/services/libraryelements/models.go @@ -115,10 +115,11 @@ type libraryElementConnection struct { // libraryElementConnectionWithMeta is the model for library element connections with meta. type libraryElementConnectionWithMeta struct { - ID int64 `xorm:"pk autoincr 'id'"` - ElementID int64 `xorm:"element_id"` - Kind int64 `xorm:"kind"` - ConnectionID int64 `xorm:"connection_id"` + ID int64 `xorm:"pk autoincr 'id'"` + ElementID int64 `xorm:"element_id"` + Kind int64 `xorm:"kind"` + ConnectionID int64 `xorm:"connection_id"` + ConnectionUID string `xorm:"connection_uid"` Created time.Time CreatedBy int64 CreatedByName string @@ -127,12 +128,13 @@ type libraryElementConnectionWithMeta struct { // LibraryElementConnectionDTO is the frontend DTO for element connections. type LibraryElementConnectionDTO struct { - ID int64 `json:"id"` - Kind int64 `json:"kind"` - ElementID int64 `json:"elementId"` - ConnectionID int64 `json:"connectionId"` - Created time.Time `json:"created"` - CreatedBy LibraryElementDTOMetaUser `json:"createdBy"` + ID int64 `json:"id"` + Kind int64 `json:"kind"` + ElementID int64 `json:"elementId"` + ConnectionID int64 `json:"connectionId"` + ConnectionUID string `json:"connectionUid"` + Created time.Time `json:"created"` + CreatedBy LibraryElementDTOMetaUser `json:"createdBy"` } var ( diff --git a/public/api-merged.json b/public/api-merged.json index 734cd29b79d..cf805d740b3 100644 --- a/public/api-merged.json +++ b/public/api-merged.json @@ -13021,6 +13021,9 @@ "type": "integer", "format": "int64" }, + "connectionUid": { + "type": "string" + }, "created": { "type": "string", "format": "date-time" diff --git a/public/api-spec.json b/public/api-spec.json index 3c7914e043e..d5f8ac0f284 100644 --- a/public/api-spec.json +++ b/public/api-spec.json @@ -11308,6 +11308,9 @@ "type": "integer", "format": "int64" }, + "connectionUid": { + "type": "string" + }, "created": { "type": "string", "format": "date-time"