forked from pulumi/pulumi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resourcemonitor.go
400 lines (354 loc) 路 11.5 KB
/
resourcemonitor.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright 2016-2018, 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 deploytest
import (
"context"
"fmt"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/rpcutil"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
"google.golang.org/grpc"
)
type ResourceMonitor struct {
conn *grpc.ClientConn
resmon pulumirpc.ResourceMonitorClient
supportsSecrets bool
supportsResourceReferences bool
}
func dialMonitor(ctx context.Context, endpoint string) (*ResourceMonitor, error) {
// Connect to the resource monitor and create an appropriate client.
conn, err := grpc.Dial(
endpoint,
grpc.WithInsecure(),
rpcutil.GrpcChannelOptions(),
)
if err != nil {
return nil, fmt.Errorf("could not connect to resource monitor: %w", err)
}
resmon := pulumirpc.NewResourceMonitorClient(conn)
// Check feature support.
supportsSecrets, err := supportsFeature(ctx, resmon, "secrets")
if err != nil {
contract.IgnoreError(conn.Close())
return nil, err
}
supportsResourceReferences, err := supportsFeature(ctx, resmon, "resourceReferences")
if err != nil {
contract.IgnoreError(conn.Close())
return nil, err
}
// Fire up a resource monitor client and return.
return &ResourceMonitor{
conn: conn,
resmon: resmon,
supportsSecrets: supportsSecrets,
supportsResourceReferences: supportsResourceReferences,
}, nil
}
func supportsFeature(ctx context.Context, resmon pulumirpc.ResourceMonitorClient, id string) (bool, error) {
resp, err := resmon.SupportsFeature(ctx, &pulumirpc.SupportsFeatureRequest{Id: id})
if err != nil {
return false, err
}
return resp.GetHasSupport(), nil
}
func (rm *ResourceMonitor) Close() error {
return rm.conn.Close()
}
func NewResourceMonitor(resmon pulumirpc.ResourceMonitorClient) *ResourceMonitor {
return &ResourceMonitor{resmon: resmon}
}
type ResourceOptions struct {
Parent resource.URN
Protect bool
Dependencies []resource.URN
Provider string
Inputs resource.PropertyMap
PropertyDeps map[resource.PropertyKey][]resource.URN
DeleteBeforeReplace *bool
Version string
PluginDownloadURL string
IgnoreChanges []string
ReplaceOnChanges []string
AliasURNs []resource.URN
Aliases []resource.Alias
ImportID resource.ID
CustomTimeouts *resource.CustomTimeouts
RetainOnDelete bool
DeletedWith resource.URN
SupportsPartialValues *bool
Remote bool
Providers map[string]string
AdditionalSecretOutputs []resource.PropertyKey
DisableSecrets bool
DisableResourceReferences bool
}
func (rm *ResourceMonitor) RegisterResource(t tokens.Type, name string, custom bool,
options ...ResourceOptions) (resource.URN, resource.ID, resource.PropertyMap, error) {
var opts ResourceOptions
if len(options) > 0 {
opts = options[0]
}
if opts.Inputs == nil {
opts.Inputs = resource.PropertyMap{}
}
// marshal inputs
ins, err := plugin.MarshalProperties(opts.Inputs, plugin.MarshalOptions{
KeepUnknowns: true,
KeepSecrets: rm.supportsSecrets,
KeepResources: rm.supportsResourceReferences,
})
if err != nil {
return "", "", nil, err
}
// marshal dependencies
deps := []string{}
for _, d := range opts.Dependencies {
deps = append(deps, string(d))
}
// marshal aliases
aliasStrings := []string{}
for _, a := range opts.AliasURNs {
aliasStrings = append(aliasStrings, string(a))
}
aliasObjects := []*pulumirpc.Alias{}
for _, a := range opts.Aliases {
var obj *pulumirpc.Alias
if a.URN == "" {
alias := &pulumirpc.Alias_Spec{
Name: a.Name,
Type: a.Type,
Project: a.Project,
Stack: a.Stack,
}
if a.NoParent() {
alias.Parent = &pulumirpc.Alias_Spec_NoParent{NoParent: a.NoParent()}
} else if a.Parent != "" {
alias.Parent = &pulumirpc.Alias_Spec_ParentUrn{ParentUrn: string(a.Parent)}
}
obj = &pulumirpc.Alias{Alias: &pulumirpc.Alias_Spec_{Spec: alias}}
} else {
obj = &pulumirpc.Alias{Alias: &pulumirpc.Alias_Urn{Urn: string(a.URN)}}
}
aliasObjects = append(aliasObjects, obj)
}
inputDeps := make(map[string]*pulumirpc.RegisterResourceRequest_PropertyDependencies)
for pk, pd := range opts.PropertyDeps {
pdeps := []string{}
for _, d := range pd {
pdeps = append(pdeps, string(d))
}
inputDeps[string(pk)] = &pulumirpc.RegisterResourceRequest_PropertyDependencies{
Urns: pdeps,
}
}
var timeouts pulumirpc.RegisterResourceRequest_CustomTimeouts
if opts.CustomTimeouts != nil {
timeouts.Create = prepareTestTimeout(opts.CustomTimeouts.Create)
timeouts.Update = prepareTestTimeout(opts.CustomTimeouts.Update)
timeouts.Delete = prepareTestTimeout(opts.CustomTimeouts.Delete)
}
deleteBeforeReplace := false
if opts.DeleteBeforeReplace != nil {
deleteBeforeReplace = *opts.DeleteBeforeReplace
}
supportsPartialValues := true
if opts.SupportsPartialValues != nil {
supportsPartialValues = *opts.SupportsPartialValues
}
additionalSecretOutputs := make([]string, len(opts.AdditionalSecretOutputs))
for i, v := range opts.AdditionalSecretOutputs {
additionalSecretOutputs[i] = string(v)
}
requestInput := &pulumirpc.RegisterResourceRequest{
Type: string(t),
Name: name,
Custom: custom,
Parent: string(opts.Parent),
Protect: opts.Protect,
Dependencies: deps,
Provider: opts.Provider,
Object: ins,
PropertyDependencies: inputDeps,
DeleteBeforeReplace: deleteBeforeReplace,
DeleteBeforeReplaceDefined: opts.DeleteBeforeReplace != nil,
IgnoreChanges: opts.IgnoreChanges,
AcceptSecrets: !opts.DisableSecrets,
AcceptResources: !opts.DisableResourceReferences,
Version: opts.Version,
AliasURNs: aliasStrings,
ImportId: string(opts.ImportID),
CustomTimeouts: &timeouts,
SupportsPartialValues: supportsPartialValues,
Remote: opts.Remote,
ReplaceOnChanges: opts.ReplaceOnChanges,
Providers: opts.Providers,
PluginDownloadURL: opts.PluginDownloadURL,
RetainOnDelete: opts.RetainOnDelete,
AdditionalSecretOutputs: additionalSecretOutputs,
Aliases: aliasObjects,
DeletedWith: string(opts.DeletedWith),
}
// submit request
resp, err := rm.resmon.RegisterResource(context.Background(), requestInput)
if err != nil {
return "", "", nil, err
}
// unmarshal outputs
//
// Note that `KeepSecrets` and `KeepResources` are set to `true` so the caller can detect secrets and resource refs
// that are erroneously returned (e.g. secrets/resource refs that are returned even though the caller has not set
// `AcceptSecrets` or `AcceptResources` to `true` above).
outs, err := plugin.UnmarshalProperties(resp.Object, plugin.MarshalOptions{
KeepUnknowns: true,
KeepSecrets: true,
KeepResources: true,
})
if err != nil {
return "", "", nil, err
}
return resource.URN(resp.Urn), resource.ID(resp.Id), outs, nil
}
func (rm *ResourceMonitor) RegisterResourceOutputs(urn resource.URN, outputs resource.PropertyMap) error {
// marshal outputs
outs, err := plugin.MarshalProperties(outputs, plugin.MarshalOptions{
KeepUnknowns: true,
})
if err != nil {
return err
}
// submit request
_, err = rm.resmon.RegisterResourceOutputs(context.Background(), &pulumirpc.RegisterResourceOutputsRequest{
Urn: string(urn),
Outputs: outs,
})
return err
}
func (rm *ResourceMonitor) ReadResource(t tokens.Type, name string, id resource.ID, parent resource.URN,
inputs resource.PropertyMap, provider string, version string) (resource.URN, resource.PropertyMap, error) {
// marshal inputs
ins, err := plugin.MarshalProperties(inputs, plugin.MarshalOptions{
KeepUnknowns: true,
KeepResources: true,
})
if err != nil {
return "", nil, err
}
// submit request
resp, err := rm.resmon.ReadResource(context.Background(), &pulumirpc.ReadResourceRequest{
Type: string(t),
Name: name,
Id: string(id),
Parent: string(parent),
Provider: provider,
Properties: ins,
Version: version,
})
if err != nil {
return "", nil, err
}
// unmarshal outputs
outs, err := plugin.UnmarshalProperties(resp.Properties, plugin.MarshalOptions{
KeepUnknowns: true,
KeepResources: true,
})
if err != nil {
return "", nil, err
}
return resource.URN(resp.Urn), outs, nil
}
func (rm *ResourceMonitor) Invoke(tok tokens.ModuleMember, inputs resource.PropertyMap,
provider string, version string) (resource.PropertyMap, []*pulumirpc.CheckFailure, error) {
// marshal inputs
ins, err := plugin.MarshalProperties(inputs, plugin.MarshalOptions{
KeepUnknowns: true,
KeepResources: true,
})
if err != nil {
return nil, nil, err
}
// submit request
resp, err := rm.resmon.Invoke(context.Background(), &pulumirpc.ResourceInvokeRequest{
Tok: string(tok),
Provider: provider,
Args: ins,
Version: version,
})
if err != nil {
return nil, nil, err
}
// handle failures
if len(resp.Failures) != 0 {
return nil, resp.Failures, nil
}
// unmarshal outputs
outs, err := plugin.UnmarshalProperties(resp.Return, plugin.MarshalOptions{
KeepUnknowns: true,
KeepResources: true,
})
if err != nil {
return nil, nil, err
}
return outs, nil, nil
}
func (rm *ResourceMonitor) Call(tok tokens.ModuleMember, inputs resource.PropertyMap,
provider string, version string) (resource.PropertyMap, map[resource.PropertyKey][]resource.URN,
[]*pulumirpc.CheckFailure, error) {
// marshal inputs
ins, err := plugin.MarshalProperties(inputs, plugin.MarshalOptions{
KeepUnknowns: true,
KeepResources: true,
})
if err != nil {
return nil, nil, nil, err
}
// submit request
resp, err := rm.resmon.Call(context.Background(), &pulumirpc.CallRequest{
Tok: string(tok),
Provider: provider,
Args: ins,
Version: version,
})
if err != nil {
return nil, nil, nil, err
}
// handle failures
if len(resp.Failures) != 0 {
return nil, nil, resp.Failures, nil
}
// unmarshal outputs
outs, err := plugin.UnmarshalProperties(resp.Return, plugin.MarshalOptions{
KeepUnknowns: true,
KeepResources: true,
})
if err != nil {
return nil, nil, nil, err
}
// unmarshal return deps
deps := make(map[resource.PropertyKey][]resource.URN)
for _, p := range resp.ReturnDependencies {
var urns []resource.URN
for _, urn := range p.Urns {
urns = append(urns, resource.URN(urn))
}
}
return outs, deps, nil, nil
}
func prepareTestTimeout(timeout float64) string {
mins := int(timeout) / 60
return fmt.Sprintf("%dm", mins)
}