/// <summary> /// Performs the tasks needed to create a new Event Hub instance with the requested /// partition count and a dynamically assigned unique name. /// </summary> /// /// <param name="partitionCount">The number of partitions that the Event Hub should be configured with.</param> /// <param name="consumerGroups">The set of consumer groups to create and associate with the Event Hub; the default consumer group should not be included, as it is implicitly created.</param> /// <param name="caller">The name of the calling method; this is intended to be populated by the runtime.</param> /// /// <returns>The <see cref="EventHubScope" /> in which the test should be executed.</returns> /// private static async Task <EventHubScope> BuildScopeWithNewEventHub(int partitionCount, IEnumerable <string> consumerGroups, [CallerMemberName] string caller = "") { caller = (caller.Length < 16) ? caller : caller.Substring(0, 15); var groups = (consumerGroups ?? Enumerable.Empty <string>()).ToList(); var resourceGroup = TestEnvironment.EventHubsResourceGroup; var eventHubNamespace = TestEnvironment.EventHubsNamespace; var token = await ResourceManager.AquireManagementTokenAsync(); string CreateName() => $"{ Guid.NewGuid().ToString("D").Substring(0, 13) }-{ caller }"; using (var client = new EventHubManagementClient(new TokenCredentials(token)) { SubscriptionId = TestEnvironment.EventHubsSubscription }) { var eventHub = new Eventhub(partitionCount: partitionCount); eventHub = await ResourceManager.CreateRetryPolicy <Eventhub>().ExecuteAsync(() => client.EventHubs.CreateOrUpdateAsync(resourceGroup, eventHubNamespace, CreateName(), eventHub)); Polly.IAsyncPolicy <ConsumerGroup> consumerPolicy = ResourceManager.CreateRetryPolicy <ConsumerGroup>(); await Task.WhenAll ( consumerGroups.Select(groupName => { var group = new ConsumerGroup(name: groupName); return(consumerPolicy.ExecuteAsync(() => client.ConsumerGroups.CreateOrUpdateAsync(resourceGroup, eventHubNamespace, eventHub.Name, groupName, group))); }) ); return(new EventHubScope(eventHub.Name, groups, wasEventHubCreated: true)); } }
private static async Task CreateEventHub() { try { if (string.IsNullOrEmpty(namespaceName)) { throw new Exception("Namespace name is empty!"); } var token = await GetToken(); var creds = new TokenCredentials(token); var ehClient = new EventHubManagementClient(creds) { SubscriptionId = SettingsCache["SubscriptionId"] }; var ehParams = new Eventhub() { }; Console.WriteLine("Creating Event Hub..."); await ehClient.EventHubs.CreateOrUpdateAsync(resourceGroupName, namespaceName, EventHubName, ehParams); Console.WriteLine("Created Event Hub successfully."); } catch (Exception e) { Console.WriteLine("Could not create an Event Hub..."); Console.WriteLine(e.Message); throw e; } }
private static async Task CreateEventHub() { try { var resourceGroupName = SettingsCache["resourceGroupName"]; var namespaceName = SettingsCache["namespaceName"]; var eventHubName = SettingsCache["EH_NAME"]; var numOfPartitions = SettingsCache["NumOfPartitions"]; if (string.IsNullOrEmpty(resourceGroupName)) { throw new Exception("Resource Group name is empty!"); } if (string.IsNullOrEmpty(namespaceName)) { throw new Exception("Namespace name is empty!"); } if (string.IsNullOrEmpty(eventHubName)) { throw new Exception("Event Hub name is empty!"); } var token = await GetToken(); var creds = new TokenCredentials(token); var ehClient = new EventHubManagementClient(creds) { SubscriptionId = SettingsCache["SubscriptionId"] }; var ehParams = new Eventhub() { PartitionCount = string.IsNullOrEmpty(numOfPartitions) ? 10 : Convert.ToInt64(numOfPartitions) }; Console.WriteLine("Creating Event Hub..."); await ehClient.EventHubs.CreateOrUpdateAsync(resourceGroupName, namespaceName, eventHubName, ehParams); Console.WriteLine("Created Event Hub successfully."); } catch (Exception e) { Console.WriteLine("Could not create an Event Hub..."); Console.WriteLine(e.Message); throw e; } }
public async Task CreateEventHubAsync(EventHubModel eventHub) { await EnsureAuthenticatedAsync(); Trace.WriteLine($"Create event hub: {eventHub.CreatedName}"); var newEventHub = new Eventhub { MessageRetentionInDays = 1, PartitionCount = 1 }; await _client.EventHubs.CreateOrUpdateAsync(_azureResourceIdentifier.ResourceGroupName, _azureResourceIdentifier.Name, eventHub.CreatedName, newEventHub); var autRuleParams = new AuthorizationRule { Rights = new List <string> { "send", "listen" } }; await _client.EventHubs.CreateOrUpdateAuthorizationRuleAsync(_azureResourceIdentifier.ResourceGroupName, _azureResourceIdentifier.Name, eventHub.CreatedName, "sender", autRuleParams); }
public static void Print(Eventhub resource) { StringBuilder info = new StringBuilder(); info.Append("Eventhub: ").Append(resource.Id) .Append("\n\tName: ").Append(resource.Name) .Append("\n\tMessage retention in Days: ").Append(resource.MessageRetentionInDays) .Append("\n\tPartition ids: ").Append(resource.PartitionIds); if (resource.CaptureDescription != null) { info.Append("\n\t\t\tSize limit in Bytes: ").Append(resource.CaptureDescription.SizeLimitInBytes); info.Append("\n\t\t\tInterval in seconds: ").Append(resource.CaptureDescription.IntervalInSeconds); if (resource.CaptureDescription.Destination != null) { info.Append("\n\t\t\tData capture storage account: ").Append(resource.CaptureDescription.Destination.StorageAccountResourceId); info.Append("\n\t\t\tData capture storage container: ").Append(resource.CaptureDescription.Destination.BlobContainer); } } Utilities.Log(info.ToString()); }
/// <summary> /// Performs the tasks needed to create a new Event Hub instance with the requested /// partition count and a dynamically assigned unique name. /// </summary> /// /// <param name="partitionCount">The number of partitions that the Event Hub should be configured with.</param> /// <param name="consumerGroups">The set of consumer groups to create and associate with the Event Hub; the default consumer group should not be included, as it is implicitly created.</param> /// <param name="caller">The name of the calling method; this is intended to be populated by the runtime.</param> /// public static async Task <EventHubScope> CreateAsync(int partitionCount, IEnumerable <string> consumerGroups, [CallerMemberName] string caller = "") { var eventHubName = $"{ caller }-{ Guid.NewGuid().ToString("D").Substring(0, 8) }"; var groups = (consumerGroups ?? Enumerable.Empty <string>()).ToList(); var resourceGroup = TestEnvironment.EventHubsResourceGroup; var eventHubNamespace = TestEnvironment.EventHubsNamespace; var token = await AquireManagementTokenAsync(); var client = new EventHubManagementClient(new TokenCredentials(token)) { SubscriptionId = TestEnvironment.EventHubsSubscription }; try { var eventHub = new Eventhub(name: eventHubName, partitionCount: partitionCount); await CreateRetryPolicy <Eventhub>().ExecuteAsync(() => client.EventHubs.CreateOrUpdateAsync(resourceGroup, eventHubNamespace, eventHubName, eventHub)); var consumerPolicy = CreateRetryPolicy <ConsumerGroup>(); await Task.WhenAll ( consumerGroups.Select(groupName => { var group = new ConsumerGroup(name: groupName); return(consumerPolicy.ExecuteAsync(() => client.ConsumerGroups.CreateOrUpdateAsync(resourceGroup, eventHubNamespace, eventHubName, groupName, group))); }) ); } finally { client?.Dispose(); } return(new EventHubScope(eventHubName, groups)); }
/// <summary> /// Performs the tasks needed to create a new Event Hub instance with the requested /// partition count and a dynamically assigned unique name. /// </summary> /// /// <param name="partitionCount">The number of partitions that the Event Hub should be configured with.</param> /// <param name="consumerGroups">The set of consumer groups to create and associate with the Event Hub; the default consumer group should not be included, as it is implicitly created.</param> /// <param name="caller">The name of the calling method; this is intended to be populated by the runtime.</param> /// /// <returns>The <see cref="EventHubScope" /> in which the test should be executed.</returns> /// private static async Task <EventHubScope> BuildScopeWithNewEventHub(int partitionCount, IEnumerable <string> consumerGroups, [CallerMemberName] string caller = "") { caller = (caller.Length < 16) ? caller : caller.Substring(0, 15); var groups = (consumerGroups ?? Enumerable.Empty <string>()).ToList(); var resourceGroup = EventHubsTestEnvironment.Instance.ResourceGroup; var eventHubNamespace = EventHubsTestEnvironment.Instance.EventHubsNamespace; var token = await ResourceManager.AcquireManagementTokenAsync().ConfigureAwait(false); string CreateName() => $"{ Guid.NewGuid().ToString("D").Substring(0, 13) }-{ caller }"; using (var client = new EventHubManagementClient(AzureResourceManagerUri, new TokenCredentials(token)) { SubscriptionId = EventHubsTestEnvironment.Instance.SubscriptionId }) { var eventHub = new Eventhub(partitionCount: partitionCount); eventHub = await ResourceManager.CreateRetryPolicy <Eventhub>().ExecuteAsync(() => client.EventHubs.CreateOrUpdateAsync(resourceGroup, eventHubNamespace, CreateName(), eventHub)).ConfigureAwait(false); var consumerPolicy = ResourceManager.CreateRetryPolicy <ConsumerGroup>(); await Task.WhenAll ( consumerGroups.Select(groupName => { var group = new ConsumerGroup(name: groupName); return(consumerPolicy.ExecuteAsync(() => client.ConsumerGroups.CreateOrUpdateAsync(resourceGroup, eventHubNamespace, eventHub.Name, groupName, group))); }) ).ConfigureAwait(false); groups.Insert(0, EventHubConsumerClient.DefaultConsumerGroupName); return(new EventHubScope(eventHub.Name, groups, shouldRemoveEventHubAtScopeCompletion: true)); } }
public virtual Response <Eventhub> CreateOrUpdate(string resourceGroupName, string namespaceName, string eventHubName, Eventhub parameters, CancellationToken cancellationToken = default) { using var scope = _clientDiagnostics.CreateScope("EventHubsOperations.CreateOrUpdate"); scope.Start(); try { return(RestClient.CreateOrUpdate(resourceGroupName, namespaceName, eventHubName, parameters, cancellationToken)); } catch (Exception e) { scope.Failed(e); throw; } }
public virtual async Task <Response <Eventhub> > CreateOrUpdateAsync(string resourceGroupName, string namespaceName, string eventHubName, Eventhub parameters, CancellationToken cancellationToken = default) { using var scope = _clientDiagnostics.CreateScope("EventHubsClient.CreateOrUpdate"); scope.Start(); try { return(await RestClient.CreateOrUpdateAsync(resourceGroupName, namespaceName, eventHubName, parameters, cancellationToken).ConfigureAwait(false)); } catch (Exception e) { scope.Failed(e); throw; } }
/// <summary> /// Creates or updates a new Event Hub as a nested resource within a Namespace. /// </summary> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='resourceGroupName'> /// Name of the resource group within the azure subscription. /// </param> /// <param name='namespaceName'> /// The Namespace name /// </param> /// <param name='eventHubName'> /// The Event Hub name /// </param> /// <param name='parameters'> /// Parameters supplied to create an Event Hub resource. /// </param> /// <param name='cancellationToken'> /// The cancellation token. /// </param> public static async Task <Eventhub> CreateOrUpdateAsync(this IEventHubsOperations operations, string resourceGroupName, string namespaceName, string eventHubName, Eventhub parameters, CancellationToken cancellationToken = default(CancellationToken)) { using (var _result = await operations.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, namespaceName, eventHubName, parameters, null, cancellationToken).ConfigureAwait(false)) { return(_result.Body); } }
/// <summary> /// Creates or updates a new Event Hub as a nested resource within a Namespace. /// </summary> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='resourceGroupName'> /// Name of the resource group within the azure subscription. /// </param> /// <param name='namespaceName'> /// The Namespace name /// </param> /// <param name='eventHubName'> /// The Event Hub name /// </param> /// <param name='parameters'> /// Parameters supplied to create an Event Hub resource. /// </param> public static Eventhub CreateOrUpdate(this IEventHubsOperations operations, string resourceGroupName, string namespaceName, string eventHubName, Eventhub parameters) { return(operations.CreateOrUpdateAsync(resourceGroupName, namespaceName, eventHubName, parameters).GetAwaiter().GetResult()); }