public async Task <string> CreateTopic(Topology.Entities.Topic topic) { lock (_lock) { if (_topicArns.TryGetValue(topic.EntityName, out var result)) { return(result); } } var request = new CreateTopicRequest(topic.EntityName) { Attributes = topic.TopicAttributes.ToDictionary(x => x.Key, x => x.Value.ToString()), Tags = topic.TopicTags.Select(x => new Tag { Key = x.Key, Value = x.Value }).ToList() }; TransportLogMessages.CreateTopic(topic.EntityName); var response = await _amazonSns.CreateTopicAsync(request, _cancellationToken).ConfigureAwait(false); EnsureSuccessfulResponse(response); var topicArn = response.TopicArn; lock (_lock) _topicArns[topic.EntityName] = topicArn; await Task.Delay(500, _cancellationToken).ConfigureAwait(false); return(topicArn); }
async Task ClientContext.DeleteTopic(Topic topic) { var topicArn = await CreateTopic(topic).ConfigureAwait(false); var response = await _amazonSns.DeleteTopicAsync(topicArn).ConfigureAwait(false); EnsureSuccessfulResponse(response); }
async Task ClientContext.DeleteTopic(Topology.Entities.Topic topic) { var topicArn = await CreateTopic(topic).ConfigureAwait(false); TransportLogMessages.DeleteTopic(topicArn); var response = await _amazonSns.DeleteTopicAsync(topicArn, _cancellationToken).ConfigureAwait(false); EnsureSuccessfulResponse(response); }
async Task ClientContext.CreateQueueSubscription(Topology.Entities.Topic topic, Queue queue) { var topicInfo = await _topicCache.Get(topic).ConfigureAwait(false); var queueInfo = await _queueCache.Get(queue).ConfigureAwait(false); Dictionary <string, string> subscriptionAttributes = topic.TopicSubscriptionAttributes.Select(x => (x.Key, x.Value.ToString())) .Concat(queue.QueueSubscriptionAttributes.Select(x => (x.Key, x.Value.ToString()))) .ToDictionary(x => x.Item1, x => x.Item2); var subscribeRequest = new SubscribeRequest { TopicArn = topicInfo.Arn, Endpoint = queueInfo.Arn, Protocol = "sqs", Attributes = subscriptionAttributes }; var response = await _amazonSns.SubscribeAsync(subscribeRequest, _cancellationToken).ConfigureAwait(false); response.EnsureSuccessfulResponse(); queueInfo.SubscriptionArns.Add(response.SubscriptionArn); var sqsQueueArn = queueInfo.Arn; var topicArnPattern = topicInfo.Arn.Substring(0, topicInfo.Arn.LastIndexOf(':') + 1) + "*"; queueInfo.Attributes.TryGetValue(QueueAttributeName.Policy, out var policyValue); var policy = string.IsNullOrEmpty(policyValue) ? new Policy() : Policy.FromJson(policyValue); if (!QueueHasTopicPermission(policy, topicArnPattern, sqsQueueArn)) { var statement = new Statement(Statement.StatementEffect.Allow); #pragma warning disable 618 statement.Actions.Add(SQSActionIdentifiers.SendMessage); #pragma warning restore 618 statement.Resources.Add(new Resource(sqsQueueArn)); statement.Conditions.Add(ConditionFactory.NewSourceArnCondition(topicArnPattern)); statement.Principals.Add(new Principal("*")); policy.Statements.Add(statement); var jsonPolicy = policy.ToJson(); var setAttributes = new Dictionary <string, string> { { QueueAttributeName.Policy, jsonPolicy } }; var setAttributesResponse = await _amazonSqs.SetQueueAttributesAsync(queueInfo.Url, setAttributes, _cancellationToken).ConfigureAwait(false); setAttributesResponse.EnsureSuccessfulResponse(); queueInfo.Attributes[QueueAttributeName.Policy] = jsonPolicy; } }
async Task ClientContext.DeleteTopic(Topology.Entities.Topic topic) { var topicInfo = await _topicCache.Get(topic, _cancellationToken).ConfigureAwait(false); TransportLogMessages.DeleteTopic(topicInfo.Arn); var response = await _amazonSns.DeleteTopicAsync(topicInfo.Arn, _cancellationToken).ConfigureAwait(false); response.EnsureSuccessfulResponse(); _topicCache.RemoveByName(topic.EntityName); }
async Task ClientContext.CreateQueueSubscription(Topic topic, Queue queue) { var results = await Task.WhenAll(CreateTopic(topic), CreateQueue(queue)).ConfigureAwait(false); var topicArn = results[0]; var queueUrl = results[1]; var response = await _amazonSns.SubscribeQueueAsync(topicArn, _amazonSqs, queueUrl).ConfigureAwait(false); await Task.Delay(500).ConfigureAwait(false); await _amazonSns.SetSubscriptionAttributesAsync(response, "RawMessageDelivery", "true").ConfigureAwait(false); }
async Task ClientContext.CreateQueueSubscription(Topology.Entities.Topic topic, Queue queue) { string[] results = await Task.WhenAll(CreateTopic(topic), CreateQueue(queue)).ConfigureAwait(false); var topicArn = results[0]; var queueUrl = results[1]; Dictionary <string, string> queueAttributes = await _amazonSqs.GetAttributesAsync(queueUrl).ConfigureAwait(false); var queueArn = queueAttributes[QueueAttributeName.QueueArn]; IDictionary <string, object> topicSubscriptionAttributes = topic.TopicSubscriptionAttributes; IDictionary <string, object> queueSubscriptionAttributes = queue.QueueSubscriptionAttributes; var subscriptionAttributes = new Dictionary <string, string>(); topicSubscriptionAttributes.ToList().ForEach(x => subscriptionAttributes[x.Key] = x.Value.ToString()); queueSubscriptionAttributes.ToList().ForEach(x => subscriptionAttributes[x.Key] = x.Value.ToString()); var subscribeRequest = new SubscribeRequest { TopicArn = topicArn, Endpoint = queueArn, Protocol = "sqs", Attributes = subscriptionAttributes }; var response = await _amazonSns.SubscribeAsync(subscribeRequest, _cancellationToken).ConfigureAwait(false); EnsureSuccessfulResponse(response); var sqsQueueArn = queueAttributes[QueueAttributeName.QueueArn]; var topicArnPattern = topicArn.Substring(0, topicArn.LastIndexOf(':') + 1) + "*"; queueAttributes.TryGetValue(QueueAttributeName.Policy, out var policyStr); var policy = string.IsNullOrEmpty(policyStr) ? new Policy() : Policy.FromJson(policyStr); if (!QueueHasTopicPermission(policy, topicArnPattern, sqsQueueArn)) { var statement = new Statement(Statement.StatementEffect.Allow); statement.Actions.Add(SQSActionIdentifiers.SendMessage); statement.Resources.Add(new Resource(sqsQueueArn)); statement.Conditions.Add(ConditionFactory.NewSourceArnCondition(topicArnPattern)); statement.Principals.Add(new Principal("*")); policy.Statements.Add(statement); var setAttributes = new Dictionary <string, string> { { QueueAttributeName.Policy, policy.ToJson() } }; await _amazonSqs.SetAttributesAsync(queueUrl, setAttributes).ConfigureAwait(false); } }
public async Task <string> CreateTopic(Topic topic) { lock (_lock) if (_topicArns.TryGetValue(topic.EntityName, out var result)) { return(result); } var response = await _amazonSns.CreateTopicAsync(topic.EntityName).ConfigureAwait(false); await Task.Delay(500).ConfigureAwait(false); var topicArn = response.TopicArn; lock (_lock) _topicArns[topic.EntityName] = topicArn; return(topicArn); }
public async Task <string> CreateTopic(Topic topic) { lock (_lock) if (_topicArns.TryGetValue(topic.EntityName, out var result)) { return(result); } var request = new CreateTopicRequest(topic.EntityName) { Attributes = topic.TopicAttributes.ToDictionary(x => x.Key, x => x.Value.ToString()) }; var response = await _amazonSns.CreateTopicAsync(request).ConfigureAwait(false); await Task.Delay(500).ConfigureAwait(false); var topicArn = response.TopicArn; lock (_lock) _topicArns[topic.EntityName] = topicArn; return(topicArn); }
public Task <TopicInfo> CreateTopic(Topology.Entities.Topic topic) { return(_topicCache.Get(topic, _cancellationToken)); }
Task ClientContext.DeleteTopic(Topology.Entities.Topic topic) { return(_context.DeleteTopic(topic)); }
Task ClientContext.CreateQueueSubscription(Topology.Entities.Topic topic, Queue queue) { return(_context.CreateQueueSubscription(topic, queue)); }
Task <TopicInfo> ClientContext.CreateTopic(Topology.Entities.Topic topic) { return(_context.CreateTopic(topic)); }
public Task <TopicInfo> CreateTopic(Topology.Entities.Topic topic) { return(_topicCache.Get(topic)); }
async Task ClientContext.DeleteTopic(Topic topic) { var topicArn = await CreateTopic(topic).ConfigureAwait(false); await _amazonSns.DeleteTopicAsync(topicArn).ConfigureAwait(false); }