JCS-pub/coordinator/internal/rpc/pub_shards.go

101 lines
3.1 KiB
Go

package rpc
import (
"context"
"encoding/hex"
"github.com/google/uuid"
"gitlink.org.cn/cloudream/jcs-pub/common/ecode"
"gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc"
corrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/coordinator"
jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
"gitlink.org.cn/cloudream/jcs-pub/coordinator/internal/db"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
func (svc *Service) CreatePubShards(ctx context.Context, msg *corrpc.CreatePubShards) (*corrpc.CreatePubShardsResp, *rpc.CodeError) {
authInfo, ok := rpc.GetAuthInfo(ctx)
if !ok {
return nil, rpc.Failed(ecode.Unauthorized, "unauthorized")
}
passHash, err := bcrypt.GenerateFromPassword([]byte(msg.Password), bcrypt.DefaultCost)
if err != nil {
return nil, rpc.Failed(ecode.OperationFailed, "generate hash: %v", err)
}
cfg, err := db.DoTx01(svc.db, func(tx db.SQLContext) (jcstypes.PubShards, error) {
_, err := svc.db.Hub().GetByID(tx, msg.MasterHub)
if err != nil {
return jcstypes.PubShards{}, err
}
cfg := jcstypes.PubShards{
PubShardsID: jcstypes.PubShardsID(uuid.NewString()),
Password: hex.EncodeToString(passHash),
Creator: authInfo.UserID,
MasterHub: msg.MasterHub,
Name: msg.Name,
Storage: msg.Storage,
Credential: msg.Credential,
ShardStore: msg.ShardStore,
Features: msg.Features,
WorkingDir: msg.WorkingDir,
Revision: 0,
}
err = svc.db.PubShards().Create(svc.db.DefCtx(), cfg)
if err != nil {
return jcstypes.PubShards{}, err
}
return cfg, nil
})
if err != nil {
return nil, rpc.Failed(ecode.OperationFailed, "%v", err)
}
return &corrpc.CreatePubShardsResp{PubShardStore: cfg}, nil
}
func (svc *Service) HubLoadPubShards(ctx context.Context, msg *corrpc.HubLoadPubShards) (*corrpc.HubLoadPubShardsResp, *rpc.CodeError) {
pubShard, err := svc.db.PubShards().Get(svc.db.DefCtx(), msg.PubShardsID)
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, rpc.Failed(ecode.DataNotFound, "not found")
}
return nil, rpc.Failed(ecode.OperationFailed, "get public shard store: %v", err)
}
return &corrpc.HubLoadPubShardsResp{PubShards: *pubShard}, nil
}
func (svc *Service) UserGetPubShards(ctx context.Context, msg *corrpc.UserGetPubShards) (*corrpc.UserGetPubShardsResp, *rpc.CodeError) {
pubShard, err := svc.db.PubShards().Get(svc.db.DefCtx(), msg.PubShardsID)
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, rpc.Failed(ecode.DataNotFound, "not found")
}
return nil, rpc.Failed(ecode.OperationFailed, "get public shard store: %v", err)
}
pass, err := hex.DecodeString(pubShard.Password)
if err != nil {
return nil, rpc.Failed(ecode.OperationFailed, "decode password: %v", err)
}
if err := bcrypt.CompareHashAndPassword(pass, []byte(msg.Password)); err != nil {
return nil, rpc.Failed(ecode.Unauthorized, "invalid password")
}
hub, err := svc.db.Hub().GetByID(svc.db.DefCtx(), pubShard.MasterHub)
if err != nil {
return nil, rpc.Failed(ecode.OperationFailed, "get master hub: %v", err)
}
return &corrpc.UserGetPubShardsResp{PubShards: *pubShard, MasterHub: hub}, nil
}