-
Notifications
You must be signed in to change notification settings - Fork 460
/
pluginimpl.go
69 lines (61 loc) · 1.86 KB
/
pluginimpl.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
package test
import (
"context"
"fmt"
"github.com/hashicorp/go-hclog"
"github.com/spiffe/spire/pkg/common/catalog"
"github.com/spiffe/spire/proto/private/test/catalogtest"
spi "github.com/spiffe/spire/proto/spire/common/plugin"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func NewPlugin() catalogtest.PluginPlugin {
return &testPlugin{}
}
type testPlugin struct {
log hclog.Logger
hs catalogtest.HostService
}
func (s *testPlugin) SetLogger(log hclog.Logger) {
s.log = log.ResetNamed("pluginimpl")
}
func (s *testPlugin) BrokerHostServices(broker catalog.HostServiceBroker) error {
has, err := broker.GetHostService(catalogtest.HostServiceHostServiceClient(&s.hs))
if err != nil {
return err
}
if !has && s.log != nil {
// s.log will only be nil if this is not used within the new plugin framework (i.e. old plugin test)
s.log.Warn("Host service not available.", "hostservice", "HostService")
}
return nil
}
func (s *testPlugin) Configure(ctx context.Context, req *spi.ConfigureRequest) (*spi.ConfigureResponse, error) {
var trustDomain string
if req.GlobalConfig != nil {
trustDomain = req.GlobalConfig.TrustDomain
}
if s.log != nil {
// s.log will only be nil if this is not used within the new plugin framework (i.e. old plugin test)
s.log.Info("Configure called.", "trustdomain", trustDomain, "config", req.Configuration)
}
if req.Configuration == "BAD" {
return nil, status.Error(codes.InvalidArgument, "BAD configuration")
}
return &spi.ConfigureResponse{}, nil
}
func (s *testPlugin) CallPlugin(ctx context.Context, req *catalogtest.Request) (*catalogtest.Response, error) {
out := req.In
if s.hs != nil {
resp, err := s.hs.CallHostService(ctx, &catalogtest.Request{
In: req.In,
})
if err != nil {
return nil, err
}
out = resp.Out
}
return &catalogtest.Response{
Out: fmt.Sprintf("plugin(%s)", out),
}, nil
}