pulumi/sdk/dotnet/Pulumi.Automation/WorkspaceStackState.cs
Josh Studt caf5fcd525
[auto/dotnet] - pulumi state delete & unprotect functionality (#8202)
Co-authored-by: Komal <komal@pulumi.com>
2021-10-18 16:26:45 -07:00

69 lines
2.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Pulumi.Automation
{
/// <summary>
/// Module class for manipulating stack state for a given <see cref="WorkspaceStack"/>.
/// </summary>
public sealed class WorkspaceStackState
{
private readonly WorkspaceStack _workspaceStack;
internal WorkspaceStackState(WorkspaceStack workspaceStack)
{
this._workspaceStack = workspaceStack;
}
/// <summary>
/// This command deletes a resource from a stacks state, as long as it is safe to do so.
/// The resource is specified by its Pulumi URN.
/// <para/>
/// Resources cant be deleted if there exist other resources that depend on it or are parented to it.
/// Protected resources will not be deleted unless it is specifically requested using the <paramref name="force"/> flag.
/// </summary>
/// <param name="urn">The Pulumi URN of the resource to be deleted.</param>
/// <param name="force">A boolean indicating whether the deletion should be forced.</param>
/// <param name="cancellationToken">A cancellation token.</param>
public Task DeleteAsync(string urn, bool force = false, CancellationToken cancellationToken = default)
{
var args = new List<string>()
{
"state",
"delete",
urn,
};
if (force)
args.Add("--force");
return this._workspaceStack.RunCommandAsync(args, null, null, null, cancellationToken);
}
/// <summary>
/// Unprotect a resource in a stack's state.
/// This command clears the protect bit on the provided resource <paramref name="urn"/>, allowing the resource to be deleted.
/// </summary>
/// <param name="urn">The Pulumi URN to be unprotected.</param>
/// <param name="cancellationToken">A cancellation token.</param>
public Task UnprotectAsync(string urn, CancellationToken cancellationToken = default)
{
var args = new string[] { "state", "unprotect", urn }.ToList();
return this._workspaceStack.RunCommandAsync(args, null, null, null, cancellationToken);
}
/// <summary>
/// Unprotect all resources in a stack's state.
/// This command clears the protect bit on all resources in the stack, allowing those resources to be deleted.
/// </summary>
/// <param name="cancellationToken">A cancellation token.</param>
public Task UnprotectAllAsync(CancellationToken cancellationToken = default)
{
var args = new string[] { "state", "unprotect", "--all", }.ToList();
return this._workspaceStack.RunCommandAsync(args, null, null, null, cancellationToken);
}
}
}