-
Notifications
You must be signed in to change notification settings - Fork 9
/
handlers.go
168 lines (131 loc) · 4 KB
/
handlers.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
160
161
162
163
164
165
166
167
168
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package provisioning
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"namespacelabs.dev/foundation/framework/rpcerrors"
"namespacelabs.dev/foundation/framework/rpcerrors/multierr"
"namespacelabs.dev/foundation/internal/planning/tool/protocol"
"namespacelabs.dev/foundation/schema"
)
type Handlers struct {
handlers []*HandlerRoute
invokeHandler InvokeFunc
}
type MatchingHandlers struct {
hs *Handlers
matches
}
type HandlerRoute struct {
h StackHandler
matches
}
type matches struct {
matchEnv *schema.Environment
}
func NewHandlers() *Handlers {
return &Handlers{}
}
func (hs *Handlers) Any() *MatchingHandlers {
matches := matches{}
m := &MatchingHandlers{hs: hs, matches: matches}
return m
}
func (hs *Handlers) MatchEnv(env *schema.Environment) *MatchingHandlers {
matches := matches{matchEnv: env}
m := &MatchingHandlers{hs: hs, matches: matches}
return m
}
func (hs *Handlers) Handler() AllHandlers {
return handlersHandler{hs}
}
func (hs *Handlers) ServiceHandler() protocol.InvocationServiceServer {
return protocolHandler{Handlers: hs.Handler()}
}
func (mh *MatchingHandlers) HandleApply(apply func(context.Context, StackRequest, *ApplyOutput) error) *HandlerRoute {
return mh.HandleStack(staticStackHandler{apply, nil})
}
type staticStackHandler struct {
apply func(context.Context, StackRequest, *ApplyOutput) error
delete func(context.Context, StackRequest, *DeleteOutput) error
}
func (s staticStackHandler) Apply(ctx context.Context, req StackRequest, out *ApplyOutput) error {
if s.apply == nil {
return rpcerrors.Errorf(codes.Unimplemented, "not implemented")
}
return s.apply(ctx, req, out)
}
func (s staticStackHandler) Delete(ctx context.Context, req StackRequest, out *DeleteOutput) error {
if s.delete == nil {
return rpcerrors.Errorf(codes.Unimplemented, "not implemented")
}
return s.delete(ctx, req, out)
}
func (mh *MatchingHandlers) HandleStack(h StackHandler) *HandlerRoute {
r := &HandlerRoute{h: h}
r.matches = mh.matches
mh.hs.handlers = append(mh.hs.handlers, r)
return r
}
type InvokeFunc func(context.Context, Request) (*protocol.InvokeResponse, error)
func (mh *MatchingHandlers) HandleInvoke(f InvokeFunc) *MatchingHandlers {
mh.hs.invokeHandler = f
return mh
}
type handlersHandler struct {
Handlers *Handlers
}
func (m matches) match(env *schema.Environment) bool {
if m.matchEnv == nil {
return true
}
if m.matchEnv.Name != "" && env.GetName() != m.matchEnv.Name {
return false
}
if m.matchEnv.Purpose != schema.Environment_PURPOSE_UNKNOWN && env.GetPurpose() != m.matchEnv.Purpose {
return false
}
if m.matchEnv.Runtime != "" && env.GetRuntime() != m.matchEnv.Runtime {
return false
}
return true
}
func (rh handlersHandler) Apply(ctx context.Context, req StackRequest, out *ApplyOutput) error {
var errs []error
for _, r := range rh.Handlers.handlers {
if !r.matches.match(req.Env) {
continue
}
if err := r.h.Apply(ctx, req, out); err != nil {
errs = append(errs, err)
}
}
return multierr.New(errs...)
}
func (rh handlersHandler) Delete(ctx context.Context, req StackRequest, out *DeleteOutput) error {
var errs []error
for _, r := range rh.Handlers.handlers {
if !r.matches.match(req.Env) {
continue
}
if err := r.h.Delete(ctx, req, out); err != nil {
errs = append(errs, err)
}
}
return multierr.New(errs...)
}
func (rh handlersHandler) Invoke(ctx context.Context, req Request) (*protocol.InvokeResponse, error) {
if rh.Handlers.invokeHandler == nil {
return nil, status.Error(codes.Unavailable, "invoke not supported")
}
return rh.Handlers.invokeHandler(ctx, req)
}
type protocolHandler struct {
Handlers AllHandlers
}
func (i protocolHandler) Invoke(ctx context.Context, req *protocol.ToolRequest) (*protocol.ToolResponse, error) {
return handleRequest(ctx, req, i.Handlers)
}