-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (74 loc) · 2.23 KB
/
main.go
File metadata and controls
87 lines (74 loc) · 2.23 KB
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
package main
import (
"context"
"fmt"
"net"
"os"
"sync"
"github.com/eblackrps/viaduct/internal/connectors/plugin"
"github.com/eblackrps/viaduct/internal/models"
)
type examplePluginServer struct {
mu sync.RWMutex
source string
discoverable bool
}
func (s *examplePluginServer) Connect(ctx context.Context, request *plugin.ConnectRequest) (*plugin.ConnectResponse, error) {
s.mu.Lock()
defer s.mu.Unlock()
s.source = request.Config.Address
s.discoverable = true
return &plugin.ConnectResponse{OK: true}, nil
}
func (s *examplePluginServer) Discover(ctx context.Context, request *plugin.DiscoverRequest) (*plugin.DiscoverResponse, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if !s.discoverable {
return nil, fmt.Errorf("plugin not connected")
}
source := s.source
if source == "" {
source = "example-plugin"
}
return &plugin.DiscoverResponse{Result: &models.DiscoveryResult{
Source: source,
Platform: models.Platform("example"),
VMs: []models.VirtualMachine{
{ID: "example-1", Name: "example-vm", Platform: models.Platform("example"), PowerState: models.PowerOn},
},
}}, nil
}
func (s *examplePluginServer) Platform(ctx context.Context, request *plugin.PlatformRequest) (*plugin.PlatformResponse, error) {
return &plugin.PlatformResponse{Platform: "example"}, nil
}
func (s *examplePluginServer) Close(ctx context.Context, request *plugin.CloseRequest) (*plugin.CloseResponse, error) {
s.mu.Lock()
defer s.mu.Unlock()
s.discoverable = false
return &plugin.CloseResponse{OK: true}, nil
}
func (s *examplePluginServer) Health(ctx context.Context, request *plugin.HealthRequest) (*plugin.HealthResponse, error) {
return &plugin.HealthResponse{Status: "ok"}, nil
}
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
address := os.Getenv("VIADUCT_PLUGIN_ADDR")
if address == "" {
address = "127.0.0.1:50071"
}
listener, err := net.Listen("tcp", address)
if err != nil {
return fmt.Errorf("listen plugin server: %w", err)
}
server := plugin.NewGRPCServer()
plugin.RegisterConnectorPluginServer(server, &examplePluginServer{})
if err := server.Serve(listener); err != nil {
return fmt.Errorf("serve plugin server: %w", err)
}
return nil
}