-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathresource.go
More file actions
1200 lines (1036 loc) · 40.5 KB
/
Copy pathresource.go
File metadata and controls
1200 lines (1036 loc) · 40.5 KB
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016, 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 pulumi
import (
"context"
"fmt"
"maps"
"reflect"
"sort"
"strings"
"sync"
"github.com/pulumi/pulumi/sdk/v3/go/common/promise"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/slice"
"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/internal"
)
type (
// ID is a unique identifier assigned by a resource provider to a resource.
ID string
// URN is an automatically generated logical URN, used to stably identify resources.
URN string
)
var (
resourceStateType = reflect.TypeFor[ResourceState]()
customResourceStateType = reflect.TypeFor[CustomResourceState]()
providerResourceStateType = reflect.TypeFor[ProviderResourceState]()
)
// This type alias is a hack to embed the internal.ResourceState type
// into pulumi.ResourceState without exporting the field to the public API.
//
//nolint:unused
type internalResourceState = internal.ResourceState
// ResourceState is the base
type ResourceState struct {
// internalResourceState marks this ResourceState as a resource
// recognized by the internal package.
internalResourceState
m sync.RWMutex
urn URNOutput `pulumi:"urn"`
rawOutputs Output
children resourceSet
providers map[string]ProviderResource
provider ProviderResource
protect *bool
version string
pluginDownloadURL string
aliases []URNOutput
name string
typ string
transformations []ResourceTransformation
keepDep bool
}
var _ Resource = (*ResourceState)(nil)
func (s *ResourceState) URN() URNOutput {
return s.urn
}
// The name assigned to the resource at construction.
func (s *ResourceState) PulumiResourceName() string {
return s.name
}
// The type assigned to the resource at construction.
func (s *ResourceState) PulumiResourceType() string {
return s.typ
}
func (s *ResourceState) GetProvider(token string) ProviderResource {
return s.providers[getPackage(token)]
}
// This is an internal method and future versions of the sdk may not support this API.
//
// InternalGetRawOutputs obtains the full PropertyMap returned during resource registration,
// allowing a caller of RegisterResource to obtain directly information about the outputs and their
// known and secret attributes.
func InternalGetRawOutputs(res *ResourceState) Output {
return res.rawOutputs
}
func (s *ResourceState) getChildren() []Resource {
s.m.RLock()
defer s.m.RUnlock()
var children []Resource
if len(s.children) != 0 {
children = slice.Prealloc[Resource](len(s.children))
for r := range s.children {
children = append(children, r)
}
}
return children
}
func (s *ResourceState) addChild(r Resource) {
s.m.Lock()
defer s.m.Unlock()
if s.children == nil {
s.children = resourceSet{}
}
s.children.add(r)
}
func (s *ResourceState) getProviders() map[string]ProviderResource {
return s.providers
}
func (s *ResourceState) getProvider() ProviderResource {
return s.provider
}
func (s *ResourceState) getProtect() *bool {
return s.protect
}
func (s *ResourceState) getVersion() string {
return s.version
}
func (s *ResourceState) getPluginDownloadURL() string {
return s.pluginDownloadURL
}
func (s *ResourceState) getAliases() []URNOutput {
return s.aliases
}
func (s *ResourceState) getTransformations() []ResourceTransformation {
return s.transformations
}
func (s *ResourceState) addTransformation(t ResourceTransformation) {
s.transformations = append(s.transformations, t)
}
func (s *ResourceState) setKeepDependency() {
s.keepDep = true
}
func (s *ResourceState) keepDependency() bool {
return s.keepDep
}
func (ctx *Context) newDependencyResource(urn URN) Resource {
var res ResourceState
res.urn.OutputState = ctx.newOutputState(res.urn.ElementType(), &res)
internal.ResolveOutput(res.urn, urn, true, false, resourcesToInternal(nil))
res.keepDep = true
return &res
}
type CustomResourceState struct {
ResourceState
id IDOutput `pulumi:"id"`
}
func (s *CustomResourceState) ID() IDOutput {
return s.id
}
func (*CustomResourceState) isCustomResource() {}
func (ctx *Context) newDependencyCustomResource(urn URN, id ID) CustomResource {
var res CustomResourceState
res.urn.OutputState = ctx.newOutputState(res.urn.ElementType(), &res)
internal.ResolveOutput(res.urn, urn, true, false, resourcesToInternal(nil))
res.id.OutputState = ctx.newOutputState(res.id.ElementType(), &res)
internal.ResolveOutput(res.id, id, id != "", false, resourcesToInternal(nil))
return &res
}
type ProviderResourceState struct {
CustomResourceState
pkg string
}
func (s *ProviderResourceState) getPackage() string {
return s.pkg
}
func (ctx *Context) newDependencyProviderResource(urn URN, id ID) ProviderResource {
var res ProviderResourceState
res.urn.OutputState = ctx.newOutputState(res.urn.ElementType(), &res)
res.id.OutputState = ctx.newOutputState(res.id.ElementType(), &res)
internal.ResolveOutput(res.urn, urn, true, false, resourcesToInternal(nil))
internal.ResolveOutput(res.id, id, id != "", false, resourcesToInternal(nil))
res.pkg = string(resource.URN(urn).Type().Name())
return &res
}
func (ctx *Context) newDependencyProviderResourceFromRef(ref string) ProviderResource {
idx := strings.LastIndex(ref, "::")
if idx == -1 {
return nil
}
urn, id := ref[:idx], ref[idx+2:]
return ctx.newDependencyProviderResource(URN(urn), ID(id))
}
// Resource represents a cloud resource managed by Pulumi.
type Resource interface {
internal.Resource
// URN is this resource's stable logical URN used to distinctly address it before, during, and after deployments.
URN() URNOutput
// PulumiResourceName returns the name of the resource.
PulumiResourceName() string
// PulumiResourceType returns the type token of the resource.
PulumiResourceType() string
// getChildren returns the resource's children.
getChildren() []Resource
// addChild adds a child to the resource.
addChild(r Resource)
// getProviders returns the provider map for this resource.
getProviders() map[string]ProviderResource
// getProvider returns the provider for the resource.
getProvider() ProviderResource
// getProtect returns the protect flag for the resource.
getProtect() *bool
// getVersion returns the version for the resource.
getVersion() string
// getPluginDownloadURL returns the provider plugin download url
getPluginDownloadURL() string
// getAliases returns the list of aliases for this resource
getAliases() []URNOutput
// getTransformations returns the transformations for the resource.
getTransformations() []ResourceTransformation
// addTransformation adds a single transformation to the resource.
addTransformation(t ResourceTransformation)
// setKeepDependency marks this resource as a resource that should be kept as a dependency.
// This is done for remote component resources, dependency resources, and rehydrated component resources.
setKeepDependency()
// keepDependency returns true if the resource should be kept as a dependency, which is the case for
// remote component resources, dependency resources, and rehydrated component resources.
keepDependency() bool
}
var _ internal.Resource = (Resource)(nil)
// CustomResource is a cloud resource whose create, read, update, and delete (CRUD) operations are managed by performing
// external operations on some physical entity. The engine understands how to diff and perform partial updates of them,
// and these CRUD operations are implemented in a dynamically loaded plugin for the defining package.
type CustomResource interface {
Resource
// ID is the provider-assigned unique identifier for this managed resource. It is set during deployments,
// but might be missing ("") during planning phases.
ID() IDOutput
isCustomResource()
}
// ComponentResource is a resource that aggregates one or more other child resources into a higher level abstraction.
// The component resource itself is a resource, but does not require custom CRUD operations for provisioning.
type ComponentResource interface {
Resource
}
// ProviderResource is a resource that represents a configured instance of a particular package's provider plugin.
// These resources are supply the implementations of their package's CRUD operations. A specific provider instance can
// be used for a given resource by passing it in ResourceOpt.Provider.
type ProviderResource interface {
CustomResource
getPackage() string
}
// CustomTimeouts specifies timeouts for resource provisioning operations.
// Use it with the [Timeouts] option when creating new resources
// to override default timeouts.
//
// Each timeout is specified as a duration string such as,
// "5ms" (5 milliseconds), "40s" (40 seconds),
// and "1m30s" (1 minute, 30 seconds).
//
// The following units are accepted.
//
// - ns: nanoseconds
// - us: microseconds
// - µs: microseconds
// - ms: milliseconds
// - s: seconds
// - m: minutes
// - h: hours
type CustomTimeouts struct {
Create string
Update string
Delete string
Read string
}
// ResourceHookOptions are the options for registering a resource hook.
type ResourceHookOptions struct {
OnDryRun bool // Run the hook during dry run (preview) operations. Defaults to false.
IgnoreErrors bool // If true, errors are logged as warnings instead of failing the program.
}
// ResourceHookArgs represents the arguments passed to a resource hook.
//
// Depending on the hook type, only some of the new/old inputs/outputs are set.
//
// | Hook Type | old_inputs | new_inputs | old_outputs | new_outputs |
// | ------------- | ---------- | ---------- | ----------- | ----------- |
// | before_create | | ✓ | | |
// | after_create | | ✓ | | ✓ |
// | before_update | ✓ | ✓ | ✓ | |
// | after_update | ✓ | ✓ | ✓ | ✓ |
// | before_delete | ✓ | | ✓ | |
// | after_delete | ✓ | | ✓ | |
type ResourceHookArgs struct {
URN URN // The URN of the resource that triggered the hook.
ID ID // The ID of the resource that triggered the hook.
Name string // The name of the resource that triggered the hook.
Type tokens.Type // The type of the resource that triggered the hook.
NewInputs resource.PropertyMap // The new inputs of the resource that triggered the hook.
OldInputs resource.PropertyMap // The old inputs of the resource that triggered the hook.
NewOutputs resource.PropertyMap // The new outputs of the resource that triggered the hook.
OldOutputs resource.PropertyMap // The old outputs of the resource that triggered the hook.
}
// ErrorHookArgs represents the arguments passed to an error hook.
//
// Depending on the failed operation, only some of the new/old inputs/outputs are set.
//
// | Failed Operation | old_inputs | new_inputs | old_outputs |
// | ---------------- | ---------- | ---------- | ----------- |
// | create | | ✓ | |
// | update | ✓ | ✓ | ✓ |
// | delete | ✓ | | ✓ |
type ErrorHookArgs struct {
URN URN // The URN of the resource that triggered the hook.
ID ID // The ID of the resource that triggered the hook.
Name string // The name of the resource that triggered the hook.
Type tokens.Type // The type of the resource that triggered the hook.
NewInputs resource.PropertyMap // The new inputs of the resource that triggered the hook.
OldInputs resource.PropertyMap // The old inputs of the resource that triggered the hook.
OldOutputs resource.PropertyMap // The old outputs of the resource that triggered the hook.
FailedOperation string // The operation that failed (create, update, or delete).
Errors []string // The errors that have been seen so far (newest first).
}
// ResourceHookFunction is a function that can be registered as a resource hook
type ResourceHookFunction func(args *ResourceHookArgs) error
// ErrorHookFunction is a function that can be registered as an error hook
type ErrorHookFunction func(args *ErrorHookArgs) (bool, error)
// ResourceHook is a named hook that can be registered as a resource hook.
type ResourceHook struct {
Name string // The unqiue name of the resource hook.
Callback ResourceHookFunction // The function that will be called when the resource hook is triggered.
Opts ResourceOptions // The options for the resource hook.
// Tracks the registration of the resource hook. The future will resolve
// once the hook has been registered, or reject if any error
registered *promise.Promise[struct{}]
}
// ErrorHook is a named hook that can be registered as an error hook.
type ErrorHook struct {
Name string // The unqiue name of the error hook.
Callback ErrorHookFunction // The function that will be called when the error hook is triggered.
// Tracks the registration of the error hook. The future will resolve
// once the hook has been registered, or reject if any error occurs.
registered *promise.Promise[struct{}]
}
// ResourceHookBinding binds `ResourceHook` instances to a resource. The
// resource hooks will be invoked during certain step of the lifecycle of the
// resource.
//
// `before_${action}` hooks that raise an exception cause the action to fail.
// `after_${action}` hooks that raise an exception will log a warning, but do
// not cause the action or the deployment to fail.
//
// When running `pulumi destroy`, `before_delete` and `after_delete` resource
// hooks require the operation to run with `--run-program`, to ensure that the
// program which defines the hooks is available.
type ResourceHookBinding struct {
BeforeCreate []*ResourceHook // Hooks to be invoked before the resource is created.
AfterCreate []*ResourceHook // Hooks to be invoked after the resource is created.
BeforeUpdate []*ResourceHook // Hooks to be invoked before the resource is updated.
AfterUpdate []*ResourceHook // Hooks to be invoked after the resource is updated.
// Hooks to be invoked before the resource is deleted.
//
// Note that delete hooks require that destroy operations are run with
// `--run-program`.
BeforeDelete []*ResourceHook
// Hooks to be invoked after the resource is deleted.
//
// Note that delete hooks require that destroy operations are run with
// `--run-program`.
AfterDelete []*ResourceHook
// Hooks to be invoked when an operation fails and is retryable.
OnError []*ErrorHook
}
// ResourceOptions is a snapshot of one or more [ResourceOption]s.
//
// You cannot pass a ResourceOptions struct to a resource constructor.
// Instead, use individual [ResourceOption] values to configure a resource.
// The ResourceOptions struct only provides a read-only preview
// of the collective effect of the options.
//
// See https://www.pulumi.com/docs/intro/concepts/resources/options/
// for more details on individual options.
type ResourceOptions struct {
// AdditionalSecretOutputs lists output properties
// that must be encrypted as secrets.
AdditionalSecretOutputs []string
// Aliases lists aliases for this resource
// that are used to find and use existing resources.
Aliases []Alias
// CustomTimeouts, if set, overrides the default timeouts
// for resource CRUD operations.
CustomTimeouts *CustomTimeouts
// DeleteBeforeReplace specifies that resources being replaced
// should be deleted before creating the replacement
// instead of Pulumi's default behavior of creating the replacement
// before performing deletion.
DeleteBeforeReplace bool
// DependsOn lists additional explicit dependencies for the resource
// in addition to those tracked automatically by Pulumi.
DependsOn []Resource
// DependsOnInputs holds explicit dependencies for the resource
// that may not be fully known yet.
DependsOnInputs []ResourceArrayInput
// IgnoreChanges lists properties changes to which should be ignored.
IgnoreChanges []string
// HideDiffs lists property paths which shouldn't be displayed during diffs.
HideDiffs []string
// Import specifies that the provider for this resource
// should import its state from a cloud resource with the given ID.
Import IDInput
// Parent is the parent resource for the resource being created,
// or nil if this resource does not have a parent.
Parent Resource
// Protect prevents this resource from being deleted.
Protect bool
// Provider is the provider resource to use for this resource's CRUD operations.
// It's nil if the default provider should be used.
Provider ProviderResource
// Providers is a bag of providers available
// to instantiate resources of various types.
// These are used for a type when a provider for that type
// was not explicitly supplied.
Providers []ProviderResource
// ReplaceOnChanges lists properties that, when modified,
// force a replacement of the resource.
// The list may include '*' to indicate that all properties trigger
// replacements.
ReplaceOnChanges []string
// If set, the engine will diff this with the last recorded value, and trigger
// a replace if they are not equal. Note that if either value is null, no
// comparison is done and no replacement is triggered. This means that the
// replacement trigger only applies to two subsequent deployments with defined
// triggers.
ReplacementTrigger Input
// Transformations is a list of functions that transform
// the resource's properties during construction.
Transformations []ResourceTransformation
// Transforms is a list of functions that transform
// the resource's properties during construction.
Transforms []ResourceTransform
// URN is the URN of a previously-registered resource of this type.
URN string
// Version changes the version of the provider plugin that should be used
// when operating on this resource.
// This will be blank if the version was automatically inferred.
Version string
// PluginDownloadURL specifies the URL from which the provider plugin
// should be downloaded.
// This will be blank if the URL was inferred automatically.
PluginDownloadURL string
// RetainOnDelete specifies that the resource should not be deleted
// in the cloud provider, even if it's deleted from Pulumi.
RetainOnDelete bool
// DeletedWith holds a container resource that, if deleted,
// also deletes this resource.
DeletedWith Resource
// ReplaceWith holds a list of container resources that, if replaced,
// also trigger a replace of this resource.
ReplaceWith []Resource
// Hooks are the optional resource hooks to bind to this resource. The hooks
// will be invoked during the lifecycle of the resource.
Hooks *ResourceHookBinding
// EnvVarMappings specifies environment variable mappings for provider resources.
// Keys are the source environment variable names, values are the target names.
EnvVarMappings map[string]string
}
// NewResourceOptions builds a preview of the effect of the provided options.
//
// Use this to get a read-only snapshot of a list of options
// inside mocks and component resources.
func NewResourceOptions(opts ...ResourceOption) (*ResourceOptions, error) {
// The error return is currently unused,
// but it's foreseeable that we'll need it
// if we begin doing option validation at option merge time.
return resourceOptionsSnapshot(merge(opts...)), nil
}
// resourceOptions is the internal representation of the effect of
// [ResourceOption]s.
type resourceOptions struct {
AdditionalSecretOutputs []string
Aliases []Alias
CustomTimeouts *CustomTimeouts
DeleteBeforeReplace *bool
DependsOn []dependencySet
IgnoreChanges []string
HideDiffs []string
Import IDInput
Parent Resource
Protect *bool
Provider ProviderResource
Providers map[string]ProviderResource
ReplaceOnChanges []string
ReplacementTrigger Input
Transformations []ResourceTransformation
Transforms []ResourceTransform
URN string
Version string
PluginDownloadURL string
RetainOnDelete *bool
DeletedWith Resource
ReplaceWith []Resource
Parameterization []byte
Hooks *ResourceHookBinding
EnvVarMappings map[string]string
}
func resourceOptionsSnapshot(ro *resourceOptions) *ResourceOptions {
var (
dependsOn []Resource
dependsOnInputs []ResourceArrayInput
)
for _, d := range ro.DependsOn {
switch d := d.(type) {
case resourceDependencySet:
dependsOn = append(dependsOn, []Resource(d)...)
case *resourceArrayInputDependencySet:
dependsOnInputs = append(dependsOnInputs, d.input)
default:
// Unreachable.
// We control all implementations of dependencySet.
contract.Failf("Unknown dependencySet %T", d)
}
}
sort.Slice(dependsOn, func(i, j int) bool {
return dependsOn[i].PulumiResourceName() < dependsOn[j].PulumiResourceName()
})
var providers []ProviderResource
if len(ro.Providers) > 0 {
providers = slice.Prealloc[ProviderResource](len(ro.Providers))
for _, p := range ro.Providers {
providers = append(providers, p)
}
sort.Slice(providers, func(i, j int) bool {
return providers[i].getPackage() < providers[j].getPackage()
})
}
flatten := func(s *bool) bool {
if s == nil {
return false
}
return *s
}
return &ResourceOptions{
AdditionalSecretOutputs: ro.AdditionalSecretOutputs,
Aliases: ro.Aliases,
CustomTimeouts: ro.CustomTimeouts,
DeleteBeforeReplace: flatten(ro.DeleteBeforeReplace),
DependsOn: dependsOn,
DependsOnInputs: dependsOnInputs,
IgnoreChanges: ro.IgnoreChanges,
HideDiffs: ro.HideDiffs,
Import: ro.Import,
Parent: ro.Parent,
Protect: flatten(ro.Protect),
Provider: ro.Provider,
Providers: providers,
ReplaceOnChanges: ro.ReplaceOnChanges,
ReplacementTrigger: ro.ReplacementTrigger,
Transformations: ro.Transformations,
Transforms: ro.Transforms,
URN: ro.URN,
Version: ro.Version,
PluginDownloadURL: ro.PluginDownloadURL,
RetainOnDelete: flatten(ro.RetainOnDelete),
DeletedWith: ro.DeletedWith,
ReplaceWith: ro.ReplaceWith,
Hooks: ro.Hooks,
EnvVarMappings: ro.EnvVarMappings,
}
}
// InvokeOptions is a snapshot of one or more [InvokeOption]s.
//
// You cannot pass an InvokeOptions struct to a provider function.
// Instead, use individual [InvokeOption] values to configure a call.
// The InvokeOptions struct only provides a read-only preview
// of the collective effect of the options.
type InvokeOptions struct {
// Parent is the parent resource for this operation.
// It may be used to determine the provider to use.
Parent Resource
// Provider specifies the provider to use for this operation.
// This is nil if the default provider should be used.
Provider ProviderResource
// Version is the version of the provider plugin that should be used.
// This will be blank if the version was automatically inferred.
Version string
// PluginDownloadURL is the URL from which the provider plugin
// should be downloaded.
// This will be blank if the URL was inferred automatically.
PluginDownloadURL string
// DependsOn lists additional explicit dependencies for the resource
// in addition to those tracked automatically by Pulumi.
DependsOn []Resource
// DependsOnInputs holds explicit dependencies for the resource
// that may not be fully known yet.
DependsOnInputs []ResourceArrayInput
}
// NewInvokeOptions builds a preview of the effect of the provided options.
//
// Use this to get a read-only snapshot of the collective effect
// of a list of [InvokeOption]s.
func NewInvokeOptions(opts ...InvokeOption) (*InvokeOptions, error) {
// The error return is currently unused,
// but it's foreseeable that we'll need it
// if we begin doing option validation at option merge time.
return invokeOptionsSnapshot(mergeInvokeOptions(opts...)), nil
}
// invokeOptions is the internal representation of the effect of
// [InvokeOptions]s.
type invokeOptions struct {
Parent Resource
Provider ProviderResource
Version string
PluginDownloadURL string
DependsOn []dependencySet
Parameterization []byte
}
func invokeOptionsSnapshot(io *invokeOptions) *InvokeOptions {
var (
dependsOn []Resource
dependsOnInputs []ResourceArrayInput
)
for _, d := range io.DependsOn {
switch d := d.(type) {
case resourceDependencySet:
dependsOn = append(dependsOn, []Resource(d)...)
case *resourceArrayInputDependencySet:
dependsOnInputs = append(dependsOnInputs, d.input)
default:
// Unreachable.
// We control all implementations of dependencySet.
contract.Failf("Unknown dependencySet %T", d)
}
}
sort.Slice(dependsOn, func(i, j int) bool {
return dependsOn[i].PulumiResourceName() < dependsOn[j].PulumiResourceName()
})
return &InvokeOptions{
Parent: io.Parent,
Provider: io.Provider,
Version: io.Version,
PluginDownloadURL: io.PluginDownloadURL,
DependsOn: dependsOn,
DependsOnInputs: dependsOnInputs,
}
}
type ResourceOption interface {
applyResourceOption(*resourceOptions)
}
type InvokeOption interface {
applyInvokeOption(*invokeOptions)
}
type ResourceOrInvokeOption interface {
ResourceOption
InvokeOption
}
type resourceOption func(*resourceOptions)
func (o resourceOption) applyResourceOption(opts *resourceOptions) {
o(opts)
}
type invokeOption func(*invokeOptions)
func (o invokeOption) applyInvokeOption(opts *invokeOptions) {
o(opts)
}
type resourceOrInvokeOption func(ro *resourceOptions, io *invokeOptions)
func (o resourceOrInvokeOption) applyResourceOption(opts *resourceOptions) {
o(opts, nil)
}
func (o resourceOrInvokeOption) applyInvokeOption(opts *invokeOptions) {
o(nil, opts)
}
// merging is handled by each functional options call
// properties that are arrays/maps are always appended/merged together
// last value wins for non-array/map values and for conflicting map values (bool, struct, etc)
func merge(opts ...ResourceOption) *resourceOptions {
options := &resourceOptions{}
for _, o := range opts {
if o != nil {
o.applyResourceOption(options)
}
}
return options
}
func mergeInvokeOptions(opts ...InvokeOption) *invokeOptions {
options := &invokeOptions{}
for _, o := range opts {
if o != nil {
o.applyInvokeOption(options)
}
}
return options
}
// AdditionalSecretOutputs specifies a list of output properties to mark as secret.
func AdditionalSecretOutputs(o []string) ResourceOption {
return resourceOption(func(ro *resourceOptions) {
ro.AdditionalSecretOutputs = append(ro.AdditionalSecretOutputs, o...)
})
}
// Aliases applies a list of identifiers to find and use existing resources.
func Aliases(o []Alias) ResourceOption {
return resourceOption(func(ro *resourceOptions) {
ro.Aliases = append(ro.Aliases, o...)
})
}
// DeleteBeforeReplace, when set to true, ensures that this resource is deleted prior to replacement.
func DeleteBeforeReplace(o bool) ResourceOption {
return resourceOption(func(ro *resourceOptions) {
ro.DeleteBeforeReplace = &o
})
}
// Composite is a resource option that contains other resource options.
func Composite(opts ...ResourceOption) ResourceOption {
return resourceOption(func(ro *resourceOptions) {
for _, o := range opts {
o.applyResourceOption(ro)
}
})
}
// CompositeInvoke is an invoke option that contains other invoke options.
func CompositeInvoke(opts ...InvokeOption) InvokeOption {
return invokeOption(func(ro *invokeOptions) {
for _, o := range opts {
o.applyInvokeOption(ro)
}
})
}
// dependencySet unifies types that can provide dependencies for a
// resource.
type dependencySet interface {
// Adds dependencies into the depSet.
// Optionally pass the last Resource arg to short-circuit component
// children cycles.
addDeps(context.Context, map[URN]Resource, Resource) error
}
// DependsOn is an optional array of explicit dependencies on other resources.
func DependsOn(o []Resource) ResourceOrInvokeOption {
return resourceOrInvokeOption(func(ro *resourceOptions, io *invokeOptions) {
switch {
case ro != nil:
ro.DependsOn = append(ro.DependsOn, resourceDependencySet(o))
case io != nil:
io.DependsOn = append(io.DependsOn, resourceDependencySet(o))
}
})
}
// resourceDependencySet is a dependencySet comprised of references to
// resources.
type resourceDependencySet []Resource
var _ dependencySet = (resourceDependencySet)(nil)
func (rs resourceDependencySet) addDeps(ctx context.Context, deps map[URN]Resource, from Resource) error {
for _, r := range rs {
if err := addDependency(ctx, deps, r, from); err != nil {
return err
}
}
return nil
}
// Declares explicit dependencies on other resources. Similar to
// `DependsOn`, but also admits resource inputs and outputs:
//
// var r Resource
// var ri ResourceInput
// var ro ResourceOutput
// allDeps := NewResourceArrayOutput(NewResourceOutput(r), ri.ToResourceOutput(), ro)
// DependsOnInputs(allDeps)
func DependsOnInputs(o ResourceArrayInput) ResourceOrInvokeOption {
return resourceOrInvokeOption(func(ro *resourceOptions, io *invokeOptions) {
switch {
case ro != nil:
ro.DependsOn = append(ro.DependsOn, &resourceArrayInputDependencySet{o})
case io != nil:
io.DependsOn = append(io.DependsOn, &resourceArrayInputDependencySet{o})
}
})
}
// resourceArrayInputDependencySet is a dependencySet built from
// collections of resources that are not yet known.
type resourceArrayInputDependencySet struct{ input ResourceArrayInput }
var _ dependencySet = (*resourceArrayInputDependencySet)(nil)
func (ra *resourceArrayInputDependencySet) addDeps(ctx context.Context, deps map[URN]Resource, from Resource) error {
out := ra.input.ToResourceArrayOutput()
value, known, _ /* secret */, _ /* deps */, err := internal.AwaitOutput(ctx, out)
if err != nil || !known {
return err
}
resources, ok := value.([]Resource)
if !ok {
return fmt.Errorf("ResourceArrayInput resolved to a value of unexpected type %v, expected []Resource",
reflect.TypeOf(value))
}
// For some reason, deps returned above are incorrect; instead:
toplevelDeps := getOutputDeps(out)
for _, r := range append(resources, toplevelDeps...) {
if err := addDependency(ctx, deps, r, from); err != nil {
return err
}
}
return nil
}
// Ignore changes to any of the specified properties.
func IgnoreChanges(o []string) ResourceOption {
return resourceOption(func(ro *resourceOptions) {
ro.IgnoreChanges = append(ro.IgnoreChanges, o...)
})
}
// Hide the diffs for a set of property paths.
func HideDiffs(paths []string) ResourceOption {
return resourceOption(func(ro *resourceOptions) {
ro.HideDiffs = append(ro.HideDiffs, paths...)
})
}
// Import, when provided with a resource ID, indicates that this resource's provider should import its state from
// the cloud resource with the given ID. The inputs to the resource's constructor must align with the resource's
// current state. Once a resource has been imported, the import property must be removed from the resource's
// options.
func Import(o IDInput) ResourceOption {
return resourceOption(func(ro *resourceOptions) {
ro.Import = o
})
}
// Parent sets the parent resource to which this resource or invoke belongs.
func Parent(r Resource) ResourceOrInvokeOption {
return resourceOrInvokeOption(func(ro *resourceOptions, io *invokeOptions) {
switch {
case ro != nil:
ro.Parent = r
case io != nil:
io.Parent = r
}
})
}
// Protect, when set to true, ensures that this resource cannot be deleted (without first setting it to false).
func Protect(o bool) ResourceOption {
return resourceOption(func(ro *resourceOptions) {
ro.Protect = &o
})
}
// If set, ResourceHooks are the resource hooks to bind to this resource. The
// hooks will be invoked during the lifecycle of the resource.
func ResourceHooks(hooks *ResourceHookBinding) ResourceOption {
return resourceOption(func(ro *resourceOptions) {
if hooks == nil {
return
}
if ro.Hooks == nil {
ro.Hooks = &ResourceHookBinding{}
}
ro.Hooks.BeforeCreate = append(ro.Hooks.BeforeCreate, hooks.BeforeCreate...)
ro.Hooks.AfterCreate = append(ro.Hooks.AfterCreate, hooks.AfterCreate...)
ro.Hooks.BeforeUpdate = append(ro.Hooks.BeforeUpdate, hooks.BeforeUpdate...)
ro.Hooks.AfterUpdate = append(ro.Hooks.AfterUpdate, hooks.AfterUpdate...)
ro.Hooks.BeforeDelete = append(ro.Hooks.BeforeDelete, hooks.BeforeDelete...)
ro.Hooks.AfterDelete = append(ro.Hooks.AfterDelete, hooks.AfterDelete...)
ro.Hooks.OnError = append(ro.Hooks.OnError, hooks.OnError...)
})
}
////////////////////////////////////////////////////////////////////////
// NOTE(Provider and Providers)
//
// For Provider vs Providers, there's a bit of complexity.
//
// # Background