From 1949b65072e6f2e1317c2ebf705d7a7d0113dc30 Mon Sep 17 00:00:00 2001 From: Justin Van Patten Date: Wed, 16 Nov 2022 11:21:53 -0800 Subject: [PATCH] Keep resource refs when invoking `pulumi:pulumi:getResource` We used to always keep resource references when marshaling the results of `pulumi:pulumi:getResource`, but this regressed in #9323 to only keep resource if request's `acceptResources` flag was set. Unfortunately, that flag was not being set when calling `pulumi:pulumi:getResource` in the Go, Node.js, and Python SDKs (although, it was being set for .NET). Two fixes: 1. Update the monitor to go back to always keeping resources when `pulumi:pulumi:getResource` is being invoked. This way, older SDKs that are not setting `acceptResources` will go back to the original behavior. 2. Update the SDKs to always set `acceptResources`, so that these newer versions of the SDKs will work with older engines that are checking `acceptResources`. (2) will help us with EKS 1.0. We'll be able to update EKS to use a version of `@pulumi/pulumi` with the fix to set the `acceptResources` flag. --- ...ces-when-pulumi-pulumi-getresource-is-invoked.yaml | 4 ++++ ...urces-when-invoking-pulumi-pulumi-getresource.yaml | 4 ++++ pkg/resource/deploy/source_eval.go | 11 ++++++++++- sdk/go/pulumi/context.go | 5 +++-- sdk/nodejs/runtime/resource.ts | 1 + sdk/python/lib/pulumi/runtime/resource.py | 11 ++++++++++- 6 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 changelog/pending/20221116--engine--always-keep-resources-when-pulumi-pulumi-getresource-is-invoked.yaml create mode 100644 changelog/pending/20221116--sdk-go-nodejs-python--set-acceptresources-when-invoking-pulumi-pulumi-getresource.yaml diff --git a/changelog/pending/20221116--engine--always-keep-resources-when-pulumi-pulumi-getresource-is-invoked.yaml b/changelog/pending/20221116--engine--always-keep-resources-when-pulumi-pulumi-getresource-is-invoked.yaml new file mode 100644 index 000000000000..086aa1befa0e --- /dev/null +++ b/changelog/pending/20221116--engine--always-keep-resources-when-pulumi-pulumi-getresource-is-invoked.yaml @@ -0,0 +1,4 @@ +changes: +- type: fix + scope: engine + description: Always keep resources when pulumi:pulumi:getResource is invoked diff --git a/changelog/pending/20221116--sdk-go-nodejs-python--set-acceptresources-when-invoking-pulumi-pulumi-getresource.yaml b/changelog/pending/20221116--sdk-go-nodejs-python--set-acceptresources-when-invoking-pulumi-pulumi-getresource.yaml new file mode 100644 index 000000000000..88b16d496c46 --- /dev/null +++ b/changelog/pending/20221116--sdk-go-nodejs-python--set-acceptresources-when-invoking-pulumi-pulumi-getresource.yaml @@ -0,0 +1,4 @@ +changes: +- type: fix + scope: sdk/go,nodejs,python + description: Set acceptResources when invoking pulumi:pulumi:getResource diff --git a/pkg/resource/deploy/source_eval.go b/pkg/resource/deploy/source_eval.go index bcdad1fd5fc6..f897565b58be 100644 --- a/pkg/resource/deploy/source_eval.go +++ b/pkg/resource/deploy/source_eval.go @@ -710,11 +710,20 @@ func (rm *resmon) Invoke(ctx context.Context, req *pulumirpc.ResourceInvokeReque if err != nil { return nil, fmt.Errorf("invocation of %v returned an error: %w", tok, err) } + + // Respect `AcceptResources` unless `tok` is for the built-in `pulumi:pulumi:getResource` function, + // in which case always keep resources to maintain the original behavior for older SDKs that are not + // setting the `AccceptResources` flag. + keepResources := req.GetAcceptResources() + if tok == "pulumi:pulumi:getResource" { + keepResources = true + } + mret, err := plugin.MarshalProperties(ret, plugin.MarshalOptions{ Label: label, KeepUnknowns: true, KeepSecrets: true, - KeepResources: req.GetAcceptResources(), + KeepResources: keepResources, }) if err != nil { return nil, fmt.Errorf("failed to marshal %v return: %w", tok, err) diff --git a/sdk/go/pulumi/context.go b/sdk/go/pulumi/context.go index 73fcbd3717f7..05245cf9722c 100644 --- a/sdk/go/pulumi/context.go +++ b/sdk/go/pulumi/context.go @@ -723,8 +723,9 @@ func (ctx *Context) getResource(urn string) (*pulumirpc.RegisterResourceResponse tok := "pulumi:pulumi:getResource" logging.V(9).Infof("Invoke(%s, #args=%d): RPC call being made synchronously", tok, len(resolvedArgsMap)) resp, err := ctx.monitor.Invoke(ctx.ctx, &pulumirpc.ResourceInvokeRequest{ - Tok: "pulumi:pulumi:getResource", - Args: rpcArgs, + Tok: "pulumi:pulumi:getResource", + Args: rpcArgs, + AcceptResources: !disableResourceReferences, }) if err != nil { return nil, fmt.Errorf("invoke(%s, ...): error: %v", tok, err) diff --git a/sdk/nodejs/runtime/resource.ts b/sdk/nodejs/runtime/resource.ts index 8c5711dc0297..443bdd75c7ce 100644 --- a/sdk/nodejs/runtime/resource.ts +++ b/sdk/nodejs/runtime/resource.ts @@ -114,6 +114,7 @@ export function getResource(res: Resource, parent: Resource | undefined, props: req.setArgs(gstruct.Struct.fromJavaScript(inputs)); req.setProvider(""); req.setVersion(""); + req.setAcceptresources(!utils.disableResourceReferences); // Now run the operation, serializing the invocation if necessary. const opLabel = `monitor.getResource(${label})`; diff --git a/sdk/python/lib/pulumi/runtime/resource.py b/sdk/python/lib/pulumi/runtime/resource.py index 75b3845ad29c..ffcd79fe485f 100644 --- a/sdk/python/lib/pulumi/runtime/resource.py +++ b/sdk/python/lib/pulumi/runtime/resource.py @@ -243,8 +243,17 @@ async def do_get(): monitor = settings.get_monitor() inputs = await rpc.serialize_properties({"urn": urn}, {}) + + accept_resources = not ( + os.getenv("PULUMI_DISABLE_RESOURCE_REFERENCES", "").upper() + in {"TRUE", "1"} + ) req = resource_pb2.ResourceInvokeRequest( - tok="pulumi:pulumi:getResource", args=inputs, provider="", version="" + tok="pulumi:pulumi:getResource", + args=inputs, + provider="", + version="", + acceptResources=accept_resources, ) def do_invoke():