From 47e2a33c19fdaa7309def76bd709c32fb765cc0e Mon Sep 17 00:00:00 2001 From: panjiayao <44628544+Counterflowwind@users.noreply.github.com> Date: Mon, 27 Sep 2021 10:18:07 +0800 Subject: [PATCH] modify micro_service dop role-list (#2123) * msp role list * format imports --- api/proto-go/msp/member/pb/member.form.pb.go | 4 +- api/proto-go/msp/member/pb/member.pb.go | 10 +- modules/msp/member/mebmer.service.go | 40 +++++ modules/msp/member/mebmer.service_test.go | 58 ++++--- ...gister_test.go => member_register_test.go} | 0 modules/msp/member/projectServer_test.go | 141 ++++++++++++++++++ modules/msp/member/provider.go | 2 + 7 files changed, 231 insertions(+), 24 deletions(-) rename modules/msp/member/{credential_register_test.go => member_register_test.go} (100%) create mode 100644 modules/msp/member/projectServer_test.go diff --git a/api/proto-go/msp/member/pb/member.form.pb.go b/api/proto-go/msp/member/pb/member.form.pb.go index 69de77fd8a7..2c5363bccf8 100644 --- a/api/proto-go/msp/member/pb/member.form.pb.go +++ b/api/proto-go/msp/member/pb/member.form.pb.go @@ -370,12 +370,12 @@ func (m *ListMemberRolesRequest) UnmarshalURLValues(prefix string, values url.Va switch prefix + key { case "scopeType": m.ScopeType = vals[0] - case "scopeID": + case "scopeId": val, err := strconv.ParseInt(vals[0], 10, 64) if err != nil { return err } - m.ScopeID = val + m.ScopeId = val } } } diff --git a/api/proto-go/msp/member/pb/member.pb.go b/api/proto-go/msp/member/pb/member.pb.go index 1241d6bfaf0..e37f4e36e52 100644 --- a/api/proto-go/msp/member/pb/member.pb.go +++ b/api/proto-go/msp/member/pb/member.pb.go @@ -910,7 +910,7 @@ type ListMemberRolesRequest struct { unknownFields protoimpl.UnknownFields ScopeType string `protobuf:"bytes,1,opt,name=scopeType,proto3" json:"scopeType,omitempty"` - ScopeID int64 `protobuf:"varint,2,opt,name=scopeID,proto3" json:"scopeID,omitempty"` + ScopeId int64 `protobuf:"varint,2,opt,name=scopeId,proto3" json:"scopeId,omitempty"` } func (x *ListMemberRolesRequest) Reset() { @@ -952,9 +952,9 @@ func (x *ListMemberRolesRequest) GetScopeType() string { return "" } -func (x *ListMemberRolesRequest) GetScopeID() int64 { +func (x *ListMemberRolesRequest) GetScopeId() int64 { if x != nil { - return x.ScopeID + return x.ScopeId } return 0 } @@ -1237,8 +1237,8 @@ var file_member_proto_rawDesc = []byte{ 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x52, 0x6f, 0x6c, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1c, 0x0a, 0x09, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x54, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, 0x63, 0x6f, 0x70, 0x65, - 0x54, 0x79, 0x70, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x49, 0x44, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x49, 0x44, 0x22, 0x48, + 0x54, 0x79, 0x70, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x49, 0x64, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x49, 0x64, 0x22, 0x48, 0x0a, 0x17, 0x4c, 0x69, 0x73, 0x74, 0x4d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x52, 0x6f, 0x6c, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x2d, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x65, 0x72, 0x64, 0x61, 0x2e, 0x6d, diff --git a/modules/msp/member/mebmer.service.go b/modules/msp/member/mebmer.service.go index 22adc875112..5c6e310c951 100644 --- a/modules/msp/member/mebmer.service.go +++ b/modules/msp/member/mebmer.service.go @@ -17,8 +17,10 @@ package member import ( "context" "encoding/json" + "strconv" "github.com/erda-project/erda-proto-go/msp/member/pb" + projectpb "github.com/erda-project/erda-proto-go/msp/tenant/project/pb" "github.com/erda-project/erda/apistructs" "github.com/erda-project/erda/pkg/common/apis" "github.com/erda-project/erda/pkg/common/errors" @@ -29,6 +31,44 @@ type memberService struct { } func (m memberService) ListMemberRoles(ctx context.Context, request *pb.ListMemberRolesRequest) (*pb.ListMemberRolesResponse, error) { + project, err := m.p.ProjectServer.GetProject(ctx, &projectpb.GetProjectRequest{ + ProjectID: strconv.Itoa(int(request.ScopeId)), + }) + if err != nil { + return nil, errors.NewInternalServerError(err) + } + orgIdStr := apis.GetOrgID(ctx) + orgId, err := strconv.Atoi(orgIdStr) + if err != nil { + return nil, errors.NewInternalServerError(err) + } + if project.Data.Type == "DOP" { + roleList, err := m.p.bdl.ListMemberRoles(apistructs.ListScopeManagersByScopeIDRequest{ + ScopeType: apistructs.ScopeType(request.ScopeType), + ScopeID: request.ScopeId, + }, int64(orgId)) + if err != nil { + return nil, errors.NewInternalServerError(err) + } + roleListResult := &pb.ListMemberRolesResponse{ + Data: &pb.RoleList{ + List: make([]*pb.RoleInfo, 0), + Total: int64(roleList.Total), + }, + } + for _, v := range roleList.List { + if v.Role == "" && v.Name == "" { + roleListResult.Data.Total-- + continue + } + role := &pb.RoleInfo{ + Role: v.Role, + Name: v.Name, + } + roleListResult.Data.List = append(roleListResult.Data.List, role) + } + return roleListResult, nil + } return &pb.ListMemberRolesResponse{ Data: &pb.RoleList{ List: []*pb.RoleInfo{ diff --git a/modules/msp/member/mebmer.service_test.go b/modules/msp/member/mebmer.service_test.go index cd8214ffb14..ccb083d26d0 100644 --- a/modules/msp/member/mebmer.service_test.go +++ b/modules/msp/member/mebmer.service_test.go @@ -24,12 +24,14 @@ import ( "github.com/golang/mock/gomock" "github.com/erda-project/erda-proto-go/msp/member/pb" + projectpb "github.com/erda-project/erda-proto-go/msp/tenant/project/pb" "github.com/erda-project/erda/apistructs" "github.com/erda-project/erda/bundle" "github.com/erda-project/erda/pkg/common/apis" ) -////go:generate mockgen -destination=./credential_register_test.go -package exporter github.com/erda-project/erda-infra/pkg/transport Register +////go:generate mockgen -destination=./member_register_test.go -package member github.com/erda-project/erda-infra/pkg/transport Register +////go:generate mockgen -destination=./projectServer_test.go -package member github.com/erda-project/erda-proto-go/msp/tenant/project/pb ProjectServiceServer func Test_memberService_ListMember(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -117,33 +119,55 @@ func Test_memberService_ListMemberRoles(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() register := NewMockRegister(ctrl) + pService := NewMockProjectServiceServer(ctrl) + defer monkey.UnpatchAll() pro := &provider{ Cfg: &config{}, Register: register, memberService: &memberService{}, bdl: &bundle.Bundle{}, + ProjectServer: pService, } - var bdl *bundle.Bundle - defer monkey.UnpatchAll() - monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "ListMemberRoles", - func(_ *bundle.Bundle, _ apistructs.ListScopeManagersByScopeIDRequest, _ int64) (*apistructs.RoleList, error) { - return &apistructs.RoleList{ - List: []apistructs.RoleInfo{ - { - Role: "xxse", - Name: "dfdf", - }, - }, - Total: 1, - }, nil - }) - monkey.Patch(apis.GetOrgID, func(ctx context.Context) string { + + pService.EXPECT().GetProject(gomock.Any(), gomock.Any()).AnyTimes().Return(&projectpb.GetProjectResponse{ + Data: &projectpb.Project{ + Id: "10", + Name: "sqw-xm", + Type: "DOP", + Relationship: nil, + CreateTime: 1632461946000000000, + UpdateTime: 1632461946000000000, + IsDeleted: false, + DisplayName: "sqw-xm", + DisplayType: "DOP", + }, + }, nil) + monkey.Patch(apis.GetOrgID, func(_ context.Context) string { return "1" }) + monkey.Patch((*bundle.Bundle).ListMemberRoles, func(_ *bundle.Bundle, _ apistructs.ListScopeManagersByScopeIDRequest, _ int64) (*apistructs.RoleList, error) { + return &apistructs.RoleList{ + List: []apistructs.RoleInfo{ + { + Role: "Owner", + Name: "项目所有者", + }, + { + Role: "", + Name: "", + }, + { + Role: "Lead", + Name: "研发主管", + }, + }, + Total: 8, + }, nil + }) pro.memberService.p = pro _, err := pro.memberService.ListMemberRoles(context.Background(), &pb.ListMemberRolesRequest{ ScopeType: "project", - ScopeID: 12, + ScopeId: 10, }) if err != nil { fmt.Println(err) diff --git a/modules/msp/member/credential_register_test.go b/modules/msp/member/member_register_test.go similarity index 100% rename from modules/msp/member/credential_register_test.go rename to modules/msp/member/member_register_test.go diff --git a/modules/msp/member/projectServer_test.go b/modules/msp/member/projectServer_test.go new file mode 100644 index 00000000000..f5bb9dce07a --- /dev/null +++ b/modules/msp/member/projectServer_test.go @@ -0,0 +1,141 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/erda-project/erda-proto-go/msp/tenant/project/pb (interfaces: ProjectServiceServer) + +// Package member is a generated GoMock package. +package member + +import ( + context "context" + reflect "reflect" + + pb "github.com/erda-project/erda-proto-go/msp/tenant/project/pb" + gomock "github.com/golang/mock/gomock" +) + +// MockProjectServiceServer is a mock of ProjectServiceServer interface. +type MockProjectServiceServer struct { + ctrl *gomock.Controller + recorder *MockProjectServiceServerMockRecorder +} + +// MockProjectServiceServerMockRecorder is the mock recorder for MockProjectServiceServer. +type MockProjectServiceServerMockRecorder struct { + mock *MockProjectServiceServer +} + +// NewMockProjectServiceServer creates a new mock instance. +func NewMockProjectServiceServer(ctrl *gomock.Controller) *MockProjectServiceServer { + mock := &MockProjectServiceServer{ctrl: ctrl} + mock.recorder = &MockProjectServiceServerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockProjectServiceServer) EXPECT() *MockProjectServiceServerMockRecorder { + return m.recorder +} + +// CreateProject mocks base method. +func (m *MockProjectServiceServer) CreateProject(arg0 context.Context, arg1 *pb.CreateProjectRequest) (*pb.CreateProjectResponse, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateProject", arg0, arg1) + ret0, _ := ret[0].(*pb.CreateProjectResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// CreateProject indicates an expected call of CreateProject. +func (mr *MockProjectServiceServerMockRecorder) CreateProject(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateProject", reflect.TypeOf((*MockProjectServiceServer)(nil).CreateProject), arg0, arg1) +} + +// DeleteProject mocks base method. +func (m *MockProjectServiceServer) DeleteProject(arg0 context.Context, arg1 *pb.DeleteProjectRequest) (*pb.DeleteProjectResponse, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteProject", arg0, arg1) + ret0, _ := ret[0].(*pb.DeleteProjectResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// DeleteProject indicates an expected call of DeleteProject. +func (mr *MockProjectServiceServerMockRecorder) DeleteProject(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteProject", reflect.TypeOf((*MockProjectServiceServer)(nil).DeleteProject), arg0, arg1) +} + +// GetProject mocks base method. +func (m *MockProjectServiceServer) GetProject(arg0 context.Context, arg1 *pb.GetProjectRequest) (*pb.GetProjectResponse, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetProject", arg0, arg1) + ret0, _ := ret[0].(*pb.GetProjectResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetProject indicates an expected call of GetProject. +func (mr *MockProjectServiceServerMockRecorder) GetProject(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetProject", reflect.TypeOf((*MockProjectServiceServer)(nil).GetProject), arg0, arg1) +} + +// GetProjectOverview mocks base method. +func (m *MockProjectServiceServer) GetProjectOverview(arg0 context.Context, arg1 *pb.GetProjectOverviewRequest) (*pb.GetProjectOverviewResponse, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetProjectOverview", arg0, arg1) + ret0, _ := ret[0].(*pb.GetProjectOverviewResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetProjectOverview indicates an expected call of GetProjectOverview. +func (mr *MockProjectServiceServerMockRecorder) GetProjectOverview(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetProjectOverview", reflect.TypeOf((*MockProjectServiceServer)(nil).GetProjectOverview), arg0, arg1) +} + +// GetProjects mocks base method. +func (m *MockProjectServiceServer) GetProjects(arg0 context.Context, arg1 *pb.GetProjectsRequest) (*pb.GetProjectsResponse, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetProjects", arg0, arg1) + ret0, _ := ret[0].(*pb.GetProjectsResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetProjects indicates an expected call of GetProjects. +func (mr *MockProjectServiceServerMockRecorder) GetProjects(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetProjects", reflect.TypeOf((*MockProjectServiceServer)(nil).GetProjects), arg0, arg1) +} + +// GetProjectsTenantsIDs mocks base method. +func (m *MockProjectServiceServer) GetProjectsTenantsIDs(arg0 context.Context, arg1 *pb.GetProjectsTenantsIDsRequest) (*pb.GetProjectsTenantsIDsResponse, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetProjectsTenantsIDs", arg0, arg1) + ret0, _ := ret[0].(*pb.GetProjectsTenantsIDsResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetProjectsTenantsIDs indicates an expected call of GetProjectsTenantsIDs. +func (mr *MockProjectServiceServerMockRecorder) GetProjectsTenantsIDs(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetProjectsTenantsIDs", reflect.TypeOf((*MockProjectServiceServer)(nil).GetProjectsTenantsIDs), arg0, arg1) +} + +// UpdateProject mocks base method. +func (m *MockProjectServiceServer) UpdateProject(arg0 context.Context, arg1 *pb.UpdateProjectRequest) (*pb.UpdateProjectResponse, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateProject", arg0, arg1) + ret0, _ := ret[0].(*pb.UpdateProjectResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// UpdateProject indicates an expected call of UpdateProject. +func (mr *MockProjectServiceServerMockRecorder) UpdateProject(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateProject", reflect.TypeOf((*MockProjectServiceServer)(nil).UpdateProject), arg0, arg1) +} diff --git a/modules/msp/member/provider.go b/modules/msp/member/provider.go index 42abfe11875..13e3ffc1503 100644 --- a/modules/msp/member/provider.go +++ b/modules/msp/member/provider.go @@ -18,6 +18,7 @@ import ( "github.com/erda-project/erda-infra/base/servicehub" "github.com/erda-project/erda-infra/pkg/transport" "github.com/erda-project/erda-proto-go/msp/member/pb" + projectpb "github.com/erda-project/erda-proto-go/msp/tenant/project/pb" "github.com/erda-project/erda/bundle" "github.com/erda-project/erda/pkg/common/apis" ) @@ -30,6 +31,7 @@ type provider struct { Register transport.Register memberService *memberService bdl *bundle.Bundle + ProjectServer projectpb.ProjectServiceServer } func (p *provider) Init(ctx servicehub.Context) error {