public override void ExecuteCmdlet()
        {
            Client = Client ?? new ServiceBusClientExtensions(Profile, Profile.Context.Subscription);
            AuthorizationRuleFilterOption options = new AuthorizationRuleFilterOption()
            {
                Namespace = Namespace,
                EntityName = EntityName,
                EntityType = EntityType,
                Name = Name
            };

            Client.RemoveAuthorizationRule(options);

            if (PassThru)
            {
                WriteObject(true);
            }
        }
        public override void ExecuteCmdlet()
        {
            Client = Client ?? new ServiceBusClientExtensions(Profile);
            List<ExtendedAuthorizationRule> rules = null;
            List<PSObject> output = new List<PSObject>();
            AuthorizationRuleFilterOption options = new AuthorizationRuleFilterOption()
            {
                EntityName = EntityName,
                EntityType = EntityType,
                Name = Name,
                Namespace = Namespace,
                Permission = Permission != null ? Permission.ToList() : null
            };

            switch (ParameterSetName)
            {
                case NamespaceSASParameterSet:
                    rules = Client.GetAuthorizationRule(options);
                    rules.ForEach(r => output.Add(ServiceBusPowerShellUtility.GetNamespacePSObject(r)));
                    break;

                case EntitySASParameterSet:
                    rules = Client.GetAuthorizationRule(options);
                    rules.ForEach(r => output.Add(ServiceBusPowerShellUtility.GetEntityPSObject(r)));
                    break;

                default:
                    throw new ArgumentException(string.Format(Resources.InvalidParameterSetName, ParameterSetName));
            }

            if (output.Count == 1)
            {
                WriteObject(output[0]);
            }
            else
            {
                WriteObject(output, true);
            }
        }
        /// <summary>
        /// Gets the authorization rule with the specified name in the entity level.
        /// </summary>
        /// <param name="namespaceName">The namespace name</param>
        /// <param name="entityName">The entity name</param>
        /// <param name="entityType">The entity type</param>
        /// <param name="ruleName">The rule name</param>
        /// <returns>The authorization rule that matches the specified name</returns>
        public virtual ExtendedAuthorizationRule GetAuthorizationRule(
            string namespaceName,
            string entityName,
            ServiceBusEntityType entityType,
            string ruleName)
        {
            AuthorizationRuleFilterOption options = new AuthorizationRuleFilterOption()
            {
                Namespace = namespaceName,
                Name = ruleName,
                EntityName = entityName,
                EntityType = entityType
            };

            return FilterAuthorizationRules(options).FirstOrDefault();
        }
        /// <summary>
        /// Gets the authorization rule with the specified name in the namespace level.
        /// </summary>
        /// <param name="namespaceName">The namespace name</param>
        /// <param name="ruleName">The rule name</param>
        /// <returns>The authorization rule that matches the specified name</returns>
        public virtual ExtendedAuthorizationRule GetAuthorizationRule(
            string namespaceName,
            string ruleName)
        {
            AuthorizationRuleFilterOption options = new AuthorizationRuleFilterOption()
            {
                Namespace = namespaceName,
                Name = ruleName
            };

            return FilterAuthorizationRules(options).FirstOrDefault();
        }
 /// <summary>
 /// Gets authorization rules based on the passed filter options.
 /// </summary>
 /// <param name="filterOptions">The filter options</param>
 /// <returns>The filtered authorization rules</returns>
 public List<ExtendedAuthorizationRule> GetAuthorizationRule(AuthorizationRuleFilterOption filterOptions)
 {
     return FilterAuthorizationRules(filterOptions);
 }
        /// <summary>
        /// Removes set of authorization rules that matches filter options.
        /// </summary>
        /// <param name="options">The filter options</param>
        public virtual void RemoveAuthorizationRule(AuthorizationRuleFilterOption options)
        {
            List<ExtendedAuthorizationRule> rules = GetAuthorizationRule(options);

            foreach (ExtendedAuthorizationRule rule in rules)
            {
                if (null == rule)
                {
                    throw new ArgumentException(Resources.ServiceBusAuthorizationRuleNotFound);
                }
                else if (!string.IsNullOrEmpty(rule.EntityName))
                {
                    RemoveAuthorizationRule(rule.Namespace, rule.EntityName, rule.EntityType, rule.Name);
                }
                else
                {
                    RemoveAuthorizationRule(rule.Namespace, rule.Name);
                }
            }
        }
        private List<ExtendedAuthorizationRule> GetAuthorizationRulesToFilter(AuthorizationRuleFilterOption options)
        {
            if (!string.IsNullOrEmpty(options.EntityName))
            {
                return GetAuthorizationRuleCore(
                    options.Namespace,
                    options.EntityName,
                    options.EntityType,
                    r => true);
            }
            else if (options.EntityTypes != null && options.EntityTypes.Count > 0)
            {
                NamespaceManager namespaceManager = CreateNamespaceManager(options.Namespace);
                List<ExtendedAuthorizationRule> rules = new List<ExtendedAuthorizationRule>();
                options.EntityTypes = options.EntityTypes.Distinct().ToList();
                
                foreach (ServiceBusEntityType type in options.EntityTypes)
                {
                    switch (type)
                    {
                        case ServiceBusEntityType.Queue:
                            rules.AddRange(namespaceManager.GetQueues()
                                .SelectMany(e => e.Authorization
                                    .Select(r => CreateExtendedAuthorizationRule(
                                        r,
                                        options.Namespace,
                                        e.Path,
                                        ServiceBusEntityType.Queue))));
                            break;

                        case ServiceBusEntityType.Topic:
                            rules.AddRange(namespaceManager.GetTopics()
                                .SelectMany(e => e.Authorization
                                    .Select(r => CreateExtendedAuthorizationRule(
                                        r,
                                        options.Namespace,
                                        e.Path,
                                        ServiceBusEntityType.Topic))));
                            break;

                        case ServiceBusEntityType.Relay:
                            rules.AddRange(namespaceManager.GetRelaysAsync().Result
                                .SelectMany(e => e.Authorization
                                    .Select(r => CreateExtendedAuthorizationRule(
                                        r,
                                        options.Namespace,
                                        e.Path,
                                        ServiceBusEntityType.Relay))));
                            break;

                        case ServiceBusEntityType.NotificationHub:
                            rules.AddRange(namespaceManager.GetNotificationHubs()
                                .SelectMany(e => e.Authorization
                                    .Select(r => CreateExtendedAuthorizationRule(
                                        r,
                                        options.Namespace,
                                        e.Path,
                                        ServiceBusEntityType.NotificationHub))));
                            break;

                        default: throw new InvalidOperationException();
                    }
                }

                return rules;
            }
            else
            {
                return ServiceBusClient.Namespaces.ListAuthorizationRules(options.Namespace)
                    .AuthorizationRules
                    .Select(ar => ar.ToSharedAccessAuthorizationRule())
                    .Select(r => CreateExtendedAuthorizationRule(r, options.Namespace))
                    .ToList();
            }
        }
        private List<ExtendedAuthorizationRule> FilterAuthorizationRules(AuthorizationRuleFilterOption options)
        {
            List<ExtendedAuthorizationRule> rules = GetAuthorizationRulesToFilter(options);
            List<ExtendedAuthorizationRule> result = new List<ExtendedAuthorizationRule>();

            if (!string.IsNullOrEmpty(options.Name))
            {
                result.Add(rules.FirstOrDefault(r => r.Name.Equals(options.Name,StringComparison.OrdinalIgnoreCase)));
            }
            else
            {
                List<ExtendedAuthorizationRule> permissionMatch = new List<ExtendedAuthorizationRule>();
                List<ExtendedAuthorizationRule> ruleTypeMatch = new List<ExtendedAuthorizationRule>();

                if (options.Permission != null && options.Permission.Count > 0)
                {
                    permissionMatch
                    .AddRange(
                    rules.FindAll(r => r.Permission.OrderBy(a => a).SequenceEqual(options.Permission.OrderBy(a => a))));
                }

                if (options.AuthorizationType != null && options.AuthorizationType.Count > 0)
                {
                    ruleTypeMatch.AddRange(
                        rules.FindAll(r => r.Rule.ClaimType.Any(t => options.AuthorizationType.Any(m => m.Equals(t)))));
                }

                result = permissionMatch.Count > 0 ? permissionMatch : rules;
                result = ruleTypeMatch.Count> 0 ? result.Union(ruleTypeMatch).ToList() : result;
            }

            return result == null ? new List<ExtendedAuthorizationRule>() : result;
        }