Esempio n. 1
0
        private static IEnumerable <EventhubNamespace> GetNamespacesForSubscription(EventHubManagementClient ehClient, ILogger log)
        {
            log.LogInformation($"Getting namespaces for {ehClient.SubscriptionId}");
            var namespaces = ehClient.Namespaces.List().ToList();

            var nsList = new List <EventhubNamespace>();

            foreach (var ns in namespaces)
            {
                if (!(ns.IsAutoInflateEnabled ?? false))
                {
                    log.LogInformation($"Namespace {ns.Name} not configured for auto-inflate - skipping");
                    continue;
                }

                log.LogInformation($"Processing namespace {ns.Name} to extract RG and Throughput Units");

                var resourceGroupName = Regex.Match(ns.Id, @".*\/resourceGroups\/([\w-]+)\/providers.*").Groups[1].Value;

                int targetThroughputUnits = 1;
                if (ns.Tags.ContainsKey(SCALE_DOWN_TU_TAG))
                {
                    int.TryParse(ns.Tags[SCALE_DOWN_TU_TAG], out targetThroughputUnits);
                }

                nsList.Add(new EventhubNamespace(ehClient.SubscriptionId, resourceGroupName, ns.Name, targetThroughputUnits));
            }

            return(nsList);
        }
Esempio n. 2
0
        private static void ScaleDownNamespacesInSubscription(string subscriptionId, ServiceClientCredentials credentials, ILogger log)
        {
            var ehClient = new EventHubManagementClient(credentials)
            {
                SubscriptionId = subscriptionId
            };

            IEnumerable <EventhubNamespace> namespaces = null;

            try
            {
                namespaces = GetNamespacesForSubscription(ehClient, log);
            }
            catch (Exception e)
            {
                log.LogError(e, $"Error getting namespaces for subscription {subscriptionId}");
                return;
            }

            foreach (var ns in namespaces)
            {
                try
                {
                    ScaleDownNamespace(ns, ehClient, log);
                }
                catch (Exception e)
                {
                    log.LogError(e, $"Error scaling down namespace {ns.Namespace}");
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        ///   Performs the tasks needed to remove the dynamically created
        ///   Event Hub.
        /// </summary>
        ///
        public async ValueTask DisposeAsync()
        {
            if (_disposed)
            {
                return;
            }

            var resourceGroup = TestEnvironment.EventHubsResourceGroup;
            var eventHubNamespace = TestEnvironment.EventHubsNamespace;
            var token = await ResourceManager.AquireManagementTokenAsync();
            var client = new EventHubManagementClient(new TokenCredentials(token)) { SubscriptionId = TestEnvironment.EventHubsSubscription };

            try
            {
                await ResourceManager.CreateRetryPolicy().ExecuteAsync(() => client.EventHubs.DeleteAsync(resourceGroup, eventHubNamespace, EventHubName));
            }
            catch
            {
                // This should not be considered a critical failure that results in a test failure.  Due
                // to ARM being temperamental, some management operations may be rejected.  Throwing here
                // does not help to ensure resource cleanup only flags the test itself as a failure.
                //
                // If an Event Hub fails to be deleted, removing of the associated namespace at the end of the
                // test run will also remove the orphan.
            }
            finally
            {
                client?.Dispose();
            }

            _disposed = 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 EventHubCreateOrUpdateParameters()
                {
                    Location = SettingsCache["DataCenterLocation"]
                };

                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;
            }
        }
Esempio n. 5
0
        /// <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));
            }
        }
Esempio n. 6
0
        protected void SetupManagementClients(MockContext context)
        {
            SynapseManagementClient      = GetSynapseManagementClient(context);
            SynapseSqlV3ManagementClient = GetSynapseSqlV3ManagementClient(context);
            SynapseClient                       = GetSynapseClient(context);
            StorageManagementClient             = GetStorageManagementClient(context);
            NewResourceManagementClient         = GetResourceManagementClient(context);
            CommonMonitorManagementClient       = GetCommonMonitorManagementClient(context);
            SDKMonitorManagementClient          = GetMonitorManagementClient(context);
            OperationalInsightsManagementClient = GetOperationalInsightsManagementClient(context);
            EventHubManagementClient            = GetEventHubManagementClient(context);
            _helper.SetupManagementClients(
                SynapseManagementClient,
                SynapseSqlV3ManagementClient,
                SynapseClient,
                StorageManagementClient,
                NewResourceManagementClient,
                CommonMonitorManagementClient,
                SDKMonitorManagementClient,
                OperationalInsightsManagementClient,
                EventHubManagementClient,
                GetNewSynapseManagementClient(context)
                );

            // register the namespace.
            _helper.SetupEnvironment(AzureModule.AzureResourceManager);
        }
