Skip to content

Commit

Permalink
#78 #80 add status and create/update time (#91)
Browse files Browse the repository at this point in the history
* add status and create/update time

* modify as comment
  • Loading branch information
GuoYL123 committed Feb 11, 2020
1 parent 19f1abd commit 9489010
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 12 deletions.
11 changes: 10 additions & 1 deletion deployments/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ db.createCollection( "kv", {
},
labels: {
bsonType: "object"
}
},
create_time: {
bsonType: "string",
},
update_time: {
bsonType: "string",
},
status: {
bsonType: "string",
},
}
} }
} );
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,10 @@ github.com/klauspost/compress v1.9.5/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE=
Expand Down Expand Up @@ -368,6 +370,7 @@ google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRn
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
Expand Down
4 changes: 3 additions & 1 deletion pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ const (
ContentTypeYaml = "text/yaml"
)

//const for server/resource/v1
//const of server
const (
PatternExact = "exact"
StatusEnabled = "enabled"
StatusDisabled = "disabled"
MsgDomainMustNotBeEmpty = "domain must not be empty"
MsgIllegalLabels = "label value can not be empty, " +
"label can not be duplicated, please check query parameters"
Expand Down
3 changes: 3 additions & 0 deletions pkg/model/db_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type KVDoc struct {
CreateRevision int64 `json:"create_revision,omitempty" bson:"create_revision," yaml:"create_revision,omitempty"`
UpdateRevision int64 `json:"update_revision,omitempty" bson:"update_revision," yaml:"update_revision,omitempty"`
Project string `json:"project,omitempty" yaml:"project,omitempty"`
Status string `json:"status,omitempty" yaml:"status,omitempty"`
CreatTime string `json:"create_time,omitempty" yaml:"create_time,omitempty"`
UpdateTime string `json:"update_time,omitempty" yaml:"update_time,omitempty"`

Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` //redundant
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"` //redundant
Expand Down
17 changes: 14 additions & 3 deletions server/resource/v1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ import (
"gopkg.in/yaml.v2"
)

//const of server

//err
var (
ErrInvalidRev = errors.New(common.MsgInvalidRev)
Expand Down Expand Up @@ -200,8 +198,18 @@ func checkPagination(limitStr, offsetStr string) (int64, int64, error) {
}
return limit, offset, err
}

func checkStatus(status string) (string, error) {
if status != "" {
if status != common.StatusEnabled && status != common.StatusDisabled {
return "", errors.New("invalid status string")
}
}
return status, nil
}

func queryAndResponse(rctx *restful.Context,
domain interface{}, project string, key string, labels map[string]string, limit, offset int64) {
domain interface{}, project string, key string, labels map[string]string, limit, offset int64, status string) {
m := getMatchPattern(rctx)
opts := []service.FindOption{
service.WithKey(key),
Expand All @@ -212,6 +220,9 @@ func queryAndResponse(rctx *restful.Context,
if m == common.PatternExact {
opts = append(opts, service.WithExactLabels())
}
if status != "" {
opts = append(opts, service.WithStatus(status))
}
kv, err := service.KVService.List(rctx.Ctx, domain.(string), project, opts...)
if err != nil {
if err == service.ErrKeyNotExists {
Expand Down
31 changes: 24 additions & 7 deletions server/resource/v1/kv_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func (r *KVResource) Put(context *restful.Context) {
kv.Key = key
kv.Domain = domain.(string)
kv.Project = project
_, err = checkStatus(kv.Status)
if err != nil {
WriteErrResponse(context, http.StatusInternalServerError, err.Error(), common.ContentTypeText)
return
}
kv, err = service.KVService.CreateOrUpdate(context.Ctx, kv)
if err != nil {
openlogging.Error(fmt.Sprintf("put [%v] err:%s", kv, err.Error()))
Expand Down Expand Up @@ -103,7 +108,13 @@ func (r *KVResource) GetByKey(rctx *restful.Context) {
WriteErrResponse(rctx, http.StatusBadRequest, err.Error(), common.ContentTypeText)
return
}
returnData(rctx, domain, project, labels, limit, offset)
statusStr := rctx.ReadQueryParameter("status")
status, err := checkStatus(statusStr)
if err != nil {
WriteErrResponse(rctx, http.StatusBadRequest, err.Error(), common.ContentTypeText)
return
}
returnData(rctx, domain, project, labels, limit, offset, status)
}

//List response kv list
Expand All @@ -127,15 +138,21 @@ func (r *KVResource) List(rctx *restful.Context) {
WriteErrResponse(rctx, http.StatusBadRequest, err.Error(), common.ContentTypeText)
return
}
returnData(rctx, domain, project, labels, limit, offset)
statusStr := rctx.ReadQueryParameter("status")
status, err := checkStatus(statusStr)
if err != nil {
WriteErrResponse(rctx, http.StatusBadRequest, err.Error(), common.ContentTypeText)
return
}
returnData(rctx, domain, project, labels, limit, offset, status)
}

func returnData(rctx *restful.Context, domain interface{}, project string, labels map[string]string, limit, offset int64) {
func returnData(rctx *restful.Context, domain interface{}, project string, labels map[string]string, limit, offset int64, status string) {
revStr := rctx.ReadQueryParameter(common.QueryParamRev)
wait := rctx.ReadQueryParameter(common.QueryParamWait)
if revStr == "" {
if wait == "" {
queryAndResponse(rctx, domain, project, "", labels, limit, offset)
queryAndResponse(rctx, domain, project, "", labels, limit, offset, status)
return
}
changed, err := eventHappened(rctx, wait, &pubsub.Topic{
Expand All @@ -149,7 +166,7 @@ func returnData(rctx *restful.Context, domain interface{}, project string, label
return
}
if changed {
queryAndResponse(rctx, domain, project, "", labels, limit, offset)
queryAndResponse(rctx, domain, project, "", labels, limit, offset, status)
return
}
rctx.WriteHeader(http.StatusNotModified)
Expand All @@ -164,7 +181,7 @@ func returnData(rctx *restful.Context, domain interface{}, project string, label
return
}
if revised {
queryAndResponse(rctx, domain, project, "", labels, limit, offset)
queryAndResponse(rctx, domain, project, "", labels, limit, offset, status)
return
} else if wait != "" {
changed, err := eventHappened(rctx, wait, &pubsub.Topic{
Expand All @@ -178,7 +195,7 @@ func returnData(rctx *restful.Context, domain interface{}, project string, label
return
}
if changed {
queryAndResponse(rctx, domain, project, "", labels, limit, offset)
queryAndResponse(rctx, domain, project, "", labels, limit, offset, status)
return
}
rctx.WriteHeader(http.StatusNotModified)
Expand Down
7 changes: 7 additions & 0 deletions server/service/mongo/kv/kv_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)

//createKey get latest revision from history
Expand All @@ -46,6 +47,8 @@ func createKey(ctx context.Context, kv *model.KVDoc) (*model.KVDoc, error) {
}
kv.UpdateRevision = revision
kv.CreateRevision = revision
kv.CreatTime = time.Now().String()
kv.UpdateTime = time.Now().String()
_, err = collection.InsertOne(ctx, kv)
if err != nil {
openlogging.Error("create error", openlogging.WithTags(openlogging.Tags{
Expand All @@ -69,6 +72,7 @@ func createKey(ctx context.Context, kv *model.KVDoc) (*model.KVDoc, error) {
//updateKeyValue update key value and add new revision
func updateKeyValue(ctx context.Context, kv *model.KVDoc) error {
var err error
kv.UpdateTime = time.Now().String()
kv.UpdateRevision, err = counter.ApplyRevision(ctx, kv.Domain)
if err != nil {
return err
Expand Down Expand Up @@ -119,6 +123,9 @@ func findKV(ctx context.Context, domain string, project string, opts service.Fin
if opts.Offset != 0 {
opt = opt.SetSkip(opts.Offset)
}
if opts.Status != "" {
filter["status"] = opts.Status
}
cur, err := collection.Find(ctx, filter, opt)
if err != nil {
if err.Error() == context.DeadlineExceeded.Error() {
Expand Down
8 changes: 8 additions & 0 deletions server/service/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func NewDefaultFindOpts() FindOptions {
//FindOptions is option to find key value
type FindOptions struct {
ExactLabels bool
Status string
Depth int
Key string
Labels map[string]string
Expand Down Expand Up @@ -59,6 +60,13 @@ func WithKey(key string) FindOption {
}
}

//WithStatus enabled/disabled
func WithStatus(status string) FindOption {
return func(o *FindOptions) {
o.Status = status
}
}

//WithTimeout will return err if execution take too long
func WithTimeout(d time.Duration) FindOption {
return func(o *FindOptions) {
Expand Down

0 comments on commit 9489010

Please sign in to comment.