Example #1
0
        private static async Task Cleanup(AmazonSQSClient sqsClient, AmazonSimpleNotificationServiceClient snsClient, List <Task <string> > subscribeTask, string topic1Arn, string topic2Arn, string queueUrl)
        {
            foreach (var task in subscribeTask)
            {
                await snsClient.UnsubscribeAsync(task.Result);
            }

            await snsClient.DeleteTopicAsync(topic1Arn);

            await snsClient.DeleteTopicAsync(topic2Arn);

            await sqsClient.DeleteQueueAsync(queueUrl);
        }
Example #2
0
        private static async Task DeleteTopicAsync(Topic topic)
        {
            var settings  = GetSettingsAsync().Result;
            var snsClient = new AmazonSimpleNotificationServiceClient(
                settings.AWSAccessKeyID,
                settings.AWSSecretAccessKey,
                RegionEndpoint.GetBySystemName(settings.AWSS3Region.SystemName));

            var sqsClient = new AmazonSQSClient(
                settings.AWSAccessKeyID,
                settings.AWSSecretAccessKey,
                RegionEndpoint.GetBySystemName(settings.AWSS3Region.SystemName));

            // Cleanup topic & queue & local file
            try { await snsClient.DeleteTopicAsync(new DeleteTopicRequest()
                {
                    TopicArn = topic.TopicARN
                }); } catch (Exception ex) { Debug.WriteLine(ex.Message); }
            try { await sqsClient.DeleteQueueAsync(new DeleteQueueRequest()
                {
                    QueueUrl = topic.QueueUrl
                }); } catch (Exception ex) { Debug.WriteLine(ex.Message); }

            // TODO Delete the errored/complete files on startup?
            File.Delete(Path.Combine(GetTempDirectory(), topic.TopicFileName));
            Debug.WriteLine($"Deleted topic {topic.TopicARN}");
            Debug.WriteLine($"Deleted topic file {topic.TopicFileName}");
        }
Example #3
0
        public static async Task Main()
        {
            string topicArn = "arn:aws:sns:us-east-2:704825161248:ExampleSNSTopic";
            IAmazonSimpleNotificationService client = new AmazonSimpleNotificationServiceClient();

            var response = await client.DeleteTopicAsync(topicArn);
        }
Example #4
0
 public void DeleteTopic(string topicName)
 {
     using (var client = new AmazonSimpleNotificationServiceClient(_credentials))
     {
         var topic = client.ListTopicsAsync().Result.Topics.SingleOrDefault(t => t.TopicArn == topicName);
         client.DeleteTopicAsync(topic.TopicArn).Wait();
     }
 }
        public async Task SetTopicConfigurationTests()
        {
            using (var snsClient = new AmazonSimpleNotificationServiceClient())
            {
                string topicName         = UtilityMethods.GenerateName("events-test");
                var    snsCreateResponse = await snsClient.CreateTopicAsync(topicName);

                var bucketName = await UtilityMethods.CreateBucketAsync(Client, "SetTopicConfigurationTests");

                try
                {
                    await snsClient.AuthorizeS3ToPublishAsync(snsCreateResponse.TopicArn, bucketName);

                    PutBucketNotificationRequest putRequest = new PutBucketNotificationRequest
                    {
                        BucketName          = bucketName,
                        TopicConfigurations = new List <TopicConfiguration>
                        {
                            new TopicConfiguration
                            {
                                Id     = "the-topic-test",
                                Topic  = snsCreateResponse.TopicArn,
                                Events = new List <EventType> {
                                    EventType.ObjectCreatedPut
                                }
                            }
                        }
                    };
                    await Client.PutBucketNotificationAsync(putRequest);

                    var getResponse = WaitUtils.WaitForComplete(
                        () =>
                    {
                        return(Client.GetBucketNotificationAsync(bucketName).Result);
                    },
                        (r) =>
                    {
                        return(r.TopicConfigurations.Count > 0);
                    });

                    Assert.Equal(1, getResponse.TopicConfigurations.Count);
                    Assert.Equal(1, getResponse.TopicConfigurations[0].Events.Count);
                    Assert.Equal(EventType.ObjectCreatedPut, getResponse.TopicConfigurations[0].Events[0]);

#pragma warning disable 618
                    Assert.Equal("s3:ObjectCreated:Put", getResponse.TopicConfigurations[0].Event);
#pragma warning restore 618
                    Assert.Equal("the-topic-test", getResponse.TopicConfigurations[0].Id);
                    Assert.Equal(snsCreateResponse.TopicArn, getResponse.TopicConfigurations[0].Topic);
                }
                finally
                {
                    await snsClient.DeleteTopicAsync(snsCreateResponse.TopicArn);

                    await UtilityMethods.DeleteBucketWithObjectsAsync(Client, bucketName);
                }
            }
        }