Esempio n. 7
0
        /// <summary>
        ///   Performs the tasks needed to remove the dynamically created
        ///   Event Hub.
        /// </summary>
        ///
        public async ValueTask DisposeAsync()
        {
            if (_disposed)
            {
                return;
            }

            var resourceGroup     = TestEnvironment.EventHubsResourceGroup;
            var eventHubNamespace = TestEnvironment.EventHubsNamespace;
            var token             = await AquireManagementTokenAsync();

            var client = new EventHubManagementClient(new TokenCredentials(token))
            {
                SubscriptionId = TestEnvironment.EventHubsSubscription
            };

            try
            {
                await CreateRetryPolicy().ExecuteAsync(() => client.EventHubs.DeleteAsync(resourceGroup, eventHubNamespace, EventHubName));
            }
            finally
            {
                client?.Dispose();
            }

            _disposed = true;
        }
        protected void Initialize(MockContext context)
        {
            if (!initialized)
            {
                lock (locker)
                {
                    if (!initialized)
                    {
                        testEnv         = TestEnvironmentFactory.GetTestEnvironment();
                        resourcesClient = IotHubTestUtilities.GetResourceManagementClient(context, new RecordedDelegatingHandler {
                            StatusCodeToReturn = HttpStatusCode.OK
                        });
                        iotHubClient = IotHubTestUtilities.GetIotHubClient(context, new RecordedDelegatingHandler {
                            StatusCodeToReturn = HttpStatusCode.OK
                        });
                        ehClient = IotHubTestUtilities.GetEhClient(context, new RecordedDelegatingHandler {
                            StatusCodeToReturn = HttpStatusCode.OK
                        });
                        sbClient = IotHubTestUtilities.GetSbClient(context, new RecordedDelegatingHandler {
                            StatusCodeToReturn = HttpStatusCode.OK
                        });

                        location = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("AZURE_LOCATION"))
                            ? IotHubTestUtilities.DefaultLocation
                            : Environment.GetEnvironmentVariable("AZURE_LOCATION").Replace(" ", "").ToLower();

                        initialized = true;
                    }
                }
            }
        }
