You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2021/01/12 11:50:48 UTC

[GitHub] [servicecomb-service-center] robotLJW commented on a change in pull request #809: 【SCB-2094】Fix: open Mongo's pkg, server and syncer test cases

robotLJW commented on a change in pull request #809:
URL: https://github.com/apache/servicecomb-service-center/pull/809#discussion_r555713898



##########
File path: datasource/mongo/role.go
##########
@@ -20,29 +20,120 @@ package mongo
 import (
 	"context"
 
+	"go.mongodb.org/mongo-driver/bson"
+
+	"github.com/apache/servicecomb-service-center/datasource"
+	"github.com/apache/servicecomb-service-center/datasource/mongo/client"
+	"github.com/apache/servicecomb-service-center/pkg/log"
 	"github.com/apache/servicecomb-service-center/pkg/rbacframe"
+	"github.com/apache/servicecomb-service-center/pkg/util"
 )
 
 func (ds *DataSource) CreateRole(ctx context.Context, r *rbacframe.Role) error {
+	exist, err := ds.RoleExist(ctx, r.Name)
+	if err != nil {
+		log.Error("failed to query role", err)
+		return err
+	}
+	if exist {
+		return datasource.ErrRoleDuplicated
+	}
+	r.ID = util.GenerateUUID()
+	_, err = client.GetMongoClient().Insert(ctx, CollectionRole, r)
+	if err != nil {
+		if client.IsDuplicateKey(err) {
+			return datasource.ErrRoleDuplicated
+		}
+		return err
+	}
+	log.Info("create new role: " + r.ID)
 	return nil
 }
 
 func (ds *DataSource) RoleExist(ctx context.Context, name string) (bool, error) {
-	return false, nil
+	filter := bson.M{
+		ColumnName: name,
+	}
+	count, err := client.GetMongoClient().Count(ctx, CollectionRole, filter)
+	if err != nil {
+		return false, err
+	}
+	if count == 0 {
+		return false, nil
+	}
+	return true, nil
 }
 
 func (ds *DataSource) GetRole(ctx context.Context, name string) (*rbacframe.Role, error) {
-	return &rbacframe.Role{}, nil
+	filter := bson.M{
+		ColumnName: name,
+	}
+	result, err := client.GetMongoClient().FindOne(ctx, CollectionRole, filter)
+	if err != nil {
+		return nil, err
+	}
+	if result.Err() != nil {

Review comment:
       已经修改,返回client.ErrNoDocuments
   
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org