Skip to content

Commit

Permalink
Merge #11883
Browse files Browse the repository at this point in the history
11883: [sdk/{go,nodejs,python}] Fix `DeletedWith` resource option r=justinvp a=justinvp

This change fixes the `DeletedWith` resource option in the Go, Node.js, and Python SDKs and adds tests.

This feature was a community contribution and while there were engine tests included with the [original PR](#11095), there weren't any tests confirming the functionality worked correctly from each SDK.

Here's a summary of the fixes:

* Go: The `DeletedWith` resource option was never usable as it accepted a URN instead of a Resource. We discussed this internally a while back and decided to go ahead and fix this. (Note: While changing the signature is technically a breaking change, the feature is currently unusable, so the change would not break anyone, so there's no need to wait for a major version bump.)

* Node.js: The `deletedWith` resource option did not work at all from the Node.js SDK because it was incorrectly passing the resource object itself in the RegisterResource request, rather than the resource's URN.

* Python: The `deleted_with` resource option did not work at all from the Python SDK because it was incorrectly passing the resource object itself in the RegisterResource request, rather than the resource's URN.

A `FailsOnDelete` resource has been added to the testprovider, which will fail when its `Delete` gRPC is called. The tests use this to ensure `Delete` is not called for resources of this type with the `DeletedWith` option specified.

Fixes #11358
Fixes #11622

Co-authored-by: Justin Van Patten <jvp@justinvp.com>
  • Loading branch information
bors[bot] and justinvp committed Jan 16, 2023
2 parents 8b7e084 + 0b13f91 commit 60195d6
Show file tree
Hide file tree
Showing 29 changed files with 803 additions and 12 deletions.
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: sdk/go,nodejs,python
description: Fix DeletedWith resource option
13 changes: 11 additions & 2 deletions sdk/go/pulumi/context.go
Expand Up @@ -604,7 +604,7 @@ func (ctx *Context) ReadResource(
return err
}

if options.DeletedWith != "" && !ctx.supportsDeletedWith {
if options.DeletedWith != nil && !ctx.supportsDeletedWith {
return errors.New("the Pulumi CLI does not support the DeletedWith option. Please update the Pulumi CLI")
}

Expand Down Expand Up @@ -1332,6 +1332,15 @@ func (ctx *Context) prepareResourceInputs(res Resource, props Input, t string, o
aliases[i] = string(urn)
}

var deletedWithURN URN
if opts.DeletedWith != nil {
urn, _, _, err := opts.DeletedWith.URN().awaitURN(context.Background())
if err != nil {
return nil, fmt.Errorf("error waiting for DeletedWith URN to resolve: %w", err)
}
deletedWithURN = urn
}

return &resourceInputs{
parent: string(resOpts.parentURN),
deps: deps,
Expand All @@ -1351,7 +1360,7 @@ func (ctx *Context) prepareResourceInputs(res Resource, props Input, t string, o
pluginDownloadURL: state.pluginDownloadURL,
replaceOnChanges: resOpts.replaceOnChanges,
retainOnDelete: opts.RetainOnDelete,
deletedWith: string(opts.DeletedWith),
deletedWith: string(deletedWithURN),
}, nil
}

Expand Down
6 changes: 3 additions & 3 deletions sdk/go/pulumi/resource.go
Expand Up @@ -313,7 +313,7 @@ type resourceOptions struct {
RetainOnDelete bool
// If set, the providers Delete method will not be called for this resource
// if specified resource is being deleted as well.
DeletedWith URN
DeletedWith Resource
}

type invokeOptions struct {
Expand Down Expand Up @@ -597,8 +597,8 @@ func RetainOnDelete(b bool) ResourceOption {

// If set, the providers Delete method will not be called for this resource
// if specified resource is being deleted as well.
func DeletedWith(dw URN) ResourceOption {
func DeletedWith(r Resource) ResourceOption {
return resourceOption(func(ro *resourceOptions) {
ro.DeletedWith = dw
ro.DeletedWith = r
})
}
10 changes: 8 additions & 2 deletions sdk/nodejs/runtime/resource.ts
Expand Up @@ -93,6 +93,9 @@ interface ResourceResolverOperation {
import: ID | undefined;
// Any important feature support from the monitor.
monitorSupportsStructuredAliases: boolean;
// If set, the providers Delete method will not be called for this resource
// if specified is being deleted as well.
deletedWithURN: URN | undefined;
}

/**
Expand Down Expand Up @@ -366,9 +369,9 @@ export function registerResource(res: Resource, parent: Resource | undefined, t:
req.setReplaceonchangesList(opts.replaceOnChanges || []);
req.setPlugindownloadurl(opts.pluginDownloadURL || "");
req.setRetainondelete(opts.retainOnDelete || false);
req.setDeletedwith(opts.deletedWith);
req.setDeletedwith(resop.deletedWithURN || "");

if (opts.deletedWith && !(await monitorSupportsDeletedWith())) {
if (resop.deletedWithURN && !(await monitorSupportsDeletedWith())) {
throw new Error("The Pulumi CLI does not support the DeletedWith option. Please update the Pulumi CLI.");
}

Expand Down Expand Up @@ -647,6 +650,8 @@ async function prepareResource(label: string, res: Resource, parent: Resource |
}
}

const deletedWithURN = opts?.deletedWith ? await opts.deletedWith.urn.promise() : undefined;

return {
resolveURN: resolveURN,
resolveID: resolveID,
Expand All @@ -660,6 +665,7 @@ async function prepareResource(label: string, res: Resource, parent: Resource |
aliases: aliases,
import: importID,
monitorSupportsStructuredAliases,
deletedWithURN,
};

} finally {
Expand Down
18 changes: 16 additions & 2 deletions sdk/python/lib/pulumi/runtime/resource.py
Expand Up @@ -90,6 +90,12 @@ class ResourceResolverOperations(NamedTuple):
A list of aliases applied to this resource.
"""

deleted_with_urn: Optional[str]
"""
If set, the providers Delete method will not be called for this resource
if specified resource is being deleted as well.
"""


# Prepares for an RPC that will manufacture a resource, and hence deals with input and output properties.
# pylint: disable=too-many-locals
Expand Down Expand Up @@ -181,6 +187,10 @@ async def prepare_resource(
if not alias_val in aliases:
aliases.append(alias_val)

deleted_with_urn: Optional[str] = ""
if opts is not None and opts.deleted_with is not None:
deleted_with_urn = await opts.deleted_with.urn.future()

return ResourceResolverOperations(
parent_urn,
serialized_props,
Expand All @@ -189,6 +199,7 @@ async def prepare_resource(
provider_refs,
property_dependencies,
aliases,
deleted_with_urn,
)


Expand Down Expand Up @@ -562,7 +573,10 @@ async def do_register():
"Expected custom_timeouts to be a CustomTimeouts object"
)

if opts.deleted_with and not await settings.monitor_supports_deleted_with():
if (
resolver.deleted_with_urn
and not await settings.monitor_supports_deleted_with()
):
raise Exception(
"The Pulumi CLI does not support the DeletedWith option. Please update the Pulumi CLI."
)
Expand Down Expand Up @@ -598,7 +612,7 @@ async def do_register():
remote=remote,
replaceOnChanges=replace_on_changes,
retainOnDelete=opts.retain_on_delete or False,
deletedWith=opts.deleted_with,
deletedWith=resolver.deleted_with_urn,
)

from ..resource import create_urn # pylint: disable=import-outside-toplevel
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/deleted_with/go/Pulumi.yaml
@@ -0,0 +1,3 @@
name: deleted_with_go
description: A program that uses the DeletedWith resource option
runtime: go
24 changes: 24 additions & 0 deletions tests/integration/deleted_with/go/fails_on_delete.go
@@ -0,0 +1,24 @@
// Copyright 2016-2023, Pulumi Corporation. All rights reserved.
//go:build !all
// +build !all

// Exposes the FailsOnDelete resource from the testprovider.

package main

import (
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

type FailsOnDelete struct {
pulumi.CustomResourceState
}

func NewFailsOnDelete(ctx *pulumi.Context, name string, opts ...pulumi.ResourceOption) (*FailsOnDelete, error) {
var resource FailsOnDelete
err := ctx.RegisterResource("testprovider:index:FailsOnDelete", name, nil, &resource, opts...)
if err != nil {
return nil, err
}
return &resource, nil
}
66 changes: 66 additions & 0 deletions tests/integration/deleted_with/go/go.mod
@@ -0,0 +1,66 @@
module github.com/pulumi/pulumi/tests/deleted_with

go 1.18

require github.com/pulumi/pulumi/sdk/v3 v3.51.1

require (
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/cheggaaa/pb v1.0.29 // indirect
github.com/djherbis/times v1.5.0 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-git/go-git/v5 v5.4.2 // indirect
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.7 // indirect
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/opentracing/basictracer-go v1.1.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/term v1.1.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 // indirect
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/spf13/cobra v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/texttheater/golang-levenshtein v1.0.1 // indirect
github.com/tweekmonster/luser v0.0.0-20161003172636-3fa38070dbd7 // indirect
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/xanzy/ssh-agent v0.3.2 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/genproto v0.0.0-20220405205423-9d709892a2bf // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/frand v1.4.2 // indirect
sourcegraph.com/sourcegraph/appdash v0.0.0-20211028080628-e2786a622600 // indirect
)

0 comments on commit 60195d6

Please sign in to comment.