-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (132 loc) · 4.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
"github.com/vmware-tanzu/octant/pkg/action"
"github.com/vmware-tanzu/octant/pkg/navigation"
"github.com/vmware-tanzu/octant/pkg/plugin"
"github.com/vmware-tanzu/octant/pkg/plugin/service"
"github.com/vmware-tanzu/octant/pkg/view/component"
"github.com/vmware-tanzu/octant/pkg/view/flexlayout"
"log"
"sync"
)
const pluginName = "actionPlugin"
const addIntAction = "actionPlugin/addInt"
const clearIntAction = "actionPlugin/clearInt"
// This is a sample plugin showing how to use actions with plugins.
func main() {
localPlugin := newActionPlugin()
// Remove the prefix from the go logger since Octant will print logs with timestamps.
log.SetPrefix("")
// Tell Octant to call this plugin when printing configuration or tabs for Pods
capabilities := &plugin.Capabilities{
ActionNames: []string{addIntAction, clearIntAction},
IsModule: true,
}
// Set up what should happen when Octant calls this plugin.
options := []service.PluginOption{
service.WithActionHandler(localPlugin.actionHandler),
service.WithNavigation(localPlugin.navHandler, localPlugin.initRoutes),
}
// Use the plugin service helper to register this plugin.
p, err := service.Register(pluginName, "a description", capabilities, options...)
if err != nil {
log.Fatal(err)
}
// The plugin can log and the log messages will show up in Octant.
log.Printf("octant-sample-plugin is starting")
p.Serve()
}
// actionPlugin is an example of a plugin that registers navigation and action handlers
// and performs some action against a remote API.
type actionPlugin struct {
store remoteAPI
}
func newActionPlugin() *actionPlugin {
return &actionPlugin{
store: remoteAPI{},
}
}
func (a *actionPlugin) navHandler(request *service.NavigationRequest) (navigation.Navigation, error) {
return navigation.Navigation{
Title: "Action Plugin",
Path: request.GeneratePath(),
IconName: "cloud",
}, nil
}
func (a *actionPlugin) routeHandler(request service.Request) (component.ContentResponse, error) {
card := component.NewCard(component.TitleFromString("Actions Example"))
layout := flexlayout.New()
var items []component.Component
for _, n := range a.store.list() {
items = append(items, component.NewText(fmt.Sprintf("%d", n)))
}
intList := component.NewList("Remote API Objects", items)
card.SetBody(intList)
form := component.Form{Fields:[]component.FormField{
component.NewFormFieldNumber("Number", "input", ""),
component.NewFormFieldHidden("action", addIntAction),
}}
addInt := component.Action{
Name: "Add Int",
Title: "Add Int to Remote API",
Form: form,
}
card.AddAction(addInt)
listSection := layout.AddSection()
err := listSection.Add(card, component.WidthFull)
if err != nil {
return component.ContentResponse{}, fmt.Errorf("error adding card to section: %w", err)
}
buttonGroup := component.NewButtonGroup()
clearButton := component.NewButton("Clear", action.Payload{"action": clearIntAction})
buttonGroup.AddButton(clearButton)
flexComponent := layout.ToComponent("Remote API Listing")
flexComponent.SetButtonGroup(buttonGroup)
contentResponse := component.NewContentResponse(component.TitleFromString("Action Example"))
contentResponse.Add(flexComponent)
return *contentResponse, nil
}
func (a *actionPlugin) actionHandler(request *service.ActionRequest) error {
actionName, err := request.Payload.String("action")
if err != nil {
return fmt.Errorf("unable to get input at string: %w", err)
}
switch actionName {
case addIntAction:
n, err := request.Payload.Uint16("input")
if err != nil {
return fmt.Errorf("unable to get input at int: %w", err)
}
a.store.add(n)
return nil
case clearIntAction:
a.store.clear()
return nil
default:
return fmt.Errorf("recieved action request for %s, but no handler defined", pluginName)
}
}
func (a *actionPlugin) initRoutes(router *service.Router) {
router.HandleFunc("*", a.routeHandler)
}
// remoteAPI is a fake remote API a plugin might call.
type remoteAPI struct {
datastore []uint16
mu sync.RWMutex
}
func (r *remoteAPI) add(n uint16) {
r.mu.Lock()
defer r.mu.Unlock()
r.datastore = append(r.datastore, n)
}
func (r *remoteAPI) list() []uint16 {
r.mu.RLock()
defer r.mu.RUnlock()
return r.datastore
}
func (r *remoteAPI) clear() {
r.mu.Lock()
defer r.mu.Unlock()
r.datastore = []uint16{}
}