Skip to content

Commit

Permalink
fix(kb): get owner uid (#26)
Browse files Browse the repository at this point in the history
Because

ListUserAdmin's filter does not work

This commit

Use the GetUserAdmin and GetOrganization as workaround.

Note:
Once mgmt supports getOwnerByID, then we can optimize it.
  • Loading branch information
Yougigun committed Jun 14, 2024
1 parent b8c4620 commit b1d8ac5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
71 changes: 47 additions & 24 deletions pkg/handler/knowledgebasefiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,22 @@ func (ph *PublicHandler) UploadKnowledgeBaseFile(ctx context.Context, req *artif
// TODO: ACL - check if the creator can upload file to this knowledge base. ACL.
// .....

//get the owner uid from the mgmt service
var ownerUID uuid.UUID
// get the owner uid from the mgmt service
var ownerUID string
{
filter := "id=" + req.OwnerId
ownerRes, err := ph.service.MgmtPrv.ListUsersAdmin(ctx, &mgmtpb.ListUsersAdminRequest{Filter: &filter})
// get the owner uid from the mgmt service
ownerUID, err = ph.getOwnerUID(ctx, req.OwnerId)
if err != nil {
log.Error("failed to get owner uid", zap.Error(err))
return nil, err
}
if ownerRes == nil || len(ownerRes.Users) == 0 {
return nil, fmt.Errorf("failed to get owner uid. err: %w", customerror.ErrNotFound)
}
ownerUID, err = uuid.Parse(*ownerRes.Users[0].Uid)
if err != nil {
log.Error("failed to parse owner uid", zap.Error(err))
return nil, err
}
}

// upload file to minio
var kb *repository.KnowledgeBase
var filePathName string
{
kb, err = ph.service.Repository.GetKnowledgeBaseByOwnerAndID(ctx, ownerUID.String(), req.KbId)
kb, err = ph.service.Repository.GetKnowledgeBaseByOwnerAndID(ctx, ownerUID, req.KbId)
if err != nil {
return nil, fmt.Errorf("failed to get knowledge base by owner and id. err: %w", err)
}
Expand All @@ -76,19 +68,24 @@ func (ph *PublicHandler) UploadKnowledgeBaseFile(ctx context.Context, req *artif
log.Error("failed to parse creator uid", zap.Error(err))
return nil, err
}

// turn ownerUIDUuID to uuid
ownerUIDUuid, err := uuid.Parse(ownerUID)
if err != nil {
log.Error("failed to parse owner uid", zap.Error(err))
return nil, err
}
kbFile := repository.KnowledgeBaseFile{
Name: req.File.Name,
Type: artifactpb.FileType_name[int32(req.File.Type)],
Owner: ownerUID,
Owner: ownerUIDUuid,
CreatorUID: creatorUID,
KnowledgeBaseUID: kb.UID,
Destination: filePathName,
ProcessStatus: artifactpb.FileProcessStatus_name[int32(artifactpb.FileProcessStatus_FILE_PROCESS_STATUS_NOTSTARTED)],
}
res, err = ph.service.Repository.CreateKnowledgeBaseFile(ctx, kbFile)
if err != nil {
err:= ph.service.MinIO.DeleteFile(ctx, filePathName)
err := ph.service.MinIO.DeleteFile(ctx, filePathName)
if err != nil {
log.Error("failed to delete file in minio", zap.Error(err))
}
Expand Down Expand Up @@ -158,17 +155,12 @@ func (ph *PublicHandler) ListKnowledgeBaseFiles(ctx context.Context, req *artifa
// get the owner uid from the mgmt service
var ownerUID string
{
filter := "id=" + req.OwnerId
ownerRes, err := ph.service.MgmtPrv.ListUsersAdmin(ctx, &mgmtpb.ListUsersAdminRequest{Filter: &filter})
// get the owner uid from the mgmt service
ownerUID, err = ph.getOwnerUID(ctx, req.OwnerId)
if err != nil {
log.Error("failed to get owner uid", zap.Error(err))
return nil, err
}
if ownerRes == nil || len(ownerRes.Users) == 0 {
return nil, fmt.Errorf("failed to get owner uid. err: %w", customerror.ErrNotFound)
}
ownerUID = *ownerRes.Users[0].Uid

}

// get the kb uid from the knowledge base table
Expand Down Expand Up @@ -290,4 +282,35 @@ func fileTypeConvertToMime(t artifactpb.FileType) string {
default:
return "application/octet-stream"
}
}
}

// use mgmt service to get the user uid
// TODO: when mgmt support get by owner.id, we can optimize this
func (ph *PublicHandler) getOwnerUID(ctx context.Context, ownerID string) (string, error) {
log, _ := logger.GetZapLogger(ctx)

// get from user rpc
ownerUID, err := ph.service.MgmtPrv.GetUserAdmin(ctx, &mgmtpb.GetUserAdminRequest{Name: "users/" + ownerID})
if err != nil {
log.Error("error occurred when get user from mgmt", zap.Error(err))
return "", err
} else {
if ownerUID.User != nil {
if ownerUID.User.Uid != nil {
return *ownerUID.User.Uid, nil
}
}
}

// get from org rpc
orgUID, err := ph.service.MgmtPrv.GetOrganizationAdmin(ctx, &mgmtpb.GetOrganizationAdminRequest{Name: "organizations/" + ownerID})
if err != nil {
log.Error("error occurred when get organization from mgmt", zap.Error(err))
return "", err
} else {
if orgUID.Organization != nil {
return orgUID.Organization.Uid, nil
}
}
return "", fmt.Errorf("failed to get owner uid from users and orgs. err: %w", customerror.ErrNotFound)
}
3 changes: 3 additions & 0 deletions pkg/minio/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package minio
import (
"context"
"encoding/base64"
"fmt"
"strings"

"github.com/instill-ai/artifact-backend/config"
Expand All @@ -26,13 +27,15 @@ type Minio struct {
}

func NewMinioClientAndInitBucket() (*Minio, error) {
fmt.Printf("Initializing Minio client and bucket\n")
cfg := config.Config.Minio
log, err := log.GetZapLogger(context.Background())
if err != nil {
return nil, err
}
client, err := minio.New(cfg.Host+":"+cfg.Port, cfg.RootUser, cfg.RootPwd, false)
if err != nil {
fmt.Printf("Initializing Minio client and bucket\n")
// log connection error
log.Error("cannot connect to minio",
zap.String("host:port", cfg.Host+":"+cfg.Port),
Expand Down

0 comments on commit b1d8ac5

Please sign in to comment.