public async Task Then_An_Exception_Is_Not_Thrown()
        {
            // Arrange
            string            topicName     = Guid.NewGuid().ToString();
            ILoggerFactory    loggerFactory = OutputHelper.ToLoggerFactory();
            IAwsClientFactory clientFactory = CreateClientFactory();

            var subjectProvider       = new NonGenericMessageSubjectProvider();
            var serializationRegister = new MessageSerializationRegister(subjectProvider);

            var client = clientFactory.GetSnsClient(Region);

            var topic = new SnsTopicByName(
                topicName,
                client,
                serializationRegister,
                loggerFactory,
                subjectProvider);

            // Act and Assert
            (await topic.CreateAsync()).ShouldBeTrue();
            (await topic.CreateAsync()).ShouldBeTrue();

            topic.Arn.ShouldNotBeNull();
            topic.Arn.ShouldEndWith(topic.TopicName);
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        void IPublicationBuilder <T> .Configure(
            JustSayingBus bus,
            IAwsClientFactoryProxy proxy,
            ILoggerFactory loggerFactory)
        {
            var logger = loggerFactory.CreateLogger <TopicPublicationBuilder <T> >();

            logger.LogInformation("Adding SNS publisher for message type '{MessageType}'.",
                                  typeof(T));

            var config = bus.Config;

            var readConfiguration  = new SqsReadConfiguration(SubscriptionType.ToTopic);
            var writeConfiguration = new SnsWriteConfiguration();

            ConfigureWrites?.Invoke(writeConfiguration);
            readConfiguration.ApplyTopicNamingConvention <T>(config.TopicNamingConvention);

            bus.SerializationRegister.AddSerializer <T>();

            foreach (var region in config.Regions)
            {
                // TODO pass region down into topic creation for when we have foreign topics so we can generate the arn
                var eventPublisher = new SnsTopicByName(
                    readConfiguration.TopicName,
                    proxy.GetAwsClientFactory().GetSnsClient(RegionEndpoint.GetBySystemName(region)),
                    bus.SerializationRegister,
                    loggerFactory,
                    writeConfiguration,
                    config.MessageSubjectProvider)
                {
                    MessageResponseLogger = config.MessageResponseLogger
                };

                async Task StartupTask()
                {
                    if (writeConfiguration.Encryption != null)
                    {
                        await eventPublisher.CreateWithEncryptionAsync(writeConfiguration.Encryption)
                        .ConfigureAwait(false);
                    }
                    else
                    {
                        await eventPublisher.CreateAsync().ConfigureAwait(false);
                    }

                    await eventPublisher.EnsurePolicyIsUpdatedAsync(config.AdditionalSubscriberAccounts)
                    .ConfigureAwait(false);
                }

                bus.AddStartupTask(StartupTask());

                bus.AddMessagePublisher <T>(eventPublisher, region);
            }

            logger.LogInformation(
                "Created SNS topic publisher on topic '{TopicName}' for message type '{MessageType}'.",
                readConfiguration.TopicName,
                typeof(T));
        }
Exemplo n.º 3
0
        protected override void When()
        {
            var snsClient = new NoTopicCreationAwsClientFactory().GetSnsClient(RegionEndpoint.EUWest1);

            _topic = new SnsTopicByName(UniqueName, snsClient, new MessageSerialisationRegister(), new LoggerFactory());
            _createWasSuccessful = _topic.CreateAsync().GetAwaiter().GetResult();
        }
        public async Task Arn_Still_Retrieved_When_It_Already_Exists()
        {
            // Arrange
            string         topicName     = Guid.NewGuid().ToString();
            ILoggerFactory loggerFactory = OutputHelper.ToLoggerFactory();

            var subjectProvider       = new NonGenericMessageSubjectProvider();
            var serializerFactor      = new NewtonsoftSerializationFactory();
            var serializationRegister = new MessageSerializationRegister(subjectProvider, serializerFactor);

            IAmazonSimpleNotificationService client = CreateSnsClient(exists: true);

            var topic = new SnsTopicByName(
                topicName,
                client,
                serializationRegister,
                loggerFactory,
                subjectProvider);

            // Act
            await topic.CreateAsync();

            // Assert
            topic.Arn.ShouldNotBeNull();
        }
        public async Task Cannot_Create_Topic_Because_It_Exists()
        {
            // Arrange
            string         topicName     = Guid.NewGuid().ToString();
            ILoggerFactory loggerFactory = OutputHelper.ToLoggerFactory();

            var subjectProvider       = new NonGenericMessageSubjectProvider();
            var serializerFactor      = new NewtonsoftSerializationFactory();
            var serializationRegister = new MessageSerializationRegister(subjectProvider, serializerFactor);

            IAmazonSimpleNotificationService client = CreateSnsClient(exists: true);

            var topic = new SnsTopicByName(
                topicName,
                client,
                serializationRegister,
                loggerFactory,
                subjectProvider);

            // Act
            bool actual = await topic.CreateAsync();

            // Assert
            actual.ShouldBeFalse();
            topic.Arn.ShouldNotBeNull();
        }
Exemplo n.º 6
0
        public async Task <SqsQueueByName> EnsureTopicExistsWithQueueSubscribedAsync(string region, IMessageSerialisationRegister serialisationRegister, SqsReadConfiguration queueConfig)
        {
            var regionEndpoint = RegionEndpoint.GetBySystemName(region);
            var sqsClient      = _awsClientFactory.GetAwsClientFactory().GetSqsClient(regionEndpoint);
            var snsClient      = _awsClientFactory.GetAwsClientFactory().GetSnsClient(regionEndpoint);

            var queue = await EnsureQueueExistsAsync(region, queueConfig).ConfigureAwait(false);

            if (TopicExistsInAnotherAccount(queueConfig))
            {
                var arnProvider = new ForeignTopicArnProvider(regionEndpoint, queueConfig.TopicSourceAccount, queueConfig.PublishEndpoint);

                var topicArn = await arnProvider.GetArnAsync().ConfigureAwait(false);

                await snsClient.SubscribeQueueAsync(topicArn, sqsClient, queue.Url).ConfigureAwait(false);
            }
            else
            {
                var eventTopic = new SnsTopicByName(queueConfig.PublishEndpoint, snsClient, serialisationRegister, _loggerFactory);
                await eventTopic.CreateAsync().ConfigureAwait(false);

                await EnsureQueueIsSubscribedToTopic(eventTopic, queue).ConfigureAwait(false);

                await SqsPolicy.SaveAsync(eventTopic.Arn, queue.Arn, queue.Url, sqsClient).ConfigureAwait(false);
            }

            return(queue);
        }
Exemplo n.º 7
0
        public QueueWithAsyncStartup EnsureTopicExistsWithQueueSubscribed(
            string region,
            SqsReadConfiguration queueConfig)
        {
            var regionEndpoint = RegionEndpoint.GetBySystemName(region);
            var sqsClient      = _awsClientFactory.GetAwsClientFactory().GetSqsClient(regionEndpoint);
            var snsClient      = _awsClientFactory.GetAwsClientFactory().GetSnsClient(regionEndpoint);

            var queueWithStartup = EnsureQueueExists(region, queueConfig);

            async Task StartupTask(CancellationToken cancellationToken)
            {
                await queueWithStartup.StartupTask.Invoke(cancellationToken).ConfigureAwait(false);

                var queue = queueWithStartup.Queue;

                if (TopicExistsInAnotherAccount(queueConfig))
                {
                    var arnProvider = new ForeignTopicArnProvider(regionEndpoint,
                                                                  queueConfig.TopicSourceAccount,
                                                                  queueConfig.PublishEndpoint);

                    var topicArn = await arnProvider.GetArnAsync().ConfigureAwait(false);
                    await SubscribeQueueAndApplyFilterPolicyAsync(snsClient,
                                                                  topicArn,
                                                                  sqsClient,
                                                                  queue.Uri,
                                                                  queueConfig.FilterPolicy).ConfigureAwait(false);
                }
                else
                {
#pragma warning disable 618
                    var eventTopic = new SnsTopicByName(queueConfig.PublishEndpoint, snsClient, _loggerFactory);
#pragma warning restore 618
                    await eventTopic.CreateAsync(cancellationToken).ConfigureAwait(false);

                    await SubscribeQueueAndApplyFilterPolicyAsync(snsClient,
                                                                  eventTopic.Arn,
                                                                  sqsClient,
                                                                  queue.Uri,
                                                                  queueConfig.FilterPolicy).ConfigureAwait(false);

                    var sqsDetails = new SqsPolicyDetails
                    {
                        SourceArn = eventTopic.Arn,
                        QueueArn  = queue.Arn,
                        QueueUri  = queue.Uri
                    };
                    await SqsPolicy
                    .SaveAsync(sqsDetails, sqsClient)
                    .ConfigureAwait(false);
                }
            }

            return(new QueueWithAsyncStartup(StartupTask, queueWithStartup.Queue));
        }
        public QueueWithAsyncStartup <SqsQueueByName> EnsureTopicExistsWithQueueSubscribed(
            string region,
            IMessageSerializationRegister serializationRegister,
            SqsReadConfiguration queueConfig,
            IMessageSubjectProvider messageSubjectProvider)
        {
            var regionEndpoint = RegionEndpoint.GetBySystemName(region);
            var sqsClient      = _awsClientFactory.GetAwsClientFactory().GetSqsClient(regionEndpoint);
            var snsClient      = _awsClientFactory.GetAwsClientFactory().GetSnsClient(regionEndpoint);

            var queueWithStartup = EnsureQueueExists(region, queueConfig);

            async Task StartupTask()
            {
                await queueWithStartup.StartupTask.ConfigureAwait(false);

                var queue = queueWithStartup.Queue;

                if (TopicExistsInAnotherAccount(queueConfig))
                {
                    var arnProvider = new ForeignTopicArnProvider(regionEndpoint,
                                                                  queueConfig.TopicSourceAccount,
                                                                  queueConfig.PublishEndpoint);

                    var topicArn = await arnProvider.GetArnAsync().ConfigureAwait(false);
                    await SubscribeQueueAndApplyFilterPolicyAsync(snsClient,
                                                                  topicArn,
                                                                  sqsClient,
                                                                  queue.Uri,
                                                                  queueConfig.FilterPolicy).ConfigureAwait(false);
                }
                else
                {
                    var eventTopic = new SnsTopicByName(queueConfig.PublishEndpoint,
                                                        snsClient,
                                                        serializationRegister,
                                                        _loggerFactory,
                                                        messageSubjectProvider);
                    await eventTopic.CreateAsync().ConfigureAwait(false);

                    await SubscribeQueueAndApplyFilterPolicyAsync(snsClient,
                                                                  eventTopic.Arn,
                                                                  sqsClient,
                                                                  queue.Uri,
                                                                  queueConfig.FilterPolicy).ConfigureAwait(false);

                    await SqsPolicy
                    .SaveAsync(eventTopic.Arn, queue.Arn, queue.Uri, sqsClient)
                    .ConfigureAwait(false);
                }
            }

            // This StartupTask is intentionally not awaited, as it will be run when the bus is started.
            return(new QueueWithAsyncStartup <SqsQueueByName>(StartupTask(), queueWithStartup.Queue));
        }
Exemplo n.º 9
0
        protected override async Task When()
        {
            _topic = new SnsTopicByName(
                UniqueName,
                Client,
                new MessageSerialisationRegister(new NonGenericMessageSubjectProvider()),
                LoggerFactory,
                new NonGenericMessageSubjectProvider());

            _createWasSuccessful = await _topic.CreateAsync();
        }
Exemplo n.º 10
0
 private static void CreatePublisher(SnsTopicByName eventPublisher, SnsWriteConfiguration snsWriteConfig)
 {
     if (snsWriteConfig.Encryption != null)
     {
         eventPublisher.CreateWithEncryptionAsync(snsWriteConfig.Encryption).GetAwaiter().GetResult();
     }
     else
     {
         eventPublisher.CreateAsync().GetAwaiter().GetResult();
     }
 }
        protected override async Task When()
        {
            var snsClient = new NoTopicCreationAwsClientFactory().GetSnsClient(Region);

            _topic = new SnsTopicByName(
                UniqueName,
                snsClient,
                new MessageSerializationRegister(new NonGenericMessageSubjectProvider()),
                LoggerFactory,
                new NonGenericMessageSubjectProvider());

            _createWasSuccessful = await _topic.CreateAsync();
        }
Exemplo n.º 12
0
        public async Task Then_An_Exception_Is_Not_Thrown()
        {
            // Arrange
            string            topicName     = Guid.NewGuid().ToString();
            ILoggerFactory    loggerFactory = OutputHelper.ToLoggerFactory();
            IAwsClientFactory clientFactory = CreateClientFactory();

            var client = clientFactory.GetSnsClient(Region);

            var topic = new SnsTopicByName(
                topicName,
                client,
                loggerFactory);

            // Shouldn't throw
            await topic.CreateAsync(CancellationToken.None);

            await topic.CreateAsync(CancellationToken.None);

            topic.Arn.ShouldNotBeNull();
            topic.Arn.ShouldEndWith(topic.TopicName);
        }
Exemplo n.º 13
0
    public async Task Cannot_Create_Topic_Because_Not_Authorized()
    {
        // Arrange
        string         topicName     = Guid.NewGuid().ToString();
        ILoggerFactory loggerFactory = OutputHelper.ToLoggerFactory();

        IAmazonSimpleNotificationService client = CreateSnsClient(exists: false);

        var topic = new SnsTopicByName(
            topicName,
            client,
            loggerFactory);

        // Act and Assert
        await Assert.ThrowsAsync <InvalidOperationException>(() => topic.CreateAsync(CancellationToken.None));
    }
Exemplo n.º 14
0
    public async Task Arn_Still_Retrieved_When_It_Already_Exists()
    {
        // Arrange
        string         topicName     = Guid.NewGuid().ToString();
        ILoggerFactory loggerFactory = OutputHelper.ToLoggerFactory();

        IAmazonSimpleNotificationService client = CreateSnsClient(exists: true);

        var topic = new SnsTopicByName(
            topicName,
            client,
            loggerFactory);

        // Act
        await topic.CreateAsync(CancellationToken.None);

        // Assert
        topic.Arn.ShouldNotBeNull();
    }
Exemplo n.º 15
0
        private IHaveFulfilledPublishRequirements AddSnsMessagePublisher <T>(Action <SnsWriteConfiguration> configBuilder) where T : Message
        {
            _log.LogInformation("Adding SNS publisher for message type '{MessageType}'.",
                                typeof(T));

            var snsWriteConfig = new SnsWriteConfiguration();

            configBuilder?.Invoke(snsWriteConfig);

            _subscriptionConfig.Topic = typeof(T).ToTopicName();
            var namingStrategy = GetNamingStrategy();

            Bus.SerializationRegister.AddSerializer <T>(_serializationFactory.GetSerializer <T>());

            var topicName = namingStrategy.GetTopicName(_subscriptionConfig.BaseTopicName, typeof(T));

            foreach (var region in Bus.Config.Regions)
            {
                // TODO pass region down into topic creation for when we have foreign topics so we can generate the arn
                var eventPublisher = new SnsTopicByName(
                    topicName,
                    _awsClientFactoryProxy.GetAwsClientFactory().GetSnsClient(RegionEndpoint.GetBySystemName(region)),
                    Bus.SerializationRegister,
                    _loggerFactory, snsWriteConfig,
                    Bus.Config.MessageSubjectProvider)
                {
                    MessageResponseLogger = Bus.Config.MessageResponseLogger
                };

                eventPublisher.CreateAsync().GetAwaiter().GetResult();

                eventPublisher.EnsurePolicyIsUpdatedAsync(Bus.Config.AdditionalSubscriberAccounts).GetAwaiter().GetResult();

                Bus.AddMessagePublisher <T>(eventPublisher, region);
            }

            _log.LogInformation("Created SNS topic publisher on topic '{TopicName}' for message type '{MessageType}'.",
                                _subscriptionConfig.Topic, typeof(T));

            return(this);
        }
Exemplo n.º 16
0
        public async Task Can_Add_Encryption_To_Existing_Topic()
        {
            // Arrange
            ILoggerFactory    loggerFactory = OutputHelper.ToLoggerFactory();
            IAwsClientFactory clientFactory = CreateClientFactory();

            var client = clientFactory.GetSnsClient(Region);

            var topic = new SnsTopicByName(
                UniqueName,
                client,
                loggerFactory);

            await topic.CreateAsync(CancellationToken.None);

            // Act
            await topic.CreateWithEncryptionAsync(new ServerSideEncryption { KmsMasterKeyId = JustSayingConstants.DefaultSnsAttributeEncryptionKeyId }, CancellationToken.None);

            // Assert
            topic.ServerSideEncryption.KmsMasterKeyId.ShouldBe(JustSayingConstants.DefaultSnsAttributeEncryptionKeyId);
        }
        public async Task Cannot_Create_Topic_Because_Not_Authorized()
        {
            // Arrange
            string         topicName     = Guid.NewGuid().ToString();
            ILoggerFactory loggerFactory = OutputHelper.ToLoggerFactory();

            var subjectProvider       = new NonGenericMessageSubjectProvider();
            var serializationRegister = new MessageSerializationRegister(subjectProvider);

            IAmazonSimpleNotificationService client = CreateSnsClient(exists: false);

            var topic = new SnsTopicByName(
                topicName,
                client,
                serializationRegister,
                loggerFactory,
                subjectProvider);

            // Act and Assert
            await Assert.ThrowsAsync <InvalidOperationException>(() => topic.CreateAsync());
        }
Exemplo n.º 18
0
        private async Task <SnsTopicByName> EnsureTopicExists(RegionEndpoint region, IMessageSerialisationRegister serialisationRegister, SqsReadConfiguration queueConfig)
        {
            var snsclient = _awsClientFactory.GetAwsClientFactory().GetSnsClient(region);

            var eventTopic = _topicCache.TryGetFromCache(region.SystemName, queueConfig.PublishEndpoint);

            if (eventTopic != null)
            {
                return(eventTopic);
            }

            eventTopic = new SnsTopicByName(queueConfig.PublishEndpoint, snsclient, serialisationRegister, _loggerFactory);
            _topicCache.AddToCache(region.SystemName, queueConfig.PublishEndpoint, eventTopic);

            var exists = await eventTopic.ExistsAsync();

            if (!exists)
            {
                await eventTopic.CreateAsync();
            }

            return(eventTopic);
        }
 protected override void When()
 {
     _topic = new SnsTopicByName(UniqueName, Bus, new MessageSerialisationRegister(new NonGenericMessageSubjectProvider()), new LoggerFactory(), new NonGenericMessageSubjectProvider());
     _createWasSuccessful = _topic.CreateAsync().GetAwaiter().GetResult();
 }