Beispiel #1
0
 public PolicyRequest(string pattern, IDictionary <string, string> arguments, int priority, PolicyAppliedTo applyTo)
 {
     Pattern   = pattern;
     Arguments = arguments;
     Priority  = priority;
     ApplyTo   = applyTo;
 }
Beispiel #2
0
        public async Task <Result> Create(string policy, string pattern, string vhost, Action <PolicyConfigurator> configurator,
                                          PolicyAppliedTo appliedTo = PolicyAppliedTo.All, int priority = 0, CancellationToken cancellationToken = default)
        {
            cancellationToken.RequestCanceled();

            var impl = new PolicyConfiguratorImpl();

            configurator?.Invoke(impl);

            impl.Validate();

            PolicyRequest request =
                new()
            {
                Pattern   = pattern,
                Priority  = priority,
                Arguments = impl.Arguments.Value,
                ApplyTo   = appliedTo
            };

            Debug.Assert(request != null);

            var errors = new List <Error>();

            errors.AddRange(impl.Errors.Value);

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

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

            if (string.IsNullOrWhiteSpace(pattern))
            {
                errors.Add(new(){ Reason = "Pattern was not set." });
            }

            string url = $"api/policies/{vhost.ToSanitizedName()}/{policy}";

            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));
        }
Beispiel #3
0
        /// <summary>
        /// Creates the specified policy on the target RabbitMQ virtual host.
        /// </summary>
        /// <param name="factory">The object factory that implements the underlying functionality.</param>
        /// <param name="policy">The name of the policy.</param>
        /// <param name="pattern">The pattern for which the policy is to be applied.</param>
        /// <param name="vhost">The name of the virtual host.</param>
        /// <param name="configurator">Describes how the policy will be created by setting arguments through set methods.</param>
        /// <param name="appliedTo">The type of broker objects to apply the policy to.</param>
        /// <param name="priority"></param>
        /// <param name="cancellationToken">Token used to cancel the operation running on the current thread.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">Throws ArgumentNullException if BrokerObjectFactory is null.</exception>
        public static async Task <Result> CreatePolicy(this IBrokerObjectFactory factory,
                                                       string policy, string pattern, string vhost, Action <PolicyConfigurator> configurator,
                                                       PolicyAppliedTo appliedTo = PolicyAppliedTo.All, int priority = 0, CancellationToken cancellationToken = default)
        {
            if (factory.IsNull())
            {
                throw new ArgumentNullException(nameof(factory));
            }

            return(await factory.Object <Policy>()
                   .Create(policy, pattern, vhost, configurator, appliedTo, priority, cancellationToken)
                   .ConfigureAwait(false));
        }