Esempio n. 9
0
 protected void InitializeClients(MockContext context)
 {
     if (!m_initialized)
     {
         lock (m_lock)
         {
             if (!m_initialized)
             {
                 _resourceManagementClient = EventHubManagementHelper.GetResourceManagementClient(context, new RecordedDelegatingHandler {
                     StatusCodeToReturn = HttpStatusCode.OK
                 });
                 _EventHubManagementClient = EventHubManagementHelper.GetEventHubManagementClient(context, new RecordedDelegatingHandler {
                     StatusCodeToReturn = HttpStatusCode.OK
                 });
                 _KeyVaultManagementClient = EventHubManagementHelper.GetKeyVaultManagementClient(context, new RecordedDelegatingHandler {
                     StatusCodeToReturn = HttpStatusCode.OK
                 });
                 _NetworkManagementClient = EventHubManagementHelper.GetNetworkManagementClient(context, new RecordedDelegatingHandler {
                     StatusCodeToReturn = HttpStatusCode.OK
                 });
                 _IdentityManagementClient = EventHubManagementHelper.GetIdentityManagementClient(context, new RecordedDelegatingHandler {
                     StatusCodeToReturn = HttpStatusCode.OK
                 });
             }
         }
     }
 }
        private static async Task CreateNamespace()
        {
            try
            {
                Console.WriteLine("What would you like to call your Event Hubs namespace?");
                namespaceName = Console.ReadLine();

                var token = await GetToken();

                var creds    = new TokenCredentials(token);
                var ehClient = new EventHubManagementClient(creds)
                {
                    SubscriptionId = SettingsCache["SubscriptionId"]
                };

                var namespaceParams = new EHNamespace()
                {
                    Location = SettingsCache["DataCenterLocation"]
                };

                Console.WriteLine("Creating namespace...");
                await ehClient.Namespaces.CreateOrUpdateAsync(resourceGroupName, namespaceName, namespaceParams);

                Console.WriteLine("Created namespace successfully.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not create a namespace...");
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        private static async Task CreateConsumerGroup()
        {
            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 consumerGroupParams = new ConsumerGroup()
                {
                };

                Console.WriteLine("Creating Consumer Group...");
                await ehClient.ConsumerGroups.CreateOrUpdateAsync(resourceGroupName, namespaceName, EventHubName, ConsumerGroupName, consumerGroupParams);

                Console.WriteLine("Created Consumer Group successfully.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not create a Consumer Group...");
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        /// <summary>
        ///   Performs the tasks needed to create a new Event Hubs namespace within a resource group, intended to be used as
        ///   an ephemeral container for the Event Hub instances used in a given test run.
        /// </summary>
        ///
        /// <returns>The key attributes for identifying and accessing a dynamically created Event Hubs namespace.</returns>
        ///
        public static async Task <NamespaceProperties> CreateNamespaceAsync()
        {
            var subscription  = TestEnvironment.EventHubsSubscription;
            var resourceGroup = TestEnvironment.EventHubsResourceGroup;
            var token         = await AquireManagementTokenAsync();

            string CreateName() => $"net-eventhubs-{ Guid.NewGuid().ToString("D") }";

            using (var client = new EventHubManagementClient(new TokenCredentials(token))
            {
                SubscriptionId = subscription
            })
            {
                var location = await QueryResourceGroupLocationAsync(token, resourceGroup, subscription);

                var tags = new Dictionary <string, string>
                {
                    { "source", typeof(EventHubScope).Assembly.GetName().Name },
                    { "platform", System.Runtime.InteropServices.RuntimeInformation.OSDescription },
                    { "framework", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription },
                    { "created", $"{ DateTimeOffset.UtcNow.ToString("s") }Z" },
                    { "cleanup-after", $"{ DateTimeOffset.UtcNow.AddDays(1).ToString("s") }Z" }
                };

                var eventHubsNamespace = new EHNamespace(sku: new Sku("Standard", "Standard", 12), tags: tags, isAutoInflateEnabled: true, maximumThroughputUnits: 20, location: location);
                eventHubsNamespace = await CreateRetryPolicy <EHNamespace>().ExecuteAsync(() => client.Namespaces.CreateOrUpdateAsync(resourceGroup, CreateName(), eventHubsNamespace));

                var accessKey = await CreateRetryPolicy <AccessKeys>().ExecuteAsync(() => client.Namespaces.ListKeysAsync(resourceGroup, eventHubsNamespace.Name, TestEnvironment.EventHubsDefaultSharedAccessKey));

                return(new NamespaceProperties(eventHubsNamespace.Name, accessKey.PrimaryConnectionString));
            }
        }
Esempio n. 13
0
 public EventHubMgmtClient(
     string subscriptionId,
     RestClient restClient
     )
 {
     _eventHubManagementClient = new EventHubManagementClient(restClient)
     {
         SubscriptionId = subscriptionId
     };
 }
Esempio n. 14
0
        /// <summary>
        ///   Performs the tasks needed to remove an ephemeral Event Hubs namespace used as a container for Event Hub instances
        ///   for a specific test run.
        /// </summary>
        ///
        /// <param name="namespaceName">The name of the namespace to delete.</param>
        ///
        public static async Task DeleteNamespaceAsync(string namespaceName)
        {
            var subscription = TestEnvironment.EventHubsSubscription;
            var resourceGroup = TestEnvironment.EventHubsResourceGroup;
            var token = await ResourceManager.AquireManagementTokenAsync();

            using (var client = new EventHubManagementClient(new TokenCredentials(token)) { SubscriptionId = subscription })
            {
                await ResourceManager.CreateRetryPolicy().ExecuteAsync(() => client.Namespaces.DeleteAsync(resourceGroup, namespaceName));
            }
        }
Esempio n. 15
0
        public static EventHubManagementClient GetEventHubManagementClient(MockContext context, RecordedDelegatingHandler handler)
        {
            if (handler != null)
            {
                handler.IsPassThrough = true;
                EventHubManagementClient nhManagementClient = context.GetServiceClient <EventHubManagementClient>(handlers: handler);
                return(nhManagementClient);
            }

            return(null);
        }
Esempio n. 16
0
        private async Task <EventHubManagementClient> GetClientAsync(GeoDRConfig config)
        {
            var token = await GetAuthorizationHeaderAsync(config);

            var creds  = new TokenCredentials(token);
            var client = new EventHubManagementClient(creds)
            {
                SubscriptionId = config.SubscriptionId
            };

            return(client);
        }
Esempio n. 17
0
        /// <summary>
        ///   Performs the tasks needed to remove an ephemeral Event Hubs namespace used as a container for Event Hub instances
        ///   for a specific test run.
        /// </summary>
        ///
        /// <param name="namespaceName">The name of the namespace to delete.</param>
        ///
        public static async Task DeleteNamespaceAsync(string namespaceName)
        {
            var subscription  = EventHubsTestEnvironment.Instance.SubscriptionId;
            var resourceGroup = EventHubsTestEnvironment.Instance.ResourceGroup;
            var token         = await ResourceManager.AcquireManagementTokenAsync().ConfigureAwait(false);

            using (var client = new EventHubManagementClient(AzureResourceManagerUri, new TokenCredentials(token))
            {
                SubscriptionId = subscription
            })
            {
                await ResourceManager.CreateRetryPolicy().ExecuteAsync(() => client.Namespaces.DeleteAsync(resourceGroup, namespaceName)).ConfigureAwait(false);
            }
        }
        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;
            }
        }
Esempio n. 19
0
        /// <summary>
        ///   It returns an EventHubScope after instanciating a management client
        ///   and querying the consumer groups from the portal.
        /// </summary>
        ///
        /// <returns>The <see cref="EventHubScope" /> that will be used in a given test run.</returns>
        ///
        private static async Task <EventHubScope> BuildScopeFromExistingEventHub()
        {
            var token = await ResourceManager.AquireManagementTokenAsync();

            using (var client = new EventHubManagementClient(new TokenCredentials(token))
            {
                SubscriptionId = TestEnvironment.EventHubsSubscription
            })
            {
                IPage <ConsumerGroup> consumerGroups = client.ConsumerGroups.ListByEventHub(TestEnvironment.EventHubsResourceGroup,
                                                                                            TestEnvironment.EventHubsNamespace,
                                                                                            TestEnvironment.EventHubName);

                return(new EventHubScope(TestEnvironment.EventHubName, consumerGroups.Select(c => c.Name).ToList(), wasEventHubCreated: false));
            }
        }
Esempio n. 20
0
        public EventHubUtil(string subscriptionId, string defaultLocation, string keyvaultName, string clientId, string tenantId, string secretPrefix)
        {
            _defaultLocation = defaultLocation;
            _keyvaultName    = keyvaultName;
            _clientId        = clientId;
            _tenantId        = tenantId;

            _secretKey = KeyVault.KeyVault.GetSecretFromKeyvault(_keyvaultName, secretPrefix + "clientsecret");

            _token          = GetAccessToken(_tenantId, _clientId, _secretKey);
            _subscriptionId = subscriptionId;
            var creds = new TokenCredentials(_token);

            _eventHubManagementClient = new EventHubManagementClient(creds)
            {
                SubscriptionId = subscriptionId
            };
        }
Esempio n. 21
0
        /// <summary>
        ///   Performs the tasks needed to create a new Event Hubs namespace within a resource group, intended to be used as
        ///   an ephemeral container for the Event Hub instances used in a given test run.
        /// </summary>
        ///
        /// <returns>The key attributes for identifying and accessing a dynamically created Event Hubs namespace.</returns>
        ///
        public static async Task<NamespaceProperties> CreateNamespaceAsync()
        {
            var subscription = TestEnvironment.EventHubsSubscription;
            var resourceGroup = TestEnvironment.EventHubsResourceGroup;
            var token = await ResourceManager.AquireManagementTokenAsync();

            string CreateName() => $"net-eventhubs-{ Guid.NewGuid().ToString("D") }";

            using (var client = new EventHubManagementClient(new TokenCredentials(token)) { SubscriptionId = subscription })
            {
                var location = await ResourceManager.QueryResourceGroupLocationAsync(token, resourceGroup, subscription);

                var eventHubsNamespace = new EHNamespace(sku: new Sku("Standard", "Standard", 12), tags: ResourceManager.GenerateTags(), isAutoInflateEnabled: true, maximumThroughputUnits: 20, location: location);
                eventHubsNamespace = await ResourceManager.CreateRetryPolicy<EHNamespace>().ExecuteAsync(() => client.Namespaces.CreateOrUpdateAsync(resourceGroup, CreateName(), eventHubsNamespace));

                AccessKeys accessKey = await ResourceManager.CreateRetryPolicy<AccessKeys>().ExecuteAsync(() => client.Namespaces.ListKeysAsync(resourceGroup, eventHubsNamespace.Name, TestEnvironment.EventHubsDefaultSharedAccessKey));
                return new NamespaceProperties(eventHubsNamespace.Name, accessKey.PrimaryConnectionString);
            }
        }
        public EventHubMgmtClient(
            string subscriptionId,
            RestClient restClient
            )
        {
            if (string.IsNullOrEmpty(subscriptionId))
            {
                throw new ArgumentNullException(nameof(subscriptionId));
            }
            if (restClient is null)
            {
                throw new ArgumentNullException(nameof(restClient));
            }

            _eventHubManagementClient = new EventHubManagementClient(restClient)
            {
                SubscriptionId = subscriptionId
            };
        }
Esempio n. 23
0
        /// <summary>
        ///   It returns a scope after instantiating a management client
        ///   and querying the consumer groups from the portal.
        /// </summary>
        ///
        /// <returns>The <see cref="EventHubScope" /> that will be used in a given test run.</returns>
        ///
        private static async Task <EventHubScope> BuildScopeFromExistingEventHub()
        {
            var token = await ResourceManager.AcquireManagementTokenAsync().ConfigureAwait(false);

            using (var client = new EventHubManagementClient(AzureResourceManagerUri, new TokenCredentials(token))
            {
                SubscriptionId = EventHubsTestEnvironment.Instance.SubscriptionId
            })
            {
                var consumerGroups = client.ConsumerGroups.ListByEventHub
                                     (
                    EventHubsTestEnvironment.Instance.ResourceGroup,
                    EventHubsTestEnvironment.Instance.EventHubsNamespace,
                    EventHubsTestEnvironment.Instance.EventHubNameOverride
                                     );

                return(new EventHubScope(EventHubsTestEnvironment.Instance.EventHubNameOverride, consumerGroups.Select(c => c.Name).ToList(), shouldRemoveEventHubAtScopeCompletion: false));
            }
        }
Esempio n. 24
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogDebug("STARTING processing");
            var         consumerGroupName     = _configuration["Consumer:consumer-group"];
            var         authorizationRuleName = _configuration["Consumer:authorization-rule"];
            var         prefix        = _configuration["Consumer:prefix"];
            List <Task> processors    = new List <Task>();
            var         ehDinifitions = await _consumersRepository.GetMonitoringEh();

            foreach (var ehDefinition in ehDinifitions)
            {
                EventHubManagementClient ehClient = await GetEhClient(ehDefinition.Subscription);

                var ehs = await GetConnectionStrings(consumerGroupName, authorizationRuleName, prefix, ehClient, ehDefinition);

                var storage = GetStorageConnection();
                foreach (var eh in ehs)
                {
                    EventProcessorHost processorHost =
                        new EventProcessorHost(
                            "splunk-host-processor",
                            eh.ehName,
                            eh.consumerGroup,
                            eh.connectionString,
                            storage.storageConnectionString,
                            storage.container,
                            $"{ehDefinition.Subscription}/{eh.ehName}/"
                            );
                    _logger.LogDebug($"Adding processor host {ehDefinition.Subscription}/{eh.ehName}/");
                    processors.Add(processorHost.RegisterEventProcessorFactoryAsync(
                                       new LogEventProcessorFactory(
                                           _logger,
                                           _consumersRepository,
                                           ehDefinition.Subscription,
                                           eh.resourceGroup,
                                           eh.ehNamespace,
                                           _httpClient)));
                    _eventProcessorHosts.Add(processorHost);
                }
            }
            Task.WaitAll(processors.ToArray());
        }
