Skip to content

Commit

Permalink
Pass partial octant state to JS plugins
Browse files Browse the repository at this point in the history
[vmware-archive#1512, vmware-archive#1513]

Signed-off-by: vikram yadav <vyadav@pivotal.io>
  • Loading branch information
vikram yadav committed Mar 29, 2021
1 parent 559d039 commit feed139
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
6 changes: 6 additions & 0 deletions internal/api/action_request_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func (a *ActionRequestManager) Handlers() []octant.ClientRequestHandler {
func (a *ActionRequestManager) PerformAction(state octant.State, payload action.Payload) error {
ctx := ocontext.WithWebsocketClientID(context.TODO(), state.GetClientID())

pluginState := ocontext.PluginState{
Filters: state.GetFilters(),
Namespace: state.GetNamespace(),
}
ctx = ocontext.WithPluginState(ctx, pluginState)

actionName, err := payload.String("action")
if err != nil {
// TODO: alert the user this action doesn't exist (GH#493)
Expand Down
5 changes: 5 additions & 0 deletions internal/api/content_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ func (cm *ContentManager) generateContent(ctx context.Context, state octant.Stat
}

ctx = ocontext.WithWebsocketClientID(ctx, state.GetClientID())
pluginState := ocontext.PluginState{
Filters: state.GetFilters(),
Namespace: state.GetNamespace(),
}
ctx = ocontext.WithPluginState(ctx, pluginState)

contentResponse, err := m.Content(ctx, modulePath, options)
if err != nil {
Expand Down
26 changes: 25 additions & 1 deletion internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

package context

import "context"
import (
"context"

"github.com/vmware-tanzu/octant/internal/octant"
)

type OctantContextKey string

Expand Down Expand Up @@ -33,3 +37,23 @@ func WebsocketClientIDFrom(ctx context.Context) string {
func WithWebsocketClientID(ctx context.Context, id string) context.Context {
return context.WithValue(ctx, WebsocketClientIDKey, id)
}

type OctantPluginState string

const PluginStateKey = OctantPluginState("clientState")

type PluginState struct {
Filters []octant.Filter `json:"filters"`
Namespace string `json:"namespace"`
}

func WithPluginState(ctx context.Context, state PluginState) context.Context {
return context.WithValue(ctx, PluginStateKey, state)
}

func PluginStateFrom(ctx context.Context) PluginState {
if ctx.Value(PluginStateKey) == nil {
return PluginState{}
}
return ctx.Value(PluginStateKey).(PluginState)
}
13 changes: 13 additions & 0 deletions pkg/plugin/javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func (t *jsPlugin) Content(ctx context.Context, contentPath string) (component.C

t.loop.RunOnLoop(func(vm *goja.Runtime) {
clientID := ocontext.WebsocketClientIDFrom(ctx)
ostate := ocontext.PluginStateFrom(ctx)

handler, err := vm.RunString("_concretePlugin.contentHandler")
if err != nil {
Expand All @@ -258,6 +259,10 @@ func (t *jsPlugin) Content(ctx context.Context, contentPath string) (component.C
if err := obj.Set("clientID", vm.ToValue(clientID)); err != nil {
errCh <- fmt.Errorf("unable to set clientID: %w", err)
}
if err := obj.Set("ostate", vm.ToValue(ostate)); err != nil {
errCh <- fmt.Errorf("unable to set octant state: %w", err)
}

s, err := cHandler(t.pluginClass, obj)
if err != nil {
errCh <- fmt.Errorf("calling contentHandler: %w", err)
Expand Down Expand Up @@ -457,6 +462,7 @@ func (t *jsPlugin) HandleAction(ctx context.Context, actionPath string, payload

t.loop.RunOnLoop(func(vm *goja.Runtime) {
clientID := ocontext.WebsocketClientIDFrom(ctx)
ostate := ocontext.PluginStateFrom(ctx)

handler, err := vm.RunString("_concretePlugin.actionHandler")
if err != nil {
Expand Down Expand Up @@ -486,6 +492,9 @@ func (t *jsPlugin) HandleAction(ctx context.Context, actionPath string, payload
errCh <- fmt.Errorf("unable to set clientID: %w", err)
return
}
if err := obj.Set("ostate", vm.ToValue(ostate)); err != nil {
errCh <- fmt.Errorf("unable to set octant state: %w", err)
}

s, err := cHandler(t.pluginClass, obj)
if err != nil {
Expand Down Expand Up @@ -570,6 +579,7 @@ func (t *jsPlugin) objectRequestCall(ctx context.Context, handlerName string, ob

t.loop.RunOnLoop(func(vm *goja.Runtime) {
clientID := ocontext.WebsocketClientIDFrom(ctx)
ostate := ocontext.PluginStateFrom(ctx)

handler, err := vm.RunString(fmt.Sprintf("_concretePlugin.%s", handlerName))
if err != nil {
Expand All @@ -592,6 +602,9 @@ func (t *jsPlugin) objectRequestCall(ctx context.Context, handlerName string, ob
errCh <- fmt.Errorf("unable to set clientID: %w", err)
return
}
if err := obj.Set("ostate", vm.ToValue(ostate)); err != nil {
errCh <- fmt.Errorf("unable to set octant state: %w", err)
}
s, err := cHandler(t.pluginClass, obj)
if err != nil {
errCh <- err
Expand Down

0 comments on commit feed139

Please sign in to comment.