Skip to content

Commit

Permalink
feature: fix menu by tenant permission (#1255)
Browse files Browse the repository at this point in the history
* fix

* sort

* fix

* fix some problem

* fix some problem

* fix some problem

* fix some problem

* fix some problem

* fix some problem

* fix some problem

* delete api

* delete openapi

* delete openapi

* get project(no delete)
  • Loading branch information
innerpeacez authored Aug 2, 2021
1 parent 0092191 commit 5305bbd
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 36 deletions.
1 change: 1 addition & 0 deletions apistructs/addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ type MicroServiceProjectResponseData struct {
Envs []string `json:"envs"`
TenantGroups []string `json:"tenantGroups"`
Workspaces map[string]string `json:"workspaces"`
CreateTime time.Time `json:"createTime"`
}

type MicroServiceMenuResponse struct {
Expand Down
16 changes: 16 additions & 0 deletions apistructs/msp.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ type TenantGroupDetails struct {
ProjectID string `json:"projectID"`
}

// MSPTenantResponse .
type MSPTenantResponse struct {
Header
Data []*Tenant `json:"data"`
}

type Tenant struct {
Id string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
ProjectID string `json:"projectID,omitempty"`
Workspace string `json:"workspace,omitempty"`
CreateTime int64 `json:"createTime,omitempty"`
UpdateTime int64 `json:"updateTime,omitempty"`
IsDeleted bool `json:"isDeleted,omitempty"`
}

// MonitorStatusMetricDetailsResponse .
type MonitorStatusMetricDetailsResponse struct {
Header
Expand Down
34 changes: 34 additions & 0 deletions bundle/msp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@
package bundle

import (
"errors"
"fmt"
"net/url"

"github.com/erda-project/erda-proto-go/msp/tenant/pb"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle/apierrors"
cerrors "github.com/erda-project/erda/pkg/common/errors"
"github.com/erda-project/erda/pkg/discover"
"github.com/erda-project/erda/pkg/http/httputil"
)

// PutRuntimeService 部署runtime之后,orchestrator需要将服务域名信息通过此接口提交给hepa
Expand Down Expand Up @@ -131,3 +136,32 @@ func (b *Bundle) CreateGatewayTenant(req *apistructs.GatewayTenantRequest) error
}
return nil
}

func (b *Bundle) CreateMSPTenant(projectID, workspace, tenantType string) (string, error) {
host := discover.MSP()
hc := b.hc

req := pb.CreateTenantRequest{
ProjectID: projectID,
TenantType: tenantType,
Workspaces: []string{workspace},
}
var resp apistructs.MSPTenantResponse
r, err := hc.Post(host).
Path("/api/msp/tenant").
Header(httputil.InternalHeader, "bundle").
JSONBody(&req).
Do().
JSON(&resp)

if err != nil {
return "", apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() || !resp.Success {
return "", toAPIError(r.StatusCode(), resp.Error)
}
if len(resp.Data) <= 0 {
return "", cerrors.NewInternalServerError(errors.New("data failed"))
}
return resp.Data[0].Id, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
github.com/elastic/cloud-on-k8s v0.0.0-20210205172912-5ce0eca90c60
github.com/elazarl/goproxy v0.0.0-20200421181703-e76ad31c14f6
github.com/erda-project/erda-infra v0.0.0-20210729162038-a2e798d921de
github.com/erda-project/erda-proto-go v0.0.0-20210731164015-dacf3a1584df
github.com/erda-project/erda-proto-go v0.0.0-20210802130403-b91c7eae263b
github.com/extrame/ole2 v0.0.0-20160812065207-d69429661ad7 // indirect
github.com/extrame/xls v0.0.1
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@ github.com/erda-project/erda-infra v0.0.0-20210729162038-a2e798d921de h1:jI4eyZ0
github.com/erda-project/erda-infra v0.0.0-20210729162038-a2e798d921de/go.mod h1:L+fFQghY2po2P3H9pzwEOufDLAhL+mRRhPGdZ7vFnAw=
github.com/erda-project/erda-proto-go v0.0.0-20210731164015-dacf3a1584df h1:kWx9qr7Hwc0Kl1w6emic5Umov7Gp4WVeWQF61P9dX8c=
github.com/erda-project/erda-proto-go v0.0.0-20210731164015-dacf3a1584df/go.mod h1:rSETXX3nKxxIhgrVn7fKDM3mla1nNlWcPz4AkepixaU=
github.com/erda-project/erda-proto-go v0.0.0-20210802100339-cc9a3f4661c6 h1:JDruiqmFLA+kDLT4+7hF3EQynNEM4KUzLw24gxlP+UQ=
github.com/erda-project/erda-proto-go v0.0.0-20210802100339-cc9a3f4661c6/go.mod h1:rSETXX3nKxxIhgrVn7fKDM3mla1nNlWcPz4AkepixaU=
github.com/erda-project/erda-proto-go v0.0.0-20210802130403-b91c7eae263b h1:Uk3GxAzm19dDVlZUpRXOdLDIcCKcE63MTxy4L16HLBE=
github.com/erda-project/erda-proto-go v0.0.0-20210802130403-b91c7eae263b/go.mod h1:rSETXX3nKxxIhgrVn7fKDM3mla1nNlWcPz4AkepixaU=
github.com/erda-project/influxql v1.1.0-ex h1:NgP5+S5Qo234IVSIJ3N/egvzCNYJURfMAett3e8a9LE=
github.com/erda-project/influxql v1.1.0-ex/go.mod h1:gHp9y86a/pxhjJ+zMjNXiQAA197Xk9wLxaz+fGG+kWk=
github.com/erda-project/remotedialer v0.2.6-0.20210713103000-da03eb9e4b23 h1:NaKo6voQVqZM6DMBVhcTT4gjd+lr1C3zE17RROspfg0=
Expand Down
5 changes: 3 additions & 2 deletions modules/core-services/dao/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (

"github.com/jinzhu/gorm"

"github.com/erda-project/erda-proto-go/msp/tenant/pb"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/pkg/strutil"

"github.com/erda-project/erda/modules/core-services/model"
"github.com/erda-project/erda/pkg/strutil"
)

// CreateProject 创建项目
Expand Down Expand Up @@ -100,6 +100,7 @@ func (client *DBClient) GetProjectsByIDs(projectIDs []uint64, params *apistructs
if params.Query != "" {
db = db.Where("(name LIKE ? OR display_name LIKE ?)", strutil.Concat("%", params.Query, "%"), strutil.Concat("%", params.Query, "%"))
}
db = db.Where("`type` != ?", pb.Type_MSP.String())
if params.OrderBy != "" {
if params.Asc {
db = db.Order(fmt.Sprintf("%s", params.OrderBy))
Expand Down
7 changes: 6 additions & 1 deletion modules/hepa/gateway/service/gateway_global_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"sync"
"time"

"github.com/erda-project/erda-proto-go/msp/tenant/pb"
"github.com/erda-project/erda/modules/hepa/bundle"
"github.com/erda-project/erda/modules/hepa/common"
"github.com/erda-project/erda/modules/hepa/common/util"
Expand Down Expand Up @@ -296,7 +297,11 @@ func md5V(str string) string {
}

func (impl *GatewayGlobalServiceImpl) GenTenantGroup(projectId, env, clusterName string) string {
return md5V(projectId + "_" + strings.ToUpper(env) + "_" + clusterName + config.ServerConf.TenantGroupKey)
tenant, err := bundle.Bundle.CreateMSPTenant(projectId, env, pb.Type_DOP.String())
if err != nil {
return ""
}
return tenant
}

func (impl *GatewayGlobalServiceImpl) GetTenantGroup(projectId, env string) (res *common.StandardResult) {
Expand Down
2 changes: 1 addition & 1 deletion modules/monitor/apm/topology/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

func (topology *provider) initRoutes(routes httpserver.Router) error {

routes.GET("/api/apm/topology/search/tags", topology.searchTags, GetTopologyPermission(topology.db))
routes.GET("/api/apm/topology/search/tags", topology.searchTags)
routes.GET("/api/apm/topology/search/tagv", topology.searchTagv)
routes.GET("/api/apm/topology", topology.topology)
routes.GET("/api/apm/topology/overview", topology.overview)
Expand Down
3 changes: 3 additions & 0 deletions modules/msp/menu/menu.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func (s *menuService) GetMenu(ctx context.Context, req *pb.GetMenuRequest) (*pb.
params := s.composeMSPMenuParams(req)
item.Params = params
for _, child := range item.Children {
if child.Key == "MonitorIntro" {
continue
}
child.Params = params
}
mspItems = append(mspItems, item)
Expand Down
15 changes: 14 additions & 1 deletion modules/msp/tenant/db/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (db *MSPProjectDB) Create(project *MSPProject) (*MSPProject, error) {

func (db *MSPProjectDB) Query(id string) (*MSPProject, error) {
project := MSPProject{}
err := db.db().Where("`id` = ?", id).Find(&project).Error
err := db.db().Where("`id` = ?", id).Where("`is_deleted` = ?", false).Find(&project).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
Expand All @@ -49,3 +49,16 @@ func (db *MSPProjectDB) Query(id string) (*MSPProject, error) {
}
return &project, err
}

func (db *MSPProjectDB) Delete(id string) (*MSPProject, error) {
project, err := db.Query(id)
if err != nil {
return nil, errors.NewDatabaseError(err)
}
project.IsDeleted = true
err = db.Model(&project).Update(&project).Error
if err != nil {
return nil, errors.NewDatabaseError(err)
}
return project, err
}
15 changes: 14 additions & 1 deletion modules/msp/tenant/db/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (db *MSPTenantDB) QueryTenant(tenantID string) (*MSPTenant, error) {
return &tenant, nil
}

func (db *MSPTenantDB) QueryTenantByProjectIDAndWorkspace(projectID int64, workspace string) (*MSPTenant, error) {
func (db *MSPTenantDB) QueryTenantByProjectIDAndWorkspace(projectID, workspace string) (*MSPTenant, error) {
tenant := MSPTenant{}
err := db.db().Where("`related_project_id` = ?", projectID).Where("`related_workspace` = ?", workspace).Find(&tenant).Error
if err == gorm.ErrRecordNotFound {
Expand All @@ -73,3 +73,16 @@ func (db *MSPTenantDB) QueryTenantByProjectID(projectID string) ([]*MSPTenant, e
}
return tenants, nil
}

func (db *MSPTenantDB) DeleteTenantByTenantID(tenantId string) (*MSPTenant, error) {
tenant, err := db.QueryTenant(tenantId)
if err != nil {
return nil, err
}
tenant.IsDeleted = true
err = db.Model(&tenant).Update(&tenant).Error
if err != nil {
return nil, err
}
return tenant, err
}
91 changes: 71 additions & 20 deletions modules/msp/tenant/project/project.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ package project
import (
context "context"
"net/url"
"sort"
"time"

"github.com/erda-project/erda-infra/providers/i18n"
tenantpb "github.com/erda-project/erda-proto-go/msp/tenant/pb"
pb "github.com/erda-project/erda-proto-go/msp/tenant/project/pb"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/msp/instance/db/monitor"
"github.com/erda-project/erda/modules/msp/tenant"
"github.com/erda-project/erda/modules/msp/tenant/db"
Expand Down Expand Up @@ -69,8 +71,16 @@ func (s *projectService) getDisplayType(lang i18n.LanguageCodes, projectType str
}
}

type Projects []*pb.Project

func (p Projects) Len() int { return len(p) }

func (p Projects) Swap(i, j int) { p[i], p[j] = p[j], p[i] }

func (p Projects) Less(i, j int) bool { return p[i].CreateTime > p[j].CreateTime }

func (s *projectService) GetProjects(ctx context.Context, req *pb.GetProjectsRequest) (*pb.GetProjectsResponse, error) {
var projects []*pb.Project
var projects Projects

// request orch for history project
params := url.Values{}
Expand All @@ -84,25 +94,7 @@ func (s *projectService) GetProjects(ctx context.Context, req *pb.GetProjectsReq
return nil, err
}
for _, project := range orchProjects {
pbProject := pb.Project{}
pbProject.Id = project.ProjectID
pbProject.Name = project.ProjectName
pbProject.DisplayName = project.ProjectName
pbProject.Type = tenantpb.Type_DOP.String()
pbProject.DisplayType = s.getDisplayType(apis.Language(ctx), tenantpb.Type_DOP.String())

var rss []*pb.TenantRelationship
for i, env := range project.Envs {
if env == "" {
continue
}
rs := pb.TenantRelationship{}
rs.Workspace = env
rs.DisplayWorkspace = s.getDisplayWorkspace(apis.Language(ctx), env)
rs.TenantID = project.TenantGroups[i]
rss = append(rss, &rs)
}
pbProject.Relationship = rss
pbProject := s.covertHistoryProjectToMSPProject(ctx, project)
projects = append(projects, &pbProject)
}

Expand All @@ -123,9 +115,34 @@ func (s *projectService) GetProjects(ctx context.Context, req *pb.GetProjectsReq
}
projects = append(projects, project)
}
sort.Sort(projects)
return &pb.GetProjectsResponse{Data: projects}, nil
}

func (s *projectService) covertHistoryProjectToMSPProject(ctx context.Context, project apistructs.MicroServiceProjectResponseData) pb.Project {
pbProject := pb.Project{}
pbProject.Id = project.ProjectID
pbProject.Name = project.ProjectName
pbProject.DisplayName = project.ProjectName
pbProject.CreateTime = project.CreateTime.UnixNano()
pbProject.Type = tenantpb.Type_DOP.String()
pbProject.DisplayType = s.getDisplayType(apis.Language(ctx), tenantpb.Type_DOP.String())

var rss []*pb.TenantRelationship
for i, env := range project.Envs {
if env == "" {
continue
}
rs := pb.TenantRelationship{}
rs.Workspace = env
rs.DisplayWorkspace = s.getDisplayWorkspace(apis.Language(ctx), env)
rs.TenantID = project.TenantGroups[i]
rss = append(rss, &rs)
}
pbProject.Relationship = rss
return pbProject
}

func (s *projectService) CreateProject(ctx context.Context, req *pb.CreateProjectRequest) (*pb.CreateProjectResponse, error) {
if req.Id == "" || req.Name == "" || req.Type == "" || req.DisplayName == "" {
return nil, errors.NewMissingParameterError("id,name,displayName or type missing.")
Expand Down Expand Up @@ -207,9 +224,43 @@ func (s *projectService) GetProject(ctx context.Context, req *pb.GetProjectReque
if err != nil {
return nil, err
}

if project == nil {
// request orch for history project
params := url.Values{}
params.Add("projectId", req.ProjectID)
userID := apis.GetUserID(ctx)
orgID := apis.GetOrgID(ctx)
orchProjects, err := s.p.bdl.GetMSProjects(orgID, userID, params)
if err != nil {
return nil, err
}
historyMicroserviceProject := orchProjects[0]
mspProject := s.covertHistoryProjectToMSPProject(ctx, historyMicroserviceProject)
project = &mspProject
}

return &pb.GetProjectResponse{Data: project}, nil
}

func (s *projectService) DeleteProject(ctx context.Context, req *pb.DeleteProjectRequest) (*pb.DeleteProjectResponse, error) {
_, err := s.MSPProjectDB.Delete(req.ProjectId)
if err != nil {
return nil, err
}
tenants, err := s.MSPTenantDB.QueryTenantByProjectID(req.ProjectId)
if err != nil {
return nil, err
}
for _, mspTenant := range tenants {
_, err := s.MSPTenantDB.DeleteTenantByTenantID(tenant.GenerateTenantID(mspTenant.RelatedProjectId, mspTenant.Type, mspTenant.RelatedWorkspace))
if err != nil {
return nil, err
}
}
return &pb.DeleteProjectResponse{Data: nil}, nil
}

func (s *projectService) convertToProject(project *db.MSPProject) *pb.Project {
return &pb.Project{
Id: project.Id,
Expand Down
5 changes: 1 addition & 4 deletions modules/msp/tenant/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ func (p *provider) Init(ctx servicehub.Context) error {
}
if p.Register != nil {
type TenantService pb.TenantServiceServer
pb.RegisterTenantServiceImp(p.Register, p.tenantService, apis.Options(), p.Perm.Check(
perm.Method(TenantService.CreateTenant, perm.ScopeProject, "msp-tenant", perm.ActionCreate, perm.FieldValue("ProjectID")),
perm.Method(TenantService.GetTenant, perm.ScopeProject, "msp-tenant", perm.ActionGet, perm.FieldValue("ProjectID")),
))
pb.RegisterTenantServiceImp(p.Register, p.tenantService, apis.Options())
}
return nil
}
Expand Down
Loading

0 comments on commit 5305bbd

Please sign in to comment.