Esempio n. 25
0
        private static void ScaleDownNamespace(EventhubNamespace ns, EventHubManagementClient ehClient, ILogger log)
        {
            log.LogInformation($"ScaleDownNamespace for ns:{ns.Namespace}");

            var nsInfo = ehClient.Namespaces.Get(ns.ResourceGroup, ns.Namespace);

            if (nsInfo.Sku.Capacity <= ns.TargetThroughputUnits)
            {
                log.LogInformation($"Namespace:{ns.Namespace} in RG:{ns.ResourceGroup} already at or below target capacity (Current:{nsInfo.Sku.Capacity} Target:{ns.TargetThroughputUnits})");
                return;
            }

            var nsUpdate = new EHNamespace()
            {
                Sku = new Sku(nsInfo.Sku.Name, capacity: ns.TargetThroughputUnits)
            };

            log.LogInformation($"Updating Namespace:{ns.Namespace} in RG:{ns.ResourceGroup} from:{nsInfo.Sku.Capacity} to:{nsUpdate.Sku.Capacity}");
            ehClient.Namespaces.Update(ns.ResourceGroup, ns.Namespace, nsUpdate);
        }
Esempio n. 26
0
        private static async Task CreateOrUpdateNamespaceAsync(
            EventHubManagementClient client,
            string rgName,
            string ns,
            string location,
            string skuName)
        {
            var namespaceParams = new EHNamespace
            {
                Location = location,
                Sku      = new Sku
                {
                    Tier = skuName,
                    Name = skuName
                }
            };

            var namespace1 = await client.Namespaces.CreateOrUpdateAsync(rgName, ns, namespaceParams);

            Console.WriteLine($"{namespace1.Name} create/update completed");
        }
