public virtual ExtendedAuthorizationRule UpdateSharedAccessAuthorization(
string namespaceName,
string entityName,
ServiceBusEntityType entityType,
string ruleName,
string primaryKey,
string secondaryKey,
params AccessRights[] permissions)
{
bool removed = false;
ExtendedAuthorizationRule rule = GetAuthorizationRule( namespaceName, entityName, entityType, ruleName);
if (null == rule)
{
throw new ArgumentException(Resources.ServiceBusAuthorizationRuleNotFound);
}
SharedAccessAuthorizationRule oldRule = (SharedAccessAuthorizationRule)rule.Rule;
SharedAccessAuthorizationRule newRule = new SharedAccessAuthorizationRule(
ruleName,
string.IsNullOrEmpty(primaryKey) ? SharedAccessAuthorizationRule.GenerateRandomKey() : primaryKey,
secondaryKey,
permissions ?? oldRule.Rights);
// Create namespace manager
NamespaceManager namespaceManager = CreateNamespaceManager(namespaceName);
// Add the SAS rule and update the entity
switch (entityType)
{
case ServiceBusEntityType.Queue:
QueueDescription queue = namespaceManager.GetQueue(entityName);
removed = queue.Authorization.Remove(oldRule);
Debug.Assert(removed);
queue.Authorization.Add(newRule);
namespaceManager.UpdateQueue(queue);
break;
case ServiceBusEntityType.Topic:
TopicDescription topic = namespaceManager.GetTopic(entityName);
removed = topic.Authorization.Remove(oldRule);
Debug.Assert(removed);
topic.Authorization.Add(newRule);
namespaceManager.UpdateTopic(topic);
break;
case ServiceBusEntityType.Relay:
RelayDescription relay = namespaceManager.GetRelayAsync(entityName).Result;
removed = relay.Authorization.Remove(oldRule);
Debug.Assert(removed);
relay.Authorization.Add(newRule);
namespaceManager.UpdateRelayAsync(relay).Wait();
break;
case ServiceBusEntityType.NotificationHub:
NotificationHubDescription notificationHub = namespaceManager.GetNotificationHub(entityName);
removed = notificationHub.Authorization.Remove(oldRule);
Debug.Assert(removed);
notificationHub.Authorization.Add(newRule);
namespaceManager.UpdateNotificationHub(notificationHub);
break;
default:
throw new Exception(string.Format(Resources.ServiceBusEntityTypeNotFound, entityType.ToString()));
}
return CreateExtendedAuthorizationRule(newRule, namespaceName, entityName, entityType);
}