Ejemplo n.º 1
0
        /// <summary>
        /// Removes target(s) from a rule so that when the rule is triggered, those targets will
        /// no longer be invoked.
        ///
        ///
        /// <para>
        ///  <b>Note:</b> When you remove a target, when the associated rule triggers, removed
        /// targets might still continue to be invoked. Please allow a short period of time for
        /// changes to take effect.
        /// </para>
        /// </summary>
        /// <param name="request">Container for the necessary parameters to execute the RemoveTargets service method.</param>
        ///
        /// <returns>The response from the RemoveTargets service method, as returned by CloudWatchEvents.</returns>
        /// <exception cref="Amazon.CloudWatchEvents.Model.ConcurrentModificationException">
        /// This exception occurs if there is concurrent modification on rule or target.
        /// </exception>
        /// <exception cref="Amazon.CloudWatchEvents.Model.InternalException">
        /// This exception occurs due to unexpected causes.
        /// </exception>
        /// <exception cref="Amazon.CloudWatchEvents.Model.ResourceNotFoundException">
        /// The rule does not exist.
        /// </exception>
        public RemoveTargetsResponse RemoveTargets(RemoveTargetsRequest request)
        {
            var marshaller   = new RemoveTargetsRequestMarshaller();
            var unmarshaller = RemoveTargetsResponseUnmarshaller.Instance;

            return(Invoke <RemoveTargetsRequest, RemoveTargetsResponse>(request, marshaller, unmarshaller));
        }
