From 506f75f0e98763b6ff2bb9ef53f3a2c9af6f098e Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Mon, 29 Jul 2019 08:52:52 -0700 Subject: [PATCH] Add EventHub client extension methodS (#7034) --- .../src/AzureClientFactoryBuilder.cs | 19 ++++--- .../src/Internal/DefaultClientOptionsSetup.cs | 9 ++-- .../tests/TestClientsBuilderExtensions.cs | 2 +- .../Extensions/IAzureClientFactoryBuilder.cs | 2 +- ...reClientFactoryBuilderWithConfiguration.cs | 4 +- ...zureClientFactoryBuilderWithCredential.cs} | 5 +- .../src/AzureClientBuilderExtensions.cs | 50 +++++++++++++++++++ .../src/SecretClientBuilderExtensions.cs | 2 +- 8 files changed, 75 insertions(+), 18 deletions(-) rename sdk/core/Azure.Core/src/Extensions/{IAzureClientsBuilderWithCredential.cs => IAzureClientFactoryBuilderWithCredential.cs} (68%) create mode 100644 sdk/eventhub/Azure.Messaging.EventHubs/src/AzureClientBuilderExtensions.cs diff --git a/sdk/core/Azure.Core.Extensions/src/AzureClientFactoryBuilder.cs b/sdk/core/Azure.Core.Extensions/src/AzureClientFactoryBuilder.cs index 334d62d8d0ddb..f19013c15ca9f 100644 --- a/sdk/core/Azure.Core.Extensions/src/AzureClientFactoryBuilder.cs +++ b/sdk/core/Azure.Core.Extensions/src/AzureClientFactoryBuilder.cs @@ -10,7 +10,7 @@ namespace Azure.Core.Extensions { - public sealed class AzureClientFactoryBuilder : IAzureClientFactoryBuilderWithConfiguration, IAzureClientsBuilderWithCredential + public sealed class AzureClientFactoryBuilder : IAzureClientFactoryBuilderWithConfiguration, IAzureClientFactoryBuilderWithCredential { private readonly IServiceCollection _serviceCollection; @@ -25,15 +25,22 @@ internal AzureClientFactoryBuilder(IServiceCollection serviceCollection) IAzureClientBuilder IAzureClientFactoryBuilder.RegisterClientFactory(Func clientFactory) { - return ((IAzureClientsBuilderWithCredential)this).RegisterClientFactory((options, _) => clientFactory(options)); + return ((IAzureClientFactoryBuilderWithCredential)this).RegisterClientFactory((options, _) => clientFactory(options)); } IAzureClientBuilder IAzureClientFactoryBuilderWithConfiguration.RegisterClientFactory(IConfiguration configuration) { - return ((IAzureClientsBuilderWithCredential)this).RegisterClientFactory( + var credentialsFromConfig = ClientFactory.CreateCredential(configuration); + var clientBuilder =((IAzureClientFactoryBuilderWithCredential)this).RegisterClientFactory( (options, credentials) => (TClient)ClientFactory.CreateClient(typeof(TClient), typeof(TOptions), options, configuration, credentials)) - .ConfigureOptions(configuration) - .WithCredential(ClientFactory.CreateCredential(configuration)); + .ConfigureOptions(configuration); + + if (credentialsFromConfig != null) + { + clientBuilder.WithCredential(credentialsFromConfig); + } + + return clientBuilder; } public AzureClientFactoryBuilder ConfigureDefaults(Action configureOptions) @@ -63,7 +70,7 @@ public AzureClientFactoryBuilder ConfigureDefaults(IConfiguration configuration) return this; } - IAzureClientBuilder IAzureClientsBuilderWithCredential.RegisterClientFactory(Func clientFactory) + IAzureClientBuilder IAzureClientFactoryBuilderWithCredential.RegisterClientFactory(Func clientFactory) { var clientRegistration = new ClientRegistration(DefaultClientName, clientFactory); _serviceCollection.AddSingleton(clientRegistration); diff --git a/sdk/core/Azure.Core.Extensions/src/Internal/DefaultClientOptionsSetup.cs b/sdk/core/Azure.Core.Extensions/src/Internal/DefaultClientOptionsSetup.cs index fa993bd919526..93d5eced8c7c5 100644 --- a/sdk/core/Azure.Core.Extensions/src/Internal/DefaultClientOptionsSetup.cs +++ b/sdk/core/Azure.Core.Extensions/src/Internal/DefaultClientOptionsSetup.cs @@ -7,7 +7,7 @@ namespace Azure.Core.Extensions { - internal class DefaultClientOptionsSetup : IConfigureNamedOptions where T : ClientOptions + internal class DefaultClientOptionsSetup : IConfigureNamedOptions where T : class { private readonly IOptions _defaultOptions; private readonly IServiceProvider _serviceProvider; @@ -20,9 +20,12 @@ public DefaultClientOptionsSetup(IOptions defaultOpti public void Configure(T options) { - foreach (var globalConfigureOption in _defaultOptions.Value.ConfigureOptionDelegates) + if (options is ClientOptions clientOptions) { - globalConfigureOption(options, _serviceProvider); + foreach (var globalConfigureOption in _defaultOptions.Value.ConfigureOptionDelegates) + { + globalConfigureOption(clientOptions, _serviceProvider); + } } } diff --git a/sdk/core/Azure.Core.Extensions/tests/TestClientsBuilderExtensions.cs b/sdk/core/Azure.Core.Extensions/tests/TestClientsBuilderExtensions.cs index aec5710d92999..77e104c765546 100644 --- a/sdk/core/Azure.Core.Extensions/tests/TestClientsBuilderExtensions.cs +++ b/sdk/core/Azure.Core.Extensions/tests/TestClientsBuilderExtensions.cs @@ -32,7 +32,7 @@ public static IAzureClientBuilder } public static IAzureClientBuilder AddTestClientWithCredentials(this TBuilder builder, Uri uri) - where TBuilder: IAzureClientsBuilderWithCredential + where TBuilder: IAzureClientFactoryBuilderWithCredential { return builder.RegisterClientFactory((options, cred) => new TestClientWithCredentials(uri, cred, options)); } diff --git a/sdk/core/Azure.Core/src/Extensions/IAzureClientFactoryBuilder.cs b/sdk/core/Azure.Core/src/Extensions/IAzureClientFactoryBuilder.cs index 2d1b2a5b3e4b1..6b3db0e6e9709 100644 --- a/sdk/core/Azure.Core/src/Extensions/IAzureClientFactoryBuilder.cs +++ b/sdk/core/Azure.Core/src/Extensions/IAzureClientFactoryBuilder.cs @@ -8,6 +8,6 @@ namespace Azure.Core.Extensions { public interface IAzureClientFactoryBuilder { - IAzureClientBuilder RegisterClientFactory(Func clientFactory) where TOptions : ClientOptions; + IAzureClientBuilder RegisterClientFactory(Func clientFactory) where TOptions : class; } } diff --git a/sdk/core/Azure.Core/src/Extensions/IAzureClientFactoryBuilderWithConfiguration.cs b/sdk/core/Azure.Core/src/Extensions/IAzureClientFactoryBuilderWithConfiguration.cs index 1d50768977a34..1755ba5b55cb4 100644 --- a/sdk/core/Azure.Core/src/Extensions/IAzureClientFactoryBuilderWithConfiguration.cs +++ b/sdk/core/Azure.Core/src/Extensions/IAzureClientFactoryBuilderWithConfiguration.cs @@ -1,13 +1,11 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using Azure.Core.Pipeline; - namespace Azure.Core.Extensions { public interface IAzureClientFactoryBuilderWithConfiguration: IAzureClientFactoryBuilder { - IAzureClientBuilder RegisterClientFactory(TConfiguration configuration) where TOptions : ClientOptions; + IAzureClientBuilder RegisterClientFactory(TConfiguration configuration) where TOptions : class; } } diff --git a/sdk/core/Azure.Core/src/Extensions/IAzureClientsBuilderWithCredential.cs b/sdk/core/Azure.Core/src/Extensions/IAzureClientFactoryBuilderWithCredential.cs similarity index 68% rename from sdk/core/Azure.Core/src/Extensions/IAzureClientsBuilderWithCredential.cs rename to sdk/core/Azure.Core/src/Extensions/IAzureClientFactoryBuilderWithCredential.cs index 328bee5e68f3d..bb6088898fc40 100644 --- a/sdk/core/Azure.Core/src/Extensions/IAzureClientsBuilderWithCredential.cs +++ b/sdk/core/Azure.Core/src/Extensions/IAzureClientFactoryBuilderWithCredential.cs @@ -2,12 +2,11 @@ // Licensed under the MIT License. using System; -using Azure.Core.Pipeline; namespace Azure.Core.Extensions { - public interface IAzureClientsBuilderWithCredential + public interface IAzureClientFactoryBuilderWithCredential { - IAzureClientBuilder RegisterClientFactory(Func clientFactory) where TOptions : ClientOptions; + IAzureClientBuilder RegisterClientFactory(Func clientFactory) where TOptions: class; } } diff --git a/sdk/eventhub/Azure.Messaging.EventHubs/src/AzureClientBuilderExtensions.cs b/sdk/eventhub/Azure.Messaging.EventHubs/src/AzureClientBuilderExtensions.cs new file mode 100644 index 0000000000000..643b716af5752 --- /dev/null +++ b/sdk/eventhub/Azure.Messaging.EventHubs/src/AzureClientBuilderExtensions.cs @@ -0,0 +1,50 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Azure.Core.Extensions; +using Azure.Messaging.EventHubs; + +namespace Azure.ApplicationModel.Configuration +{ + /// + /// Extension methods to add client to clients builder + /// + public static class AzureClientBuilderExtensions + { + /// + /// Registers a instance with the provided + /// + public static IAzureClientBuilder AddEventHubClient(this TBuilder builder, string connectionString) + where TBuilder: IAzureClientFactoryBuilder + { + return builder.RegisterClientFactory(options => new EventHubClient(connectionString, options)); + } + + /// + /// Registers a instance with the provided and + /// + public static IAzureClientBuilder AddEventHubClient(this TBuilder builder, string connectionString, string eventHubPath) + where TBuilder: IAzureClientFactoryBuilder + { + return builder.RegisterClientFactory(options => new EventHubClient(connectionString, eventHubPath, options)); + } + + /// + /// Registers a instance with the provided and + /// + public static IAzureClientBuilder AddEventHubClientWithHost(this TBuilder builder, string host, string eventHubPath) + where TBuilder: IAzureClientFactoryBuilderWithCredential + { + return builder.RegisterClientFactory((options, token) => new EventHubClient(host, eventHubPath, token, options)); + } + + /// + /// Registers a instance with connection options loaded from the provided instance. + /// + public static IAzureClientBuilder AddEventHubClient(this TBuilder builder, TConfiguration configuration) + where TBuilder: IAzureClientFactoryBuilderWithConfiguration + { + return builder.RegisterClientFactory(configuration); + } + } +} diff --git a/sdk/keyvault/Azure.Security.KeyVault.Secrets/src/SecretClientBuilderExtensions.cs b/sdk/keyvault/Azure.Security.KeyVault.Secrets/src/SecretClientBuilderExtensions.cs index 7de41f4b11208..4bfb6dfdb3cde 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Secrets/src/SecretClientBuilderExtensions.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Secrets/src/SecretClientBuilderExtensions.cs @@ -15,7 +15,7 @@ public static class AzureClientBuilderExtensions /// Registers a instance with the provided /// public static IAzureClientBuilder AddSecretClient(this TBuilder builder, Uri vaultUri) - where TBuilder: IAzureClientsBuilderWithCredential + where TBuilder: IAzureClientFactoryBuilderWithCredential { return builder.RegisterClientFactory((options, cred) => new SecretClient(vaultUri, cred, options)); }