Example #6
0
        public async ValueTask DisposeAsync()
        {
            await _snsClient.DeleteTopicAsync(TopicArn);

            await _sqsClient.DeleteQueueAsync(QueueUrl);

            _snsClient.Dispose();
            _sqsClient.Dispose();
        }
Example #7
0
        public async Task <bool> DeleteMessage(string topicArn)
        {
            //delete an SNS topic
            DeleteTopicRequest  deleteTopicRequest  = new DeleteTopicRequest(topicArn);
            DeleteTopicResponse deleteTopicResponse = await _sns.DeleteTopicAsync(deleteTopicRequest);

            Console.WriteLine("DeleteTopic RequestId: {0}", deleteTopicResponse.ResponseMetadata.RequestId);
            return(true);
        }
Example #8
0
        static async Task <(string topicArn, Func <Task> topicCleanup)> CreateTopic(
            AmazonSimpleNotificationServiceClient snsClient,
            string timestamp,
            string name,
            CancellationToken cancellationToken)
        {
            var topic = await snsClient.CreateTopicAsync($"Beetles_{name}_{timestamp}", cancellationToken);

            return(topic.TopicArn, async() => await snsClient.DeleteTopicAsync(topic.TopicArn));
        }
Example #9
0
 public void DeleteTopic(Connection connection)
 {
     using (var snsClient = new AmazonSimpleNotificationServiceClient(_awsConnection.Credentials, _awsConnection.Region))
     {
         //TODO: could be a seperate method
         var exists = snsClient.ListTopicsAsync().Result.Topics.SingleOrDefault(topic => topic.TopicArn == connection.RoutingKey);
         if (exists != null)
         {
             snsClient.DeleteTopicAsync(connection.RoutingKey).Wait();
         }
     }
 }
        public async System.Threading.Tasks.Task <DeleteTopicResponse> DeleteTopic(string topicArn)
        {
            DeleteTopicResponse deleteTopicResponse = new DeleteTopicResponse();

            using (AmazonSimpleNotificationServiceClient snsClient = new AmazonSimpleNotificationServiceClient(credentials, Amazon.RegionEndpoint.USEast2))
            {
                DeleteTopicRequest deleteRequest = new DeleteTopicRequest(topicArn);
                deleteTopicResponse = await snsClient.DeleteTopicAsync(deleteRequest);
            }

            return(deleteTopicResponse);
        }
Example #11
0
        public async System.Threading.Tasks.Task RemoveTopicAsync()
        {
            AmazonSimpleNotificationServiceClient snsClient = new AmazonSimpleNotificationServiceClient(RegionEndpoint.APSoutheast1);

            string topicArn = "";

            // Delete an Amazon SNS topic.
            DeleteTopicRequest  deleteTopicRequest  = new DeleteTopicRequest(topicArn);
            DeleteTopicResponse deleteTopicResponse = await snsClient.DeleteTopicAsync(deleteTopicRequest);

            // Print the request ID for the DeleteTopicRequest action.
            Console.WriteLine("DeleteTopicRequest: " + deleteTopicResponse.ResponseMetadata.RequestId);
        }
