Exemple #1
0
        public async Task <string> GetEventHubNamespaceConnectionStringAsync(
            IResourceGroup resourceGroup,
            EHNamespaceInner eventHubNamespace,
            CancellationToken cancellationToken = default
            )
        {
            try {
                Log.Verbose($"Fetching connection string for Event Hub Namespace: {eventHubNamespace.Name} ...");

                var eventHubNamespacesAccessKeys = await _eventHubManagementClient
                                                   .Namespaces
                                                   .ListKeysAsync(
                    resourceGroup.Name,
                    eventHubNamespace.Name,
                    kEVENT_HUB_NAMESPACE_AUTHORIZATION_RULE,
                    cancellationToken
                    );

                Log.Verbose($"Fetched connection string for Event Hub Namespace: {eventHubNamespace.Name}");

                return(eventHubNamespacesAccessKeys.PrimaryConnectionString);
            }
            catch (Exception ex) {
                Log.Error(ex, $"Failed to fetch connection string for Event Hub Namespace: {eventHubNamespace.Name}");
                throw;
            }
        }
        /// <summary>
        /// Create Standard tier Event Hub Namespace.
        /// </summary>
        /// <param name="resourceGroup"></param>
        /// <param name="eventHubNamespaceName"></param>
        /// <param name="tags"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <EHNamespaceInner> CreateEventHubNamespaceAsync(
            IResourceGroup resourceGroup,
            string eventHubNamespaceName,
            IDictionary <string, string> tags   = null,
            CancellationToken cancellationToken = default
            )
        {
            if (resourceGroup is null)
            {
                throw new ArgumentNullException(nameof(resourceGroup));
            }
            if (string.IsNullOrEmpty(eventHubNamespaceName))
            {
                throw new ArgumentNullException(nameof(eventHubNamespaceName));
            }

            try {
                tags ??= new Dictionary <string, string>();

                Log.Information($"Creating Azure Event Hub Namespace: {eventHubNamespaceName} ...");

                var eventHubNamespaceParameters = new EHNamespaceInner {
                    Location = resourceGroup.RegionName,
                    Tags     = tags,

                    Sku = new Sku {
                        Name     = SkuName.Standard,
                        Tier     = SkuTier.Standard,
                        Capacity = 1
                    },
                    IsAutoInflateEnabled   = false,
                    MaximumThroughputUnits = 0
                };

                eventHubNamespaceParameters.Validate();

                var eventHubNamespace = await _eventHubManagementClient
                                        .Namespaces
                                        .CreateOrUpdateAsync(
                    resourceGroup.Name,
                    eventHubNamespaceName,
                    eventHubNamespaceParameters,
                    cancellationToken
                    );

                Log.Information($"Created Azure Event Hub Namespace: {eventHubNamespaceName}");

                return(eventHubNamespace);
            }
            catch (Exception ex) {
                Log.Error(ex, $"Failed to create Azure Event Hub Namespace: {eventHubNamespaceName}");
                throw;
            }
        }
        /// <summary>
        /// Create a consumer group for an Event Hub.
        /// </summary>
        /// <param name="resourceGroup"></param>
        /// <param name="eventHubNamespace"></param>
        /// <param name="eventHub"></param>
        /// <param name="consumerGroupName"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ConsumerGroupInner> CreateConsumerGroupAsync(
            IResourceGroup resourceGroup,
            EHNamespaceInner eventHubNamespace,
            EventhubInner eventHub,
            string consumerGroupName,
            CancellationToken cancellationToken = default
            )
        {
            if (resourceGroup is null)
            {
                throw new ArgumentNullException(nameof(resourceGroup));
            }
            if (eventHubNamespace is null)
            {
                throw new ArgumentNullException(nameof(eventHubNamespace));
            }
            if (eventHub is null)
            {
                throw new ArgumentNullException(nameof(eventHub));
            }
            if (string.IsNullOrEmpty(consumerGroupName))
            {
                throw new ArgumentNullException(nameof(consumerGroupName));
            }

            try {
                var consumerGroup = await _eventHubManagementClient
                                    .ConsumerGroups
                                    .CreateOrUpdateAsync(
                    resourceGroup.Name,
                    eventHubNamespace.Name,
                    eventHub.Name,
                    consumerGroupName,
                    cancellationToken : cancellationToken
                    );

                return(consumerGroup);
            }
            catch (Exception ex) {
                Log.Error(ex, $"Failed to created a consumer group for " +
                          $"'{eventHub.Name}' Event Hub: {consumerGroupName}");
                throw;
            }
        }
Exemple #4
0
        public async Task <EventhubInner> CreateEventHubAsync(
            IResourceGroup resourceGroup,
            EHNamespaceInner eventHubNamespace,
            string eventHubName,
            IDictionary <string, string> tags   = null,
            CancellationToken cancellationToken = default

            )
        {
            try {
                tags ??= new Dictionary <string, string>();

                Log.Information($"Creating Azure Event Hub: {eventHubName} ...");

                var eventHubParameters = new EventhubInner {
                    Location = resourceGroup.RegionName,
                    Tags     = tags,

                    MessageRetentionInDays = 1,
                    PartitionCount         = 2,
                    Status = EntityStatus.Active
                };

                eventHubParameters.Validate();

                var eventHub = await _eventHubManagementClient
                               .EventHubs
                               .CreateOrUpdateAsync(
                    resourceGroup.Name,
                    eventHubNamespace.Name,
                    eventHubName,
                    eventHubParameters,
                    cancellationToken
                    );

                // Create Azure Event Hub Authorization Rule
                var eventHubAuthorizationRuleName = SdkContext
                                                    .RandomResourceName("iothubroutes-" + eventHubName + "-", 5);

                var eventHubAuthorizationRuleRights = new List <AccessRights> {
                    AccessRights.Send
                };

                var eventHubAuthorizationRule = await _eventHubManagementClient
                                                .EventHubs
                                                .CreateOrUpdateAuthorizationRuleAsync(
                    resourceGroup.Name,
                    eventHubNamespace.Name,
                    eventHubName,
                    eventHubAuthorizationRuleName,
                    eventHubAuthorizationRuleRights,
                    cancellationToken
                    );

                Log.Information($"Created Azure Event Hub: {eventHubName}");

                return(eventHub);
            }
            catch (Exception ex) {
                Log.Error(ex, $"Failed to created Azure Event Hub: {eventHubName}");
                throw;
            }
        }
Exemple #5
0
 /// <summary>
 /// Creates or updates a namespace. Once created, this namespace's resource
 /// manifest is immutable. This operation is idempotent.
 /// </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='parameters'>
 /// Parameters for creating a namespace resource.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <EHNamespaceInner> BeginCreateOrUpdateAsync(this INamespacesOperations operations, string resourceGroupName, string namespaceName, EHNamespaceInner parameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.BeginCreateOrUpdateWithHttpMessagesAsync(resourceGroupName, namespaceName, parameters, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }