Skip to content

Commit

Permalink
Merge ed17437 into f35cbba
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxiaoliang committed Jan 4, 2020
2 parents f35cbba + ed17437 commit 3d707cf
Show file tree
Hide file tree
Showing 20 changed files with 335 additions and 654 deletions.
2 changes: 1 addition & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ var _ = Describe("Client", func() {
Endpoint: "http://127.0.0.1:30110",
})
It("should be 204", func() {
err := client3.Delete(context.TODO(), kv.ID.String(), "", WithProject("test"))
err := client3.Delete(context.TODO(), kv.ID, "", WithProject("test"))
Ω(err).ShouldNot(HaveOccurred())
})
})
Expand Down
1 change: 1 addition & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
QueryByLabelsCon = "&"
QueryParamWait = "wait"
QueryParamMatch = "match"
QueryParamKeyID = "kvID"
)

//http headers
Expand Down
27 changes: 6 additions & 21 deletions pkg/model/mongodb_doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,17 @@

package model

import (
"github.com/apache/servicecomb-kie/server/id"
)

//LabelDoc is database struct to store labels
type LabelDoc struct {
ID id.ID `json:"_id,omitempty" bson:"_id,omitempty" yaml:"_id,omitempty" swag:"string"`
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
Revision int `json:"revision,omitempty" yaml:"revision,omitempty"`
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"` //tenant info
Project string `json:"project,omitempty" yaml:"project,omitempty"`
ID string `json:"id,omitempty" bson:"id,omitempty" yaml:"id,omitempty" swag:"string"`
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"`
}