Example #12
0
        public void DeleteTopic()
        {
            if (_connection == null)
            {
                return;
            }

            using (var snsClient = new AmazonSimpleNotificationServiceClient(_awsConnection.Credentials, _awsConnection.Region))
            {
                //TODO: could be a seperate method
                var exists = snsClient.ListTopicsAsync().Result.Topics.SingleOrDefault(topic => topic.TopicArn == _channelTopicARN);
                if (exists != null)
                {
                    try
                    {
                        var response = snsClient.ListSubscriptionsByTopicAsync(new ListSubscriptionsByTopicRequest {
                            TopicArn = _channelTopicARN
                        }).Result;
                        foreach (var sub in response.Subscriptions)
                        {
                            var unsubscribe = snsClient.UnsubscribeAsync(new UnsubscribeRequest {
                                SubscriptionArn = sub.SubscriptionArn
                            }).Result;
                            if (unsubscribe.HttpStatusCode != HttpStatusCode.OK)
                            {
                                _logger.Value.Error($"Error unsubscribing from {_channelTopicARN} for sub {sub.SubscriptionArn}");
                            }
                        }

                        snsClient.DeleteTopicAsync(_channelTopicARN).Wait();
                    }
                    catch (Exception)
                    {
                        //don't break on an exception here, if we can't delete, just exit
                        _logger.Value.Error($"Could not delete topic {_channelTopicARN}");
                    }
                }
            }
        }
        public void DeleteAllTopics()
        {
            AmazonSimpleNotificationServiceClient clientSNS = AwsFactory.CreateClient <AmazonSimpleNotificationServiceClient>();
            AmazonSQSClient    clientSQS    = AwsFactory.CreateClient <AmazonSQSClient>();
            AmazonLambdaClient lambdaClient = AwsFactory.CreateClient <AmazonLambdaClient>();


            var topics = clientSNS.ListTopicsAsync();
            // var subs = clientSNS.ListSubscriptionsAsync(new ListSubscriptionsRequest());
            var filas = clientSQS.ListQueuesAsync("subs");

            filas.Result.QueueUrls.ForEach(i =>
            {
                var deleted = clientSQS.DeleteQueueAsync(i);
                if (deleted.Result.HttpStatusCode != HttpStatusCode.OK)
                {
                    int x = 0;
                }
            });

            string nextToken = "";

            do
            {
                var subs = clientSNS.ListSubscriptionsAsync(new ListSubscriptionsRequest(nextToken));

                subs.Result.Subscriptions.ForEach(i =>
                {
                    var deleted = clientSNS.UnsubscribeAsync(i.SubscriptionArn);
                });

                nextToken = subs.Result.NextToken;
            } while (!String.IsNullOrEmpty(nextToken));



            var mapper = lambdaClient.ListEventSourceMappingsAsync(new Amazon.Lambda.Model.ListEventSourceMappingsRequest
            {
                FunctionName = "WebhookDispatcher"
            });

            mapper.Result.EventSourceMappings.ToList().ForEach(i =>
            {
                var result = lambdaClient.DeleteEventSourceMappingAsync(new Amazon.Lambda.Model.DeleteEventSourceMappingRequest()
                {
                    UUID = i.UUID
                });
                if (result.Result.HttpStatusCode != HttpStatusCode.OK)
                {
                    int x = 0;
                }
            });


            topics.Result.Topics.ForEach(i =>
            {
                var deleted = clientSNS.DeleteTopicAsync(new DeleteTopicRequest()
                {
                    TopicArn = i.TopicArn
                });
            });
        }
Example #14
0
 private void DeleteTopic(AmazonSimpleNotificationServiceClient snsClient)
 {
     snsClient.DeleteTopicAsync(_channelTopicArn).GetAwaiter().GetResult();
 }
        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");
        }
Example #16
0
 private async Task DeleteTopic()
 {
     await snsClient.DeleteTopicAsync(topicArn);
 }