-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
322 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/SignalRServiceExtension/Config/IConfigurationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Azure.Core; | ||
using Microsoft.Azure.SignalR; | ||
using Microsoft.Extensions.Azure; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.SignalRService | ||
{ | ||
/// <summary> | ||
/// A helper class to parse <see cref="ServiceEndpoint"/> from configuration. | ||
/// </summary> | ||
internal static class IConfigurationExtensions | ||
{ | ||
public static IEnumerable<ServiceEndpoint> GetEndpoints(this IConfiguration config, AzureComponentFactory azureComponentFactory) | ||
{ | ||
foreach (IConfigurationSection child in config.GetChildren()) | ||
{ | ||
if (child.TryGetNamedEndpointFromIdentity(azureComponentFactory, out ServiceEndpoint endpoint)) | ||
{ | ||
yield return endpoint; | ||
continue; | ||
} | ||
|
||
foreach (ServiceEndpoint item in child.GetNamedEndpointsFromConnectionString()) | ||
{ | ||
yield return item; | ||
} | ||
} | ||
} | ||
|
||
public static IEnumerable<ServiceEndpoint> GetNamedEndpointsFromConnectionString(this IConfigurationSection section) | ||
{ | ||
string endpointName = section.Key; | ||
if (section.Value != null) | ||
{ | ||
yield return new ServiceEndpoint(section.Key, section.Value); | ||
} | ||
|
||
if (section["primary"] != null) | ||
{ | ||
yield return new ServiceEndpoint(section["primary"], EndpointType.Primary, endpointName); | ||
} | ||
|
||
if (section["secondary"] != null) | ||
{ | ||
yield return new ServiceEndpoint(section["secondary"], EndpointType.Secondary, endpointName); | ||
} | ||
} | ||
|
||
public static bool TryGetNamedEndpointFromIdentity(this IConfigurationSection section, AzureComponentFactory azureComponentFactory, out ServiceEndpoint endpoint) | ||
{ | ||
string text = section["ServiceUri"]; | ||
if (text != null) | ||
{ | ||
string key = section.Key; | ||
EndpointType value = section.GetValue("Type", EndpointType.Primary); | ||
TokenCredential credential = azureComponentFactory.CreateTokenCredential(section); | ||
endpoint = new ServiceEndpoint(new Uri(text), credential, value, key); | ||
return true; | ||
} | ||
|
||
endpoint = null; | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
test/SignalRServiceExtension.Tests/Config/IConfigurationExtensionsFacts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Linq; | ||
using Azure.Identity; | ||
using Microsoft.Azure.SignalR; | ||
using Microsoft.Azure.SignalR.Tests.Common; | ||
using Microsoft.Azure.WebJobs.Extensions.SignalRService; | ||
using Microsoft.Extensions.Azure; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace SignalRServiceExtension.Tests.Config | ||
{ | ||
public class IConfigurationExtensionsFacts | ||
{ | ||
[Fact] | ||
public void TestGetNamedEndpointFromIdentityWithNoUri() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddAzureClientsCore(); | ||
var factory = services.BuildServiceProvider().GetRequiredService<AzureComponentFactory>(); | ||
var config = new ConfigurationBuilder().AddInMemoryCollection().Build(); | ||
Assert.False(config.GetSection("eastus").TryGetNamedEndpointFromIdentity(factory, out _)); | ||
} | ||
|
||
[Fact] | ||
public void TestGetNamedEndpointFromIdentityWithOnlyUri() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddAzureClientsCore(); | ||
var factory = services.BuildServiceProvider().GetRequiredService<AzureComponentFactory>(); | ||
|
||
var config = new ConfigurationBuilder().AddInMemoryCollection().Build(); | ||
var uri = "http://signalr.service.uri.com:441"; | ||
config["eastus:serviceUri"] = uri; | ||
|
||
Assert.True(config.GetSection("eastus").TryGetNamedEndpointFromIdentity(factory, out var endpoint)); | ||
Assert.Equal("eastus", endpoint.Name); | ||
Assert.Equal(uri, endpoint.Endpoint); | ||
Assert.IsType<DefaultAzureCredential>((endpoint.AccessKey as AadAccessKey).TokenCredential); | ||
Assert.Equal(EndpointType.Primary, endpoint.EndpointType); | ||
} | ||
|
||
[Fact] | ||
public void TestGetNamedEndpointFromIdentityWithAllEndpointField() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddAzureClientsCore(); | ||
var factory = services.BuildServiceProvider().GetRequiredService<AzureComponentFactory>(); | ||
|
||
var config = new ConfigurationBuilder().AddInMemoryCollection().Build(); | ||
var uri = "http://signalr.service.uri.com:441"; | ||
config["eastus:serviceUri"] = uri; | ||
config["eastus:credential"] = "managedidentity"; | ||
config["eastus:type"] = "secondary"; | ||
|
||
Assert.True(config.GetSection("eastus").TryGetNamedEndpointFromIdentity(factory, out var endpoint)); | ||
Assert.Equal("eastus", endpoint.Name); | ||
Assert.Equal(uri, endpoint.Endpoint); | ||
Assert.IsType<ManagedIdentityCredential>((endpoint.AccessKey as AadAccessKey).TokenCredential); | ||
Assert.Equal(EndpointType.Secondary, endpoint.EndpointType); | ||
} | ||
|
||
[Fact] | ||
public void TestGetNamedEndpointsFromConnectionString() | ||
{ | ||
var connectionString = FakeEndpointUtils.GetFakeConnectionString(1).Single(); | ||
var config = new ConfigurationBuilder().AddInMemoryCollection().Build(); | ||
config["eastus"] = connectionString; | ||
config["eastus:primary"] = connectionString; | ||
config["eastus:secondary"] = connectionString; | ||
var endpoints = config.GetSection("eastus").GetNamedEndpointsFromConnectionString().ToArray(); | ||
Assert.Equal(3, endpoints.Length); | ||
Assert.All(endpoints, e => Assert.Equal("eastus", e.Name)); | ||
Assert.Equal(EndpointType.Primary, endpoints[0].EndpointType); | ||
Assert.Equal(EndpointType.Primary, endpoints[1].EndpointType); | ||
Assert.Equal(EndpointType.Secondary, endpoints[2].EndpointType); | ||
} | ||
|
||
[Fact] | ||
public void TestGetEndpointsFromIdentityAndConnectionString() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddAzureClientsCore(); | ||
var factory = services.BuildServiceProvider().GetRequiredService<AzureComponentFactory>(); | ||
var config = new ConfigurationBuilder().AddInMemoryCollection().Build(); | ||
|
||
var serviceUri = "http://signalr.service.uri.com:441"; | ||
config["endpoints:eastus:serviceUri"] = serviceUri; | ||
config["endpoints:westus:secondary"] = FakeEndpointUtils.GetFakeConnectionString(1).Single(); | ||
|
||
var endpoints = config.GetSection("endpoints").GetEndpoints(factory).ToArray(); | ||
Assert.Collection(endpoints, e => | ||
{ | ||
Assert.Equal("eastus", e.Name); | ||
Assert.Equal(serviceUri, e.Endpoint); | ||
}, e => | ||
{ | ||
Assert.Equal("westus", e.Name); | ||
Assert.Equal(EndpointType.Secondary, e.EndpointType); | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.