Delete data part 1 (#360)

This commit is contained in:
Dominic Wong
2022-02-01 12:09:33 +00:00
committed by GitHub
parent 1f494e6638
commit 9b8d144dfd
9 changed files with 177 additions and 35 deletions

View File

@@ -9,7 +9,10 @@ import (
"github.com/micro/micro/v3/service/errors"
log "github.com/micro/micro/v3/service/logger"
pb "github.com/micro/services/cache/proto"
pauth "github.com/micro/services/pkg/auth"
"github.com/micro/services/pkg/cache"
adminpb "github.com/micro/services/pkg/service/proto"
"github.com/micro/services/pkg/tenant"
)
type Cache struct{}
@@ -133,3 +136,29 @@ func (c *Cache) ListKeys(ctx context.Context, req *pb.ListKeysRequest, rsp *pb.L
return nil
}
func (c *Cache) DeleteData(ctx context.Context, request *adminpb.DeleteDataRequest, response *adminpb.DeleteDataResponse) error {
method := "admin.DeleteData"
_, err := pauth.VerifyMicroAdmin(ctx, method)
if err != nil {
return err
}
if len(request.TenantId) == 0 {
return errors.BadRequest(method, "Missing tenant ID")
}
split := strings.Split(request.TenantId, "/")
tctx := tenant.NewContext(split[1], split[0], split[1])
keys, err := cache.Context(tctx).ListKeys()
if err != nil {
return err
}
for _, k := range keys {
if err := cache.Context(tctx).Delete(k); err != nil {
return err
}
}
log.Infof("Deleted %d keys for %s", len(keys), request.TenantId)
return nil
}