Skip to content

Commit

Permalink
(service-client): Add constructors in registry manager to accept aad …
Browse files Browse the repository at this point in the history
…and sas tokens. (#1788)
  • Loading branch information
vinagesh committed Mar 22, 2021
1 parent 601e37d commit 362fa2f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
12 changes: 4 additions & 8 deletions iothub/service/src/HttpRegistryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ internal class HttpRegistryManager : RegistryManager
private IHttpClientHelper _httpClientHelper;
private readonly string _iotHubName;

internal HttpRegistryManager(IotHubConnectionString connectionString, HttpTransportSettings transportSettings)
internal HttpRegistryManager(IotHubConnectionProperties connectionProperties, HttpTransportSettings transportSettings)
{
_iotHubName = connectionString.IotHubName;
_iotHubName = connectionProperties.IotHubName;
_httpClientHelper = new HttpClientHelper(
connectionString.HttpsEndpoint,
connectionString,
connectionProperties.HttpsEndpoint,
connectionProperties,
ExceptionHandlingHelper.GetDefaultErrorMapping(),
s_defaultOperationTimeout,
transportSettings.Proxy);
Expand Down Expand Up @@ -735,7 +735,6 @@ public override Task<BulkRegistryOperationResult> UpdateDevices2Async(IEnumerabl
{
Logging.Exit(this, $"Updating multiple devices: count: {devices?.Count()} - Force update: {forceUpdate}", nameof(UpdateDevices2Async));
}

}

public override Task RemoveDeviceAsync(string deviceId)
Expand Down Expand Up @@ -937,7 +936,6 @@ public override Task<RegistryStatistics> GetRegistryStatisticsAsync(Cancellation

return _httpClientHelper.GetAsync<RegistryStatistics>(GetStatisticsUri(), errorMappingOverrides, null, cancellationToken);
}

catch (Exception ex)
{
Logging.Error(this, $"{nameof(GetRegistryStatisticsAsync)} threw an exception: {ex}", nameof(GetRegistryStatisticsAsync));
Expand Down Expand Up @@ -1037,7 +1035,6 @@ public override Task<IEnumerable<Module>> GetModulesOnDeviceAsync(string deviceI

try
{

EnsureInstanceNotClosed();

return _httpClientHelper.GetAsync<IEnumerable<Module>>(
Expand Down Expand Up @@ -1473,7 +1470,6 @@ public override Task<JobProperties> ImportDevicesAsync(JobProperties jobParamete
Logging.Enter(this, $"Import Job running with {jobParameters}", nameof(ImportDevicesAsync));
try
{

jobParameters.Type = JobType.ImportDevices;
return CreateJobAsync(jobParameters, cancellationToken);
}
Expand Down
63 changes: 63 additions & 0 deletions iothub/service/src/RegistryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Shared;

#if !NET451

using Azure;
using Azure.Core;

#endif

namespace Microsoft.Azure.Devices
{
/// <summary>
Expand Down Expand Up @@ -46,6 +53,62 @@ public static RegistryManager CreateFromConnectionString(string connectionString
return new HttpRegistryManager(iotHubConnectionString, transportSettings);
}

#if !NET451

/// <summary>
/// Creates an instance of <see cref="RegistryManager"/>.
/// </summary>
/// <param name="hostName">IoT hub host name.</param>
/// <param name="credential">Azure Active Directory credentials to authenticate with IoT hub. See <see cref="TokenCredential"/></param>
/// <param name="transportSettings">The HTTP transport settings.</param>
/// <returns>An instance of <see cref="RegistryManager"/>.</returns>
public static RegistryManager Create(
string hostName,
TokenCredential credential,
HttpTransportSettings transportSettings = default)
{
if (string.IsNullOrEmpty(hostName))
{
throw new ArgumentNullException($"{nameof(hostName)} is null or empty");
}

if (credential == null)
{
throw new ArgumentNullException($"{nameof(credential)} is null");
}

var tokenCredentialProperties = new IotHubTokenCrendentialProperties(hostName, credential);
return new HttpRegistryManager(tokenCredentialProperties, transportSettings ?? new HttpTransportSettings());
}

/// <summary>
/// Creates an instance of <see cref="RegistryManager"/>.
/// </summary>
/// <param name="hostName">IoT hub host name.</param>
/// <param name="credential">Credential that generates a SAS token to authenticate with IoT hub. See <see cref="IotHubSasCredential"/></param>
/// <param name="transportSettings">The HTTP transport settings.</param>
/// <returns>An instance of <see cref="RegistryManager"/>.</returns>
public static RegistryManager Create(
string hostName,
IotHubSasCredential credential,
HttpTransportSettings transportSettings = default)
{
if (string.IsNullOrEmpty(hostName))
{
throw new ArgumentNullException($"{nameof(hostName)} is null or empty");
}

if (credential == null)
{
throw new ArgumentNullException($"{nameof(credential)} is null");
}

var sasCredentialProperties = new IotHubSasCredentialProperties(hostName, credential);
return new HttpRegistryManager(sasCredentialProperties, transportSettings ?? new HttpTransportSettings());
}

#endif

/// <inheritdoc />
public void Dispose()
{
Expand Down

0 comments on commit 362fa2f

Please sign in to comment.