/*******************************************************************************
* 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
{
///
/// Creates one or more flow logs to capture information about IP traffic for a specific
/// network interface, subnet, or VPC.
///
///
///
/// Flow log data for a monitored network interface is recorded as flow log records, which
/// are log events consisting of fields that describe the traffic flow. For more information,
/// see Flow
/// log records in the Amazon Virtual Private Cloud User Guide.
///
/// When publishing to CloudWatch Logs, flow log records are published to a log group,
/// and each network interface has a unique log stream in the log group. When publishing
/// to Amazon S3, flow log records for all of the monitored network interfaces are published
/// to a single log file object that is stored in the specified bucket.
///
/// For more information, see VPC
/// Flow Logs in the Amazon Virtual Private Cloud User Guide.
///
///
[Cmdlet("New", "EC2FlowLog", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
[OutputType("Amazon.EC2.Model.CreateFlowLogsResponse")]
[AWSCmdlet("Calls the Amazon Elastic Compute Cloud (EC2) CreateFlowLogs API operation.", Operation = new[] {"CreateFlowLogs"}, SelectReturnType = typeof(Amazon.EC2.Model.CreateFlowLogsResponse), LegacyAlias="New-EC2FlowLogs")]
[AWSCmdletOutput("Amazon.EC2.Model.CreateFlowLogsResponse",
"This cmdlet returns an Amazon.EC2.Model.CreateFlowLogsResponse object containing multiple properties. The object can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class NewEC2FlowLogCmdlet : AmazonEC2ClientCmdlet, IExecutor
{
#region Parameter DeliverCrossAccountRole
///
///
/// The ARN of the IAM role that allows Amazon EC2 to publish flow logs across accounts.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String DeliverCrossAccountRole { get; set; }
#endregion
#region Parameter DeliverLogsPermissionArn
///
///
/// The ARN of the IAM role that allows Amazon EC2 to publish flow logs to a CloudWatch
/// Logs log group in your account.This parameter is required if the destination type is cloud-watch-logs
/// and unsupported otherwise.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String DeliverLogsPermissionArn { get; set; }
#endregion
#region Parameter DestinationOptions_FileFormat
///
///
/// The format for the flow log. The default is plain-text
.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.EC2.DestinationFileFormat")]
public Amazon.EC2.DestinationFileFormat DestinationOptions_FileFormat { get; set; }
#endregion
#region Parameter DestinationOptions_HiveCompatiblePartition
///
///
/// Indicates whether to use Hive-compatible prefixes for flow logs stored in Amazon S3.
/// The default is false
.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("DestinationOptions_HiveCompatiblePartitions")]
public System.Boolean? DestinationOptions_HiveCompatiblePartition { get; set; }
#endregion
#region Parameter LogDestination
///
///
/// The destination for the flow log data. The meaning of this parameter depends on the
/// destination type.- If the destination type is
cloud-watch-logs
, specify the ARN of a CloudWatch
/// Logs log group. For example:arn:aws:logs:region:account_id:log-group:my_groupAlternatively, use the LogGroupName
parameter. - If the destination type is
s3
, specify the ARN of an S3 bucket. For example:arn:aws:s3:::my_bucket/my_subfolder/The subfolder is optional. Note that you can't use AWSLogs
as a subfolder
/// name. - If the destination type is
kinesis-data-firehose
, specify the ARN of
/// a Kinesis Data Firehose delivery stream. For example:arn:aws:firehose:region:account_id:deliverystream:my_stream
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String LogDestination { get; set; }
#endregion
#region Parameter LogDestinationType
///
///
/// The type of destination for the flow log data.Default: cloud-watch-logs
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.EC2.LogDestinationType")]
public Amazon.EC2.LogDestinationType LogDestinationType { get; set; }
#endregion
#region Parameter LogFormat
///
///
/// The fields to include in the flow log record. List the fields in the order in which
/// they should appear. If you omit this parameter, the flow log is created using the
/// default format. If you specify this parameter, you must include at least one field.
/// For more information about the available fields, see Flow
/// log records in the Amazon VPC User Guide or Transit
/// Gateway Flow Log records in the Amazon Web Services Transit Gateway Guide.Specify the fields using the ${field-id}
format, separated by spaces.
/// For the CLI, surround this parameter value with single quotes on Linux or double quotes
/// on Windows.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String LogFormat { get; set; }
#endregion
#region Parameter LogGroupName
///
///
/// The name of a new or existing CloudWatch Logs log group where Amazon EC2 publishes
/// your flow logs.This parameter is valid only if the destination type is cloud-watch-logs
.
///
///
[System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true)]
public System.String LogGroupName { get; set; }
#endregion
#region Parameter MaxAggregationInterval
///
///
/// The maximum interval of time during which a flow of packets is captured and aggregated
/// into a flow log record. The possible values are 60 seconds (1 minute) or 600 seconds
/// (10 minutes). This parameter must be 60 seconds for transit gateway resource types.When a network interface is attached to a Nitro-based
/// instance, the aggregation interval is always 60 seconds or less, regardless of
/// the value that you specify.Default: 600
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.Int32? MaxAggregationInterval { get; set; }
#endregion
#region Parameter DestinationOptions_PerHourPartition
///
///
/// Indicates whether to partition the flow log per hour. This reduces the cost and response
/// time for queries. The default is false
.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.Boolean? DestinationOptions_PerHourPartition { get; set; }
#endregion
#region Parameter ResourceId
///
///
/// The IDs of the resources to monitor. For example, if the resource type is VPC
,
/// specify the IDs of the VPCs.Constraints: Maximum of 25 for transit gateway resource types. Maximum of 1000 for
/// the other resource types.
///
///
#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]
[Alias("ResourceIds")]
public System.String[] ResourceId { get; set; }
#endregion
#region Parameter ResourceType
///
///
/// The type of resource to monitor.
///
///
#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]
[AWSConstantClassSource("Amazon.EC2.FlowLogsResourceType")]
public Amazon.EC2.FlowLogsResourceType ResourceType { get; set; }
#endregion
#region Parameter TagSpecification
///
///
/// The tags to apply to the flow logs.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("TagSpecifications")]
public Amazon.EC2.Model.TagSpecification[] TagSpecification { get; set; }
#endregion
#region Parameter TrafficType
///
///
/// The type of traffic to monitor (accepted traffic, rejected traffic, or all traffic).
/// This parameter is not supported for transit gateway resource types. It is required
/// for the other resource types.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.EC2.TrafficType")]
public Amazon.EC2.TrafficType TrafficType { get; set; }
#endregion
#region Parameter ClientToken
///
///
/// Unique, case-sensitive identifier that you provide to ensure the idempotency of the
/// request. For more information, see How
/// to ensure 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 '*'.
/// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.EC2.Model.CreateFlowLogsResponse).
/// Specifying the name of a property of type Amazon.EC2.Model.CreateFlowLogsResponse 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; } = "*";
#endregion
#region Parameter PassThru
///
/// Changes the cmdlet behavior to return the value passed to the LogGroupName parameter.
/// The -PassThru parameter is deprecated, use -Select '^LogGroupName' instead. This parameter will be removed in a future version.
///
[System.Obsolete("The -PassThru parameter is deprecated, use -Select '^LogGroupName' 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.LogGroupName), MyInvocation.BoundParameters);
if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "New-EC2FlowLog (CreateFlowLogs)"))
{
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.LogGroupName;
}
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
context.ClientToken = this.ClientToken;
context.DeliverCrossAccountRole = this.DeliverCrossAccountRole;
context.DeliverLogsPermissionArn = this.DeliverLogsPermissionArn;
context.DestinationOptions_FileFormat = this.DestinationOptions_FileFormat;
context.DestinationOptions_HiveCompatiblePartition = this.DestinationOptions_HiveCompatiblePartition;
context.DestinationOptions_PerHourPartition = this.DestinationOptions_PerHourPartition;
context.LogDestination = this.LogDestination;
context.LogDestinationType = this.LogDestinationType;
context.LogFormat = this.LogFormat;
context.LogGroupName = this.LogGroupName;
context.MaxAggregationInterval = this.MaxAggregationInterval;
if (this.ResourceId != null)
{
context.ResourceId = new List(this.ResourceId);
}
#if MODULAR
if (this.ResourceId == null && ParameterWasBound(nameof(this.ResourceId)))
{
WriteWarning("You are passing $null as a value for parameter ResourceId 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.ResourceType = this.ResourceType;
#if MODULAR
if (this.ResourceType == null && ParameterWasBound(nameof(this.ResourceType)))
{
WriteWarning("You are passing $null as a value for parameter ResourceType 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.TagSpecification != null)
{
context.TagSpecification = new List(this.TagSpecification);
}
context.TrafficType = this.TrafficType;
// 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.CreateFlowLogsRequest();
if (cmdletContext.ClientToken != null)
{
request.ClientToken = cmdletContext.ClientToken;
}
if (cmdletContext.DeliverCrossAccountRole != null)
{
request.DeliverCrossAccountRole = cmdletContext.DeliverCrossAccountRole;
}
if (cmdletContext.DeliverLogsPermissionArn != null)
{
request.DeliverLogsPermissionArn = cmdletContext.DeliverLogsPermissionArn;
}
// populate DestinationOptions
var requestDestinationOptionsIsNull = true;
request.DestinationOptions = new Amazon.EC2.Model.DestinationOptionsRequest();
Amazon.EC2.DestinationFileFormat requestDestinationOptions_destinationOptions_FileFormat = null;
if (cmdletContext.DestinationOptions_FileFormat != null)
{
requestDestinationOptions_destinationOptions_FileFormat = cmdletContext.DestinationOptions_FileFormat;
}
if (requestDestinationOptions_destinationOptions_FileFormat != null)
{
request.DestinationOptions.FileFormat = requestDestinationOptions_destinationOptions_FileFormat;
requestDestinationOptionsIsNull = false;
}
System.Boolean? requestDestinationOptions_destinationOptions_HiveCompatiblePartition = null;
if (cmdletContext.DestinationOptions_HiveCompatiblePartition != null)
{
requestDestinationOptions_destinationOptions_HiveCompatiblePartition = cmdletContext.DestinationOptions_HiveCompatiblePartition.Value;
}
if (requestDestinationOptions_destinationOptions_HiveCompatiblePartition != null)
{
request.DestinationOptions.HiveCompatiblePartitions = requestDestinationOptions_destinationOptions_HiveCompatiblePartition.Value;
requestDestinationOptionsIsNull = false;
}
System.Boolean? requestDestinationOptions_destinationOptions_PerHourPartition = null;
if (cmdletContext.DestinationOptions_PerHourPartition != null)
{
requestDestinationOptions_destinationOptions_PerHourPartition = cmdletContext.DestinationOptions_PerHourPartition.Value;
}
if (requestDestinationOptions_destinationOptions_PerHourPartition != null)
{
request.DestinationOptions.PerHourPartition = requestDestinationOptions_destinationOptions_PerHourPartition.Value;
requestDestinationOptionsIsNull = false;
}
// determine if request.DestinationOptions should be set to null
if (requestDestinationOptionsIsNull)
{
request.DestinationOptions = null;
}
if (cmdletContext.LogDestination != null)
{
request.LogDestination = cmdletContext.LogDestination;
}
if (cmdletContext.LogDestinationType != null)
{
request.LogDestinationType = cmdletContext.LogDestinationType;
}
if (cmdletContext.LogFormat != null)
{
request.LogFormat = cmdletContext.LogFormat;
}
if (cmdletContext.LogGroupName != null)
{
request.LogGroupName = cmdletContext.LogGroupName;
}
if (cmdletContext.MaxAggregationInterval != null)
{
request.MaxAggregationInterval = cmdletContext.MaxAggregationInterval.Value;
}
if (cmdletContext.ResourceId != null)
{
request.ResourceIds = cmdletContext.ResourceId;
}
if (cmdletContext.ResourceType != null)
{
request.ResourceType = cmdletContext.ResourceType;
}
if (cmdletContext.TagSpecification != null)
{
request.TagSpecifications = cmdletContext.TagSpecification;
}
if (cmdletContext.TrafficType != null)
{
request.TrafficType = cmdletContext.TrafficType;
}
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.CreateFlowLogsResponse CallAWSServiceOperation(IAmazonEC2 client, Amazon.EC2.Model.CreateFlowLogsRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon Elastic Compute Cloud (EC2)", "CreateFlowLogs");
try
{
#if DESKTOP
return client.CreateFlowLogs(request);
#elif CORECLR
return client.CreateFlowLogsAsync(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.String DeliverCrossAccountRole { get; set; }
public System.String DeliverLogsPermissionArn { get; set; }
public Amazon.EC2.DestinationFileFormat DestinationOptions_FileFormat { get; set; }
public System.Boolean? DestinationOptions_HiveCompatiblePartition { get; set; }
public System.Boolean? DestinationOptions_PerHourPartition { get; set; }
public System.String LogDestination { get; set; }
public Amazon.EC2.LogDestinationType LogDestinationType { get; set; }
public System.String LogFormat { get; set; }
public System.String LogGroupName { get; set; }
public System.Int32? MaxAggregationInterval { get; set; }
public List ResourceId { get; set; }
public Amazon.EC2.FlowLogsResourceType ResourceType { get; set; }
public List TagSpecification { get; set; }
public Amazon.EC2.TrafficType TrafficType { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response;
}
}
}