Ejemplo n.º 1
0
        public async Task <Result> Create(string shovel, string uri, string vhost,
                                          Action <ShovelConfigurator> configurator, CancellationToken cancellationToken = default)
        {
            cancellationToken.RequestCanceled();

            var errors = new List <Error>();

            if (configurator.IsNull())
            {
                errors.Add(new (){ Reason = "The shovel configurator is missing." });
            }

            if (errors.Any())
            {
                return new FaultedResult {
                           DebugInfo = new (){ Errors = errors }
                }
            }
            ;

            if (string.IsNullOrWhiteSpace(uri))
            {
                errors.Add(new(){ Reason = "The connection URI is missing." });
            }

            var impl = new ShovelConfiguratorImpl(uri);

            configurator?.Invoke(impl);

            impl.Validate();

            ShovelRequest request = impl.BuildRequest();

            Debug.Assert(request != null);

            errors.AddRange(impl.Errors.Value);

            if (string.IsNullOrWhiteSpace(shovel))
            {
                errors.Add(new (){ Reason = "The name of the shovel is missing." });
            }

            if (string.IsNullOrWhiteSpace(vhost))
            {
                errors.Add(new (){ Reason = "The name of the virtual host is missing." });
            }

            string url = $"api/parameters/shovel/{vhost.ToSanitizedName()}/{shovel}";

            if (errors.Any())
            {
                return new FaultedResult {
                           DebugInfo = new (){ URL = url, Request = request.ToJsonString(Deserializer.Options), Errors = errors }
                }
            }
            ;

            return(await PutRequest(url, request, cancellationToken).ConfigureAwait(false));
        }
Ejemplo n.º 2
0
        public async Task Verify_can_create_dynamic_shovel1()
        {
            var services = GetContainerBuilder().BuildServiceProvider();
            var result   = await services.GetService <IBrokerObjectFactory>()
                           .Object <Shovel>()
                           .Create("test-shovel1", "amqp://user1@localhost", "TestHareDu", x =>
            {
                x.AcknowledgementMode(AckMode.OnPublish);
                x.Source("queue1", c =>
                {
                    c.DeleteAfter(DeleteShovelMode.QueueLength);
                });
                x.Destination("queue2");
            });

            Assert.Multiple(() =>
            {
                Assert.IsFalse(result.HasFaulted);
                Assert.IsNotNull(result.DebugInfo);

                Console.WriteLine(result.DebugInfo.Request);

                ShovelRequest request = result.DebugInfo.Request.ToObject <ShovelRequest>();

                Assert.IsNotNull(request.Value);
                Assert.AreEqual(AckMode.OnPublish, request.Value.AcknowledgeMode);
                Assert.AreEqual(1000, request.Value.SourcePrefetchCount);
                Assert.AreEqual(ShovelProtocolType.Amqp091, request.Value.SourceProtocol);
                Assert.AreEqual("queue1", request.Value.SourceQueue);
                Assert.AreEqual("amqp://user1@localhost", request.Value.SourceUri);
                Assert.AreEqual(DeleteShovelMode.QueueLength.ConvertTo(), request.Value.SourceDeleteAfter.ToString());
                Assert.AreEqual("queue2", request.Value.DestinationQueue);
                Assert.AreEqual("amqp://user1@localhost", request.Value.DestinationUri);

                Console.WriteLine(request.ToJsonString(Deserializer.Options));
            });
        }