/*******************************************************************************
* Copyright 2012-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file.
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* *****************************************************************************
*
* AWS Tools for Windows (TM) PowerShell (TM)
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using Amazon.PowerShell.Common;
using Amazon.Runtime;
using Amazon.CloudFormation;
using Amazon.CloudFormation.Model;
namespace Amazon.PowerShell.Cmdlets.CFN
{
///
/// Deletes stack instances for the specified accounts, in the specified Amazon Web Services
/// Regions.
///
[Cmdlet("Remove", "CFNStackInstance", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.High)]
[OutputType("System.String")]
[AWSCmdlet("Calls the AWS CloudFormation DeleteStackInstances API operation.", Operation = new[] {"DeleteStackInstances"}, SelectReturnType = typeof(Amazon.CloudFormation.Model.DeleteStackInstancesResponse))]
[AWSCmdletOutput("System.String or Amazon.CloudFormation.Model.DeleteStackInstancesResponse",
"This cmdlet returns a System.String object.",
"The service call response (type Amazon.CloudFormation.Model.DeleteStackInstancesResponse) can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class RemoveCFNStackInstanceCmdlet : AmazonCloudFormationClientCmdlet, IExecutor
{
#region Parameter DeploymentTargets_AccountFilterType
///
///
/// Limit deployment targets to individual accounts or include additional accounts with
/// provided OUs.The following is a list of possible values for the AccountFilterType
/// operation.INTERSECTION
: StackSets deploys to the accounts specified in Accounts
/// parameter. DIFFERENCE
: StackSets excludes the accounts specified in Accounts
/// parameter. This enables user to avoid certain accounts within an OU such as suspended
/// accounts.UNION
: StackSets includes additional accounts deployment targets. This is the default value if AccountFilterType
is not provided. This
/// enables user to update an entire OU and individual accounts from a different OU in
/// one request, which used to be two separate requests.NONE
: Deploys to all the accounts in specified organizational units
/// (OU).
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.CloudFormation.AccountFilterType")]
public Amazon.CloudFormation.AccountFilterType DeploymentTargets_AccountFilterType { get; set; }
#endregion
#region Parameter Account
///
///
/// [Self-managed permissions] The names of the Amazon Web Services accounts that you
/// want to delete stack instances for.You can specify Accounts
or DeploymentTargets
, but not both.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Accounts")]
public System.String[] Account { get; set; }
#endregion
#region Parameter DeploymentTargets_Account
///
///
/// The names of one or more Amazon Web Services accounts for which you want to deploy
/// stack set updates.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("DeploymentTargets_Accounts")]
public System.String[] DeploymentTargets_Account { get; set; }
#endregion
#region Parameter DeploymentTargets_AccountsUrl
///
///
/// Returns the value of the AccountsUrl
property.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String DeploymentTargets_AccountsUrl { get; set; }
#endregion
#region Parameter CallAs
///
///
/// [Service-managed permissions] Specifies whether you are acting as an account administrator
/// in the organization's management account or as a delegated administrator in a member
/// account.By default, SELF
is specified. Use SELF
for stack sets with
/// self-managed permissions.- If you are signed in to the management account, specify
SELF
. - If you are signed in to a delegated administrator account, specify
DELEGATED_ADMIN
.Your Amazon Web Services account must be registered as a delegated administrator in
/// the management account. For more information, see Register
/// a delegated administrator in the CloudFormation User Guide.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.CloudFormation.CallAs")]
public Amazon.CloudFormation.CallAs CallAs { get; set; }
#endregion
#region Parameter OperationId
///
///
/// The unique identifier for this stack set operation.If you don't specify an operation ID, the SDK generates one automatically.The operation ID also functions as an idempotency token, to ensure that CloudFormation
/// performs the stack set operation only once, even if you retry the request multiple
/// times. You can retry stack set operation requests to ensure that CloudFormation successfully
/// received them.Repeating this stack set operation with a new operation ID retries all stack instances
/// whose status is OUTDATED
.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String OperationId { get; set; }
#endregion
#region Parameter OperationPreference
///
///
/// Preferences for how CloudFormation performs this stack set operation.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("OperationPreferences")]
public Amazon.CloudFormation.Model.StackSetOperationPreferences OperationPreference { get; set; }
#endregion
#region Parameter DeploymentTargets_OrganizationalUnitId
///
///
/// The organization root ID or organizational unit (OU) IDs to which StackSets deploys.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("DeploymentTargets_OrganizationalUnitIds")]
public System.String[] DeploymentTargets_OrganizationalUnitId { get; set; }
#endregion
#region Parameter StackInstanceRegion
///
///
/// The Amazon Web Services Regions where you want to delete stack set instances.
///
///
#if !MODULAR
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
#else
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true)]
[System.Management.Automation.AllowEmptyCollection]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
public System.String[] StackInstanceRegion { get; set; }
#endregion
#region Parameter RetainStack
///
///
/// Removes the stack instances from the specified stack set, but doesn't delete the stacks.
/// You can't reassociate a retained stack or add an existing, saved stack to a new stack
/// set.For more information, see Stack
/// set operation options.
///
///
#if !MODULAR
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
#else
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true)]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
[Alias("RetainStacks")]
public System.Boolean? RetainStack { get; set; }
#endregion
#region Parameter StackSetName
///
///
/// The name or unique ID of the stack set that you want to delete stack instances for.
///
///
#if !MODULAR
[System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true)]
#else
[System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true, Mandatory = true)]
[System.Management.Automation.AllowEmptyString]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
public System.String StackSetName { get; set; }
#endregion
#region Parameter Select
///
/// Use the -Select parameter to control the cmdlet output. The default value is 'OperationId'.
/// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.CloudFormation.Model.DeleteStackInstancesResponse).
/// Specifying the name of a property of type Amazon.CloudFormation.Model.DeleteStackInstancesResponse will result in that property being returned.
/// Specifying -Select '^ParameterName' will result in the cmdlet returning the selected cmdlet parameter value.
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public string Select { get; set; } = "OperationId";
#endregion
#region Parameter PassThru
///
/// Changes the cmdlet behavior to return the value passed to the StackSetName parameter.
/// The -PassThru parameter is deprecated, use -Select '^StackSetName' instead. This parameter will be removed in a future version.
///
[System.Obsolete("The -PassThru parameter is deprecated, use -Select '^StackSetName' instead. This parameter will be removed in a future version.")]
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter PassThru { get; set; }
#endregion
#region Parameter Force
///
/// This parameter overrides confirmation prompts to force
/// the cmdlet to continue its operation. This parameter should always
/// be used with caution.
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter Force { get; set; }
#endregion
protected override void ProcessRecord()
{
this._AWSSignerType = "v4";
base.ProcessRecord();
var resourceIdentifiersText = FormatParameterValuesForConfirmationMsg(nameof(this.StackSetName), MyInvocation.BoundParameters);
if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "Remove-CFNStackInstance (DeleteStackInstances)"))
{
return;
}
var context = new CmdletContext();
// allow for manipulation of parameters prior to loading into context
PreExecutionContextLoad(context);
#pragma warning disable CS0618, CS0612 //A class member was marked with the Obsolete attribute
if (ParameterWasBound(nameof(this.Select)))
{
context.Select = CreateSelectDelegate(Select) ??
throw new System.ArgumentException("Invalid value for -Select parameter.", nameof(this.Select));
if (this.PassThru.IsPresent)
{
throw new System.ArgumentException("-PassThru cannot be used when -Select is specified.", nameof(this.Select));
}
}
else if (this.PassThru.IsPresent)
{
context.Select = (response, cmdlet) => this.StackSetName;
}
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
if (this.Account != null)
{
context.Account = new List(this.Account);
}
context.CallAs = this.CallAs;
context.DeploymentTargets_AccountFilterType = this.DeploymentTargets_AccountFilterType;
if (this.DeploymentTargets_Account != null)
{
context.DeploymentTargets_Account = new List(this.DeploymentTargets_Account);
}
context.DeploymentTargets_AccountsUrl = this.DeploymentTargets_AccountsUrl;
if (this.DeploymentTargets_OrganizationalUnitId != null)
{
context.DeploymentTargets_OrganizationalUnitId = new List(this.DeploymentTargets_OrganizationalUnitId);
}
context.OperationId = this.OperationId;
context.OperationPreference = this.OperationPreference;
if (this.StackInstanceRegion != null)
{
context.StackInstanceRegion = new List(this.StackInstanceRegion);
}
#if MODULAR
if (this.StackInstanceRegion == null && ParameterWasBound(nameof(this.StackInstanceRegion)))
{
WriteWarning("You are passing $null as a value for parameter StackInstanceRegion which is marked as required. In case you believe this parameter was incorrectly marked as required, report this by opening an issue at https://github.com/aws/aws-tools-for-powershell/issues.");
}
#endif
context.RetainStack = this.RetainStack;
#if MODULAR
if (this.RetainStack == null && ParameterWasBound(nameof(this.RetainStack)))
{
WriteWarning("You are passing $null as a value for parameter RetainStack which is marked as required. In case you believe this parameter was incorrectly marked as required, report this by opening an issue at https://github.com/aws/aws-tools-for-powershell/issues.");
}
#endif
context.StackSetName = this.StackSetName;
#if MODULAR
if (this.StackSetName == null && ParameterWasBound(nameof(this.StackSetName)))
{
WriteWarning("You are passing $null as a value for parameter StackSetName which is marked as required. In case you believe this parameter was incorrectly marked as required, report this by opening an issue at https://github.com/aws/aws-tools-for-powershell/issues.");
}
#endif
// allow further manipulation of loaded context prior to processing
PostExecutionContextLoad(context);
var output = Execute(context) as CmdletOutput;
ProcessOutput(output);
}
#region IExecutor Members
public object Execute(ExecutorContext context)
{
var cmdletContext = context as CmdletContext;
// create request
var request = new Amazon.CloudFormation.Model.DeleteStackInstancesRequest();
if (cmdletContext.Account != null)
{
request.Accounts = cmdletContext.Account;
}
if (cmdletContext.CallAs != null)
{
request.CallAs = cmdletContext.CallAs;
}
// populate DeploymentTargets
var requestDeploymentTargetsIsNull = true;
request.DeploymentTargets = new Amazon.CloudFormation.Model.DeploymentTargets();
Amazon.CloudFormation.AccountFilterType requestDeploymentTargets_deploymentTargets_AccountFilterType = null;
if (cmdletContext.DeploymentTargets_AccountFilterType != null)
{
requestDeploymentTargets_deploymentTargets_AccountFilterType = cmdletContext.DeploymentTargets_AccountFilterType;
}
if (requestDeploymentTargets_deploymentTargets_AccountFilterType != null)
{
request.DeploymentTargets.AccountFilterType = requestDeploymentTargets_deploymentTargets_AccountFilterType;
requestDeploymentTargetsIsNull = false;
}
List requestDeploymentTargets_deploymentTargets_Account = null;
if (cmdletContext.DeploymentTargets_Account != null)
{
requestDeploymentTargets_deploymentTargets_Account = cmdletContext.DeploymentTargets_Account;
}
if (requestDeploymentTargets_deploymentTargets_Account != null)
{
request.DeploymentTargets.Accounts = requestDeploymentTargets_deploymentTargets_Account;
requestDeploymentTargetsIsNull = false;
}
System.String requestDeploymentTargets_deploymentTargets_AccountsUrl = null;
if (cmdletContext.DeploymentTargets_AccountsUrl != null)
{
requestDeploymentTargets_deploymentTargets_AccountsUrl = cmdletContext.DeploymentTargets_AccountsUrl;
}
if (requestDeploymentTargets_deploymentTargets_AccountsUrl != null)
{
request.DeploymentTargets.AccountsUrl = requestDeploymentTargets_deploymentTargets_AccountsUrl;
requestDeploymentTargetsIsNull = false;
}
List requestDeploymentTargets_deploymentTargets_OrganizationalUnitId = null;
if (cmdletContext.DeploymentTargets_OrganizationalUnitId != null)
{
requestDeploymentTargets_deploymentTargets_OrganizationalUnitId = cmdletContext.DeploymentTargets_OrganizationalUnitId;
}
if (requestDeploymentTargets_deploymentTargets_OrganizationalUnitId != null)
{
request.DeploymentTargets.OrganizationalUnitIds = requestDeploymentTargets_deploymentTargets_OrganizationalUnitId;
requestDeploymentTargetsIsNull = false;
}
// determine if request.DeploymentTargets should be set to null
if (requestDeploymentTargetsIsNull)
{
request.DeploymentTargets = null;
}
if (cmdletContext.OperationId != null)
{
request.OperationId = cmdletContext.OperationId;
}
if (cmdletContext.OperationPreference != null)
{
request.OperationPreferences = cmdletContext.OperationPreference;
}
if (cmdletContext.StackInstanceRegion != null)
{
request.Regions = cmdletContext.StackInstanceRegion;
}
if (cmdletContext.RetainStack != null)
{
request.RetainStacks = cmdletContext.RetainStack.Value;
}
if (cmdletContext.StackSetName != null)
{
request.StackSetName = cmdletContext.StackSetName;
}
CmdletOutput output;
// issue call
var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint);
try
{
var response = CallAWSServiceOperation(client, request);
object pipelineOutput = null;
pipelineOutput = cmdletContext.Select(response, this);
output = new CmdletOutput
{
PipelineOutput = pipelineOutput,
ServiceResponse = response
};
}
catch (Exception e)
{
output = new CmdletOutput { ErrorResponse = e };
}
return output;
}
public ExecutorContext CreateContext()
{
return new CmdletContext();
}
#endregion
#region AWS Service Operation Call
private Amazon.CloudFormation.Model.DeleteStackInstancesResponse CallAWSServiceOperation(IAmazonCloudFormation client, Amazon.CloudFormation.Model.DeleteStackInstancesRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "AWS CloudFormation", "DeleteStackInstances");
try
{
#if DESKTOP
return client.DeleteStackInstances(request);
#elif CORECLR
return client.DeleteStackInstancesAsync(request).GetAwaiter().GetResult();
#else
#error "Unknown build edition"
#endif
}
catch (AmazonServiceException exc)
{
var webException = exc.InnerException as System.Net.WebException;
if (webException != null)
{
throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException);
}
throw;
}
}
#endregion
internal partial class CmdletContext : ExecutorContext
{
public List Account { get; set; }
public Amazon.CloudFormation.CallAs CallAs { get; set; }
public Amazon.CloudFormation.AccountFilterType DeploymentTargets_AccountFilterType { get; set; }
public List DeploymentTargets_Account { get; set; }
public System.String DeploymentTargets_AccountsUrl { get; set; }
public List DeploymentTargets_OrganizationalUnitId { get; set; }
public System.String OperationId { get; set; }
public Amazon.CloudFormation.Model.StackSetOperationPreferences OperationPreference { get; set; }
public List StackInstanceRegion { get; set; }
public System.Boolean? RetainStack { get; set; }
public System.String StackSetName { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response.OperationId;
}
}
}