Skip to content

Commit

Permalink
feature exposed creationTime & modificationTime for kuma
Browse files Browse the repository at this point in the history
* fixed remote store create and update function and updated tests
Feature kumahq#661
  • Loading branch information
tharun208 committed Apr 14, 2020
1 parent e104eb0 commit 6acce39
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
16 changes: 9 additions & 7 deletions pkg/plugins/resources/remote/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,23 @@ 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
}
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
Expand Down
23 changes: 14 additions & 9 deletions pkg/plugins/resources/remote/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand All @@ -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())
Expand All @@ -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
Expand All @@ -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())
Expand Down

0 comments on commit 6acce39

Please sign in to comment.