Esempio n. 27
0
        /// <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));
        }
Esempio n. 28
0
        public static async Task <AzureResourceProperties> CreateNamespaceAsync()
        {
            var subscription  = TestUtility.EventHubsSubscription;
            var resourceGroup = TestUtility.EventHubsResourceGroup;
            var token         = await AquireManagementTokenAsync();

            string CreateName() => $"net-eventhubs-track-one-{ Guid.NewGuid().ToString("D").Substring(0, 8) }";

            using (var client = new EventHubManagementClient(TestUtility.ResourceManager, new TokenCredentials(token))
            {
                SubscriptionId = subscription
            })
            {
                var location = await QueryResourceGroupLocationAsync(token, resourceGroup, subscription);

                var eventHubsNamespace = new EHNamespace(sku: new Sku("Standard", "Standard", 12), tags: GetResourceTags(), isAutoInflateEnabled: true, maximumThroughputUnits: 20, location: location);
                eventHubsNamespace = await CreateRetryPolicy <EHNamespace>().ExecuteAsync(() => client.Namespaces.CreateOrUpdateAsync(resourceGroup, CreateName(), eventHubsNamespace));

                var accessKey = await CreateRetryPolicy <AccessKeys>().ExecuteAsync(() => client.Namespaces.ListKeysAsync(resourceGroup, eventHubsNamespace.Name, "RootManageSharedAccessKey"));

                return(new AzureResourceProperties(eventHubsNamespace.Name, accessKey.PrimaryConnectionString, true));
            }
        }
Esempio n. 29
0
        /// <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));
            }
        }
Esempio n. 30
0
 public EventHubsManagementClient(EventHubManagementClient eventHubManagementClient, AppConfig appConfig)
 {
     this.eventHubManagementClient = eventHubManagementClient;
     this.appConfig = appConfig;
 }