//KVDoc is database struct to store kv
type KVDoc struct {
ID id.ID `json:"_id,omitempty" bson:"_id,omitempty" yaml:"_id,omitempty" swag:"string"`
ID string `json:"id,omitempty" bson:"id,omitempty" yaml:"id,omitempty" swag:"string"`
LabelID string `json:"label_id,omitempty" bson:"label_id,omitempty" yaml:"label_id,omitempty"`
Key string `json:"key" yaml:"key"`
Value string `json:"value,omitempty" yaml:"value,omitempty"`
Expand All @@ -41,16 +36,6 @@ type KVDoc struct {

Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` //redundant
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"` //redundant
Revision int `json:"revision,omitempty" bson:"-" yaml:"revision,omitempty"`
Revision int `json:"revision,omitempty" bson:"revision," yaml:"revision,omitempty"`
Project string `json:"project,omitempty" yaml:"project,omitempty"`
}

//LabelRevisionDoc is database struct to store label history stats
type LabelRevisionDoc struct {
ID id.ID `json:"_id,omitempty" bson:"_id,omitempty" yaml:"_id,omitempty" swag:"string"`
LabelID string `json:"label_id,omitempty" bson:"label_id,omitempty" yaml:"label_id,omitempty"`
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
Domain string `json:"-" yaml:"-"`
KVs []*KVDoc `json:"data,omitempty" bson:"data,omitempty" yaml:"data,omitempty"`
Revision int `json:"revision" yaml:"revision"`
}
5 changes: 3 additions & 2 deletions server/resource/v1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ func writeResponse(ctx *restful.Context, v interface{}) error {
}
return ctx.WriteJSON(v, goRestful.MIME_JSON) // json is default
}
func getLabels(labelStr string) (map[string]string, error) {
labelsSlice := strings.Split(labelStr, ",")

//GetLabels parse labels
func GetLabels(labelsSlice []string) (map[string]string, error) {
labels := make(map[string]string, len(labelsSlice))
for _, v := range labelsSlice {
v := strings.Split(v, ":")
Expand Down
65 changes: 27 additions & 38 deletions server/resource/v1/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,36 @@
package v1_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

. "github.com/apache/servicecomb-kie/server/resource/v1"
v1 "github.com/apache/servicecomb-kie/server/resource/v1"
"github.com/emicklei/go-restful"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

var _ = Describe("Common", func() {
Describe("set query combination", func() {
Context("valid param", func() {
r, err := http.NewRequest("GET",
"/kv?q=app:mall+service:payment&q=app:mall+service:payment+version:1.0.0",
nil)
It("should not return err ", func() {
Expect(err).Should(BeNil())
})
c, err := ReadLabelCombinations(restful.NewRequest(r))
It("should not return err ", func() {
Expect(err).Should(BeNil())
})
It("should has 2 combinations", func() {
Expect(len(c)).Should(Equal(2))
})
func TestGetLabels(t *testing.T) {
r, err := http.NewRequest("GET",
"/kv?q=app:mall+service:payment&q=app:mall+service:payment+version:1.0.0",
nil)
assert.NoError(t, err)
c, err := v1.ReadLabelCombinations(restful.NewRequest(r))
assert.NoError(t, err)
assert.Equal(t, 2, len(c))

})
Context("find default", func() {
r, err := http.NewRequest("GET",
"/kv",
nil)
It("should not return err ", func() {
Expect(err).Should(BeNil())
})
c, err := ReadLabelCombinations(restful.NewRequest(r))
It("should not return err ", func() {
Expect(err).Should(BeNil())
})
It("should has 1 combinations", func() {
Expect(len(c)).Should(Equal(1))
})
r, err = http.NewRequest("GET",
"/kv",
nil)
assert.NoError(t, err)
c, err = v1.ReadLabelCombinations(restful.NewRequest(r))
assert.NoError(t, err)
assert.Equal(t, 1, len(c))

})
})
})
r, err = http.NewRequest("GET",
"/kv?label=app:mall&label=service:payment",
nil)
assert.NoError(t, err)
req := restful.NewRequest(r)
m, err := v1.GetLabels(req.QueryParameters("label"))
assert.NoError(t, err)
assert.Equal(t, 2, len(m))
}
8 changes: 4 additions & 4 deletions server/resource/v1/doc_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ var (
"if it is empty, server will return kv which's labels partial match the label query param. " +
"uf it is exact, server will return kv which's labels exact match the label query param",
}
DocQueryKVIDParameters = &restful.Parameters{
DocQueryKeyIDParameters = &restful.Parameters{
DataType: "string",
Name: "kvID",
Name: common.QueryParamKeyID,
ParamType: goRestful.QueryParameterKind,
Required: true,
}
Expand Down Expand Up @@ -101,9 +101,9 @@ var (
ParamType: goRestful.PathParameterKind,
Required: true,
}
DocPathLabelID = &restful.Parameters{
DocPathKeyID = &restful.Parameters{
DataType: "string",
Name: "label_id",
Name: "key_id",
ParamType: goRestful.PathParameterKind,
Required: true,
}
Expand Down
14 changes: 7 additions & 7 deletions server/resource/v1/history_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ type HistoryResource struct {
//GetRevisions search key only by label
func (r *HistoryResource) GetRevisions(context *restful.Context) {
var err error
labelID := context.ReadPathParameter("label_id")
labelID := context.ReadPathParameter("key_id")
if labelID == "" {
openlogging.Debug("label id is null")
WriteErrResponse(context, http.StatusForbidden, "label_id must not be empty", common.ContentTypeText)
openlogging.Error("key id is nil")
WriteErrResponse(context, http.StatusForbidden, "key_id must not be empty", common.ContentTypeText)
return
}
key := context.ReadQueryParameter("key")
Expand Down Expand Up @@ -67,17 +67,17 @@ func (r *HistoryResource) URLPatterns() []restful.Route {
return []restful.Route{
{
Method: http.MethodGet,
Path: "/v1/{project}/kie/revision/{label_id}",
Path: "/v1/{project}/kie/revision/{key_id}",
ResourceFunc: r.GetRevisions,
FuncDesc: "get all revisions by label id",
FuncDesc: "get all revisions by key id",
Parameters: []*restful.Parameters{
DocPathProject, DocPathLabelID, DocQueryKeyParameters,
DocPathProject, DocPathKeyID,
},
Returns: []*restful.Returns{
{
Code: http.StatusOK,
Message: "true",
Model: []model.LabelHistoryResponse{},
Model: []model.KVDoc{},
},
},
Consumes: []string{goRestful.MIME_JSON, common.ContentTypeYaml},
Expand Down
111 changes: 52 additions & 59 deletions server/resource/v1/history_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,74 +19,67 @@ package v1_test
import (
"context"
"encoding/json"
"fmt"
"github.com/apache/servicecomb-kie/pkg/model"
v1 "github.com/apache/servicecomb-kie/server/resource/v1"
"github.com/apache/servicecomb-kie/server/service"
"io/ioutil"

"github.com/go-chassis/go-chassis/core/common"
"github.com/go-chassis/go-chassis/core/handler"

"fmt"
"github.com/go-chassis/go-chassis/server/restful/restfultest"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/apache/servicecomb-kie/pkg/model"
"github.com/apache/servicecomb-kie/server/config"
v1 "github.com/apache/servicecomb-kie/server/resource/v1"
_ "github.com/apache/servicecomb-kie/server/service/mongo"
"github.com/go-chassis/go-chassis/server/restful/restfultest"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("v1 history resource", func() {

config.Configurations = &config.Config{
DB: config.DB{},
func TestHistoryResource_GetRevisions(t *testing.T) {
kv := &model.KVDoc{
Key: "test",
Value: "revisions",
Labels: map[string]string{
"test": "revisions",
},
Domain: "default",
Project: "test",
}
kv, _ = service.KVService.CreateOrUpdate(context.Background(), kv)
path := fmt.Sprintf("/v1/test/kie/revision/%s", kv.ID)
r, _ := http.NewRequest("GET", path, nil)
revision := &v1.HistoryResource{}
chain, _ := handler.GetChain(common.Provider, "")
c, err := restfultest.New(revision, chain)
assert.NoError(t, err)
resp := httptest.NewRecorder()
c.ServeHTTP(resp, r)
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
data := make([]*model.KVDoc, 0)
err = json.Unmarshal(body, &data)
assert.NoError(t, err)
before := len(data)
assert.GreaterOrEqual(t, before, 1)

Describe("get history revisions", func() {
config.Configurations.DB.URI = "mongodb://kie:123@127.0.0.1:27017"
err := service.DBInit()
It("should not return err", func() {
Expect(err).Should(BeNil())
})
Context("valid param", func() {
kv := &model.KVDoc{
Key: "test",
Value: "revisions",
Labels: map[string]string{
"test": "revisions",
},
Domain: "default",
Project: "test",
}
kv, _ = service.KVService.CreateOrUpdate(context.Background(), kv)
path := fmt.Sprintf("/v1/%s/kie/revision/%s", "test", kv.LabelID)
r, _ := http.NewRequest("GET", path, nil)
revision := &v1.HistoryResource{}
chain, _ := handler.GetChain(common.Provider, "")
c, err := restfultest.New(revision, chain)
It("should not return err or nil", func() {
Expect(err).Should(BeNil())
})
resp := httptest.NewRecorder()
c.ServeHTTP(resp, r)

body, err := ioutil.ReadAll(resp.Body)
It("should not return err", func() {
Expect(err).Should(BeNil())
})
data := make([]*model.LabelRevisionDoc, 0)
err = json.Unmarshal(body, &data)
It("should not return err", func() {
Expect(err).Should(BeNil())
})

It("should return all revisions with the same label ID", func() {
Expect(len(data) > 0).Should(Equal(true))
Expect((data[0]).LabelID).Should(Equal(kv.LabelID))
})
})
t.Run("put again, should has 2 revision", func(t *testing.T) {
kv.Domain = "default"
kv.Project = "test"
kv, err = service.KVService.CreateOrUpdate(context.Background(), kv)
assert.NoError(t, err)
path := fmt.Sprintf("/v1/test/kie/revision/%s", kv.ID)
r, _ := http.NewRequest("GET", path, nil)
revision := &v1.HistoryResource{}
chain, _ := handler.GetChain(common.Provider, "")
c, err := restfultest.New(revision, chain)
assert.NoError(t, err)
resp := httptest.NewRecorder()
c.ServeHTTP(resp, r)
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
data := make([]*model.KVDoc, 0)
err = json.Unmarshal(body, &data)
assert.Equal(t, before+1, len(data))
})
})

}
25 changes: 11 additions & 14 deletions server/resource/v1/kv_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ func (r *KVResource) GetByKey(rctx *restful.Context) {
return
}
project := rctx.ReadPathParameter("project")
labelStr := rctx.ReadQueryParameter("label")
labelSlice := rctx.Req.QueryParameters("label")
var labels map[string]string
if labelStr != "" {
labels, err = getLabels(labelStr)
if len(labelSlice) != 0 {
labels, err = GetLabels(labelSlice)
if err != nil {
WriteErrResponse(rctx, http.StatusBadRequest, MsgIllegalLabels, common.ContentTypeText)
return
Expand Down Expand Up @@ -131,10 +131,10 @@ func (r *KVResource) List(rctx *restful.Context) {
WriteErrResponse(rctx, http.StatusInternalServerError, MsgDomainMustNotBeEmpty, common.ContentTypeText)
return
}
labelStr := rctx.ReadQueryParameter("label")
labelSlice := rctx.Req.QueryParameters("label")
var labels map[string]string
if labelStr != "" {
labels, err = getLabels(labelStr)
if len(labelSlice) != 0 {
labels, err = GetLabels(labelSlice)
if err != nil {
WriteErrResponse(rctx, http.StatusBadRequest, MsgIllegalLabels, common.ContentTypeText)
return
Expand Down Expand Up @@ -234,18 +234,16 @@ func (r *KVResource) Delete(context *restful.Context) {
if domain == nil {
WriteErrResponse(context, http.StatusInternalServerError, MsgDomainMustNotBeEmpty, common.ContentTypeText)
}
kvID := context.ReadQueryParameter("kvID")
kvID := context.ReadQueryParameter(common.QueryParamKeyID)
if kvID == "" {
WriteErrResponse(context, http.StatusBadRequest, ErrKvIDMustNotEmpty, common.ContentTypeText)
return
}
labelID := context.ReadQueryParameter("labelID")
err := service.KVService.Delete(kvID, labelID, domain.(string), project)
err := service.KVService.Delete(context.Ctx, kvID, domain.(string), project)
if err != nil {
openlogging.Error("delete failed ,", openlogging.WithTags(openlogging.Tags{
"kvID": kvID,
"labelID": labelID,
"error": err.Error(),
"kvID": kvID,
"error": err.Error(),
}))
WriteErrResponse(context, http.StatusInternalServerError, err.Error(), common.ContentTypeText)
return
Expand Down Expand Up @@ -334,8 +332,7 @@ func (r *KVResource) URLPatterns() []restful.Route {
FuncDesc: "delete key by kvID and labelID. Want better performance, give labelID",
Parameters: []*restful.Parameters{
DocPathProject,
DocQueryKVIDParameters,
DocQueryLabelIDParameters,
DocQueryKeyIDParameters,
},
Returns: []*restful.Returns{
{
Expand Down
Loading

0 comments on commit 3d707cf

Please sign in to comment.