/*******************************************************************************
* 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.Redshift;
using Amazon.Redshift.Model;
namespace Amazon.PowerShell.Cmdlets.RS
{
///
/// Creates an Amazon Redshift event notification subscription. This action requires an
/// ARN (Amazon Resource Name) of an Amazon SNS topic created by either the Amazon Redshift
/// console, the Amazon SNS console, or the Amazon SNS API. To obtain an ARN with Amazon
/// SNS, you must create a topic in Amazon SNS and subscribe to the topic. The ARN is
/// displayed in the SNS console.
///
///
///
/// You can specify the source type, and lists of Amazon Redshift source IDs, event categories,
/// and event severities. Notifications will be sent for all events you want that match
/// those criteria. For example, you can specify source type = cluster, source ID = my-cluster-1
/// and mycluster2, event categories = Availability, Backup, and severity = ERROR. The
/// subscription will only send notifications for those ERROR events in the Availability
/// and Backup categories for the specified clusters.
///
/// If you specify both the source type and source IDs, such as source type = cluster
/// and source identifier = my-cluster-1, notifications will be sent for all the cluster
/// events for my-cluster-1. If you specify a source type but do not specify a source
/// identifier, you will receive notice of the events for the objects of that type in
/// your Amazon Web Services account. If you do not specify either the SourceType nor
/// the SourceIdentifier, you will be notified of events generated from all Amazon Redshift
/// sources belonging to your Amazon Web Services account. You must specify a source type
/// if you specify a source ID.
///
///
[Cmdlet("New", "RSEventSubscription", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
[OutputType("Amazon.Redshift.Model.EventSubscription")]
[AWSCmdlet("Calls the Amazon Redshift CreateEventSubscription API operation.", Operation = new[] {"CreateEventSubscription"}, SelectReturnType = typeof(Amazon.Redshift.Model.CreateEventSubscriptionResponse))]
[AWSCmdletOutput("Amazon.Redshift.Model.EventSubscription or Amazon.Redshift.Model.CreateEventSubscriptionResponse",
"This cmdlet returns an Amazon.Redshift.Model.EventSubscription object.",
"The service call response (type Amazon.Redshift.Model.CreateEventSubscriptionResponse) can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class NewRSEventSubscriptionCmdlet : AmazonRedshiftClientCmdlet, IExecutor
{
#region Parameter Enabled
///
///
/// A boolean value; set to true
to activate the subscription, and set to
/// false
to create the subscription but not activate it.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.Boolean? Enabled { get; set; }
#endregion
#region Parameter EventCategory
///
///
/// Specifies the Amazon Redshift event categories to be published by the event notification
/// subscription.Values: configuration, management, monitoring, security, pending
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("EventCategories")]
public System.String[] EventCategory { get; set; }
#endregion
#region Parameter Severity
///
///
/// Specifies the Amazon Redshift event severity to be published by the event notification
/// subscription.Values: ERROR, INFO
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String Severity { get; set; }
#endregion
#region Parameter SnsTopicArn
///
///
/// The Amazon Resource Name (ARN) of the Amazon SNS topic used to transmit the event
/// notifications. The ARN is created by Amazon SNS when you create a topic and subscribe
/// to it.
///
///
#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 SnsTopicArn { get; set; }
#endregion
#region Parameter SourceId
///
///
/// A list of one or more identifiers of Amazon Redshift source objects. All of the objects
/// must be of the same type as was specified in the source type parameter. The event
/// subscription will return only events generated by the specified objects. If not specified,
/// then events are returned for all objects within the source type specified.Example: my-cluster-1, my-cluster-2Example: my-snapshot-20131010
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("SourceIds")]
public System.String[] SourceId { get; set; }
#endregion
#region Parameter SourceType
///
///
/// The type of source that will be generating the events. For example, if you want to
/// be notified of events generated by a cluster, you would set this parameter to cluster.
/// If this value is not specified, events are returned for all Amazon Redshift objects
/// in your Amazon Web Services account. You must specify a source type in order to specify
/// source IDs.Valid values: cluster, cluster-parameter-group, cluster-security-group, cluster-snapshot,
/// and scheduled-action.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String SourceType { get; set; }
#endregion
#region Parameter SubscriptionName
///
///
/// The name of the event subscription to be created.Constraints:- Cannot be null, empty, or blank.
- Must contain from 1 to 255 alphanumeric characters or hyphens.
- First character must be a letter.
- Cannot end with a hyphen or contain two consecutive hyphens.
///
///
#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 SubscriptionName { get; set; }
#endregion
#region Parameter Tag
///
///
/// A list of tag instances.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Tags")]
public Amazon.Redshift.Model.Tag[] Tag { get; set; }
#endregion
#region Parameter Select
///
/// Use the -Select parameter to control the cmdlet output. The default value is 'EventSubscription'.
/// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.Redshift.Model.CreateEventSubscriptionResponse).
/// Specifying the name of a property of type Amazon.Redshift.Model.CreateEventSubscriptionResponse 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; } = "EventSubscription";
#endregion
#region Parameter PassThru
///
/// Changes the cmdlet behavior to return the value passed to the SubscriptionName parameter.
/// The -PassThru parameter is deprecated, use -Select '^SubscriptionName' instead. This parameter will be removed in a future version.
///
[System.Obsolete("The -PassThru parameter is deprecated, use -Select '^SubscriptionName' 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.SubscriptionName), MyInvocation.BoundParameters);
if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "New-RSEventSubscription (CreateEventSubscription)"))
{
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.SubscriptionName;
}
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
context.Enabled = this.Enabled;
if (this.EventCategory != null)
{
context.EventCategory = new List(this.EventCategory);
}
context.Severity = this.Severity;
context.SnsTopicArn = this.SnsTopicArn;
#if MODULAR
if (this.SnsTopicArn == null && ParameterWasBound(nameof(this.SnsTopicArn)))
{
WriteWarning("You are passing $null as a value for parameter SnsTopicArn 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.SourceId != null)
{
context.SourceId = new List(this.SourceId);
}
context.SourceType = this.SourceType;
context.SubscriptionName = this.SubscriptionName;
#if MODULAR
if (this.SubscriptionName == null && ParameterWasBound(nameof(this.SubscriptionName)))
{
WriteWarning("You are passing $null as a value for parameter SubscriptionName 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.Tag != null)
{
context.Tag = new List(this.Tag);
}
// 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.Redshift.Model.CreateEventSubscriptionRequest();
if (cmdletContext.Enabled != null)
{
request.Enabled = cmdletContext.Enabled.Value;
}
if (cmdletContext.EventCategory != null)
{
request.EventCategories = cmdletContext.EventCategory;
}
if (cmdletContext.Severity != null)
{
request.Severity = cmdletContext.Severity;
}
if (cmdletContext.SnsTopicArn != null)
{
request.SnsTopicArn = cmdletContext.SnsTopicArn;
}
if (cmdletContext.SourceId != null)
{
request.SourceIds = cmdletContext.SourceId;
}
if (cmdletContext.SourceType != null)
{
request.SourceType = cmdletContext.SourceType;
}
if (cmdletContext.SubscriptionName != null)
{
request.SubscriptionName = cmdletContext.SubscriptionName;
}
if (cmdletContext.Tag != null)
{
request.Tags = cmdletContext.Tag;
}
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.Redshift.Model.CreateEventSubscriptionResponse CallAWSServiceOperation(IAmazonRedshift client, Amazon.Redshift.Model.CreateEventSubscriptionRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon Redshift", "CreateEventSubscription");
try
{
#if DESKTOP
return client.CreateEventSubscription(request);
#elif CORECLR
return client.CreateEventSubscriptionAsync(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.Boolean? Enabled { get; set; }
public List EventCategory { get; set; }
public System.String Severity { get; set; }
public System.String SnsTopicArn { get; set; }
public List SourceId { get; set; }
public System.String SourceType { get; set; }
public System.String SubscriptionName { get; set; }
public List Tag { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response.EventSubscription;
}
}
}