From 6acce3909d5541a6b73311ca289ae03359232ab7 Mon Sep 17 00:00:00 2001 From: Tharun Date: Tue, 14 Apr 2020 18:13:21 +0530 Subject: [PATCH] feature exposed creationTime & modificationTime for kuma * fixed remote store create and update function and updated tests Feature #661 --- pkg/plugins/resources/remote/store.go | 16 ++++++++------- pkg/plugins/resources/remote/store_test.go | 23 +++++++++++++--------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/pkg/plugins/resources/remote/store.go b/pkg/plugins/resources/remote/store.go index b9f6877039b2..139c048c83f7 100644 --- a/pkg/plugins/resources/remote/store.go +++ b/pkg/plugins/resources/remote/store.go @@ -33,9 +33,10 @@ type remoteStore struct { func (s *remoteStore) Create(ctx context.Context, res model.Resource, fs ...store.CreateOptionsFunc) error { opts := store.NewCreateOptions(fs...) meta := rest.ResourceMeta{ - Type: string(res.GetType()), - Name: opts.Name, - Mesh: opts.Mesh, + Type: string(res.GetType()), + Name: opts.Name, + Mesh: opts.Mesh, + CreationTime: opts.CreationTime, } if err := s.upsert(ctx, res, meta); err != nil { return err @@ -43,11 +44,12 @@ func (s *remoteStore) Create(ctx context.Context, res model.Resource, fs ...stor return nil } func (s *remoteStore) Update(ctx context.Context, res model.Resource, fs ...store.UpdateOptionsFunc) error { - _ = store.NewUpdateOptions(fs...) + opts := store.NewUpdateOptions(fs...) meta := rest.ResourceMeta{ - Type: string(res.GetType()), - Name: res.GetMeta().GetName(), - Mesh: res.GetMeta().GetMesh(), + Type: string(res.GetType()), + Name: res.GetMeta().GetName(), + Mesh: res.GetMeta().GetMesh(), + ModificationTime: opts.ModificationTime, } if err := s.upsert(ctx, res, meta); err != nil { return err diff --git a/pkg/plugins/resources/remote/store_test.go b/pkg/plugins/resources/remote/store_test.go index 7e2983f7dcab..92b75c6b5c06 100644 --- a/pkg/plugins/resources/remote/store_test.go +++ b/pkg/plugins/resources/remote/store_test.go @@ -9,6 +9,7 @@ import ( "os" "path/filepath" "strings" + "time" "github.com/Kong/kuma/api/mesh/v1alpha1" "github.com/Kong/kuma/pkg/core/resources/apis/mesh" @@ -29,6 +30,7 @@ import ( var _ = Describe("RemoteStore", func() { + var t1, t2 time.Time type RequestAssertion = func(req *http.Request) setupStore := func(file string, assertion RequestAssertion) core_store.ResourceStore { @@ -72,7 +74,10 @@ var _ = Describe("RemoteStore", func() { } return remote.NewStore(client, apis) } - + BeforeEach(func() { + t1, _ = time.Parse(time.RFC3339, "2018-07-17T16:05:36.995+00:00") + t2, _ = time.Parse(time.RFC3339, "2019-07-17T16:05:36.995+00:00") + }) Describe("Get()", func() { It("should get resource", func() { // setup @@ -158,7 +163,7 @@ var _ = Describe("RemoteStore", func() { Expect(req.URL.Path).To(Equal(fmt.Sprintf("/meshes/default/traffic-routes/%s", name))) bytes, err := ioutil.ReadAll(req.Body) Expect(err).ToNot(HaveOccurred()) - Expect(bytes).To(MatchJSON(`{"mesh":"default","name":"res-1","path":"/some-path","type":"SampleTrafficRoute"}`)) + Expect(bytes).To(MatchJSON(`{"mesh":"default","name":"res-1","path":"/some-path","type":"SampleTrafficRoute","creationTime": "2018-07-17T16:05:36.995Z","modificationTime": "0001-01-01T00:00:00Z"}`)) }) // when @@ -167,7 +172,7 @@ var _ = Describe("RemoteStore", func() { Path: "/some-path", }, } - err := store.Create(context.Background(), &resource, core_store.CreateByKey(name, "default")) + err := store.Create(context.Background(), &resource, core_store.CreateByKey(name, "default"), core_store.CreatedAt(t1)) // then Expect(err).ToNot(HaveOccurred()) @@ -180,14 +185,14 @@ var _ = Describe("RemoteStore", func() { Expect(req.URL.Path).To(Equal(fmt.Sprintf("/meshes/%s", meshName))) bytes, err := ioutil.ReadAll(req.Body) Expect(err).ToNot(HaveOccurred()) - Expect(bytes).To(MatchJSON(`{"mesh":"someMesh","name":"someMesh","type":"Mesh"}`)) + Expect(bytes).To(MatchJSON(`{"mesh":"someMesh","name":"someMesh","type":"Mesh","creationTime": "2018-07-17T16:05:36.995Z","modificationTime": "0001-01-01T00:00:00Z"}`)) }) // when resource := mesh.MeshResource{ Spec: v1alpha1.Mesh{}, } - err := store.Create(context.Background(), &resource, core_store.CreateByKey(meshName, meshName)) + err := store.Create(context.Background(), &resource, core_store.CreateByKey(meshName, meshName), core_store.CreatedAt(t1)) // then Expect(err).ToNot(HaveOccurred()) @@ -234,7 +239,7 @@ var _ = Describe("RemoteStore", func() { Expect(req.URL.Path).To(Equal(fmt.Sprintf("/meshes/default/traffic-routes/%s", name))) bytes, err := ioutil.ReadAll(req.Body) Expect(err).ToNot(HaveOccurred()) - Expect(bytes).To(MatchJSON(`{"mesh":"default","name":"res-1","path":"/some-path","type":"SampleTrafficRoute"}`)) + Expect(bytes).To(MatchJSON(`{"mesh":"default","name":"res-1","path":"/some-path","type":"SampleTrafficRoute","creationTime": "0001-01-01T00:00:00Z","modificationTime": "2019-07-17T16:05:36.995Z"}`)) }) // when @@ -247,7 +252,7 @@ var _ = Describe("RemoteStore", func() { Name: name, }, } - err := store.Update(context.Background(), &resource) + err := store.Update(context.Background(), &resource, core_store.ModifiedAt(t2)) // then Expect(err).ToNot(HaveOccurred()) @@ -260,7 +265,7 @@ var _ = Describe("RemoteStore", func() { Expect(req.URL.Path).To(Equal(fmt.Sprintf("/meshes/%s", meshName))) bytes, err := ioutil.ReadAll(req.Body) Expect(err).ToNot(HaveOccurred()) - Expect(bytes).To(MatchJSON(`{"mesh":"someMesh","mtls":{"ca":{"builtin":{}}},"name":"someMesh","type":"Mesh"}`)) + Expect(bytes).To(MatchJSON(`{"mesh":"someMesh","mtls":{"ca":{"builtin":{}}},"name":"someMesh","type":"Mesh","creationTime": "0001-01-01T00:00:00Z","modificationTime": "2019-07-17T16:05:36.995Z"}`)) }) // when @@ -279,7 +284,7 @@ var _ = Describe("RemoteStore", func() { Name: meshName, }, } - err := store.Update(context.Background(), &resource) + err := store.Update(context.Background(), &resource, core_store.ModifiedAt(t2)) // then Expect(err).ToNot(HaveOccurred())