/*******************************************************************************
* 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.OpsWorks;
using Amazon.OpsWorks.Model;
namespace Amazon.PowerShell.Cmdlets.OPS
{
///
/// Creates an instance in a specified stack. For more information, see Adding
/// an Instance to a Layer.
///
///
/// Required Permissions: To use this action, an IAM user must have a Manage permissions
/// level for the stack, or an attached policy that explicitly grants permissions. For
/// more information on user permissions, see Managing
/// User Permissions.
///
///
[Cmdlet("New", "OPSInstance", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
[OutputType("System.String")]
[AWSCmdlet("Calls the AWS OpsWorks CreateInstance API operation.", Operation = new[] {"CreateInstance"}, SelectReturnType = typeof(Amazon.OpsWorks.Model.CreateInstanceResponse))]
[AWSCmdletOutput("System.String or Amazon.OpsWorks.Model.CreateInstanceResponse",
"This cmdlet returns a System.String object.",
"The service call response (type Amazon.OpsWorks.Model.CreateInstanceResponse) can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class NewOPSInstanceCmdlet : AmazonOpsWorksClientCmdlet, IExecutor
{
#region Parameter AgentVersion
///
///
/// The default AWS OpsWorks Stacks agent version. You have the following options:INHERIT
- Use the stack's default agent version setting.- version_number - Use the specified agent version. This value overrides the
/// stack's default setting. To update the agent version, edit the instance configuration
/// and specify a new version. AWS OpsWorks Stacks then automatically installs that version
/// on the instance.
The default setting is INHERIT
. To specify an agent version, you must
/// use the complete version number, not the abbreviated number shown on the console.
/// For a list of available agent version numbers, call DescribeAgentVersions.
/// AgentVersion cannot be set to Chef 12.2.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String AgentVersion { get; set; }
#endregion
#region Parameter AmiId
///
///
/// A custom AMI ID to be used to create the instance. The AMI should be based on one
/// of the supported operating systems. For more information, see Using
/// Custom AMIs.If you specify a custom AMI, you must set Os
to Custom
.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String AmiId { get; set; }
#endregion
#region Parameter Architecture
///
///
/// The instance architecture. The default option is x86_64
. Instance types
/// do not necessarily support both architectures. For a list of the architectures that
/// are supported by the different instance types, see Instance
/// Families and Types.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.OpsWorks.Architecture")]
public Amazon.OpsWorks.Architecture Architecture { get; set; }
#endregion
#region Parameter AutoScalingType
///
///
/// For load-based or time-based instances, the type. Windows stacks can use only time-based
/// instances.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.OpsWorks.AutoScalingType")]
public Amazon.OpsWorks.AutoScalingType AutoScalingType { get; set; }
#endregion
#region Parameter AvailabilityZone
///
///
/// The instance Availability Zone. For more information, see Regions
/// and Endpoints.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String AvailabilityZone { get; set; }
#endregion
#region Parameter BlockDeviceMapping
///
///
/// An array of BlockDeviceMapping
objects that specify the instance's block
/// devices. For more information, see Block
/// Device Mapping. Note that block device mappings are not supported for custom AMIs.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("BlockDeviceMappings")]
public Amazon.OpsWorks.Model.BlockDeviceMapping[] BlockDeviceMapping { get; set; }
#endregion
#region Parameter EbsOptimized
///
///
/// Whether to create an Amazon EBS-optimized instance.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.Boolean? EbsOptimized { get; set; }
#endregion
#region Parameter Hostname
///
///
/// The instance host name.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String Hostname { get; set; }
#endregion
#region Parameter InstallUpdatesOnBoot
///
///
/// Whether to install operating system and package updates when the instance boots. The
/// default value is true
. To control when updates are installed, set this
/// value to false
. You must then update your instances manually by using
/// CreateDeployment to run the update_dependencies
stack command
/// or by manually running yum
(Amazon Linux) or apt-get
(Ubuntu)
/// on the instances. We strongly recommend using the default value of true
to ensure that
/// your instances have the latest security updates.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.Boolean? InstallUpdatesOnBoot { get; set; }
#endregion
#region Parameter InstanceType
///
///
/// The instance type, such as t2.micro
. For a list of supported instance
/// types, open the stack in the console, choose Instances, and choose + Instance.
/// The Size list contains the currently supported types. For more information,
/// see Instance
/// Families and Types. The parameter values that you use to specify the various types
/// are in the API Name column of the Available Instance Types table.
///
///
#if !MODULAR
[System.Management.Automation.Parameter(Position = 2, ValueFromPipelineByPropertyName = true)]
#else
[System.Management.Automation.Parameter(Position = 2, ValueFromPipelineByPropertyName = true, Mandatory = true)]
[System.Management.Automation.AllowEmptyString]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
public System.String InstanceType { get; set; }
#endregion
#region Parameter LayerId
///
///
/// An array that contains the instance's layer IDs.
///
///
#if !MODULAR
[System.Management.Automation.Parameter(Position = 1, ValueFromPipelineByPropertyName = true)]
#else
[System.Management.Automation.Parameter(Position = 1, ValueFromPipelineByPropertyName = true, Mandatory = true)]
[System.Management.Automation.AllowEmptyCollection]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
[Alias("LayerIds")]
public System.String[] LayerId { get; set; }
#endregion
#region Parameter Os
///
///
/// The instance's operating system, which must be set to one of the following.- A supported Linux operating system: An Amazon Linux version, such as
Amazon
/// Linux 2018.03
, Amazon Linux 2017.09
, Amazon Linux 2017.03
,
/// Amazon Linux 2016.09
, Amazon Linux 2016.03
, Amazon
/// Linux 2015.09
, or Amazon Linux 2015.03
. - A supported Ubuntu operating system, such as
Ubuntu 16.04 LTS
, Ubuntu
/// 14.04 LTS
, or Ubuntu 12.04 LTS
. CentOS Linux 7
Red Hat Enterprise Linux 7
- A supported Windows operating system, such as
Microsoft Windows Server 2012
/// R2 Base
, Microsoft Windows Server 2012 R2 with SQL Server Express
,
/// Microsoft Windows Server 2012 R2 with SQL Server Standard
, or Microsoft
/// Windows Server 2012 R2 with SQL Server Web
. - A custom AMI:
Custom
.
For more information about the supported operating systems, see AWS
/// OpsWorks Stacks Operating Systems.The default option is the current Amazon Linux version. If you set this parameter
/// to Custom
, you must use the CreateInstance action's AmiId parameter
/// to specify the custom AMI that you want to use. Block device mappings are not supported
/// if the value is Custom
. For more information about supported operating
/// systems, see Operating
/// SystemsFor more information about how to use custom AMIs with AWS OpsWorks Stacks,
/// see Using
/// Custom AMIs.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String Os { get; set; }
#endregion
#region Parameter RootDeviceType
///
///
/// The instance root device type. For more information, see Storage
/// for the Root Device.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.OpsWorks.RootDeviceType")]
public Amazon.OpsWorks.RootDeviceType RootDeviceType { get; set; }
#endregion
#region Parameter SshKeyName
///
///
/// The instance's Amazon EC2 key-pair name.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String SshKeyName { get; set; }
#endregion
#region Parameter StackId
///
///
/// The stack ID.
///
///
#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 StackId { get; set; }
#endregion
#region Parameter SubnetId
///
///
/// The ID of the instance's subnet. If the stack is running in a VPC, you can use this
/// parameter to override the stack's default subnet ID value and direct AWS OpsWorks
/// Stacks to launch the instance in a different subnet.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String SubnetId { get; set; }
#endregion
#region Parameter Tenancy
///
///
/// The instance's tenancy option. The default option is no tenancy, or if the instance
/// is running in a VPC, inherit tenancy settings from the VPC. The following are valid
/// values for this parameter: dedicated
, default
, or host
.
/// Because there are costs associated with changes in tenancy options, we recommend that
/// you research tenancy options before choosing them for your instances. For more information
/// about dedicated hosts, see Dedicated
/// Hosts Overview and Amazon
/// EC2 Dedicated Hosts. For more information about dedicated instances, see Dedicated
/// Instances and Amazon
/// EC2 Dedicated Instances.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String Tenancy { get; set; }
#endregion
#region Parameter VirtualizationType
///
///
/// The instance's virtualization type, paravirtual
or hvm
.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.OpsWorks.VirtualizationType")]
public Amazon.OpsWorks.VirtualizationType VirtualizationType { get; set; }
#endregion
#region Parameter Select
///
/// Use the -Select parameter to control the cmdlet output. The default value is 'InstanceId'.
/// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.OpsWorks.Model.CreateInstanceResponse).
/// Specifying the name of a property of type Amazon.OpsWorks.Model.CreateInstanceResponse 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; } = "InstanceId";
#endregion
#region Parameter PassThru
///
/// Changes the cmdlet behavior to return the value passed to the StackId parameter.
/// The -PassThru parameter is deprecated, use -Select '^StackId' instead. This parameter will be removed in a future version.
///
[System.Obsolete("The -PassThru parameter is deprecated, use -Select '^StackId' 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.StackId), MyInvocation.BoundParameters);
if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "New-OPSInstance (CreateInstance)"))
{
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.StackId;
}
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
context.AgentVersion = this.AgentVersion;
context.AmiId = this.AmiId;
context.Architecture = this.Architecture;
context.AutoScalingType = this.AutoScalingType;
context.AvailabilityZone = this.AvailabilityZone;
if (this.BlockDeviceMapping != null)
{
context.BlockDeviceMapping = new List(this.BlockDeviceMapping);
}
context.EbsOptimized = this.EbsOptimized;
context.Hostname = this.Hostname;
context.InstallUpdatesOnBoot = this.InstallUpdatesOnBoot;
context.InstanceType = this.InstanceType;
#if MODULAR
if (this.InstanceType == null && ParameterWasBound(nameof(this.InstanceType)))
{
WriteWarning("You are passing $null as a value for parameter InstanceType 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
if (this.LayerId != null)
{
context.LayerId = new List(this.LayerId);
}
#if MODULAR
if (this.LayerId == null && ParameterWasBound(nameof(this.LayerId)))
{
WriteWarning("You are passing $null as a value for parameter LayerId 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.Os = this.Os;
context.RootDeviceType = this.RootDeviceType;
context.SshKeyName = this.SshKeyName;
context.StackId = this.StackId;
#if MODULAR
if (this.StackId == null && ParameterWasBound(nameof(this.StackId)))
{
WriteWarning("You are passing $null as a value for parameter StackId 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.SubnetId = this.SubnetId;
context.Tenancy = this.Tenancy;
context.VirtualizationType = this.VirtualizationType;
// 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.OpsWorks.Model.CreateInstanceRequest();
if (cmdletContext.AgentVersion != null)
{
request.AgentVersion = cmdletContext.AgentVersion;
}
if (cmdletContext.AmiId != null)
{
request.AmiId = cmdletContext.AmiId;
}
if (cmdletContext.Architecture != null)
{
request.Architecture = cmdletContext.Architecture;
}
if (cmdletContext.AutoScalingType != null)
{
request.AutoScalingType = cmdletContext.AutoScalingType;
}
if (cmdletContext.AvailabilityZone != null)
{
request.AvailabilityZone = cmdletContext.AvailabilityZone;
}
if (cmdletContext.BlockDeviceMapping != null)
{
request.BlockDeviceMappings = cmdletContext.BlockDeviceMapping;
}
if (cmdletContext.EbsOptimized != null)
{
request.EbsOptimized = cmdletContext.EbsOptimized.Value;
}
if (cmdletContext.Hostname != null)
{
request.Hostname = cmdletContext.Hostname;
}
if (cmdletContext.InstallUpdatesOnBoot != null)
{
request.InstallUpdatesOnBoot = cmdletContext.InstallUpdatesOnBoot.Value;
}
if (cmdletContext.InstanceType != null)
{
request.InstanceType = cmdletContext.InstanceType;
}
if (cmdletContext.LayerId != null)
{
request.LayerIds = cmdletContext.LayerId;
}
if (cmdletContext.Os != null)
{
request.Os = cmdletContext.Os;
}
if (cmdletContext.RootDeviceType != null)
{
request.RootDeviceType = cmdletContext.RootDeviceType;
}
if (cmdletContext.SshKeyName != null)
{
request.SshKeyName = cmdletContext.SshKeyName;
}
if (cmdletContext.StackId != null)
{
request.StackId = cmdletContext.StackId;
}
if (cmdletContext.SubnetId != null)
{
request.SubnetId = cmdletContext.SubnetId;
}
if (cmdletContext.Tenancy != null)
{
request.Tenancy = cmdletContext.Tenancy;
}
if (cmdletContext.VirtualizationType != null)
{
request.VirtualizationType = cmdletContext.VirtualizationType;
}
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.OpsWorks.Model.CreateInstanceResponse CallAWSServiceOperation(IAmazonOpsWorks client, Amazon.OpsWorks.Model.CreateInstanceRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "AWS OpsWorks", "CreateInstance");
try
{
#if DESKTOP
return client.CreateInstance(request);
#elif CORECLR
return client.CreateInstanceAsync(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 System.String AgentVersion { get; set; }
public System.String AmiId { get; set; }
public Amazon.OpsWorks.Architecture Architecture { get; set; }
public Amazon.OpsWorks.AutoScalingType AutoScalingType { get; set; }
public System.String AvailabilityZone { get; set; }
public List BlockDeviceMapping { get; set; }
public System.Boolean? EbsOptimized { get; set; }
public System.String Hostname { get; set; }
public System.Boolean? InstallUpdatesOnBoot { get; set; }
public System.String InstanceType { get; set; }
public List LayerId { get; set; }
public System.String Os { get; set; }
public Amazon.OpsWorks.RootDeviceType RootDeviceType { get; set; }
public System.String SshKeyName { get; set; }
public System.String StackId { get; set; }
public System.String SubnetId { get; set; }
public System.String Tenancy { get; set; }
public Amazon.OpsWorks.VirtualizationType VirtualizationType { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response.InstanceId;
}
}
}