/*******************************************************************************
* 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
{
///
/// Initiates the copy of an AMI. You can copy an AMI from one Region to another, or from
/// a Region to an Outpost. You can't copy an AMI from an Outpost to a Region, from one
/// Outpost to another, or within the same Outpost. To copy an AMI to another partition,
/// see CreateStoreImageTask.
///
///
///
/// To copy an AMI from one Region to another, specify the source Region using the SourceRegion
/// parameter, and specify the destination Region using its endpoint. Copies of encrypted
/// backing snapshots for the AMI are encrypted. Copies of unencrypted backing snapshots
/// remain unencrypted, unless you set Encrypted
during the copy operation.
/// You cannot create an unencrypted copy of an encrypted backing snapshot.
///
/// To copy an AMI from a Region to an Outpost, specify the source Region using the SourceRegion
/// parameter, and specify the ARN of the destination Outpost using DestinationOutpostArn.
/// Backing snapshots copied to an Outpost are encrypted by default using the default
/// encryption key for the Region, or a different key that you specify in the request
/// using KmsKeyId. Outposts do not support unencrypted snapshots. For more information,
///
/// Amazon EBS local snapshots on Outposts in the Amazon EC2 User Guide.
///
/// For more information about the prerequisites and limits when copying an AMI, see Copy an
/// AMI in the Amazon EC2 User Guide.
///
///
[Cmdlet("Copy", "EC2Image", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
[OutputType("System.String")]
[AWSCmdlet("Calls the Amazon Elastic Compute Cloud (EC2) CopyImage API operation.", Operation = new[] {"CopyImage"}, SelectReturnType = typeof(Amazon.EC2.Model.CopyImageResponse))]
[AWSCmdletOutput("System.String or Amazon.EC2.Model.CopyImageResponse",
"This cmdlet returns a System.String object.",
"The service call response (type Amazon.EC2.Model.CopyImageResponse) can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class CopyEC2ImageCmdlet : AmazonEC2ClientCmdlet, IExecutor
{
#region Parameter CopyImageTag
///
///
/// Indicates whether to include your user-defined AMI tags when copying the AMI.The following tags will not be copied:- System tags (prefixed with
aws:
) - For public and shared AMIs, user-defined tags that are attached by other Amazon Web
/// Services accounts
Default: Your user-defined AMI tags are not copied.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("CopyImageTags")]
public System.Boolean? CopyImageTag { get; set; }
#endregion
#region Parameter Description
///
///
/// A description for the new AMI in the destination Region.
///
///
[System.Management.Automation.Parameter(Position = 2, ValueFromPipelineByPropertyName = true)]
public System.String Description { get; set; }
#endregion
#region Parameter DestinationOutpostArn
///
///
/// The Amazon Resource Name (ARN) of the Outpost to which to copy the AMI. Only specify
/// this parameter when copying an AMI from an Amazon Web Services Region to an Outpost.
/// The AMI must be in the Region of the destination Outpost. You cannot copy an AMI from
/// an Outpost to a Region, from one Outpost to another, or within the same Outpost.For more information, see
/// Copy AMIs from an Amazon Web Services Region to an Outpost in the Amazon EC2
/// User Guide.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String DestinationOutpostArn { get; set; }
#endregion
#region Parameter Encrypted
///
///
/// Specifies whether the destination snapshots of the copied image should be encrypted.
/// You can encrypt a copy of an unencrypted snapshot, but you cannot create an unencrypted
/// copy of an encrypted snapshot. The default KMS key for Amazon EBS is used unless you
/// specify a non-default Key Management Service (KMS) KMS key using KmsKeyId
.
/// For more information, see Amazon
/// EBS encryption in the Amazon EC2 User Guide.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.Boolean? Encrypted { get; set; }
#endregion
#region Parameter KmsKeyId
///
///
/// The identifier of the symmetric Key Management Service (KMS) KMS key to use when creating
/// encrypted volumes. If this parameter is not specified, your Amazon Web Services managed
/// KMS key for Amazon EBS is used. If you specify a KMS key, you must also set the encrypted
/// state to true
.You can specify a KMS key using any of the following:- Key ID. For example, 1234abcd-12ab-34cd-56ef-1234567890ab.
- Key alias. For example, alias/ExampleAlias.
- Key ARN. For example, arn:aws:kms:us-east-1:012345678910:key/1234abcd-12ab-34cd-56ef-1234567890ab.
- Alias ARN. For example, arn:aws:kms:us-east-1:012345678910:alias/ExampleAlias.
Amazon Web Services authenticates the KMS key asynchronously. Therefore, if you specify
/// an identifier that is not valid, the action can appear to complete, but eventually
/// fails.The specified KMS key must exist in the destination Region.Amazon EBS does not support asymmetric KMS keys.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String KmsKeyId { get; set; }
#endregion
#region Parameter Name
///
///
/// The name of the new AMI in the destination Region.
///
///
#if !MODULAR
[System.Management.Automation.Parameter(Position = 3, ValueFromPipelineByPropertyName = true)]
#else
[System.Management.Automation.Parameter(Position = 3, ValueFromPipelineByPropertyName = true, Mandatory = true)]
[System.Management.Automation.AllowEmptyString]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
public System.String Name { get; set; }
#endregion
#region Parameter SourceImageId
///
///
/// The ID of the AMI to copy.
///
///
#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 SourceImageId { get; set; }
#endregion
#region Parameter SourceRegion
///
///
/// The name of the Region that contains the AMI to copy.
///
///
#if !MODULAR
[System.Management.Automation.Parameter(Position = 1, ValueFromPipelineByPropertyName = true)]
#else
[System.Management.Automation.Parameter(Position = 1, ValueFromPipelineByPropertyName = true, Mandatory = true)]
[System.Management.Automation.AllowEmptyString]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
public System.String SourceRegion { get; set; }
#endregion
#region Parameter ClientToken
///
///
/// Unique, case-sensitive identifier you provide to ensure idempotency of the request.
/// For more information, see Ensuring
/// idempotency in the Amazon EC2 API Reference.
///
///
[System.Management.Automation.Parameter(Position = 4, 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 'ImageId'.
/// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.EC2.Model.CopyImageResponse).
/// Specifying the name of a property of type Amazon.EC2.Model.CopyImageResponse 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; } = "ImageId";
#endregion
#region Parameter PassThru
///
/// Changes the cmdlet behavior to return the value passed to the SourceImageId parameter.
/// The -PassThru parameter is deprecated, use -Select '^SourceImageId' instead. This parameter will be removed in a future version.
///
[System.Obsolete("The -PassThru parameter is deprecated, use -Select '^SourceImageId' 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.SourceImageId), MyInvocation.BoundParameters);
if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "Copy-EC2Image (CopyImage)"))
{
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.SourceImageId;
}
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
context.ClientToken = this.ClientToken;
context.CopyImageTag = this.CopyImageTag;
context.Description = this.Description;
context.DestinationOutpostArn = this.DestinationOutpostArn;
context.Encrypted = this.Encrypted;
context.KmsKeyId = this.KmsKeyId;
context.Name = this.Name;
#if MODULAR
if (this.Name == null && ParameterWasBound(nameof(this.Name)))
{
WriteWarning("You are passing $null as a value for parameter Name 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.SourceImageId = this.SourceImageId;
#if MODULAR
if (this.SourceImageId == null && ParameterWasBound(nameof(this.SourceImageId)))
{
WriteWarning("You are passing $null as a value for parameter SourceImageId 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.SourceRegion = this.SourceRegion;
#if MODULAR
if (this.SourceRegion == null && ParameterWasBound(nameof(this.SourceRegion)))
{
WriteWarning("You are passing $null as a value for parameter SourceRegion 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.EC2.Model.CopyImageRequest();
if (cmdletContext.ClientToken != null)
{
request.ClientToken = cmdletContext.ClientToken;
}
if (cmdletContext.CopyImageTag != null)
{
request.CopyImageTags = cmdletContext.CopyImageTag.Value;
}
if (cmdletContext.Description != null)
{
request.Description = cmdletContext.Description;
}
if (cmdletContext.DestinationOutpostArn != null)
{
request.DestinationOutpostArn = cmdletContext.DestinationOutpostArn;
}
if (cmdletContext.Encrypted != null)
{
request.Encrypted = cmdletContext.Encrypted.Value;
}
if (cmdletContext.KmsKeyId != null)
{
request.KmsKeyId = cmdletContext.KmsKeyId;
}
if (cmdletContext.Name != null)
{
request.Name = cmdletContext.Name;
}
if (cmdletContext.SourceImageId != null)
{
request.SourceImageId = cmdletContext.SourceImageId;
}
if (cmdletContext.SourceRegion != null)
{
request.SourceRegion = cmdletContext.SourceRegion;
}
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.CopyImageResponse CallAWSServiceOperation(IAmazonEC2 client, Amazon.EC2.Model.CopyImageRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon Elastic Compute Cloud (EC2)", "CopyImage");
try
{
#if DESKTOP
return client.CopyImage(request);
#elif CORECLR
return client.CopyImageAsync(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 ClientToken { get; set; }
public System.Boolean? CopyImageTag { get; set; }
public System.String Description { get; set; }
public System.String DestinationOutpostArn { get; set; }
public System.Boolean? Encrypted { get; set; }
public System.String KmsKeyId { get; set; }
public System.String Name { get; set; }
public System.String SourceImageId { get; set; }
public System.String SourceRegion { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response.ImageId;
}
}
}