mirror of https://github.com/grafana/grafana.git
				
				
				
			Chore: add dashboarduid in the connection endpoint response (#52761)
* Chore: add dashboarduid in the connection endpoint response * add uid of dashboard in the markdown
This commit is contained in:
		
							parent
							
								
									9df9b1b5c3
								
							
						
					
					
						commit
						ac502e5013
					
				|  | @ -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, | ||||
|  |  | |||
|  | @ -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, | ||||
|  |  | |||
|  | @ -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() | ||||
| 
 | ||||
|  |  | |||
|  | @ -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 ( | ||||
|  |  | |||
|  | @ -13021,6 +13021,9 @@ | |||
|           "type": "integer", | ||||
|           "format": "int64" | ||||
|         }, | ||||
|         "connectionUid": { | ||||
|           "type": "string" | ||||
|         }, | ||||
|         "created": { | ||||
|           "type": "string", | ||||
|           "format": "date-time" | ||||
|  |  | |||
|  | @ -11308,6 +11308,9 @@ | |||
|           "type": "integer", | ||||
|           "format": "int64" | ||||
|         }, | ||||
|         "connectionUid": { | ||||
|           "type": "string" | ||||
|         }, | ||||
|         "created": { | ||||
|           "type": "string", | ||||
|           "format": "date-time" | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue