-
Notifications
You must be signed in to change notification settings - Fork 25
/
preview.go
81 lines (73 loc) · 2.67 KB
/
preview.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
package agentfunctions
import (
"context"
cmdv1 "github.com/fluxninja/aperture/api/gen/proto/go/aperture/cmd/v1"
previewv1 "github.com/fluxninja/aperture/api/gen/proto/go/aperture/flowcontrol/preview/v1"
"github.com/fluxninja/aperture/pkg/config"
"github.com/fluxninja/aperture/pkg/policies/flowcontrol/service/preview"
previewconfig "github.com/fluxninja/aperture/pkg/policies/flowcontrol/service/preview/config"
"github.com/fluxninja/aperture/pkg/rpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// PreviewHandler is a handler for preview-family of functions.
//
// Simply forwards methods to grpc handler, which provides actual implementation.
type PreviewHandler struct {
handler *preview.Handler
isEnabled bool
}
// ProvidePreviewHandler provides PreviewHandler.
func ProvidePreviewHandler(
handler *preview.Handler,
unmarshaller config.Unmarshaller,
) (*PreviewHandler, error) {
var previewConfig previewconfig.FlowPreviewConfig
if err := unmarshaller.UnmarshalKey(previewconfig.Key, &previewConfig); err != nil {
return nil, err
}
return &PreviewHandler{
handler: handler,
isEnabled: previewConfig.Enabled,
}, nil
}
// PreviewFlowLabels previews flow labels on given control point.
func (h *PreviewHandler) PreviewFlowLabels(
ctx context.Context,
req *cmdv1.PreviewFlowLabelsRequest,
) (*previewv1.PreviewFlowLabelsResponse, error) {
if !h.isEnabled {
return nil, status.Error(codes.FailedPrecondition, "preview disabled")
}
// gRPC handlers assume non-nillness of argument.
if req.Request == nil {
return nil, status.Error(codes.InvalidArgument, "missing request")
}
return h.handler.PreviewFlowLabels(ctx, req.Request)
}
// PreviewHTTPRequests previews HTTP requests on given control point.
func (h *PreviewHandler) PreviewHTTPRequests(
ctx context.Context,
req *cmdv1.PreviewHTTPRequestsRequest,
) (*previewv1.PreviewHTTPRequestsResponse, error) {
if !h.isEnabled {
return nil, status.Error(codes.FailedPrecondition, "preview disabled")
}
// gRPC handlers assume non-nillness of argument.
if req.Request == nil {
return nil, status.Error(codes.InvalidArgument, "missing request")
}
return h.handler.PreviewHTTPRequests(ctx, req.Request)
}
// RegisterPreviewHandler registers PreviewHandler in handler registry.
func RegisterPreviewHandler(handler *PreviewHandler, registry *rpc.HandlerRegistry) error {
// Note: Registering also when handler is disabled, so that we can send
// more specific error code than Unimplemented.
if err := rpc.RegisterFunction(registry, handler.PreviewFlowLabels); err != nil {
return err
}
if err := rpc.RegisterFunction(registry, handler.PreviewHTTPRequests); err != nil {
return err
}
return nil
}