Skip to content

Commit

Permalink
* added createdAt and modifiedAt params to upsert function in apply
Browse files Browse the repository at this point in the history
Feature #661
  • Loading branch information
tharun208 committed Apr 15, 2020
1 parent f57aa4f commit e5ed83b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 4 additions & 4 deletions app/kumactl/cmd/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func NewApplyCmd(pctx *kumactl_cmd.RootContext) *cobra.Command {
return err
}

if err := upsert(rs, res); err != nil {
if err := upsert(pctx.Now(), rs, res); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -154,23 +154,23 @@ func processConfigTemplate(config string, values map[string]string) ([]byte, err
return []byte(data), nil
}

func upsert(rs store.ResourceStore, res model.Resource) error {
func upsert(now time.Time, rs store.ResourceStore, res model.Resource) error {
newRes, err := registry.Global().NewObject(res.GetType())
if err != nil {
return err
}
meta := res.GetMeta()
if err := rs.Get(context.Background(), newRes, store.GetByKey(meta.GetName(), meta.GetMesh())); err != nil {
if store.IsResourceNotFound(err) {
return rs.Create(context.Background(), res, store.CreateByKey(meta.GetName(), meta.GetMesh()))
return rs.Create(context.Background(), res, store.CreateByKey(meta.GetName(), meta.GetMesh()), store.CreatedAt(now))
} else {
return err
}
}
if err := newRes.SetSpec(res.GetSpec()); err != nil {
return err
}
return rs.Update(context.Background(), newRes)
return rs.Update(context.Background(), newRes, store.ModifiedAt(now))
}

func parseResource(bytes []byte) (model.Resource, error) {
Expand Down
4 changes: 3 additions & 1 deletion app/kumactl/cmd/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"strconv"
"strings"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
Expand All @@ -33,10 +34,11 @@ var _ = Describe("kumactl apply", func() {
var rootCtx *kumactl_cmd.RootContext
var rootCmd *cobra.Command
var store core_store.ResourceStore

now, _ := time.Parse(time.RFC3339, "2018-07-17T16:05:36.995Z")
BeforeEach(func() {
rootCtx = &kumactl_cmd.RootContext{
Runtime: kumactl_cmd.RootRuntime{
Now: func() time.Time { return now },
NewResourceStore: func(*config_proto.ControlPlaneCoordinates_ApiServer) (core_store.ResourceStore, error) {
return store, nil
},
Expand Down

0 comments on commit e5ed83b

Please sign in to comment.