LibraryElements: Enables creating library elements with specific UID (#39019)

* LibraryPanels: Enables create/update library panels with specific UID

* Chore: added check for uid length after PR comments

* Refactor: creates IsShortUIDTooLong function

* Refactor: adds UID to PATCH endpoint

* Refactor: clarifies the patch code

* Refactor: changes  after PR comments
This commit is contained in:
Hugo Häggmark
2021-09-10 11:22:13 +02:00
committed by GitHub
parent 580b03c952
commit fc73bc1161
9 changed files with 224 additions and 6 deletions
+28 -2
View File
@@ -2,6 +2,7 @@ package libraryelements
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
@@ -92,10 +93,20 @@ func (l *LibraryElementService) createLibraryElement(c *models.ReqContext, cmd C
if err := l.requireSupportedElementKind(cmd.Kind); err != nil {
return LibraryElementDTO{}, err
}
createUID := cmd.UID
if len(createUID) == 0 {
createUID = util.GenerateShortUID()
} else {
if !util.IsValidShortUID(createUID) {
return LibraryElementDTO{}, errLibraryElementInvalidUID
} else if util.IsShortUIDTooLong(createUID) {
return LibraryElementDTO{}, errLibraryElementUIDTooLong
}
}
element := LibraryElement{
OrgID: c.SignedInUser.OrgId,
FolderID: cmd.FolderID,
UID: util.GenerateShortUID(),
UID: createUID,
Name: cmd.Name,
Model: cmd.Model,
Version: 1,
@@ -434,12 +445,27 @@ func (l *LibraryElementService) patchLibraryElement(c *models.ReqContext, cmd pa
if elementInDB.Version != cmd.Version {
return errLibraryElementVersionMismatch
}
updateUID := cmd.UID
if len(updateUID) == 0 {
updateUID = uid
} else if updateUID != uid {
if !util.IsValidShortUID(updateUID) {
return errLibraryElementInvalidUID
} else if util.IsShortUIDTooLong(updateUID) {
return errLibraryElementUIDTooLong
}
_, err := getLibraryElement(l.SQLStore.Dialect, session, updateUID, c.SignedInUser.OrgId)
if !errors.Is(err, errLibraryElementNotFound) {
return errLibraryElementAlreadyExists
}
}
var libraryElement = LibraryElement{
ID: elementInDB.ID,
OrgID: c.SignedInUser.OrgId,
FolderID: cmd.FolderID,
UID: uid,
UID: updateUID,
Name: cmd.Name,
Kind: elementInDB.Kind,
Type: elementInDB.Type,