/******************************************************************************* * 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.EC2; using Amazon.EC2.Model; namespace Amazon.PowerShell.Cmdlets.EC2 { /// /// Allocates a Dedicated Host to your account. At a minimum, specify the supported instance /// type or instance family, the Availability Zone in which to allocate the host, and /// the number of hosts to allocate. /// [Cmdlet("New", "EC2Host", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)] [OutputType("System.String")] [AWSCmdlet("Calls the Amazon Elastic Compute Cloud (EC2) AllocateHosts API operation.", Operation = new[] {"AllocateHosts"}, SelectReturnType = typeof(Amazon.EC2.Model.AllocateHostsResponse), LegacyAlias="New-EC2Hosts")] [AWSCmdletOutput("System.String or Amazon.EC2.Model.AllocateHostsResponse", "This cmdlet returns a collection of System.String objects.", "The service call response (type Amazon.EC2.Model.AllocateHostsResponse) can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack." )] public partial class NewEC2HostCmdlet : AmazonEC2ClientCmdlet, IExecutor { #region Parameter AssetId /// /// /// The IDs of the Outpost hardware assets on which to allocate the Dedicated Hosts. Targeting /// specific hardware assets on an Outpost can help to minimize latency between your workloads. /// This parameter is supported only if you specify OutpostArn. If you are allocating /// the Dedicated Hosts in a Region, omit this parameter. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [Alias("AssetIds")] public System.String[] AssetId { get; set; } #endregion #region Parameter AutoPlacement /// /// /// Indicates whether the host accepts any untargeted instance launches that match its /// instance type configuration, or if it only accepts Host tenancy instance launches /// that specify its unique host ID. For more information, see /// Understanding auto-placement and affinity in the Amazon EC2 User Guide.Default: on /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [AWSConstantClassSource("Amazon.EC2.AutoPlacement")] public Amazon.EC2.AutoPlacement AutoPlacement { get; set; } #endregion #region Parameter AvailabilityZone /// /// /// The Availability Zone in which to allocate the Dedicated Host. /// /// #if !MODULAR [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] #else [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true)] [System.Management.Automation.AllowEmptyString] [System.Management.Automation.AllowNull] #endif [Amazon.PowerShell.Common.AWSRequiredParameter] public System.String AvailabilityZone { get; set; } #endregion #region Parameter HostMaintenance /// /// /// Indicates whether to enable or disable host maintenance for the Dedicated Host. For /// more information, see Host /// maintenance in the Amazon EC2 User Guide. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [AWSConstantClassSource("Amazon.EC2.HostMaintenance")] public Amazon.EC2.HostMaintenance HostMaintenance { get; set; } #endregion #region Parameter HostRecovery /// /// /// Indicates whether to enable or disable host recovery for the Dedicated Host. Host /// recovery is disabled by default. For more information, see /// Host recovery in the Amazon EC2 User Guide.Default: off /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [AWSConstantClassSource("Amazon.EC2.HostRecovery")] public Amazon.EC2.HostRecovery HostRecovery { get; set; } #endregion #region Parameter InstanceFamily /// /// /// Specifies the instance family to be supported by the Dedicated Hosts. If you specify /// an instance family, the Dedicated Hosts support multiple instance types within that /// instance family.If you want the Dedicated Hosts to support a specific instance type only, omit this /// parameter and specify InstanceType instead. You cannot specify InstanceFamily /// and InstanceType in the same request. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public System.String InstanceFamily { get; set; } #endregion #region Parameter InstanceType /// /// /// Specifies the instance type to be supported by the Dedicated Hosts. If you specify /// an instance type, the Dedicated Hosts support instances of the specified instance /// type only.If you want the Dedicated Hosts to support multiple instance types in a specific instance /// family, omit this parameter and specify InstanceFamily instead. You cannot /// specify InstanceType and InstanceFamily in the same request. /// /// [System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true)] public System.String InstanceType { get; set; } #endregion #region Parameter OutpostArn /// /// /// The Amazon Resource Name (ARN) of the Amazon Web Services Outpost on which to allocate /// the Dedicated Host. If you specify OutpostArn, you can optionally specify AssetIds.If you are allocating the Dedicated Host in a Region, omit this parameter. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public System.String OutpostArn { get; set; } #endregion #region Parameter Quantity /// /// /// The number of Dedicated Hosts to allocate to your account with these parameters. If /// you are allocating the Dedicated Hosts on an Outpost, and you specify AssetIds, /// you can omit this parameter. In this case, Amazon EC2 allocates a Dedicated Host on /// each specified hardware asset. If you specify both AssetIds and Quantity, /// then the value that you specify for Quantity must be equal to the number of /// asset IDs specified. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public System.Int32? Quantity { get; set; } #endregion #region Parameter TagSpecification /// /// /// The tags to apply to the Dedicated Host during creation. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [Alias("TagSpecifications")] public Amazon.EC2.Model.TagSpecification[] TagSpecification { get; set; } #endregion #region Parameter ClientToken /// /// /// Unique, case-sensitive identifier that you provide to ensure the idempotency of the /// request. For more information, see Ensuring /// Idempotency. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public System.String ClientToken { get; set; } #endregion #region Parameter Select /// /// Use the -Select parameter to control the cmdlet output. The default value is 'HostIds'. /// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.EC2.Model.AllocateHostsResponse). /// Specifying the name of a property of type Amazon.EC2.Model.AllocateHostsResponse 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; } = "HostIds"; #endregion #region Parameter PassThru /// /// Changes the cmdlet behavior to return the value passed to the InstanceType parameter. /// The -PassThru parameter is deprecated, use -Select '^InstanceType' instead. This parameter will be removed in a future version. /// [System.Obsolete("The -PassThru parameter is deprecated, use -Select '^InstanceType' 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.InstanceType), MyInvocation.BoundParameters); if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "New-EC2Host (AllocateHosts)")) { 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.InstanceType; } #pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute if (this.AssetId != null) { context.AssetId = new List(this.AssetId); } context.AutoPlacement = this.AutoPlacement; context.AvailabilityZone = this.AvailabilityZone; #if MODULAR if (this.AvailabilityZone == null && ParameterWasBound(nameof(this.AvailabilityZone))) { WriteWarning("You are passing $null as a value for parameter AvailabilityZone 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.ClientToken = this.ClientToken; context.HostMaintenance = this.HostMaintenance; context.HostRecovery = this.HostRecovery; context.InstanceFamily = this.InstanceFamily; context.InstanceType = this.InstanceType; context.OutpostArn = this.OutpostArn; context.Quantity = this.Quantity; if (this.TagSpecification != null) { context.TagSpecification = new List(this.TagSpecification); } // 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.EC2.Model.AllocateHostsRequest(); if (cmdletContext.AssetId != null) { request.AssetIds = cmdletContext.AssetId; } if (cmdletContext.AutoPlacement != null) { request.AutoPlacement = cmdletContext.AutoPlacement; } if (cmdletContext.AvailabilityZone != null) { request.AvailabilityZone = cmdletContext.AvailabilityZone; } if (cmdletContext.ClientToken != null) { request.ClientToken = cmdletContext.ClientToken; } if (cmdletContext.HostMaintenance != null) { request.HostMaintenance = cmdletContext.HostMaintenance; } if (cmdletContext.HostRecovery != null) { request.HostRecovery = cmdletContext.HostRecovery; } if (cmdletContext.InstanceFamily != null) { request.InstanceFamily = cmdletContext.InstanceFamily; } if (cmdletContext.InstanceType != null) { request.InstanceType = cmdletContext.InstanceType; } if (cmdletContext.OutpostArn != null) { request.OutpostArn = cmdletContext.OutpostArn; } if (cmdletContext.Quantity != null) { request.Quantity = cmdletContext.Quantity.Value; } if (cmdletContext.TagSpecification != null) { request.TagSpecifications = cmdletContext.TagSpecification; } 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.EC2.Model.AllocateHostsResponse CallAWSServiceOperation(IAmazonEC2 client, Amazon.EC2.Model.AllocateHostsRequest request) { Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon Elastic Compute Cloud (EC2)", "AllocateHosts"); try { #if DESKTOP return client.AllocateHosts(request); #elif CORECLR return client.AllocateHostsAsync(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 AssetId { get; set; } public Amazon.EC2.AutoPlacement AutoPlacement { get; set; } public System.String AvailabilityZone { get; set; } public System.String ClientToken { get; set; } public Amazon.EC2.HostMaintenance HostMaintenance { get; set; } public Amazon.EC2.HostRecovery HostRecovery { get; set; } public System.String InstanceFamily { get; set; } public System.String InstanceType { get; set; } public System.String OutpostArn { get; set; } public System.Int32? Quantity { get; set; } public List TagSpecification { get; set; } public System.Func Select { get; set; } = (response, cmdlet) => response.HostIds; } } }