/
destroyer_workspace.go
136 lines (116 loc) · 3.62 KB
/
destroyer_workspace.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
package plugin
import (
"context"
"github.com/hashicorp/go-argmapper"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"google.golang.org/grpc"
empty "google.golang.org/protobuf/types/known/emptypb"
"github.com/hashicorp/waypoint-plugin-sdk/component"
"github.com/hashicorp/waypoint-plugin-sdk/internal/funcspec"
"github.com/hashicorp/waypoint-plugin-sdk/internal/pluginargs"
pb "github.com/hashicorp/waypoint-plugin-sdk/proto/gen"
)
// workspaceDestroyerClient implements component.WorkspaceDestroyer for a service that
// has the destroy methods implemented.
type workspaceDestroyerClient struct {
Client workspaceDestroyerProtoClient
Logger hclog.Logger
Broker *plugin.GRPCBroker
Mappers []*argmapper.Func
}
func (c *workspaceDestroyerClient) Implements(ctx context.Context) (bool, error) {
if c == nil {
return false, nil
}
resp, err := c.Client.IsWorkspaceDestroyer(ctx, &empty.Empty{})
if err != nil {
return false, err
}
return resp.Implements, nil
}
func (c *workspaceDestroyerClient) DestroyWorkspaceFunc() interface{} {
impl, err := c.Implements(context.Background())
if err != nil {
return funcErr(err)
}
if !impl {
return nil
}
// Get the spec
spec, err := c.Client.DestroyWorkspaceSpec(context.Background(), &empty.Empty{})
if err != nil {
return funcErr(err)
}
return funcspec.Func(spec, c.destroy,
argmapper.Logger(c.Logger),
argmapper.Typed(&pluginargs.Internal{
Broker: c.Broker,
Mappers: c.Mappers,
Cleanup: &pluginargs.Cleanup{},
}),
)
}
func (c *workspaceDestroyerClient) destroy(
ctx context.Context,
args funcspec.Args,
internal *pluginargs.Internal,
) error {
// Run the cleanup
defer internal.Cleanup.Close()
// Call our function
_, err := c.Client.DestroyWorkspace(ctx, &pb.FuncSpec_Args{Args: args})
return err
}
// workspaceDestroyerServer implements the common WorkspaceDestroyer-related RPC calls.
// This should be embedded into the service implementation.
type workspaceDestroyerServer struct {
*base
Impl interface{}
}
func (s *workspaceDestroyerServer) IsWorkspaceDestroyer(
ctx context.Context,
empty *empty.Empty,
) (*pb.ImplementsResp, error) {
d, ok := s.Impl.(component.WorkspaceDestroyer)
return &pb.ImplementsResp{
Implements: ok && d.DestroyWorkspaceFunc() != nil,
}, nil
}
func (s *workspaceDestroyerServer) DestroyWorkspaceSpec(
ctx context.Context,
args *empty.Empty,
) (*pb.FuncSpec, error) {
return funcspec.Spec(s.Impl.(component.WorkspaceDestroyer).DestroyWorkspaceFunc(),
//argmapper.WithNoOutput(), // we only expect an error value so ignore the rest
argmapper.ConverterFunc(s.Mappers...),
argmapper.Logger(s.Logger),
argmapper.Typed(s.internal()),
)
}
func (s *workspaceDestroyerServer) DestroyWorkspace(
ctx context.Context,
args *pb.FuncSpec_Args,
) (*empty.Empty, error) {
internal := s.internal()
defer internal.Cleanup.Close()
_, err := callDynamicFunc2(s.Impl.(component.WorkspaceDestroyer).DestroyWorkspaceFunc(), args.Args,
argmapper.ConverterFunc(s.Mappers...),
argmapper.Typed(internal),
argmapper.Typed(ctx),
)
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
// workspaceDestroyerProtoClient is the interface we expect any gRPC service that
// supports destroy to implement.
type workspaceDestroyerProtoClient interface {
IsWorkspaceDestroyer(context.Context, *empty.Empty, ...grpc.CallOption) (*pb.ImplementsResp, error)
DestroyWorkspaceSpec(context.Context, *empty.Empty, ...grpc.CallOption) (*pb.FuncSpec, error)
DestroyWorkspace(context.Context, *pb.FuncSpec_Args, ...grpc.CallOption) (*empty.Empty, error)
}
var (
_ component.WorkspaceDestroyer = (*workspaceDestroyerClient)(nil)
)