Ejemplo n.º 1
0
        public async void TestMultipleQueueSubscription()
        {
            var topicName1 = UtilityMethods.GenerateName("TestMultipleQueueSubscription");
            var topic      = await Client.CreateTopicAsync(topicName1);

            _topicArns.Add(topic.TopicArn);

            var topicName2 = UtilityMethods.GenerateName("TestMultipleQueueSubscription2");
            var response   = await Client.CreateTopicAsync(topicName2);

            _topicArns.Add(response.TopicArn);

            var queueName = UtilityMethods.GenerateName("TestMultipleQueueSubscription");
            var queueUrl  = (await sqsClient.CreateQueueAsync(queueName)).QueueUrl;

            _queueUrl.Add(queueUrl);

            ICoreAmazonSQS coreSqs = sqsClient as ICoreAmazonSQS;
            var            topics  = await Client.SubscribeQueueToTopicsAsync(_topicArns, coreSqs, queueUrl);

            var subscriptionArns = topics.Values;

            Assert.Equal(2, subscriptionArns.Count);

            Thread.Sleep(TimeSpan.FromSeconds(5));

            var attributes = (await sqsClient.GetQueueAttributesAsync(queueUrl, new List <string> {
                "All"
            })).Attributes;
            var policy     = Policy.FromJson(attributes["Policy"]);

            Assert.Equal(2, policy.Statements.Count);
        }
 /// <summary>
 /// Subscribes an existing Amazon SQS queue to an existing Amazon SNS topic.
 /// <para>
 /// The policy applied to the SQS queue is similar to this:
 /// <code>
 /// {
 ///     "Version" : "2008-10-17",
 ///     "Statement" : [{
 ///         "Sid" : "topic-subscription-arn:aws:sns:us-west-2:599109622955:myTopic",
 ///         "Effect" : "Allow",
 ///         "Principal" : "*",
 ///         "Action" : ["sqs:SendMessage"],
 ///         "Resource":["arn:aws:sqs:us-west-2:599109622955:myQueue"],
 ///         "Condition" : {
 ///             "ArnLike":{
 ///                 "aws:SourceArn":["arn:aws:sns:us-west-2:599109622955:myTopic"]
 ///             }
 ///         }
 ///     }]
 /// }
 /// </code>
 /// </para>
 /// <para>
 /// There might be a small time period immediately after
 /// subscribing the SQS queue to the SNS topic and updating the SQS queue's
 /// policy, where messages are not able to be delivered to the queue. After a
 /// moment, the new queue policy will propagate and the queue will be able to
 /// receive messages. This delay only occurs immediately after initially
 /// subscribing the queue.
 /// </para>
 /// </summary>
 /// <param name="topicArn">The topic to subscribe to</param>
 /// <param name="sqsClient">The SQS client used to get attributes and set the policy on the SQS queue.</param>
 /// <param name="sqsQueueUrl">The queue to add a subscription to.</param>
 /// <returns>The subscription ARN as returned by Amazon SNS when the queue is
 /// successfully subscribed to the topic.</returns>
 public string SubscribeQueue(string topicArn, ICoreAmazonSQS sqsClient, string sqsQueueUrl)
 {
     return(SubscribeQueueToTopics(new List <string>()
     {
         topicArn
     }, sqsClient, sqsQueueUrl).Values.First());
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="topicArn"></param>
        /// <param name="sqsClient"></param>
        /// <param name="sqsQueueUrl"></param>
        public async Task SubscribeSQS(string topicArn, ICoreAmazonSQS sqsClient, string sqsQueueUrl)
        {
            using (var client = new AmazonSimpleNotificationServiceClient(accessKey, secretKey, region))
            {
                var subscriptionArn = await client.SubscribeQueueAsync(topicArn, sqsClient, sqsQueueUrl);

                await client.SetSubscriptionAttributesAsync(subscriptionArn, "RawMessageDelivery", "true");
            }
        }
Ejemplo n.º 4
0
        private async Task <string> SubscribeQueue(string topicArn, string queueUrl)
        {
            ICoreAmazonSQS coreSqs         = sqsClient as ICoreAmazonSQS;
            var            subscriptionARN = await Client.SubscribeQueueAsync(topicArn, coreSqs, queueUrl);

            // Sleep to wait for the subscribe to complete.
            Thread.Sleep(TimeSpan.FromSeconds(5));

            return(subscriptionARN);
        }
        public async Task SubscribeQueueToSnsTopic(string topicArn, ICoreAmazonSQS sqsClient, string sqsUrl)
        {
            if (string.IsNullOrWhiteSpace(topicArn))
            {
                throw new ArgumentNullException(nameof(topicArn));
            }

            if (sqsClient == null)
            {
                throw new ArgumentNullException(nameof(sqsClient));
            }

            if (string.IsNullOrWhiteSpace(sqsUrl))
            {
                throw new ArgumentNullException(nameof(sqsUrl));
            }

            this._logger?.LogInformation($"subscribe queue to SNS topic {topicArn} with queue {sqsUrl}");
            await this._snsClient.SubscribeQueueAsync(topicArn, sqsClient, sqsUrl);

            this._logger?.LogInformation($"subscribe queue to SNS topic {topicArn} with queue {sqsUrl} completed");
        }
        /// <summary>
        /// Subscribes an existing Amazon SQS queue to existing Amazon SNS topics.
        /// <para>
        /// The policy applied to the SQS queue is similar to this:
        /// <code>
        /// {
        ///     "Version" : "2008-10-17",
        ///     "Statement" : [{
        ///         "Sid" : "topic-subscription-arn:aws:sns:us-west-2:599109622955:myTopic",
        ///         "Effect" : "Allow",
        ///         "Principal" : "*",
        ///         "Action" : ["sqs:SendMessage"],
        ///         "Resource":["arn:aws:sqs:us-west-2:599109622955:myQueue"],
        ///         "Condition" : {
        ///             "ArnLike":{
        ///                 "aws:SourceArn":["arn:aws:sns:us-west-2:599109622955:myTopic"]
        ///             }
        ///         }
        ///     }]
        /// }
        /// </code>
        /// </para>
        /// <para>
        /// There might be a small time period immediately after
        /// subscribing the SQS queue to the SNS topic and updating the SQS queue's
        /// policy, where messages are not able to be delivered to the queue. After a
        /// moment, the new queue policy will propagate and the queue will be able to
        /// receive messages. This delay only occurs immediately after initially
        /// subscribing the queue.
        /// </para>
        /// </summary>
        /// <param name="topicArns">The topics to subscribe to</param>
        /// <param name="sqsClient">The SQS client used to get attributes and set the policy on the SQS queue.</param>
        /// <param name="sqsQueueUrl">The queue to add a subscription to.</param>
        /// <returns>The mapping of topic ARNs to subscription ARNs as returned by Amazon SNS when the queue is
        /// successfully subscribed to each topic.</returns>
        public IDictionary <string, string> SubscribeQueueToTopics(IList <string> topicArns, ICoreAmazonSQS sqsClient, string sqsQueueUrl)
        {
            // Get the queue's existing policy and ARN
            var queueAttributes = sqsClient.GetAttributes(sqsQueueUrl);

            string sqsQueueArn = queueAttributes["QueueArn"];

            Policy policy;
            string policyStr = null;

            if (queueAttributes.ContainsKey("Policy"))
            {
                policyStr = queueAttributes["Policy"];
            }
            if (string.IsNullOrEmpty(policyStr))
            {
                policy = new Policy();
            }
            else
            {
                policy = Policy.FromJson(policyStr);
            }

            var subscriptionArns = new Dictionary <string, string>();

            foreach (var topicArn in topicArns)
            {
                if (!HasSQSPermission(policy, topicArn, sqsQueueArn))
                {
                    AddSQSPermission(policy, topicArn, sqsQueueArn);
                }

                var arn = this.Subscribe(new SubscribeRequest
                {
                    TopicArn = topicArn,
                    Protocol = "sqs",
                    Endpoint = sqsQueueArn
                }).SubscriptionArn;

                subscriptionArns.Add(topicArn, arn);
            }

            var setAttributes = new Dictionary <string, string> {
                { "Policy", policy.ToJson() }
            };

            sqsClient.SetAttributes(sqsQueueUrl, setAttributes);

            return(subscriptionArns);
        }
        /// <summary>
        /// Subscribes an existing Amazon SQS queue to an existing Amazon SNS topic asynchronously.
        /// <para>
        /// The policy applied to the SQS queue is similar to this:
        /// <code>
        /// {
        ///     "Version" : "2008-10-17",
        ///     "Statement" : [{
        ///         "Sid" : "topic-subscription-arn:aws:sns:us-west-2:599109622955:myTopic",
        ///         "Effect" : "Allow",
        ///         "Principal" : "*",
        ///         "Action" : ["sqs:SendMessage"],
        ///         "Resource":["arn:aws:sqs:us-west-2:599109622955:myQueue"],
        ///         "Condition" : {
        ///             "ArnLike":{
        ///                 "aws:SourceArn":["arn:aws:sns:us-west-2:599109622955:myTopic"]
        ///             }
        ///         }
        ///     }]
        /// }
        /// </code>
        /// </para>
        /// <para>
        /// There might be a small time period immediately after
        /// subscribing the SQS queue to the SNS topic and updating the SQS queue's
        /// policy, where messages are not able to be delivered to the queue. After a
        /// moment, the new queue policy will propagate and the queue will be able to
        /// receive messages. This delay only occurs immediately after initially
        /// subscribing the queue.
        /// </para>
        /// </summary>
        /// <param name="topicArn">The topic to subscribe to</param>
        /// <param name="sqsClient">The SQS client used to get attributes and set the policy on the SQS queue.</param>
        /// <param name="sqsQueueUrl">The queue to add a subscription to.</param>
        /// <returns>The subscription ARN as returned by Amazon SNS when the queue is
        /// successfully subscribed to the topic.</returns>
        public async Task <string> SubscribeQueueAsync(string topicArn, ICoreAmazonSQS sqsClient, string sqsQueueUrl)
        {
            var topics = await SubscribeQueueToTopicsAsync(new List <string>() { topicArn }, sqsClient, sqsQueueUrl).ConfigureAwait(false);

            return(topics.Values.First());
        }
Ejemplo n.º 8
0
        public async void TestQueueSubscription()
        {
            // create new topic
            var topicName          = UtilityMethods.GenerateName("TestQueueSubscription");
            var createTopicRequest = new CreateTopicRequest
            {
                Name = topicName
            };
            var createTopicResult = await Client.CreateTopicAsync(createTopicRequest);

            var topicArn = createTopicResult.TopicArn;

            _topicArns.Add(topicArn);

            var queueName = UtilityMethods.GenerateName("TestQueueSubscription");
            var queueUrl  = (await sqsClient.CreateQueueAsync(new CreateQueueRequest
            {
                QueueName = queueName
            })).QueueUrl;

            _queueUrl.Add(queueUrl);

            ICoreAmazonSQS coreSqs         = sqsClient as ICoreAmazonSQS;
            var            subscriptionARN = await Client.SubscribeQueueAsync(topicArn, coreSqs, queueUrl);

            // Sleep to wait for the subscribe to complete.
            Thread.Sleep(TimeSpan.FromSeconds(5));

            var publishRequest = new PublishRequest
            {
                TopicArn          = topicArn,
                Subject           = "Test Subject",
                Message           = "Test Message",
                MessageAttributes = new Dictionary <string, SNSMessageAttributeValue>
                {
                    { "Color", new SNSMessageAttributeValue {
                          StringValue = "Red", DataType = "String"
                      } },
                    { "Binary", new SNSMessageAttributeValue {
                          DataType = "Binary", BinaryValue = new MemoryStream(Encoding.UTF8.GetBytes("Yes please"))
                      } },
                    { "Prime", new SNSMessageAttributeValue {
                          StringValue = "31", DataType = "Number"
                      } },
                }
            };
            await Client.PublishAsync(publishRequest);

            var messages = (await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest
            {
                QueueUrl = queueUrl,
                WaitTimeSeconds = 20
            })).Messages;

            Assert.Equal(1, messages.Count);
            var message = messages[0];

            string bodyJson;

            // Handle some accounts returning message body as base 64 encoded.
            if (message.Body.Trim()[0] == '{')
            {
                bodyJson = message.Body;
            }
            else
            {
                bodyJson = Encoding.UTF8.GetString(Convert.FromBase64String(message.Body));
            }

            var json           = ThirdParty.Json.LitJson.JsonMapper.ToObject(bodyJson);
            var messageText    = json["Message"];
            var messageSubject = json["Subject"];

            Assert.Equal(publishRequest.Message, messageText.ToString());
            Assert.Equal(publishRequest.Subject, messageSubject.ToString());
            var messageAttributes = json["MessageAttributes"];

            Assert.Equal(publishRequest.MessageAttributes.Count, messageAttributes.Count);
            foreach (var ma in publishRequest.MessageAttributes)
            {
                var name  = ma.Key;
                var value = ma.Value;
                Assert.True(messageAttributes.PropertyNames.Contains(name, StringComparer.Ordinal));
                var jsonAttribute = messageAttributes[name];
                var jsonType      = jsonAttribute["Type"].ToString();
                var jsonValue     = jsonAttribute["Value"].ToString();
                Assert.NotNull(jsonType);
                Assert.NotNull(jsonValue);
                Assert.Equal(value.DataType, jsonType);
                Assert.Equal(value.DataType != "Binary"
                                    ? value.StringValue
                                    : Convert.ToBase64String(value.BinaryValue.ToArray()), jsonValue);
            }

            await sqsClient.DeleteMessageAsync(new DeleteMessageRequest
            {
                QueueUrl      = queueUrl,
                ReceiptHandle = messages[0].ReceiptHandle
            });

            // This will unsubscribe but leave the policy in place.
            await Client.UnsubscribeAsync(new UnsubscribeRequest
            {
                SubscriptionArn = subscriptionARN
            });

            // Subscribe again to see if this affects the policy.
            await Client.SubscribeQueueAsync(topicArn, coreSqs, queueUrl);

            await Client.PublishAsync(new PublishRequest
            {
                TopicArn = topicArn,
                Message  = "Test Message again"
            });

            messages = (await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest
            {
                QueueUrl = queueUrl,
                WaitTimeSeconds = 20
            })).Messages;

            Assert.Equal(1, messages.Count);

            var response = WaitUtils.WaitForComplete(
                () => {
                return(sqsClient.GetQueueAttributesAsync(new GetQueueAttributesRequest
                {
                    AttributeNames = new List <string> {
                        "All"
                    },
                    QueueUrl = queueUrl
                }).Result);
            },
                (r) =>
            {
                return(!string.IsNullOrEmpty(r.Policy));
            });

            var policy = Policy.FromJson(response.Policy);

            Assert.Equal(1, policy.Statements.Count);
        }
 /// <summary>
 /// Subscribes an existing Amazon SQS queue to an existing Amazon SNS topic asynchronously.
 /// <para>
 /// The policy applied to the SQS queue is similar to this:
 /// <code>
 /// {
 /// 	"Version" : "2008-10-17",
 /// 	"Statement" : [{
 /// 	    "Sid" : "topic-subscription-arn:aws:sns:us-west-2:599109622955:myTopic",
 /// 		"Effect" : "Allow",
 /// 		"Principal" : "*",
 /// 		"Action" : ["sqs:SendMessage"],
 /// 		"Resource":["arn:aws:sqs:us-west-2:599109622955:myQueue"],
 /// 		"Condition" : {
 /// 			"ArnLike":{
 /// 				"aws:SourceArn":["arn:aws:sns:us-west-2:599109622955:myTopic"]
 /// 			}
 /// 		}
 ///     }]
 /// }
 /// </code>
 /// </para>
 /// <para>
 /// There might be a small time period immediately after
 /// subscribing the SQS queue to the SNS topic and updating the SQS queue's
 /// policy, where messages are not able to be delivered to the queue. After a
 /// moment, the new queue policy will propagate and the queue will be able to
 /// receive messages. This delay only occurs immediately after initially
 /// subscribing the queue.
 /// </para>
 /// </summary>
 /// <param name="topicArn">The topic to subscribe to</param>
 /// <param name="sqsClient">The SQS client used to get attributes and set the policy on the SQS queue.</param>
 /// <param name="sqsQueueUrl">The queue to add a subscription to.</param>
 /// <returns>The subscription ARN as returned by Amazon SNS when the queue is 
 /// successfully subscribed to the topic.</returns>
 public async Task<string> SubscribeQueueAsync(string topicArn, ICoreAmazonSQS sqsClient, string sqsQueueUrl)
 {
     var topics = await SubscribeQueueToTopicsAsync(new List<string>() { topicArn }, sqsClient, sqsQueueUrl).ConfigureAwait(false);
     return topics.Values.First();
 }
        /// <summary>
        /// Subscribes an existing Amazon SQS queue to existing Amazon SNS topics asynchronously.
        /// <para>
        /// The policy applied to the SQS queue is similar to this:
        /// <code>
        /// {
        /// 	"Version" : "2008-10-17",
        /// 	"Statement" : [{
        /// 	    "Sid" : "topic-subscription-arn:aws:sns:us-west-2:599109622955:myTopic",
        /// 		"Effect" : "Allow",
        /// 		"Principal" : "*",
        /// 		"Action" : ["sqs:SendMessage"],
        /// 		"Resource":["arn:aws:sqs:us-west-2:599109622955:myQueue"],
        /// 		"Condition" : {
        /// 			"ArnLike":{
        /// 				"aws:SourceArn":["arn:aws:sns:us-west-2:599109622955:myTopic"]
        /// 			}
        /// 		}
        ///     }]
        /// }
        /// </code>
        /// </para>
        /// <para>
        /// There might be a small time period immediately after
        /// subscribing the SQS queue to the SNS topic and updating the SQS queue's
        /// policy, where messages are not able to be delivered to the queue. After a
        /// moment, the new queue policy will propagate and the queue will be able to
        /// receive messages. This delay only occurs immediately after initially
        /// subscribing the queue.
        /// </para>
        /// </summary>
        /// <param name="topicArns">The topics to subscribe to</param>
        /// <param name="sqsClient">The SQS client used to get attributes and set the policy on the SQS queue.</param>
        /// <param name="sqsQueueUrl">The queue to add a subscription to.</param>
        /// <returns>The mapping of topic ARNs to subscription ARNs as returned by Amazon SNS when the queue is 
        /// successfully subscribed to each topic.</returns>
        public async Task<IDictionary<string, string>> SubscribeQueueToTopicsAsync(IList<string> topicArns, ICoreAmazonSQS sqsClient, string sqsQueueUrl)
        {
            // Get the queue's existing policy and ARN
            var queueAttributes = await sqsClient.GetAttributesAsync(sqsQueueUrl).ConfigureAwait(false);

            string sqsQueueArn = queueAttributes["QueueArn"];

            Policy policy;
            string policyStr = null;
            if(queueAttributes.ContainsKey("Policy"))
                policyStr = queueAttributes["Policy"];
            if (string.IsNullOrEmpty(policyStr))
                policy = new Policy();
            else
                policy = Policy.FromJson(policyStr);

            var subscriptionArns = new Dictionary<string,string>();

            foreach (var topicArn in topicArns)
            {
                if (!HasSQSPermission(policy, topicArn, sqsQueueArn))
                    AddSQSPermission(policy, topicArn, sqsQueueArn);

                SubscribeResponse response = await this.SubscribeAsync(new SubscribeRequest
                {
                    TopicArn = topicArn,
                    Protocol = "sqs",
                    Endpoint = sqsQueueArn,
                }).ConfigureAwait(false);
                subscriptionArns.Add(topicArn, response.SubscriptionArn);
            }

            var setAttributes = new Dictionary<string, string> { { "Policy", policy.ToJson() } };
            await sqsClient.SetAttributesAsync(sqsQueueUrl, setAttributes).ConfigureAwait(false);

            return subscriptionArns;
        }
 /// <summary>
 /// Subscribes an existing Amazon SQS queue to an existing Amazon SNS topic.
 /// <para>
 /// The policy applied to the SQS queue is similar to this:
 /// <code>
 /// {
 /// 	"Version" : "2008-10-17",
 /// 	"Statement" : [{
 /// 	    "Sid" : "topic-subscription-arn:aws:sns:us-west-2:599109622955:myTopic",
 /// 		"Effect" : "Allow",
 /// 		"Principal" : "*",
 /// 		"Action" : ["sqs:SendMessage"],
 /// 		"Resource":["arn:aws:sqs:us-west-2:599109622955:myQueue"],
 /// 		"Condition" : {
 /// 			"ArnLike":{
 /// 				"aws:SourceArn":["arn:aws:sns:us-west-2:599109622955:myTopic"]
 /// 			}
 /// 		}
 ///     }]
 /// }
 /// </code>
 /// </para>
 /// <para>
 /// There might be a small time period immediately after
 /// subscribing the SQS queue to the SNS topic and updating the SQS queue's
 /// policy, where messages are not able to be delivered to the queue. After a
 /// moment, the new queue policy will propagate and the queue will be able to
 /// receive messages. This delay only occurs immediately after initially
 /// subscribing the queue.
 /// </para>
 /// </summary>
 /// <param name="topicArn">The topic to subscribe to</param>
 /// <param name="sqsClient">The SQS client used to get attributes and set the policy on the SQS queue.</param>
 /// <param name="sqsQueueUrl">The queue to add a subscription to.</param>
 /// <returns>The subscription ARN as returned by Amazon SNS when the queue is 
 /// successfully subscribed to the topic.</returns>
 public string SubscribeQueue(string topicArn, ICoreAmazonSQS sqsClient, string sqsQueueUrl)
 {
     return SubscribeQueueToTopics(new List<string>() { topicArn }, sqsClient, sqsQueueUrl).Values.First();
 }
Ejemplo n.º 12
0
 public Task <IDictionary <string, string> > SubscribeQueueToTopicsAsync(IList <string> topicArns, ICoreAmazonSQS sqsClient, string sqsQueueUrl)
 {
     throw new System.NotImplementedException();
 }
Ejemplo n.º 13
0
 public Task <string> SubscribeQueueAsync(string topicArn, ICoreAmazonSQS sqsClient, string sqsQueueUrl)
 {
     throw new System.NotImplementedException();
 }
Ejemplo n.º 14
0
 public IDictionary <string, string> SubscribeQueueToTopics(IList <string> topicArns, ICoreAmazonSQS sqsClient, string sqsQueueUrl)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 15
0
 public string SubscribeQueue(string topicArn, ICoreAmazonSQS sqsClient, string sqsQueueUrl)
 {
     throw new NotImplementedException();
 }