You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ti...@apache.org on 2021/05/20 03:23:10 UTC

[servicecomb-service-center] branch master updated: SCB-2176 Add env query string (#987)

This is an automated email from the ASF dual-hosted git repository.

tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git


The following commit(s) were added to refs/heads/master by this push:
     new c690532  SCB-2176 Add env query string (#987)
c690532 is described below

commit c6905323dc7f6b467e7a6bc9087ef3b0a300ec05
Author: little-cui <su...@qq.com>
AuthorDate: Thu May 20 11:23:02 2021 +0800

    SCB-2176 Add env query string (#987)
---
 datasource/etcd/ms.go               | 26 +++++++++++++++++---------
 datasource/mongo/ms.go              | 26 +++++++++++++++++---------
 datasource/mongo/util.go            |  9 ++-------
 server/rest/govern/controller_v4.go |  1 +
 4 files changed, 37 insertions(+), 25 deletions(-)

diff --git a/datasource/etcd/ms.go b/datasource/etcd/ms.go
index 9339c20..8455400 100644
--- a/datasource/etcd/ms.go
+++ b/datasource/etcd/ms.go
@@ -298,17 +298,9 @@ func (ds *DataSource) GetServicesInfo(ctx context.Context, request *pb.GetServic
 	allServiceDetails := make([]*pb.ServiceDetail, 0, len(services))
 	domainProject := util.ParseDomainProject(ctx)
 	for _, service := range services {
-		if !request.WithShared && core.IsGlobal(pb.MicroServiceToKey(domainProject, service)) {
+		if !ds.filterServices(domainProject, request, service) {
 			continue
 		}
-		if len(request.AppId) > 0 {
-			if request.AppId != service.AppId {
-				continue
-			}
-			if len(request.ServiceName) > 0 && request.ServiceName != service.ServiceName {
-				continue
-			}
-		}
 
 		serviceDetail, err := getServiceDetailUtil(ctx, ServiceDetailOpt{
 			domainProject: domainProject,
@@ -345,6 +337,22 @@ func (ds *DataSource) GetServicesInfo(ctx context.Context, request *pb.GetServic
 	}, nil
 }
 
+func (ds *DataSource) filterServices(domainProject string, request *pb.GetServicesInfoRequest, service *pb.MicroService) bool {
+	if !request.WithShared && core.IsGlobal(pb.MicroServiceToKey(domainProject, service)) {
+		return false
+	}
+	if len(request.Environment) > 0 && request.Environment != service.Environment {
+		return false
+	}
+	if len(request.AppId) > 0 && request.AppId != service.AppId {
+		return false
+	}
+	if len(request.ServiceName) > 0 && request.ServiceName != service.ServiceName {
+		return false
+	}
+	return true
+}
+
 func (ds *DataSource) GetServicesStatistics(ctx context.Context, request *pb.GetServicesRequest) (
 	*pb.GetServicesInfoStatisticsResponse, error) {
 	ctx = util.WithCacheOnly(ctx)
diff --git a/datasource/mongo/ms.go b/datasource/mongo/ms.go
index 175d4d0..f300828 100644
--- a/datasource/mongo/ms.go
+++ b/datasource/mongo/ms.go
@@ -482,7 +482,8 @@ func (ds *DataSource) GetServicesInfo(ctx context.Context, request *discovery.Ge
 			}, nil
 		}
 	}
-	services, err := dao.GetServices(ctx, bson.M{})
+	filters := ds.filterServices(ctx, request)
+	services, err := dao.GetServices(ctx, filters)
 	if err != nil {
 		log.Error("get all services by domain failed", err)
 		return &discovery.GetServicesInfoResponse{
@@ -495,14 +496,6 @@ func (ds *DataSource) GetServicesInfo(ctx context.Context, request *discovery.Ge
 		if !request.WithShared && apt.IsGlobal(discovery.MicroServiceToKey(domainProject, mgSvc.Service)) {
 			continue
 		}
-		if len(request.AppId) > 0 {
-			if request.AppId != mgSvc.Service.AppId {
-				continue
-			}
-			if len(request.ServiceName) > 0 && request.ServiceName != mgSvc.Service.ServiceName {
-				continue
-			}
-		}
 
 		serviceDetail, err := getServiceDetailUtil(ctx, mgSvc, request.CountOnly, options)
 		if err != nil {
@@ -534,6 +527,21 @@ func (ds *DataSource) GetServicesInfo(ctx context.Context, request *discovery.Ge
 	}, nil
 }
 
+func (ds *DataSource) filterServices(ctx context.Context, request *discovery.GetServicesInfoRequest) bson.M {
+	var opts []func(filter bson.M)
+
+	if len(request.Environment) > 0 {
+		opts = append(opts, mutil.ServiceEnv(request.Environment))
+	}
+	if len(request.AppId) > 0 {
+		opts = append(opts, mutil.ServiceAppID(request.AppId))
+	}
+	if len(request.ServiceName) > 0 {
+		opts = append(opts, mutil.ServiceAppID(request.ServiceName))
+	}
+	return mutil.NewBasicFilter(ctx, opts...)
+}
+
 func (ds *DataSource) GetServicesStatistics(ctx context.Context, request *discovery.GetServicesRequest) (
 	*discovery.GetServicesInfoStatisticsResponse, error) {
 	ctx = util.WithCacheOnly(ctx)
diff --git a/datasource/mongo/util.go b/datasource/mongo/util.go
index 543027d..225d157 100644
--- a/datasource/mongo/util.go
+++ b/datasource/mongo/util.go
@@ -20,15 +20,12 @@ package mongo
 import (
 	"context"
 
-	pb "github.com/go-chassis/cari/discovery"
-	"go.mongodb.org/mongo-driver/bson"
-
 	"github.com/apache/servicecomb-service-center/datasource"
 	"github.com/apache/servicecomb-service-center/datasource/mongo/client/dao"
-	"github.com/apache/servicecomb-service-center/datasource/mongo/client/model"
 	mutil "github.com/apache/servicecomb-service-center/datasource/mongo/util"
 	"github.com/apache/servicecomb-service-center/pkg/gopool"
 	"github.com/apache/servicecomb-service-center/pkg/util"
+	pb "github.com/go-chassis/cari/discovery"
 )
 
 type InstanceSlice []*pb.MicroServiceInstance
@@ -51,10 +48,8 @@ func statistics(ctx context.Context, withShared bool) (*pb.Statistics, error) {
 		Instances: &pb.StInstance{},
 		Apps:      &pb.StApp{},
 	}
-	domain := util.ParseDomain(ctx)
-	project := util.ParseProject(ctx)
 
-	filter := bson.M{model.ColumnDomain: domain, model.ColumnProject: project}
+	filter := mutil.NewBasicFilter(ctx)
 
 	services, err := dao.GetMicroServices(ctx, filter)
 	if err != nil {
diff --git a/server/rest/govern/controller_v4.go b/server/rest/govern/controller_v4.go
index 51e376f..6707a7f 100644
--- a/server/rest/govern/controller_v4.go
+++ b/server/rest/govern/controller_v4.go
@@ -147,6 +147,7 @@ func (governService *ResourceV4) GetAllServicesInfo(w http.ResponseWriter, r *ht
 	request.Options = strings.Split(optsStr, ",")
 	request.AppId = query.Get("appId")
 	request.ServiceName = query.Get("serviceName")
+	request.Environment = query.Get("env")
 	request.WithShared = util.StringTRUE(query.Get("withShared"))
 	countOnly := query.Get("countOnly")
 	if countOnly != "0" && countOnly != "1" && strings.TrimSpace(countOnly) != "" {