Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
Add lookup for additional metadata from JS plugins
Browse files Browse the repository at this point in the history
Related to #2042

Signed-off-by: Vikram Yadav <yvikram@vmware.com>
  • Loading branch information
Vikram Yadav committed Mar 3, 2021
1 parent d201535 commit d6ad3d2
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 4 deletions.
10 changes: 10 additions & 0 deletions pkg/plugin/javascript/dashboard_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ func (d *DashboardDelete) Call(ctx context.Context, vm *goja.Runtime) func(c goj
// This will never error since &key is a pointer to a type.
_ = vm.ExportTo(obj, &key)

if len(c.Arguments) > 1 {
var metadata map[string]interface{}
metadataObj := c.Argument(1).ToObject(vm)

_ = vm.ExportTo(metadataObj, &metadata)
for key, val := range metadata {
ctx = context.WithValue(ctx, key, val)
}
}

if err := d.storage.ObjectStore().Delete(ctx, key); err != nil {
panic(panicMessage(vm, err, ""))
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/plugin/javascript/dashboard_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func TestDashboardDelete_Call(t *testing.T) {
name: "in general",
ctorArgs: ctorArgs{
storage: func(ctx context.Context, ctrl *gomock.Controller) octant.Storage {
ctx = context.WithValue(ctx, "accessToken", "secret")
objectStore := fake2.NewMockStore(ctrl)

objectStore.EXPECT().
Delete(ctx, store.Key{
Namespace: "test",
Expand All @@ -61,7 +63,7 @@ func TestDashboardDelete_Call(t *testing.T) {
return storage
},
},
call: `dashClient.Delete({namespace:'test', apiVersion: 'v1', kind:'ReplicaSet', name: 'my-replica-set'})`,
call: `dashClient.Delete({namespace:'test', apiVersion: 'v1', kind:'ReplicaSet', name: 'my-replica-set'},{"accessToken": "secret"})`,
},
{
name: "delete fails",
Expand Down
10 changes: 10 additions & 0 deletions pkg/plugin/javascript/dashboard_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ func (d *DashboardGet) Call(ctx context.Context, vm *goja.Runtime) func(c goja.F
// This will never error since &key is a pointer to a type.
_ = vm.ExportTo(obj, &key)

if len(c.Arguments) > 1 {
var metadata map[string]interface{}
metadataObj := c.Argument(1).ToObject(vm)

_ = vm.ExportTo(metadataObj, &metadata)
for key, val := range metadata {
ctx = context.WithValue(ctx, key, val)
}
}

u, err := d.storage.ObjectStore().Get(ctx, key)
if err != nil {
panic(panicMessage(vm, err, ""))
Expand Down
3 changes: 2 additions & 1 deletion pkg/plugin/javascript/dashboard_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestDashboardGet_Call(t *testing.T) {
name: "in general",
ctorArgs: ctorArgs{
storage: func(ctx context.Context, ctrl *gomock.Controller) octant.Storage {
ctx = context.WithValue(ctx, "accessToken", "secret")
objectStore := fake2.NewMockStore(ctrl)
objectStore.EXPECT().
Get(ctx, store.Key{
Expand All @@ -62,7 +63,7 @@ func TestDashboardGet_Call(t *testing.T) {
return storage
},
},
call: `dashClient.Get({namespace:'test', apiVersion: 'v1', kind:'Pod', name: 'pod'})`,
call: `dashClient.Get({namespace:'test', apiVersion: 'v1', kind:'Pod', name: 'pod'},{"accessToken": "secret"})`,
},
{
name: "delete fails",
Expand Down
10 changes: 10 additions & 0 deletions pkg/plugin/javascript/dashboard_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ func (d *DashboardList) Call(ctx context.Context, vm *goja.Runtime) func(c goja.
panicMessage(vm, fmt.Errorf("key is invalid: %w", err), "")
}

if len(c.Arguments) > 1 {
var metadata map[string]interface{}
metadataObj := c.Argument(1).ToObject(vm)

_ = vm.ExportTo(metadataObj, &metadata)
for key, val := range metadata {
ctx = context.WithValue(ctx, key, val)
}
}

u, _, err := d.storage.ObjectStore().List(ctx, key)
if err != nil {
panic(panicMessage(vm, err, ""))
Expand Down
3 changes: 2 additions & 1 deletion pkg/plugin/javascript/dashboard_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestDashboardList_Call(t *testing.T) {
name: "in general",
ctorArgs: ctorArgs{
storage: func(ctx context.Context, ctrl *gomock.Controller) octant.Storage {
ctx = context.WithValue(ctx, "accessToken", "secret")
objectStore := fake2.NewMockStore(ctrl)
objectStore.EXPECT().
List(ctx, store.Key{
Expand All @@ -61,7 +62,7 @@ func TestDashboardList_Call(t *testing.T) {
return storage
},
},
call: `dashClient.List({namespace:'test', apiVersion: 'v1', kind:'Pod'})`,
call: `dashClient.List({namespace:'test', apiVersion: 'v1', kind:'Pod'},{"accessToken": "secret"})`,
},
{
name: "list fails",
Expand Down
10 changes: 10 additions & 0 deletions pkg/plugin/javascript/dashboard_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ func (d *DashboardUpdate) Call(ctx context.Context, vm *goja.Runtime) func(c goj
namespace := c.Argument(0).String()
update := c.Argument(1).String()

if len(c.Arguments) > 2 {
var metadata map[string]interface{}
metadataObj := c.Argument(2).ToObject(vm)

_ = vm.ExportTo(metadataObj, &metadata)
for key, val := range metadata {
ctx = context.WithValue(ctx, key, val)
}
}

results, err := d.storage.ObjectStore().CreateOrUpdateFromYAML(ctx, namespace, update)
if err != nil {
panic(panicMessage(vm, err, ""))
Expand Down
3 changes: 2 additions & 1 deletion pkg/plugin/javascript/dashboard_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestDashboardUpdate_Call(t *testing.T) {
name: "in general",
ctorArgs: ctorArgs{
storage: func(ctx context.Context, ctrl *gomock.Controller) octant.Storage {
ctx = context.WithValue(ctx, "accessToken", "secret")
objectStore := fake2.NewMockStore(ctrl)
objectStore.EXPECT().
CreateOrUpdateFromYAML(ctx, "test", "create-yaml").
Expand All @@ -55,7 +56,7 @@ func TestDashboardUpdate_Call(t *testing.T) {
return storage
},
},
call: `dashClient.Update('test', 'create-yaml')`,
call: `dashClient.Update('test', 'create-yaml',{"accessToken": "secret"})`,
},
{
name: "create fails",
Expand Down

0 comments on commit d6ad3d2

Please sign in to comment.