Example #1
0
        public async Task CheckBucketRegistration()
        {
            foreach (var bucket in _webhooks.SelectMany(t => t.GetBucketsToMonitor().Distinct()))
            {
                if (!await Client.BucketExistsAsync(bucket))
                {
                    await Client.MakeBucketAsync(bucket).ConfigureAwait(false);
                }

                var currentNotifications = await Client.GetBucketNotificationsAsync(bucket).ConfigureAwait(false);

                bool haveRightConfiguration = false;
                currentNotifications.QueueConfigs.ForEach(qc =>
                {
                    if (qc.Queue == _options.WebhookARN &&
                        qc.Events.Select(t => t.value).Contains(EventType.ObjectCreatedAll.value) &&
                        qc.Events.Select(t => t.value).Contains(EventType.ObjectRemovedAll.value) &&
                        (!_options.HookOnAccessedObjects || qc.Events.Select(t => t.value).Contains(EventType.ObjectAccessedAll.value))
                        )
                    {
                        haveRightConfiguration = true;
                    }
                });

                if (!haveRightConfiguration)
                {
                    //await Client.RemoveAllBucketNotificationsAsync(bucket).ConfigureAwait(false);

                    BucketNotification notification = new BucketNotification();

                    QueueConfig queueConfiguration = new QueueConfig(_options.WebhookARN);
                    var         events             = new List <EventType>()
                    {
                        EventType.ObjectCreatedAll, EventType.ObjectRemovedAll
                    };
                    if (_options.HookOnAccessedObjects)
                    {
                        events.Add(EventType.ObjectAccessedAll);
                    }
                    queueConfiguration.AddEvents(events);

                    notification.AddQueue(queueConfiguration);

                    await Client.SetBucketNotificationsAsync(bucket,
                                                             notification);
                }
            }
        }
        public void TestBucketNotificationMethods()
        {
            BucketNotification notification = new BucketNotification();
            // remove non-existent lambda, topic and queue configs
            notification.RemoveLambdaByArn(new Arn("blahblah"));
            notification.RemoveQueueByArn(new Arn("somequeue"));
            notification.RemoveTopicByArn(new Arn("nonexistenttopic"));
            // now test add & remove
            notification.AddLambda(new LambdaConfig("blahblah"));
            notification.RemoveLambdaByArn(new Arn("blahblah"));

            notification.AddQueue(new QueueConfig("somequeue"));
            notification.RemoveQueueByArn(new Arn("somequeue"));

            notification.AddTopic(new TopicConfig("nonexistenttopic"));
            notification.RemoveTopicByArn(new Arn("nonexistenttopic"));
        }