Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
11095: Add `DeletedWith` resource option r=Frassle a=same-id

# Description

In many cases there is no need to delete resources if the container resource is going to be deleted as well.

A few examples:
 * Database object (roles, tables) when database is being deleted
 * Cloud IAM bindings when user itself is being deleted

This helps with:
 * Speeding the deletion process
 * Removing unnecessary calls to providers
 * Avoiding failed deletions when the pulumi user running the plan has access to the container resource but not the contained ones

To avoid deleting contained resources, set the `DeletedWith` resource option to the container resource.

TODO:
 Should we support DeletedWith with PendingDeletes?
 Special case might be when contained resource is marked as pending deletion
 but we now want to delete the container resource, so ultimately there is no
 need to delete the contained anymore

<!--- 
Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation.
-->


Fixes # (issue)

## Checklist

<!--- Please provide details if the checkbox below is to be left unchecked. -->
- [x] I have added tests that prove my fix is effective or that my feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the Pulumi Service,
then the service should honor older versions of the CLI where this change would not exist.
You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add
it to the service.

Not sure - maybe
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Service API version
  <!-- `@Pulumi` employees: If yes, you must submit corresponding changes in the service repo. -->


Co-authored-by: Sam Eiderman <sameid@gmail.com>
Co-authored-by: Fraser Waters <fraser@pulumi.com>
  • Loading branch information
3 people committed Nov 4, 2022
2 parents 9b28dcd + 0d634ac commit 3e12811
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
5 changes: 5 additions & 0 deletions sdk/Pulumi/Deployment/Deployment.cs
Expand Up @@ -217,6 +217,11 @@ internal Task<bool> MonitorSupportsOutputValues()
return MonitorSupportsFeature("outputValues");
}

internal Task<bool> MonitorSupportsDeletedWith()
{
return MonitorSupportsFeature("deletedWith");
}

// Because the secrets feature predates the Pulumi .NET SDK, we assume
// that the monitor supports secrets.
}
Expand Down
13 changes: 11 additions & 2 deletions sdk/Pulumi/Deployment/Deployment_RegisterResource.cs
Expand Up @@ -21,7 +21,11 @@ public partial class Deployment
var label = $"resource:{name}[{type}]";
Log.Debug($"Registering resource start: t={type}, name={name}, custom={custom}, remote={remote}");

var request = CreateRegisterResourceRequest(type, name, custom, remote, options);
if (options.DeletedWith != null && !(await MonitorSupportsDeletedWith().ConfigureAwait(false))) {
throw new Exception("The Pulumi CLI does not support the DeletedWith option. Please update the Pulumi CLI.");
}

var request = await CreateRegisterResourceRequest(type, name, custom, remote, options);

Log.Debug($"Preparing resource: t={type}, name={name}, custom={custom}, remote={remote}");
var prepareResult = await PrepareResourceAsync(label, resource, custom, remote, args, options).ConfigureAwait(false);
Expand Down Expand Up @@ -64,11 +68,15 @@ private static void PopulateRequest(RegisterResourceRequest request, PrepareResu
}
}

private static RegisterResourceRequest CreateRegisterResourceRequest(
private async static Task<RegisterResourceRequest> CreateRegisterResourceRequest(
string type, string name, bool custom, bool remote, ResourceOptions options)
{
var customOpts = options as CustomResourceOptions;
var deleteBeforeReplace = customOpts?.DeleteBeforeReplace;
var deletedWith = "";
if (options.DeletedWith != null) {
deletedWith = await options.DeletedWith.Urn.GetValueAsync("").ConfigureAwait(false);
}

var request = new RegisterResourceRequest
{
Expand All @@ -91,6 +99,7 @@ private static void PopulateRequest(RegisterResourceRequest request, PrepareResu
},
Remote = remote,
RetainOnDelete = options.RetainOnDelete ?? false,
DeletedWith = deletedWith,
};

if (customOpts != null)
Expand Down
2 changes: 2 additions & 0 deletions sdk/Pulumi/PublicAPI.Shipped.txt
Expand Up @@ -200,6 +200,8 @@ Pulumi.ResourceOptions.PluginDownloadURL.get -> string
Pulumi.ResourceOptions.PluginDownloadURL.set -> void
Pulumi.ResourceOptions.RetainOnDelete.get -> bool?
Pulumi.ResourceOptions.RetainOnDelete.set -> void
Pulumi.ResourceOptions.DeletedWith.get -> Pulumi.Resource
Pulumi.ResourceOptions.DeletedWith.set -> void
Pulumi.ResourceTransformation
Pulumi.ResourceTransformationArgs
Pulumi.ResourceTransformationArgs.Args.get -> Pulumi.ResourceArgs
Expand Down
6 changes: 6 additions & 0 deletions sdk/Pulumi/Resources/ResourceOptions.cs
Expand Up @@ -123,5 +123,11 @@ public List<string> ReplaceOnChanges
/// If set to True, the providers Delete method will not be called for this resource.
/// </summary>
public bool? RetainOnDelete { get; set; }

/// <summary>
/// If set, the providers Delete method will not be called for this resource
/// if specified resource is being deleted as well.
/// </summary>
public Resource? DeletedWith { get; set; }
}
}
3 changes: 2 additions & 1 deletion sdk/Pulumi/Resources/ResourceOptions_Copy.cs
Expand Up @@ -23,7 +23,8 @@ internal static TResourceOptions CreateCopy<TResourceOptions>(ResourceOptions op
Urn = options.Urn,
Version = options.Version,
PluginDownloadURL = options.PluginDownloadURL,
RetainOnDelete = options.RetainOnDelete
RetainOnDelete = options.RetainOnDelete,
DeletedWith = options.DeletedWith
};

internal static CustomResourceOptions CreateCustomResourceOptionsCopy(ResourceOptions? options)
Expand Down
1 change: 1 addition & 0 deletions sdk/Pulumi/Resources/ResourceOptions_Merge.cs
Expand Up @@ -15,6 +15,7 @@ internal static void MergeNormalOptions(ResourceOptions options1, ResourceOption
options1.Provider = options2.Provider ?? options1.Provider;
options1.CustomTimeouts = options2.CustomTimeouts ?? options1.CustomTimeouts;
options1.RetainOnDelete = options2.RetainOnDelete ?? options1.RetainOnDelete;
options1.DeletedWith = options2.DeletedWith ?? options1.DeletedWith;

options1.IgnoreChanges.AddRange(options2.IgnoreChanges);
options1.ResourceTransformations.AddRange(options2.ResourceTransformations);
Expand Down

0 comments on commit 3e12811

Please sign in to comment.