/******************************************************************************* * 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 { /// /// Creates a stack as specified in the template. After the call completes successfully, /// the stack creation starts. You can check the status of the stack through the DescribeStacks /// operation. /// [Cmdlet("New", "CFNStack", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)] [OutputType("System.String")] [AWSCmdlet("Calls the AWS CloudFormation CreateStack API operation.", Operation = new[] {"CreateStack"}, SelectReturnType = typeof(Amazon.CloudFormation.Model.CreateStackResponse))] [AWSCmdletOutput("System.String or Amazon.CloudFormation.Model.CreateStackResponse", "This cmdlet returns a System.String object.", "The service call response (type Amazon.CloudFormation.Model.CreateStackResponse) can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack." )] public partial class NewCFNStackCmdlet : AmazonCloudFormationClientCmdlet, IExecutor { #region Parameter Capability /// /// /// In some cases, you must explicitly acknowledge that your stack template contains certain /// capabilities in order for CloudFormation to create the stack. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [Alias("Capabilities")] public System.String[] Capability { get; set; } #endregion #region Parameter ClientRequestToken /// /// /// A unique identifier for this CreateStack request. Specify this token /// if you plan to retry requests so that CloudFormation knows that you're not attempting /// to create a stack with the same name. You might retry CreateStack requests /// to ensure that CloudFormation successfully received them.All events initiated by a given stack operation are assigned the same client request /// token, which you can use to track operations. For example, if you execute a CreateStack /// operation with the token token1, then all the StackEvents /// generated by that operation will have ClientRequestToken set as token1.In the console, stack operations display the client request token on the Events tab. /// Stack operations that are initiated from the console use the token format Console-StackOperation-ID, /// which helps you easily identify the stack operation . For example, if you create a /// stack using the console, each stack event would be assigned the same token in the /// following format: Console-CreateStack-7f59c3cf-00d2-40c7-b2ff-e75db0987002. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public System.String ClientRequestToken { get; set; } #endregion #region Parameter DisableRollback /// /// /// Set to true to disable rollback of the stack if stack creation failed. /// You can specify either DisableRollback or OnFailure, but /// not both.Default: false /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public System.Boolean? DisableRollback { get; set; } #endregion #region Parameter EnableTerminationProtection /// /// /// Whether to enable termination protection on the specified stack. If a user attempts /// to delete a stack with termination protection enabled, the operation fails and the /// stack remains unchanged. For more information, see Protecting /// a Stack From Being Deleted in the CloudFormation User Guide. Termination /// protection is deactivated on stacks by default.For nested /// stacks, termination protection is set on the root stack and can't be changed directly /// on the nested stack. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public System.Boolean? EnableTerminationProtection { get; set; } #endregion #region Parameter RollbackConfiguration_MonitoringTimeInMinute /// /// /// The amount of time, in minutes, during which CloudFormation should monitor all the /// rollback triggers after the stack creation or update operation deploys all necessary /// resources.The default is 0 minutes.If you specify a monitoring period but don't specify any rollback triggers, CloudFormation /// still waits the specified period of time before cleaning up old resources after update /// operations. You can use this monitoring period to perform any manual stack validation /// desired, and manually cancel the stack creation or update (using CancelUpdateStack, /// for example) as necessary.If you specify 0 for this parameter, CloudFormation still monitors the specified rollback /// triggers during stack creation and update operations. Then, for update operations, /// it begins disposing of old resources immediately once the operation completes. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [Alias("RollbackConfiguration_MonitoringTimeInMinutes")] public System.Int32? RollbackConfiguration_MonitoringTimeInMinute { get; set; } #endregion #region Parameter NotificationARNs /// /// /// The Amazon Simple Notification Service (Amazon SNS) topic ARNs to publish stack related /// events. You can find your Amazon SNS topic ARNs using the Amazon SNS console or your /// Command Line Interface (CLI). /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public System.String[] NotificationARNs { get; set; } #endregion #region Parameter OnFailure /// /// /// Determines what action will be taken if stack creation fails. This must be one of: /// DO_NOTHING, ROLLBACK, or DELETE. You can specify /// either OnFailure or DisableRollback, but not both.Default: ROLLBACK /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [AWSConstantClassSource("Amazon.CloudFormation.OnFailure")] public Amazon.CloudFormation.OnFailure OnFailure { get; set; } #endregion #region Parameter Parameter /// /// /// A list of Parameter structures that specify input parameters for the /// stack. For more information, see the Parameter /// data type. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [Alias("Parameters")] public Amazon.CloudFormation.Model.Parameter[] Parameter { get; set; } #endregion #region Parameter ResourceType /// /// /// The template resource types that you have permissions to work with for this create /// stack action, such as AWS::EC2::Instance, AWS::EC2::*, or /// Custom::MyCustomInstance. Use the following syntax to describe template /// resource types: AWS::* (for all Amazon Web Services resources), Custom::* /// (for all custom resources), Custom::logical_ID (for a specific /// custom resource), AWS::service_name::* (for all resources of a /// particular Amazon Web Services service), and AWS::service_name::resource_logical_ID (for a specific Amazon Web Services resource).If the list of resource types doesn't include a resource that you're creating, the /// stack creation fails. By default, CloudFormation grants permissions to all resource /// types. Identity and Access Management (IAM) uses this parameter for CloudFormation-specific /// condition keys in IAM policies. For more information, see Controlling /// Access with Identity and Access Management. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [Alias("ResourceTypes")] public System.String[] ResourceType { get; set; } #endregion #region Parameter RetainExceptOnCreate /// /// /// This deletion policy deletes newly created resources, but retains existing resources, /// when a stack operation is rolled back. This ensures new, empty, and unused resources /// are deleted, while critical resources and their data are retained. RetainExceptOnCreate /// can be specified for any resource that supports the /// DeletionPolicy attribute. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public System.Boolean? RetainExceptOnCreate { get; set; } #endregion #region Parameter RoleARN /// /// /// The Amazon Resource Name (ARN) of an Identity and Access Management (IAM) role that /// CloudFormation assumes to create the stack. CloudFormation uses the role's credentials /// to make calls on your behalf. CloudFormation always uses this role for all future /// operations on the stack. Provided that users have permission to operate on the stack, /// CloudFormation uses this role even if the users don't have permission to pass it. /// Ensure that the role grants least privilege.If you don't specify a value, CloudFormation uses the role that was previously associated /// with the stack. If no role is available, CloudFormation uses a temporary session that's /// generated from your user credentials. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public System.String RoleARN { get; set; } #endregion #region Parameter RollbackConfiguration_RollbackTrigger /// /// /// The triggers to monitor during stack creation or update actions.By default, CloudFormation saves the rollback triggers specified for a stack and applies /// them to any subsequent update operations for the stack, unless you specify otherwise. /// If you do specify rollback triggers for this parameter, those triggers replace any /// list of triggers previously specified for the stack. This means:If a specified trigger is missing, the entire stack operation fails and is rolled /// back. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [Alias("RollbackConfiguration_RollbackTriggers")] public Amazon.CloudFormation.Model.RollbackTrigger[] RollbackConfiguration_RollbackTrigger { get; set; } #endregion #region Parameter StackName /// /// /// The name that's associated with the stack. The name must be unique in the Region in /// which you are creating the stack.A stack name can contain only alphanumeric characters (case sensitive) and hyphens. /// It must start with an alphabetical character and can't be longer than 128 characters. /// /// #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 StackName { get; set; } #endregion #region Parameter StackPolicyBody /// /// /// Structure containing the stack policy body. For more information, go to /// Prevent Updates to Stack Resources in the CloudFormation User Guide. You /// can specify either the StackPolicyBody or the StackPolicyURL /// parameter, but not both. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public System.String StackPolicyBody { get; set; } #endregion #region Parameter StackPolicyURL /// /// /// Location of a file containing the stack policy. The URL must point to a policy (maximum /// size: 16 KB) located in an S3 bucket in the same Region as the stack. You can specify /// either the StackPolicyBody or the StackPolicyURL parameter, /// but not both. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public System.String StackPolicyURL { get; set; } #endregion #region Parameter Tag /// /// /// Key-value pairs to associate with this stack. CloudFormation also propagates these /// tags to the resources created in the stack. A maximum number of 50 tags can be specified. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [Alias("Tags")] public Amazon.CloudFormation.Model.Tag[] Tag { get; set; } #endregion #region Parameter TemplateBody /// /// /// Structure containing the template body with a minimum length of 1 byte and a maximum /// length of 51,200 bytes. For more information, go to Template /// anatomy in the CloudFormation User Guide.Conditional: You must specify either the TemplateBody or the TemplateURL /// parameter, but not both. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public System.String TemplateBody { get; set; } #endregion #region Parameter TemplateURL /// /// /// Location of file containing the template body. The URL must point to a template (max /// size: 460,800 bytes) that's located in an Amazon S3 bucket or a Systems Manager document. /// For more information, go to the Template /// anatomy in the CloudFormation User Guide.Conditional: You must specify either the TemplateBody or the TemplateURL /// parameter, but not both. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public System.String TemplateURL { get; set; } #endregion #region Parameter TimeoutInMinutes /// /// /// The amount of time that can pass before the stack status becomes CREATE_FAILED; if /// DisableRollback is not set or is set to false, the stack /// will be rolled back. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public System.Int32? TimeoutInMinutes { get; set; } #endregion #region Parameter Select /// /// Use the -Select parameter to control the cmdlet output. The default value is 'StackId'. /// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.CloudFormation.Model.CreateStackResponse). /// Specifying the name of a property of type Amazon.CloudFormation.Model.CreateStackResponse 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; } = "StackId"; #endregion #region Parameter PassThru /// /// Changes the cmdlet behavior to return the value passed to the StackName parameter. /// The -PassThru parameter is deprecated, use -Select '^StackName' instead. This parameter will be removed in a future version. /// [System.Obsolete("The -PassThru parameter is deprecated, use -Select '^StackName' 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.StackName), MyInvocation.BoundParameters); if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "New-CFNStack (CreateStack)")) { 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.StackName; } #pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute if (this.Capability != null) { context.Capability = new List(this.Capability); } context.ClientRequestToken = this.ClientRequestToken; context.DisableRollback = this.DisableRollback; context.EnableTerminationProtection = this.EnableTerminationProtection; if (this.NotificationARNs != null) { context.NotificationARNs = new List(this.NotificationARNs); } context.OnFailure = this.OnFailure; if (this.Parameter != null) { context.Parameter = new List(this.Parameter); } if (this.ResourceType != null) { context.ResourceType = new List(this.ResourceType); } context.RetainExceptOnCreate = this.RetainExceptOnCreate; context.RoleARN = this.RoleARN; context.RollbackConfiguration_MonitoringTimeInMinute = this.RollbackConfiguration_MonitoringTimeInMinute; if (this.RollbackConfiguration_RollbackTrigger != null) { context.RollbackConfiguration_RollbackTrigger = new List(this.RollbackConfiguration_RollbackTrigger); } context.StackName = this.StackName; #if MODULAR if (this.StackName == null && ParameterWasBound(nameof(this.StackName))) { WriteWarning("You are passing $null as a value for parameter StackName 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.StackPolicyBody = this.StackPolicyBody; context.StackPolicyURL = this.StackPolicyURL; if (this.Tag != null) { context.Tag = new List(this.Tag); } context.TemplateBody = this.TemplateBody; context.TemplateURL = this.TemplateURL; context.TimeoutInMinutes = this.TimeoutInMinutes; // 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.CreateStackRequest(); if (cmdletContext.Capability != null) { request.Capabilities = cmdletContext.Capability; } if (cmdletContext.ClientRequestToken != null) { request.ClientRequestToken = cmdletContext.ClientRequestToken; } if (cmdletContext.DisableRollback != null) { request.DisableRollback = cmdletContext.DisableRollback.Value; } if (cmdletContext.EnableTerminationProtection != null) { request.EnableTerminationProtection = cmdletContext.EnableTerminationProtection.Value; } if (cmdletContext.NotificationARNs != null) { request.NotificationARNs = cmdletContext.NotificationARNs; } if (cmdletContext.OnFailure != null) { request.OnFailure = cmdletContext.OnFailure; } if (cmdletContext.Parameter != null) { request.Parameters = cmdletContext.Parameter; } if (cmdletContext.ResourceType != null) { request.ResourceTypes = cmdletContext.ResourceType; } if (cmdletContext.RetainExceptOnCreate != null) { request.RetainExceptOnCreate = cmdletContext.RetainExceptOnCreate.Value; } if (cmdletContext.RoleARN != null) { request.RoleARN = cmdletContext.RoleARN; } // populate RollbackConfiguration var requestRollbackConfigurationIsNull = true; request.RollbackConfiguration = new Amazon.CloudFormation.Model.RollbackConfiguration(); System.Int32? requestRollbackConfiguration_rollbackConfiguration_MonitoringTimeInMinute = null; if (cmdletContext.RollbackConfiguration_MonitoringTimeInMinute != null) { requestRollbackConfiguration_rollbackConfiguration_MonitoringTimeInMinute = cmdletContext.RollbackConfiguration_MonitoringTimeInMinute.Value; } if (requestRollbackConfiguration_rollbackConfiguration_MonitoringTimeInMinute != null) { request.RollbackConfiguration.MonitoringTimeInMinutes = requestRollbackConfiguration_rollbackConfiguration_MonitoringTimeInMinute.Value; requestRollbackConfigurationIsNull = false; } List requestRollbackConfiguration_rollbackConfiguration_RollbackTrigger = null; if (cmdletContext.RollbackConfiguration_RollbackTrigger != null) { requestRollbackConfiguration_rollbackConfiguration_RollbackTrigger = cmdletContext.RollbackConfiguration_RollbackTrigger; } if (requestRollbackConfiguration_rollbackConfiguration_RollbackTrigger != null) { request.RollbackConfiguration.RollbackTriggers = requestRollbackConfiguration_rollbackConfiguration_RollbackTrigger; requestRollbackConfigurationIsNull = false; } // determine if request.RollbackConfiguration should be set to null if (requestRollbackConfigurationIsNull) { request.RollbackConfiguration = null; } if (cmdletContext.StackName != null) { request.StackName = cmdletContext.StackName; } if (cmdletContext.StackPolicyBody != null) { request.StackPolicyBody = cmdletContext.StackPolicyBody; } if (cmdletContext.StackPolicyURL != null) { request.StackPolicyURL = cmdletContext.StackPolicyURL; } if (cmdletContext.Tag != null) { request.Tags = cmdletContext.Tag; } if (cmdletContext.TemplateBody != null) { request.TemplateBody = cmdletContext.TemplateBody; } if (cmdletContext.TemplateURL != null) { request.TemplateURL = cmdletContext.TemplateURL; } if (cmdletContext.TimeoutInMinutes != null) { request.TimeoutInMinutes = cmdletContext.TimeoutInMinutes.Value; } 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.CreateStackResponse CallAWSServiceOperation(IAmazonCloudFormation client, Amazon.CloudFormation.Model.CreateStackRequest request) { Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "AWS CloudFormation", "CreateStack"); try { #if DESKTOP return client.CreateStack(request); #elif CORECLR return client.CreateStackAsync(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 Capability { get; set; } public System.String ClientRequestToken { get; set; } public System.Boolean? DisableRollback { get; set; } public System.Boolean? EnableTerminationProtection { get; set; } public List NotificationARNs { get; set; } public Amazon.CloudFormation.OnFailure OnFailure { get; set; } public List Parameter { get; set; } public List ResourceType { get; set; } public System.Boolean? RetainExceptOnCreate { get; set; } public System.String RoleARN { get; set; } public System.Int32? RollbackConfiguration_MonitoringTimeInMinute { get; set; } public List RollbackConfiguration_RollbackTrigger { get; set; } public System.String StackName { get; set; } public System.String StackPolicyBody { get; set; } public System.String StackPolicyURL { get; set; } public List Tag { get; set; } public System.String TemplateBody { get; set; } public System.String TemplateURL { get; set; } public System.Int32? TimeoutInMinutes { get; set; } public System.Func Select { get; set; } = (response, cmdlet) => response.StackId; } } }