Ejemplo n.º 2
0
        internal virtual RemoveTargetsResponse RemoveTargets(RemoveTargetsRequest request)
        {
            var marshaller   = RemoveTargetsRequestMarshaller.Instance;
            var unmarshaller = RemoveTargetsResponseUnmarshaller.Instance;

            return(Invoke <RemoveTargetsRequest, RemoveTargetsResponse>(request, marshaller, unmarshaller));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initiates the asynchronous execution of the RemoveTargets operation.
        /// </summary>
        ///
        /// <param name="request">Container for the necessary parameters to execute the RemoveTargets operation.</param>
        /// <param name="cancellationToken">
        ///     A cancellation token that can be used by other objects or threads to receive notice of cancellation.
        /// </param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public Task <RemoveTargetsResponse> RemoveTargetsAsync(RemoveTargetsRequest request, System.Threading.CancellationToken cancellationToken = default(CancellationToken))
        {
            var marshaller   = new RemoveTargetsRequestMarshaller();
            var unmarshaller = RemoveTargetsResponseUnmarshaller.Instance;

            return(InvokeAsync <RemoveTargetsRequest, RemoveTargetsResponse>(request, marshaller,
                                                                             unmarshaller, cancellationToken));
        }
        /// <summary>
        /// Initiates the asynchronous execution of the RemoveTargets operation.
        /// </summary>
        ///
        /// <param name="request">Container for the necessary parameters to execute the RemoveTargets operation.</param>
        /// <param name="cancellationToken">
        ///     A cancellation token that can be used by other objects or threads to receive notice of cancellation.
        /// </param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        /// <seealso href="http://docs.aws.amazon.com/goto/WebAPI/events-2015-10-07/RemoveTargets">REST API Reference for RemoveTargets Operation</seealso>
        public virtual Task <RemoveTargetsResponse> RemoveTargetsAsync(RemoveTargetsRequest request, System.Threading.CancellationToken cancellationToken = default(CancellationToken))
        {
            var options = new InvokeOptions();

            options.RequestMarshaller    = RemoveTargetsRequestMarshaller.Instance;
            options.ResponseUnmarshaller = RemoveTargetsResponseUnmarshaller.Instance;

            return(InvokeAsync <RemoveTargetsResponse>(request, options, cancellationToken));
        }
        internal virtual RemoveTargetsResponse RemoveTargets(RemoveTargetsRequest request)
        {
            var options = new InvokeOptions();

            options.RequestMarshaller    = RemoveTargetsRequestMarshaller.Instance;
            options.ResponseUnmarshaller = RemoveTargetsResponseUnmarshaller.Instance;

            return(Invoke <RemoveTargetsResponse>(request, options));
        }
        public string Remove(Notification input, ILambdaContext context)
        {
            var region = RegionEndpoint.APSoutheast1;

            var topicId = input.TopicId;

            //Get item from DynamoDB
            var key = new Dictionary <string, AttributeValue>();

            key.Add("id", new AttributeValue()
            {
                S = topicId
            });

            var dynamoDBClient  = new AmazonDynamoDBClient(region);
            var getItemResponse = dynamoDBClient.GetItemAsync("Topics", key).Result;

            if (getItemResponse.HttpStatusCode != HttpStatusCode.OK)
            {
                throw new TestDonkeyException("Can't find item");
            }

            var item = getItemResponse.Item;

            var topicArn = item.GetValueOrDefault("arn").S;

            //Remove permission to accept CloudWatch Events trigger for Lambda
            var lambdaClient = new AmazonLambdaClient(region);

            var removePermissionRequest = new Amazon.Lambda.Model.RemovePermissionRequest();

            removePermissionRequest.FunctionName = "TestDonkeyLambda";
            removePermissionRequest.StatementId  = $"testdonkey-lambda-{ topicId }";

            var removePermissionResponse = lambdaClient.RemovePermissionAsync(removePermissionRequest).Result;

            if (removePermissionResponse.HttpStatusCode != HttpStatusCode.NoContent)
            {
                throw new TestDonkeyException("Can't remove permission");
            }

            //Remove target for CloudWatch Events
            var cloudWatchEventClient = new AmazonCloudWatchEventsClient(region);

            var removeTargetsRequest = new RemoveTargetsRequest();

            removeTargetsRequest.Ids = new List <string> {
                $"testdonkey-target-{ topicId }"
            };
            removeTargetsRequest.Rule = $"testdonkey-rule-{ topicId }";

            var removeTargetsResponse = cloudWatchEventClient.RemoveTargetsAsync(removeTargetsRequest).Result;

            if (removeTargetsResponse.HttpStatusCode != HttpStatusCode.OK)
            {
                throw new TestDonkeyException("Can't remove target");
            }

            //Delete rule in CloudWatch Events
            var deleteRuleRequest = new DeleteRuleRequest();

            deleteRuleRequest.Name = removeTargetsRequest.Rule;

            var deleteRuleResponse = cloudWatchEventClient.DeleteRuleAsync(deleteRuleRequest).Result;

            if (deleteRuleResponse.HttpStatusCode != HttpStatusCode.OK)
            {
                throw new TestDonkeyException("Can't delete rule");
            }

            //Remove subscribers from SNS
            var snsClient = new AmazonSimpleNotificationServiceClient(region);

            var listSubscriptionsByTopicRequest = new ListSubscriptionsByTopicRequest();

            listSubscriptionsByTopicRequest.TopicArn = topicArn;

            ListSubscriptionsByTopicResponse listSubscriptionsByTopicResponse = null;

            do
            {
                listSubscriptionsByTopicResponse = snsClient.ListSubscriptionsByTopicAsync(listSubscriptionsByTopicRequest).Result;
                if (listSubscriptionsByTopicResponse.HttpStatusCode != HttpStatusCode.OK)
                {
                    throw new TestDonkeyException("Can't list subscriptions");
                }

                if (listSubscriptionsByTopicResponse.Subscriptions != null && listSubscriptionsByTopicResponse.Subscriptions.Count > 0)
                {
                    foreach (var subscription in listSubscriptionsByTopicResponse.Subscriptions)
                    {
                        if (!subscription.SubscriptionArn.Equals("pendingconfirmation", StringComparison.OrdinalIgnoreCase))
                        {
                            snsClient.UnsubscribeAsync(subscription.SubscriptionArn).GetAwaiter().GetResult();
                        }
                    }
                }

                listSubscriptionsByTopicRequest.NextToken = listSubscriptionsByTopicResponse.NextToken;

                Thread.Sleep(1_000); //Wait for 1 second. Throttle: 100 transactions per second (TPS)
            } while (!string.IsNullOrWhiteSpace(listSubscriptionsByTopicResponse.NextToken));

            //Delete topic from SNS
            var deleteTopicResponse = snsClient.DeleteTopicAsync(topicArn).Result;

            if (deleteTopicResponse.HttpStatusCode != HttpStatusCode.OK)
            {
                throw new TestDonkeyException("Can't delete topic");
            }

            //Delete item from DynamoDB
            var dynamoDBDeleteItemResponse = dynamoDBClient.DeleteItemAsync("Topics", key).Result;

            if (dynamoDBDeleteItemResponse.HttpStatusCode != HttpStatusCode.OK)
            {
                throw new TestDonkeyException("Can't delete item");
            }

            return("success");
        }