Skip to content

Commit

Permalink
Merge 198ff0a into 545ebf7
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoYL123 committed Jan 17, 2020
2 parents 545ebf7 + 198ff0a commit 36a2d78
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/model/db_schema.go
Expand Up @@ -23,6 +23,7 @@ type LabelDoc struct {
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"` //tenant info
Project string `json:"project,omitempty" yaml:"project,omitempty"`
Alias string `json:"alias,omitempty" yaml:"alias,omitempty"`
}

//KVDoc is database struct to store kv
Expand Down
6 changes: 6 additions & 0 deletions server/resource/v1/doc_struct.go
Expand Up @@ -89,6 +89,12 @@ var (
ParamType: goRestful.QueryParameterKind,
Desc: "label pairs,for example &label=service:order&label=version:1.0.0",
}
DocQueryAliasParameters = &restful.Parameters{
DataType: "string",
Name: "alias",
ParamType: goRestful.QueryParameterKind,
Desc: "label's alias , a string",
}
)

//swagger doc path params
Expand Down
56 changes: 56 additions & 0 deletions server/resource/v1/history_resource.go
Expand Up @@ -62,6 +62,45 @@ func (r *HistoryResource) GetRevisions(context *restful.Context) {
}
}

//PutLabelAlias by label_id
func (r *HistoryResource) PutLabelAlias(context *restful.Context) {
var err error
labelID := context.ReadPathParameter("key_id")
project := context.ReadPathParameter("project")
alias := new(map[string]string)
if err = readRequest(context, alias); err != nil {
WriteErrResponse(context, http.StatusBadRequest, err.Error(), common.ContentTypeText)
return
}
domain := ReadDomain(context)
if domain == nil {
WriteErrResponse(context, http.StatusInternalServerError, MsgDomainMustNotBeEmpty, common.ContentTypeText)
return
}
if labelID == "" {
openlogging.Error("key id is nil")
WriteErrResponse(context, http.StatusForbidden, "key_id must not be empty", common.ContentTypeText)
return
}
res, err := service.LabelService.CreateOrUpdateAlias(context.Ctx, domain.(string), project, labelID, (*alias)["alias"])
if err != nil {
if err == service.ErrRevisionNotExist {
WriteErrResponse(context, http.StatusNotFound, err.Error(), common.ContentTypeText)
return
}
WriteErrResponse(context, http.StatusInternalServerError, err.Error(), common.ContentTypeText)
return
}
if res == "" {
WriteErrResponse(context, http.StatusNotFound, "put alias fail", common.ContentTypeText)
return
}
err = writeResponse(context, res)
if err != nil {
openlogging.Error(err.Error())
}
}

//URLPatterns defined config operations
func (r *HistoryResource) URLPatterns() []restful.Route {
return []restful.Route{
Expand All @@ -83,5 +122,22 @@ func (r *HistoryResource) URLPatterns() []restful.Route {
Consumes: []string{goRestful.MIME_JSON, common.ContentTypeYaml},
Produces: []string{goRestful.MIME_JSON, common.ContentTypeYaml},
},
{
Method: http.MethodPost,
Path: "/v1/{project}/kie/label/alias/{key_id}",
ResourceFunc: r.PutLabelAlias,
FuncDesc: "put alias for label",
Parameters: []*restful.Parameters{
DocPathProject, DocPathKeyID,
},
Returns: []*restful.Returns{
{
Code: http.StatusOK,
Message: "put success",
},
},
Consumes: []string{goRestful.MIME_JSON, common.ContentTypeYaml},
Produces: []string{goRestful.MIME_JSON, common.ContentTypeYaml},
},
}
}
2 changes: 2 additions & 0 deletions server/service/mongo/init.go
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/apache/servicecomb-kie/server/service/mongo/counter"
"github.com/apache/servicecomb-kie/server/service/mongo/history"
"github.com/apache/servicecomb-kie/server/service/mongo/kv"
"github.com/apache/servicecomb-kie/server/service/mongo/label"
"github.com/apache/servicecomb-kie/server/service/mongo/session"
"github.com/go-mesh/openlogging"
)
Expand All @@ -32,4 +33,5 @@ func init() {
service.KVService = &kv.Service{}
service.HistoryService = &history.Service{}
service.RevisionService = &counter.Service{}
service.LabelService = &label.Service{}
}
25 changes: 20 additions & 5 deletions server/service/mongo/label/label_dao.go
Expand Up @@ -25,27 +25,30 @@ import (
"github.com/go-mesh/openlogging"
uuid "github.com/satori/go.uuid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)

const (
defaultLabels = "default"
defaultValues = "default"
)

//FindLabels find label doc by labels and project, check if the project has certain labels
//if map is empty. will return default labels doc which has no labels
func FindLabels(ctx context.Context, domain, project string, labels map[string]string) (*model.LabelDoc, error) {
func FindLabels(ctx context.Context, domain, project, alias string, labels map[string]string) (*model.LabelDoc, error) {
collection := session.GetDB().Collection(session.CollectionLabel)

filter := bson.M{"domain": domain, "project": project}
for k, v := range labels {
filter["labels."+k] = v
}
if len(labels) == 0 {
filter["labels"] = defaultLabels //allow key without labels
filter["labels"] = defaultValues //allow key without labels
}
if len(alias) != 0 {
filter["alias"] = alias
}
cur, err := collection.Find(ctx, filter)
if err != nil {

return nil, err
}
defer cur.Close(ctx)
Expand Down Expand Up @@ -74,7 +77,7 @@ func FindLabels(ctx context.Context, domain, project string, labels map[string]s

//Exist check whether the project has certain label or not and return label ID
func Exist(ctx context.Context, domain string, project string, labels map[string]string) (string, error) {
l, err := FindLabels(ctx, domain, project, labels)
l, err := FindLabels(ctx, domain, project, "", labels)
if err != nil {
if err.Error() == context.DeadlineExceeded.Error() {
openlogging.Error("find label failed, dead line exceeded", openlogging.WithTags(openlogging.Tags{
Expand Down Expand Up @@ -104,3 +107,15 @@ func CreateLabel(ctx context.Context, domain string, labels map[string]string, p
}
return l, nil
}

//PutAlias by label id
func PutAlias(ctx context.Context, label_id, alias string) (string, error) {
collection := session.GetDB().Collection(session.CollectionLabel)
queryFilter := bson.M{"id": label_id}
updateFilter := bson.D{primitive.E{Key: "$set", Value: bson.M{"alias": alias}}}
cur := collection.FindOneAndUpdate(ctx, queryFilter, updateFilter)
if cur.Err() != nil {
return "", cur.Err()
}
return alias, nil
}
12 changes: 12 additions & 0 deletions server/service/mongo/label/label_service.go
@@ -0,0 +1,12 @@
package label

import (
"context"
)

type Service struct {
}

func (s *Service) CreateOrUpdateAlias(ctx context.Context, domain, project, label_id, alias string) (string, error) {
return PutAlias(ctx, label_id, alias)
}
5 changes: 5 additions & 0 deletions server/service/service.go
Expand Up @@ -29,6 +29,7 @@ var (
HistoryService History
RevisionService Revision
DBInit Init
LabelService Label
)

//db errors
Expand All @@ -55,5 +56,9 @@ type Revision interface {
GetRevision(ctx context.Context) (int64, error)
}

type Label interface {
CreateOrUpdateAlias(ctx context.Context, domain, project, label_id, alias string) (string, error)
}

//Init init db session
type Init func() error

0 comments on commit 36a2d78

Please sign in to comment.