-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_wrapper.go
168 lines (132 loc) · 5.05 KB
/
server_wrapper.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
package plugin
import (
"context"
"github.com/outblocks/outblocks-plugin-go/env"
apiv1 "github.com/outblocks/outblocks-plugin-go/gen/api/v1"
"github.com/outblocks/outblocks-plugin-go/log"
"github.com/outblocks/outblocks-plugin-go/registry"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type BasicPluginHandler interface {
Init(context.Context, env.Enver, log.Logger, apiv1.HostServiceClient) error
Start(context.Context, *apiv1.StartRequest) (*apiv1.StartResponse, error)
ProjectInit(context.Context, *apiv1.ProjectInitRequest) (*apiv1.ProjectInitResponse, error)
}
type DeployPluginHandler interface {
Plan(context.Context, *registry.Registry, *apiv1.PlanRequest) (*apiv1.PlanResponse, error)
Apply(*apiv1.ApplyRequest, *registry.Registry, apiv1.DeployPluginService_ApplyServer) error
}
type DNSPluginHandler interface {
PlanDNS(context.Context, *registry.Registry, *apiv1.PlanDNSRequest) (*apiv1.PlanDNSResponse, error)
ApplyDNS(*apiv1.ApplyDNSRequest, *registry.Registry, apiv1.DNSPluginService_ApplyDNSServer) error
}
type MonitoringPluginHandler interface {
PlanMonitoring(context.Context, *registry.Registry, *apiv1.PlanMonitoringRequest) (*apiv1.PlanMonitoringResponse, error)
ApplyMonitoring(*apiv1.ApplyMonitoringRequest, *registry.Registry, apiv1.MonitoringPluginService_ApplyMonitoringServer) error
}
type LogsPluginHandler interface {
apiv1.LogsPluginServiceServer
}
type CommandPluginHandler interface {
apiv1.CommandPluginServiceServer
}
type RunPluginHandler interface {
apiv1.RunPluginServiceServer
}
type StatePluginHandler interface {
apiv1.StatePluginServiceServer
}
type LockingPluginHandler interface {
apiv1.LockingPluginServiceServer
}
type DeployHookHandler interface {
apiv1.DeployHookServiceServer
}
type SecretPluginHandler interface {
apiv1.SecretPluginServiceServer
}
type Cleanup interface {
Cleanup() error
}
type basicPluginHandlerWrapper struct {
env env.Enver
conn *grpc.ClientConn
BasicPluginHandler
}
func (s *basicPluginHandlerWrapper) Init(ctx context.Context, req *apiv1.InitRequest) (*apiv1.InitResponse, error) {
conn, err := grpc.DialContext(ctx, req.HostAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, err
}
s.conn = conn
cli := apiv1.NewHostServiceClient(conn)
l := log.NewLogger(cli)
return &apiv1.InitResponse{}, s.BasicPluginHandler.Init(ctx, s.env, l, cli)
}
type deployPluginHandlerWrapper struct {
DeployPluginHandler
RegistryOptions RegistryOptions
}
func createRegistry(opts *registry.Options, apps []*apiv1.AppPlan) *registry.Registry {
reg := registry.NewRegistry(opts)
for _, plan := range apps {
if plan.Skip {
reg.SkipAppResources(plan.State.App)
}
}
return reg
}
func (s *deployPluginHandlerWrapper) createRegistry(apps []*apiv1.AppPlan, destroy, read bool) *registry.Registry {
return createRegistry(®istry.Options{
Read: read,
Destroy: destroy,
AllowDuplicates: s.RegistryOptions.AllowDuplicates,
}, apps)
}
func (s *deployPluginHandlerWrapper) Plan(ctx context.Context, r *apiv1.PlanRequest) (*apiv1.PlanResponse, error) {
reg := s.createRegistry(r.Apps, r.Destroy, r.Verify)
return s.DeployPluginHandler.Plan(ctx, reg, r)
}
func (s *deployPluginHandlerWrapper) Apply(r *apiv1.ApplyRequest, stream apiv1.DeployPluginService_ApplyServer) error {
reg := s.createRegistry(r.Apps, r.Destroy, false)
return s.DeployPluginHandler.Apply(r, reg, stream)
}
type dnsPluginHandlerWrapper struct {
DNSPluginHandler
RegistryOptions RegistryOptions
}
func (s *dnsPluginHandlerWrapper) createRegistry(read, destroy bool) *registry.Registry {
return createRegistry(®istry.Options{
Read: read,
Destroy: destroy,
AllowDuplicates: s.RegistryOptions.AllowDuplicates,
}, nil)
}
func (s *dnsPluginHandlerWrapper) PlanDNS(ctx context.Context, r *apiv1.PlanDNSRequest) (*apiv1.PlanDNSResponse, error) {
reg := s.createRegistry(r.Verify, r.Destroy)
return s.DNSPluginHandler.PlanDNS(ctx, reg, r)
}
func (s *dnsPluginHandlerWrapper) ApplyDNS(r *apiv1.ApplyDNSRequest, stream apiv1.DNSPluginService_ApplyDNSServer) error {
reg := s.createRegistry(false, r.Destroy)
return s.DNSPluginHandler.ApplyDNS(r, reg, stream)
}
type monitoringPluginHandlerWrapper struct {
MonitoringPluginHandler
RegistryOptions RegistryOptions
}
func (s *monitoringPluginHandlerWrapper) createRegistry(read, destroy bool) *registry.Registry {
return createRegistry(®istry.Options{
Read: read,
Destroy: destroy,
AllowDuplicates: s.RegistryOptions.AllowDuplicates,
}, nil)
}
func (s *monitoringPluginHandlerWrapper) PlanMonitoring(ctx context.Context, r *apiv1.PlanMonitoringRequest) (*apiv1.PlanMonitoringResponse, error) {
reg := s.createRegistry(r.Verify, r.Destroy)
return s.MonitoringPluginHandler.PlanMonitoring(ctx, reg, r)
}
func (s *monitoringPluginHandlerWrapper) ApplyMonitoring(r *apiv1.ApplyMonitoringRequest, stream apiv1.MonitoringPluginService_ApplyMonitoringServer) error {
reg := s.createRegistry(false, r.Destroy)
return s.MonitoringPluginHandler.ApplyMonitoring(r, reg, stream)
}