/
types.go
568 lines (477 loc) 路 17.7 KB
/
types.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Copyright 2016-2022, 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.
//nolint:lll, interfacer
package pulumi
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"github.com/pulumi/pulumi/sdk/v3/go/internal"
"github.com/pulumi/pulumi/sdk/v3/go/pulumix"
)
// Output helps encode the relationship between resources in a Pulumi application. Specifically an output property
// holds onto a value and the resource it came from. An output value can then be provided when constructing new
// resources, allowing that new resource to know both the value as well as the resource the value came from. This
// allows for a precise "dependency graph" to be created, which properly tracks the relationship between resources.
type Output = internal.Output
var (
outputType = reflect.TypeOf((*Output)(nil)).Elem()
inputType = reflect.TypeOf((*Input)(nil)).Elem()
)
// RegisterOutputType registers an Output type with the Pulumi runtime. If a value of this type's concrete type is
// returned by an Apply, the Apply will return the specific Output type.
func RegisterOutputType(output Output) {
internal.RegisterOutputType(output)
}
// RegisterInputType registers an Input type with the Pulumi runtime. This allows the input type to be instantiated
// for a given input interface.
func RegisterInputType(interfaceType reflect.Type, input Input) {
internal.RegisterInputType(interfaceType, input)
}
// OutputState holds the internal details of an Output and implements the Apply and ApplyWithContext methods.
type OutputState = internal.OutputState
func newAnyOutput(wg *workGroup) (Output, func(interface{}), func(error)) {
out := internal.NewOutputState(wg, anyType)
resolve := func(v interface{}) {
internal.ResolveOutput(out, v, true, false, nil)
}
reject := func(err error) {
internal.RejectOutput(out, err)
}
return AnyOutput{out}, resolve, reject
}
// NewOutput returns an output value that can be used to rendezvous with the production of a value or error. The
// function returns the output itself, plus two functions: one for resolving a value, and another for rejecting with an
// error; exactly one function must be called. This acts like a promise.
//
// Deprecated: use Context.NewOutput instead.
func NewOutput() (Output, func(interface{}), func(error)) {
return newAnyOutput(nil)
}
var anyOutputType = reflect.TypeOf((*AnyOutput)(nil)).Elem()
// IsSecret returns a bool representing the secretness of the Output
//
// IsSecret may return an inaccurate results if the Output is unknowable (during a
// preview) or contains an error.
func IsSecret(o Output) bool {
return internal.IsSecret(o)
}
// Unsecret will unwrap a secret output as a new output with a resolved value and no secretness
func Unsecret(input Output) Output {
return internal.Unsecret(input)
}
// UnsecretWithContext will unwrap a secret output as a new output with a resolved value and no secretness
func UnsecretWithContext(ctx context.Context, input Output) Output {
return internal.UnsecretWithContext(ctx, input)
}
// ToSecret wraps the input in an Output marked as secret
// that will resolve when all Inputs contained in the given value have resolved.
func ToSecret(input interface{}) Output {
return internal.ToSecret(input)
}
// UnsafeUnknownOutput Creates an unknown output. This is a low level API and should not be used in programs as this
// will cause "pulumi up" to fail if called and used during a non-dryrun deployment.
func UnsafeUnknownOutput(deps []Resource) Output {
output, _, _ := newAnyOutput(nil)
internal.ResolveOutput(output, nil, false, false, resourcesToInternal(deps))
return output
}
// ToSecretWithContext wraps the input in an Output marked as secret
// that will resolve when all Inputs contained in the given value have resolved.
func ToSecretWithContext(ctx context.Context, input interface{}) Output {
return internal.ToSecretWithContext(ctx, input)
}
// All returns an ArrayOutput that will resolve when all of the provided inputs will resolve. Each element of the
// array will contain the resolved value of the corresponding output. The output will be rejected if any of the inputs
// is rejected.
func All(inputs ...interface{}) ArrayOutput {
return AllWithContext(context.Background(), inputs...)
}
// AllWithContext returns an ArrayOutput that will resolve when all of the provided inputs will resolve. Each
// element of the array will contain the resolved value of the corresponding output. The output will be rejected if any
// of the inputs is rejected.
func AllWithContext(ctx context.Context, inputs ...interface{}) ArrayOutput {
return ToOutputWithContext(ctx, inputs).(ArrayOutput)
}
// JSONMarshal uses "encoding/json".Marshal to serialize the given Output value into a JSON string.
func JSONMarshal(v interface{}) StringOutput {
return JSONMarshalWithContext(context.Background(), v)
}
// JSONMarshalWithContext uses "encoding/json".Marshal to serialize the given Output value into a JSON string.
func JSONMarshalWithContext(ctx context.Context, v interface{}) StringOutput {
o := ToOutputWithContext(ctx, v)
return o.ApplyTWithContext(ctx, func(_ context.Context, v interface{}) (string, error) {
json, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(json), nil
}).(StringOutput)
}
// JSONUnmarshal uses "encoding/json".Unmarshal to deserialize the given Input JSON string into a value.
func JSONUnmarshal(data StringInput) AnyOutput {
return JSONUnmarshalWithContext(context.Background(), data)
}
// JSONUnmarshalWithContext uses "encoding/json".Unmarshal to deserialize the given Input JSON string into a value.
func JSONUnmarshalWithContext(ctx context.Context, data StringInput) AnyOutput {
o := ToOutputWithContext(ctx, data)
return o.ApplyTWithContext(ctx, func(_ context.Context, data string) (interface{}, error) {
var v interface{}
err := json.Unmarshal([]byte(data), &v)
if err != nil {
return nil, err
}
return v, nil
}).(AnyOutput)
}
// ToOutput returns an Output that will resolve when all Inputs contained in the given value have resolved.
func ToOutput(v interface{}) Output {
return internal.ToOutput(v)
}
// ToOutputWithContext returns an Output that will resolve when all Outputs contained in the given value have
// resolved.
func ToOutputWithContext(ctx context.Context, v interface{}) Output {
return internal.ToOutputWithContext(ctx, v)
}
func init() {
internal.AnyOutputType = anyOutputType
}
// Input is the type of a generic input value for a Pulumi resource. This type is used in conjunction with Output
// to provide polymorphism over strongly-typed input values.
//
// The intended pattern for nested Pulumi value types is to define an input interface and a plain, input, and output
// variant of the value type that implement the input interface.
//
// For example, given a nested Pulumi value type with the following shape:
//
// type Nested struct {
// Foo int
// Bar string
// }
//
// We would define the following:
//
// var nestedType = reflect.TypeOf((*Nested)(nil)).Elem()
//
// type NestedInput interface {
// pulumi.Input
//
// ToNestedOutput() NestedOutput
// ToNestedOutputWithContext(context.Context) NestedOutput
// }
//
// type Nested struct {
// Foo int `pulumi:"foo"`
// Bar string `pulumi:"bar"`
// }
//
// type NestedInputValue struct {
// Foo pulumi.IntInput `pulumi:"foo"`
// Bar pulumi.StringInput `pulumi:"bar"`
// }
//
// func (NestedInputValue) ElementType() reflect.Type {
// return nestedType
// }
//
// func (v NestedInputValue) ToNestedOutput() NestedOutput {
// return pulumi.ToOutput(v).(NestedOutput)
// }
//
// func (v NestedInputValue) ToNestedOutputWithContext(ctx context.Context) NestedOutput {
// return pulumi.ToOutputWithContext(ctx, v).(NestedOutput)
// }
//
// type NestedOutput struct { *pulumi.OutputState }
//
// func (NestedOutput) ElementType() reflect.Type {
// return nestedType
// }
//
// func (o NestedOutput) ToNestedOutput() NestedOutput {
// return o
// }
//
// func (o NestedOutput) ToNestedOutputWithContext(ctx context.Context) NestedOutput {
// return o
// }
type Input = internal.Input
var anyType = reflect.TypeOf((*interface{})(nil)).Elem()
func Any(v interface{}) AnyOutput {
return AnyWithContext(context.Background(), v)
}
func AnyWithContext(ctx context.Context, v interface{}) AnyOutput {
return internal.ToOutputWithOutputType(ctx, anyOutputType, v).(AnyOutput)
}
type AnyOutput struct{ *OutputState }
var _ pulumix.Input[any] = AnyOutput{}
func (AnyOutput) MarshalJSON() ([]byte, error) {
return nil, errors.New("outputs can not be marshaled to JSON")
}
func (AnyOutput) ElementType() reflect.Type {
return anyType
}
func (o AnyOutput) ToOutput(context.Context) pulumix.Output[any] {
return pulumix.Output[any]{
OutputState: o.OutputState,
}
}
func (in ID) ToStringPtrOutput() StringPtrOutput {
return in.ToStringPtrOutputWithContext(context.Background())
}
func (in ID) ToStringPtrOutputWithContext(ctx context.Context) StringPtrOutput {
return in.ToStringOutputWithContext(ctx).ToStringPtrOutputWithContext(ctx)
}
func (o IDOutput) ToStringPtrOutput() StringPtrOutput {
return o.ToStringPtrOutputWithContext(context.Background())
}
func (o IDOutput) ToStringPtrOutputWithContext(ctx context.Context) StringPtrOutput {
return o.ToStringOutputWithContext(ctx).ToStringPtrOutputWithContext(ctx)
}
func (o IDOutput) awaitID(ctx context.Context) (ID, bool, bool, error) {
id, known, secret, _, err := internal.AwaitOutput(ctx, o)
if !known || err != nil {
return "", known, false, err
}
return ID(convert(id, stringType).(string)), true, secret, nil
}
func (in URN) ToStringPtrOutput() StringPtrOutput {
return in.ToStringPtrOutputWithContext(context.Background())
}
func (in URN) ToStringPtrOutputWithContext(ctx context.Context) StringPtrOutput {
return in.ToStringOutputWithContext(ctx).ToStringPtrOutputWithContext(ctx)
}
func (o URNOutput) ToStringPtrOutput() StringPtrOutput {
return o.ToStringPtrOutputWithContext(context.Background())
}
func (o URNOutput) ToStringPtrOutputWithContext(ctx context.Context) StringPtrOutput {
return o.ToStringOutputWithContext(ctx).ToStringPtrOutputWithContext(ctx)
}
func (o URNOutput) awaitURN(ctx context.Context) (URN, bool, bool, error) {
id, known, secret, _, err := internal.AwaitOutput(ctx, o)
if !known || err != nil {
return "", known, secret, err
}
return URN(convert(id, stringType).(string)), true, secret, nil
}
func convert(v interface{}, to reflect.Type) interface{} {
rv := reflect.ValueOf(v)
if !rv.Type().ConvertibleTo(to) {
panic(fmt.Errorf("cannot convert output value of type %s to %s", rv.Type(), to))
}
return rv.Convert(to).Interface()
}
// ResourceOutput is an Output that returns Resource values.
// TODO: ResourceOutput and the init() should probably be code generated.
type ResourceOutput struct{ *OutputState }
var _ pulumix.Input[Resource] = ResourceOutput{}
func (ResourceOutput) MarshalJSON() ([]byte, error) {
return nil, errors.New("Outputs can not be marshaled to JSON")
}
// ElementType returns the element type of this Output (Resource).
func (ResourceOutput) ElementType() reflect.Type {
return reflect.TypeOf((*Resource)(nil)).Elem()
}
func (o ResourceOutput) ToOutput(context.Context) pulumix.Output[Resource] {
return pulumix.Output[Resource]{
OutputState: o.OutputState,
}
}
func (o ResourceOutput) ToResourceOutput() ResourceOutput {
return o
}
func (o ResourceOutput) ToResourceOutputWithContext(ctx context.Context) ResourceOutput {
return o
}
// ResourceInput is an Input type carrying Resource values.
//
// Unfortunately `Resource` values do not implement `ResourceInput` in
// the current version. Use `NewResourceInput` instead.
type ResourceInput interface {
Input
ToResourceOutput() ResourceOutput
ToResourceOutputWithContext(context.Context) ResourceOutput
}
func NewResourceInput(resource Resource) ResourceInput {
return NewResourceOutput(resource)
}
func NewResourceOutput(resource Resource) ResourceOutput {
return Int(0).ToIntOutput().ApplyT(func(int) Resource { return resource }).(ResourceOutput)
}
var _ ResourceInput = &ResourceOutput{}
var resourceArrayType = reflect.TypeOf((*[]Resource)(nil)).Elem()
// ResourceArrayInput is an input type that accepts ResourceArray and ResourceArrayOutput values.
type ResourceArrayInput interface {
Input
ToResourceArrayOutput() ResourceArrayOutput
ToResourceArrayOutputWithContext(ctx context.Context) ResourceArrayOutput
}
// ResourceArray is an input type for []ResourceInput values.
type ResourceArray []ResourceInput
var _ pulumix.Input[[]Resource] = ResourceArray{}
// ElementType returns the element type of this Input ([]Resource).
func (ResourceArray) ElementType() reflect.Type {
return resourceArrayType
}
func (in ResourceArray) ToOutput(ctx context.Context) pulumix.Output[[]Resource] {
return pulumix.Output[[]Resource]{
OutputState: internal.GetOutputState(ToOutputWithContext(ctx, in)),
}
}
func (in ResourceArray) ToResourceArrayOutput() ResourceArrayOutput {
return ToOutput(in).(ResourceArrayOutput)
}
func (in ResourceArray) ToResourceArrayOutputWithContext(ctx context.Context) ResourceArrayOutput {
return ToOutputWithContext(ctx, in).(ResourceArrayOutput)
}
// ResourceArrayOutput is an Output that returns []Resource values.
type ResourceArrayOutput struct{ *OutputState }
var _ pulumix.Input[[]Resource] = ResourceArrayOutput{}
func (ResourceArrayOutput) MarshalJSON() ([]byte, error) {
return nil, errors.New("Outputs can not be marshaled to JSON")
}
// ElementType returns the element type of this Output ([]Resource).
func (ResourceArrayOutput) ElementType() reflect.Type {
return resourceArrayType
}
func (o ResourceArrayOutput) ToOutput(context.Context) pulumix.Output[[]Resource] {
return pulumix.Output[[]Resource]{
OutputState: o.OutputState,
}
}
func (o ResourceArrayOutput) ToResourceArrayOutput() ResourceArrayOutput {
return o
}
func (o ResourceArrayOutput) ToResourceArrayOutputWithContext(ctx context.Context) ResourceArrayOutput {
return o
}
// Index looks up the i'th element of the array if it is in bounds or returns the zero value of the appropriate
// type if the index is out of bounds.
func (o ResourceArrayOutput) Index(i IntInput) ResourceOutput {
return All(o, i).ApplyT(func(vs []interface{}) Resource {
arr := vs[0].([]Resource)
idx := vs[1].(int)
var ret Resource
if idx >= 0 && idx < len(arr) {
ret = arr[idx]
}
return ret
}).(ResourceOutput)
}
func ToResourceArray(in []Resource) ResourceArray {
return NewResourceArray(in...)
}
func NewResourceArray(in ...Resource) ResourceArray {
a := make(ResourceArray, len(in))
for i, v := range in {
a[i] = NewResourceInput(v)
}
return a
}
func ToResourceArrayOutput(in []ResourceOutput) ResourceArrayOutput {
return NewResourceArrayOutput(in...)
}
func NewResourceArrayOutput(in ...ResourceOutput) ResourceArrayOutput {
a := make(ResourceArray, len(in))
for i, v := range in {
a[i] = v
}
return a.ToResourceArrayOutput()
}
func init() {
RegisterInputType(reflect.TypeOf((*ResourceArrayInput)(nil)).Elem(), ResourceArray{})
RegisterOutputType(ResourceOutput{})
RegisterOutputType(ResourceArrayOutput{})
}
// coerceTypeConversion assigns src to dst, performing deep type coercion as necessary.
func coerceTypeConversion(src interface{}, dst reflect.Type) (interface{}, error) {
makeError := func(src, dst reflect.Value) error {
return fmt.Errorf("expected value of type %s, not %s", dst.Type(), src.Type())
}
var coerce func(reflect.Value, reflect.Value) error
coerce = func(src, dst reflect.Value) error {
if src.Type().Kind() == reflect.Interface && !src.IsNil() {
src = src.Elem()
}
if src.Type().AssignableTo(dst.Type()) {
dst.Set(src)
return nil
}
//nolint:exhaustive // We only handle a few types here.
switch dst.Type().Kind() {
case reflect.Map:
if src.Kind() != reflect.Map {
return makeError(src, dst)
}
dst.Set(reflect.MakeMapWithSize(dst.Type(), src.Len()))
for iter := src.MapRange(); iter.Next(); {
dstKey := reflect.New(dst.Type().Key()).Elem()
dstVal := reflect.New(dst.Type().Elem()).Elem()
if err := coerce(iter.Key(), dstKey); err != nil {
return fmt.Errorf("invalid key: %w", err)
}
if err := coerce(iter.Value(), dstVal); err != nil {
return fmt.Errorf("[%#v]: %w", dstKey.Interface(), err)
}
dst.SetMapIndex(dstKey, dstVal)
}
return nil
case reflect.Slice:
if src.Kind() != reflect.Slice {
return makeError(src, dst)
}
dst.Set(reflect.MakeSlice(dst.Type(), src.Len(), src.Cap()))
for i := 0; i < src.Len(); i++ {
dstVal := reflect.New(dst.Type().Elem()).Elem()
if err := coerce(src.Index(i), dstVal); err != nil {
return fmt.Errorf("[%d]: %w", i, err)
}
dst.Index(i).Set(dstVal)
}
return nil
default:
return makeError(src, dst)
}
}
srcV, dstV := reflect.ValueOf(src), reflect.New(dst).Elem()
if err := coerce(srcV, dstV); err != nil {
return nil, err
}
return dstV.Interface(), nil
}
func getOutputDeps(o Output) []Resource {
return resourcesFromInternal(internal.OutputDependencies(o))
}
func resourcesToInternal(in []Resource) []internal.Resource {
if in == nil {
return nil
}
out := make([]internal.Resource, len(in))
for i, r := range in {
out[i] = r
}
return out
}
func resourcesFromInternal(in []internal.Resource) []Resource {
if in == nil {
return nil
}
out := make([]Resource, len(in))
for i, r := range in {
out[i] = r.(Resource)
}
return out
}