-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
converter_plugin.go
176 lines (150 loc) 路 5.38 KB
/
converter_plugin.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
169
170
171
172
173
174
175
176
// Copyright 2016-2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package plugin
import (
"context"
"fmt"
"os"
"github.com/blang/semver"
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
"github.com/hashicorp/hcl/v2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/rpcutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/rpcutil/rpcerror"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
)
// converter reflects a converter plugin, loaded dynamically from another process over gRPC.
type converter struct {
name string
plug *plugin // the actual plugin process wrapper.
clientRaw pulumirpc.ConverterClient // the raw provider client; usually unsafe to use directly.
}
func NewConverter(ctx *Context, name string, version *semver.Version) (Converter, error) {
prefix := fmt.Sprintf("%v (converter)", name)
// Load the plugin's path by using the standard workspace logic.
path, err := workspace.GetPluginPath(ctx.Diag, apitype.ConverterPlugin, name, version, ctx.Host.GetProjectPlugins())
if err != nil {
return nil, err
}
contract.Assertf(path != "", "unexpected empty path for plugin %s", name)
plug, err := newPlugin(ctx, ctx.Pwd, path, prefix,
apitype.ConverterPlugin, []string{}, os.Environ(), converterPluginDialOptions(ctx, name, ""))
if err != nil {
return nil, err
}
contract.Assertf(plug != nil, "unexpected nil converter plugin for %s", name)
c := &converter{
name: name,
plug: plug,
clientRaw: pulumirpc.NewConverterClient(plug.Conn),
}
return c, nil
}
func converterPluginDialOptions(ctx *Context, name string, path string) []grpc.DialOption {
dialOpts := append(
rpcutil.OpenTracingInterceptorDialOptions(otgrpc.SpanDecorator(decorateProviderSpans)),
grpc.WithTransportCredentials(insecure.NewCredentials()),
rpcutil.GrpcChannelOptions(),
)
if ctx.DialOptions != nil {
metadata := map[string]interface{}{
"mode": "client",
"kind": "converter",
}
if name != "" {
metadata["name"] = name
}
if path != "" {
metadata["path"] = path
}
dialOpts = append(dialOpts, ctx.DialOptions(metadata)...)
}
return dialOpts
}
// label returns a base label for tracing functions.
func (c *converter) label() string {
return fmt.Sprintf("Converter[%s, %p]", c.name, c)
}
func (c *converter) Close() error {
if c.plug == nil {
return nil
}
return c.plug.Close()
}
func (c *converter) ConvertState(ctx context.Context, req *ConvertStateRequest) (*ConvertStateResponse, error) {
label := c.label() + ".ConvertState"
logging.V(7).Infof("%s executing", label)
resp, err := c.clientRaw.ConvertState(ctx, &pulumirpc.ConvertStateRequest{
MapperTarget: req.MapperTarget,
Args: req.Args,
})
if err != nil {
rpcError := rpcerror.Convert(err)
logging.V(8).Infof("%s converter received rpc error `%s`: `%s`", label, rpcError.Code(), rpcError.Message())
return nil, err
}
resources := make([]ResourceImport, len(resp.Resources))
for i, resource := range resp.Resources {
resources[i] = ResourceImport{
Type: resource.Type,
Name: resource.Name,
ID: resource.Id,
Version: resource.Version,
PluginDownloadURL: resource.PluginDownloadURL,
LogicalName: resource.LogicalName,
IsRemote: resource.IsRemote,
IsComponent: resource.IsComponent,
}
}
// Translate the rpc diagnostics into hcl.Diagnostics.
var diags hcl.Diagnostics
for _, rpcDiag := range resp.Diagnostics {
diags = append(diags, RPCDiagnosticToHclDiagnostic(rpcDiag))
}
logging.V(7).Infof("%s success", label)
return &ConvertStateResponse{
Resources: resources,
Diagnostics: diags,
}, nil
}
func (c *converter) ConvertProgram(ctx context.Context, req *ConvertProgramRequest) (*ConvertProgramResponse, error) {
label := c.label() + ".ConvertProgram"
logging.V(7).Infof("%s executing", label)
resp, err := c.clientRaw.ConvertProgram(ctx, &pulumirpc.ConvertProgramRequest{
SourceDirectory: req.SourceDirectory,
TargetDirectory: req.TargetDirectory,
MapperTarget: req.MapperTarget,
LoaderTarget: req.LoaderTarget,
Args: req.Args,
})
if err != nil {
rpcError := rpcerror.Convert(err)
logging.V(8).Infof("%s converter received rpc error `%s`: `%s`", label, rpcError.Code(), rpcError.Message())
return nil, err
}
// Translate the rpc diagnostics into hcl.Diagnostics.
var diags hcl.Diagnostics
for _, rpcDiag := range resp.Diagnostics {
diags = append(diags, RPCDiagnosticToHclDiagnostic(rpcDiag))
}
logging.V(7).Infof("%s success", label)
return &ConvertProgramResponse{
Diagnostics: diags,
}, nil
}