Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sdk/{go,nodejs,python}] Fix DeletedWith resource option #11883

Merged
merged 1 commit into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2016-2023, Pulumi Corporation. All rights reserved.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth considering moving testprovider to its own repo and publishing it. Then we could use it from this repo and others (like pulumi-dotnet, pulumi-java, etc.), and we wouldn't need to have multiple copies of these hand-written files for calling the resources in the provider in each test that uses them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think pretty much the entire integration test set should be callable from each language repo, not sure if that means testprovider needs to be moved out to it's own repo as well or not though. And as you'll have seen in dotnet I re-wrote the testprovider to C# in that repo, might make sense to do that for other languages as well.

//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
Original file line number Diff line number Diff line change
@@ -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
)