Skip to content

Commit

Permalink
updates lists restore flow for replace collisions (#5073)
Browse files Browse the repository at this point in the history
updates lists restore flow for replace collisions

#### Does this PR need a docs update or release note?

- [ ] ✅ Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x] ⛔ No

#### Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [x] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [ ] 🧹 Tech Debt/Cleanup

#### Issue(s)
#4754 

#### Test Plan

<!-- How will this be tested prior to merging.-->
- [x] 💪 Manual
- [x] ⚡ Unit test
- [x] 💚 E2E
  • Loading branch information
HiteshRepo authored Jan 23, 2024
1 parent c251186 commit c82abce
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 180 deletions.
9 changes: 9 additions & 0 deletions src/internal/m365/collection/site/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type getItemser interface {

type restoreHandler interface {
PostLister
PatchLister
DeleteLister
GetLister
GetListsByCollisionKeyser
Expand All @@ -50,6 +51,14 @@ type PostLister interface {
) (models.Listable, error)
}

type PatchLister interface {
PatchList(
ctx context.Context,
listID string,
list models.Listable,
) (models.Listable, error)
}

type DeleteLister interface {
DeleteList(
ctx context.Context,
Expand Down
8 changes: 8 additions & 0 deletions src/internal/m365/collection/site/lists_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ func (rh listsRestoreHandler) PostList(
return rh.ac.PostList(ctx, rh.protectedResource, listName, storedList, errs)
}

func (rh listsRestoreHandler) PatchList(
ctx context.Context,
listID string,
list models.Listable,
) (models.Listable, error) {
return rh.ac.PatchList(ctx, rh.protectedResource, listID, list)
}

func (rh listsRestoreHandler) DeleteList(
ctx context.Context,
listID string,
Expand Down
34 changes: 13 additions & 21 deletions src/internal/m365/collection/site/mock/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,16 @@ func (lh *ListHandler) Check(t *testing.T, expected []string) {
}

type ListRestoreHandler struct {
getListErr error
deleteListErr error
postListErrs []error
postListCalls int
postListErr error
patchListErr error
}

func NewListRestoreHandler(getListErr, deleteListErr error, postListErrs []error) *ListRestoreHandler {
func NewListRestoreHandler(deleteListErr error, postListErr, patchListErr error) *ListRestoreHandler {
return &ListRestoreHandler{
getListErr: getListErr,
deleteListErr: deleteListErr,
postListErrs: postListErrs,
postListErr: postListErr,
patchListErr: patchListErr,
}
}

Expand All @@ -115,15 +114,16 @@ func (lh *ListRestoreHandler) PostList(
storedList models.Listable,
_ *fault.Bus,
) (models.Listable, error) {
lh.postListCalls++

ls, _ := api.ToListable(storedList, listName)
return ls, lh.postListErr
}

if lh.postListCalls > len(lh.postListErrs) {
return ls, nil
}

return nil, lh.postListErrs[lh.postListCalls-1]
func (lh *ListRestoreHandler) PatchList(
ctx context.Context,
listID string,
list models.Listable,
) (models.Listable, error) {
return nil, lh.patchListErr
}

func (lh *ListRestoreHandler) GetList(
Expand All @@ -133,10 +133,6 @@ func (lh *ListRestoreHandler) GetList(
ls := models.NewList()
ls.SetId(ptr.To(listID))

if lh.getListErr != nil {
return nil, nil, lh.getListErr
}

return ls, api.ListToSPInfo(ls), nil
}

Expand All @@ -148,10 +144,6 @@ func (lh *ListRestoreHandler) GetListsByCollisionKey(ctx context.Context) (map[s
return map[string]string{}, nil
}

func (lh *ListRestoreHandler) CheckPostListCalls(t *testing.T, expectedCalls int) {
assert.Equal(t, expectedCalls, lh.postListCalls, "unequal number of post-list calls")
}

func StubLists(ids ...string) []models.Listable {
lists := make([]models.Listable, 0, len(ids))

Expand Down
49 changes: 25 additions & 24 deletions src/internal/m365/collection/site/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func restoreListItem(
ctr.Inc(count.CollisionSkip)
log.Debug("skipping item with collision")

return dii, core.ErrAlreadyExists
return dii, clues.Stack(core.ErrAlreadyExists)
}

collisionID = id
Expand All @@ -86,18 +86,19 @@ func restoreListItem(
if collisionPolicy != control.Replace {
restoredList, err = rh.PostList(ctx, newName, storedList, errs)
if err != nil {
return dii, clues.WrapWC(ctx, err, "restoring list")
return dii, clues.Wrap(err, "restoring list")
}
} else {
restoredList, err = handleListReplace(
ctx,
collisionID,
storedList,
newName,
rh,
ctr,
errs)
if err != nil {
return dii, err
return dii, clues.Stack(err)
}
}

Expand All @@ -110,42 +111,42 @@ func restoreListItem(

func handleListReplace(
ctx context.Context,
collisionID string,
storedList models.Listable,
listID string,
listFromBackup models.Listable,
newName string,
rh restoreHandler,
ctr *count.Bus,
errs *fault.Bus,
) (models.Listable, error) {
collidedList, _, err := rh.GetList(ctx, collisionID)
restoredList, err := rh.PostList(
ctx,
newName,
listFromBackup,
errs)
if err != nil {
return nil, clues.WrapWC(ctx, err, "fetching collided list")
return nil, clues.WrapWC(ctx, err, "restoring list")
}

err = rh.DeleteList(ctx, collisionID)
err = rh.DeleteList(ctx, listID)
if err != nil {
return nil, clues.WrapWC(ctx, err, "deleting collided list")
}

restoredList, err := rh.PostList(
patchList := models.NewList()
patchList.SetDisplayName(listFromBackup.GetDisplayName())
_, err = rh.PatchList(
ctx,
ptr.Val(storedList.GetDisplayName()),
storedList,
errs)
if err == nil {
ctr.Inc(count.CollisionReplace)
return restoredList, nil
}
ptr.Val(restoredList.GetId()),
patchList)

_, collidedListErr := rh.PostList(
ctx,
ptr.Val(collidedList.GetDisplayName()),
collidedList,
errs)
if collidedListErr != nil {
return nil, clues.WrapWC(ctx, collidedListErr, "re-creating collided list")
if err != nil {
return nil, clues.WrapWC(ctx, err, "patching list")
}

return nil, clues.WrapWC(ctx, err, "restoring list")
restoredList.SetDisplayName(listFromBackup.GetDisplayName())
ctr.Inc(count.CollisionReplace)

return restoredList, nil
}

func RestoreListCollection(
Expand Down
Loading

0 comments on commit c82abce

Please sign in to comment.