Skip to content

Commit

Permalink
feat(segment): add deleteAll for segments (monaco purge)
Browse files Browse the repository at this point in the history
  • Loading branch information
jskelin committed Dec 19, 2024
1 parent aa70a36 commit b4cbe16
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
9 changes: 9 additions & 0 deletions pkg/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ func All(ctx context.Context, clients client.ClientSet, apis api.APIs) error {
}
}

if featureflags.Temporary[featureflags.Segments].Enabled() {
if clients.SegmentClient == nil {
log.Warn("Skipped deletion of %s configurations as appropriate client was unavailable.", config.SegmentID)
} else if err := segment.DeleteAll(ctx, clients.SegmentClient); err != nil {
log.Error("Failed to delete all %s configurations: %v", config.SegmentID, err)
errCount++
}
}

if errCount > 0 {
return fmt.Errorf("failed to delete all configurations for %d types", errCount)
}
Expand Down
51 changes: 39 additions & 12 deletions pkg/delete/internal/segment/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,20 @@ func deleteSingle(ctx context.Context, c client, dp pointer.DeletePointer) error
}

func findEntryWithExternalID(ctx context.Context, c client, dp pointer.DeletePointer) (string, error) {
listResp, err := c.List(ctx)
items, err := list(c, ctx)
if err != nil {
return "", err
}

var items []struct {
Uid string `json:"uid"`
ExternalId string `json:"externalId"`
}
if err = json.Unmarshal(listResp.Data, &items); err != nil {
return "", fmt.Errorf("problem with reading recieved data: %w", err)
}

extID, err := idutils.GenerateExternalIDForDocument(dp.AsCoordinate())
if err != nil {
return "", fmt.Errorf("unable to generate externalID: %w", err)
}

var foundUid []string
for _, item := range items {
if item.ExternalId == extID {
foundUid = append(foundUid, item.Uid)
for _, i := range items {
if i.ExternalId == extID {
foundUid = append(foundUid, i.Uid)
}
}

Expand All @@ -122,3 +114,38 @@ func isAPIErrorStatusNotFound(err error) bool {

return apiErr.StatusCode == http.StatusNotFound
}

func DeleteAll(ctx context.Context, c client) error {
items, err := list(c, ctx)
if err != nil {
return err
}

var retErr error
for _, i := range items {
err := deleteSingle(ctx, c, pointer.DeletePointer{Type: string(config.SegmentID), OriginObjectId: i.Uid})
if err != nil {
retErr = errors.Join(retErr, err)
}
}
return retErr
}

type items []struct {
Uid string `json:"uid"`
ExternalId string `json:"externalId"`
}

func list(c client, ctx context.Context) (items, error) {
listResp, err := c.List(ctx)
if err != nil {
return nil, err
}

var items items
if err = json.Unmarshal(listResp.Data, &items); err != nil {
return nil, fmt.Errorf("problem with reading recieved data: %w", err)
}

return items, nil
}
26 changes: 26 additions & 0 deletions pkg/delete/internal/segment/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,29 @@ func TestDelete(t *testing.T) {
assert.Error(t, err)
})
}

func TestDeleteAll(t *testing.T) {
t.Run("simple case", func(t *testing.T) {
c := client.NewMockSegmentClient(gomock.NewController(t))
c.EXPECT().List(gomock.Any()).Times(1).
Return(libSegment.Response{Data: []byte(`[{"uid": "uid_1"},{"uid": "uid_2"},{"uid": "uid_3"}]`)}, nil)
c.EXPECT().Delete(gomock.Any(), gomock.Eq("uid_1")).Times(1)
c.EXPECT().Delete(gomock.Any(), gomock.Eq("uid_2")).Times(1)
c.EXPECT().Delete(gomock.Any(), gomock.Eq("uid_3")).Times(1)

err := segment.DeleteAll(context.TODO(), c)
assert.NoError(t, err)
})

t.Run("error during delete - continue to delete, an error", func(t *testing.T) {
c := client.NewMockSegmentClient(gomock.NewController(t))
c.EXPECT().List(gomock.Any()).Times(1).
Return(libSegment.Response{Data: []byte(`[{"uid": "uid_1"},{"uid": "uid_2"},{"uid": "uid_3"}]`)}, nil)
c.EXPECT().Delete(gomock.Any(), gomock.Eq("uid_1")).Times(1)
c.EXPECT().Delete(gomock.Any(), gomock.Eq("uid_2")).Times(1).Return(libAPI.Response{}, libAPI.APIError{StatusCode: http.StatusInternalServerError}) // the error can be any kind except 404
c.EXPECT().Delete(gomock.Any(), gomock.Eq("uid_3")).Times(1)

err := segment.DeleteAll(context.TODO(), c)
assert.Error(t, err)
})
}

0 comments on commit b4cbe16

Please sign in to comment.