Skip to content

Commit 98111e7

Browse files
added ASP.NET APIs to transfer connections (Azure#47469)
* added ASP.NET APIs to transfer connections * PR feedback
1 parent fb28819 commit 98111e7

File tree

6 files changed

+50
-3
lines changed

6 files changed

+50
-3
lines changed

sdk/cloudmachine/Azure.CloudMachine.Web/api/Azure.CloudMachine.Web.net8.0.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ namespace Azure.CloudMachine
22
{
33
public static partial class CloudMachineExtensions
44
{
5+
public static void AddCloudMachine(this Microsoft.Extensions.Hosting.IHostApplicationBuilder builder, Azure.Core.ClientWorkspace workspace) { }
6+
public static void MapCloudMachineApplication<T>(this Microsoft.AspNetCore.Builder.WebApplication application) where T : class { }
57
public static void Map<T>(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routeBuilder, T serviceImplementation) where T : class { }
68
public static System.Threading.Tasks.Task UploadFormAsync(this Azure.CloudMachine.StorageServices storage, Microsoft.AspNetCore.Http.HttpRequest multiPartFormData) { throw null; }
79
}

sdk/cloudmachine/Azure.CloudMachine.Web/src/CloudMachineExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
using System.Reflection;
88
using System.Threading.Tasks;
99
using Azure.CloudMachine;
10+
using Azure.Core;
1011
using Microsoft.AspNetCore.Builder;
1112
using Microsoft.AspNetCore.Http;
1213
using Microsoft.AspNetCore.Mvc;
1314
using Microsoft.AspNetCore.Routing;
15+
using Microsoft.Extensions.DependencyInjection;
16+
using Microsoft.Extensions.Hosting;
1417

1518
namespace Azure.CloudMachine;
1619

@@ -19,6 +22,29 @@ namespace Azure.CloudMachine;
1922
/// </summary>
2023
public static class CloudMachineExtensions
2124
{
25+
/// <summary>
26+
/// Maps TSP endpoints. CloudMachineClient needs to be in the container for this method to work.
27+
/// </summary>
28+
/// <typeparam name="T"></typeparam>
29+
/// <param name="application"></param>
30+
public static void MapCloudMachineApplication<T>(this WebApplication application) where T : class
31+
{
32+
CloudMachineClient cm = application.Services.GetRequiredService<CloudMachineClient>();
33+
T service = (T)Activator.CreateInstance(typeof(T), cm)!;
34+
application.Map<T>(service);
35+
}
36+
37+
/// <summary>
38+
/// Adds a CloudMachine client to the service collection.
39+
/// </summary>
40+
/// <param name="builder"></param>
41+
/// <param name="workspace"></param>
42+
public static void AddCloudMachine(this IHostApplicationBuilder builder, ClientWorkspace workspace)
43+
{
44+
var connections = workspace.GetAllConnectionOptions();
45+
builder.Services.AddSingleton(new CloudMachineClient(connections));
46+
}
47+
2248
/// <summary>
2349
/// Uploads a document to the storage service.
2450
/// </summary>

sdk/cloudmachine/Azure.CloudMachine/api/Azure.CloudMachine.net8.0.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ namespace Azure.CloudMachine
33
public partial class CloudMachineClient : Azure.Core.ClientWorkspace
44
{
55
protected CloudMachineClient() : base (default(Azure.Core.TokenCredential)) { }
6-
public CloudMachineClient(Azure.Core.ConnectionCollection connections = null, Azure.Core.TokenCredential credential = null) : base (default(Azure.Core.TokenCredential)) { }
76
public CloudMachineClient(Microsoft.Extensions.Configuration.IConfiguration configuration, Azure.Core.TokenCredential credential = null) : base (default(Azure.Core.TokenCredential)) { }
7+
public CloudMachineClient(System.Collections.Generic.IEnumerable<Azure.Core.ClientConnection> connections = null, Azure.Core.TokenCredential credential = null) : base (default(Azure.Core.TokenCredential)) { }
88
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
99
public Azure.Core.ConnectionCollection Connections { get { throw null; } }
1010
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
@@ -13,6 +13,7 @@ public CloudMachineClient(Microsoft.Extensions.Configuration.IConfiguration conf
1313
public Azure.CloudMachine.StorageServices Storage { get { throw null; } }
1414
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
1515
public override bool Equals(object obj) { throw null; }
16+
public override System.Collections.Generic.IEnumerable<Azure.Core.ClientConnection> GetAllConnectionOptions() { throw null; }
1617
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
1718
public override Azure.Core.ClientConnection GetConnectionOptions(string connectionId) { throw null; }
1819
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
@@ -169,6 +170,7 @@ protected ClientWorkspace(Azure.Core.TokenCredential credential) { }
169170
public Azure.Core.TokenCredential Credential { get { throw null; } }
170171
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
171172
public Azure.Core.ClientCache Subclients { get { throw null; } }
173+
public abstract System.Collections.Generic.IEnumerable<Azure.Core.ClientConnection> GetAllConnectionOptions();
172174
public abstract Azure.Core.ClientConnection GetConnectionOptions(string connectionId);
173175
}
174176
public static partial class CloudMachineClientConfiguration

sdk/cloudmachine/Azure.CloudMachine/api/Azure.CloudMachine.netstandard2.0.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ namespace Azure.CloudMachine
33
public partial class CloudMachineClient : Azure.Core.ClientWorkspace
44
{
55
protected CloudMachineClient() : base (default(Azure.Core.TokenCredential)) { }
6-
public CloudMachineClient(Azure.Core.ConnectionCollection connections = null, Azure.Core.TokenCredential credential = null) : base (default(Azure.Core.TokenCredential)) { }
76
public CloudMachineClient(Microsoft.Extensions.Configuration.IConfiguration configuration, Azure.Core.TokenCredential credential = null) : base (default(Azure.Core.TokenCredential)) { }
7+
public CloudMachineClient(System.Collections.Generic.IEnumerable<Azure.Core.ClientConnection> connections = null, Azure.Core.TokenCredential credential = null) : base (default(Azure.Core.TokenCredential)) { }
88
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
99
public Azure.Core.ConnectionCollection Connections { get { throw null; } }
1010
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
@@ -13,6 +13,7 @@ public CloudMachineClient(Microsoft.Extensions.Configuration.IConfiguration conf
1313
public Azure.CloudMachine.StorageServices Storage { get { throw null; } }
1414
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
1515
public override bool Equals(object obj) { throw null; }
16+
public override System.Collections.Generic.IEnumerable<Azure.Core.ClientConnection> GetAllConnectionOptions() { throw null; }
1617
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
1718
public override Azure.Core.ClientConnection GetConnectionOptions(string connectionId) { throw null; }
1819
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
@@ -169,6 +170,7 @@ protected ClientWorkspace(Azure.Core.TokenCredential credential) { }
169170
public Azure.Core.TokenCredential Credential { get { throw null; } }
170171
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
171172
public Azure.Core.ClientCache Subclients { get { throw null; } }
173+
public abstract System.Collections.Generic.IEnumerable<Azure.Core.ClientConnection> GetAllConnectionOptions();
172174
public abstract Azure.Core.ClientConnection GetConnectionOptions(string connectionId);
173175
}
174176
public static partial class CloudMachineClientConfiguration

sdk/cloudmachine/Azure.CloudMachine/src/ClientWorkspace.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System.Collections.Generic;
45
using System.ComponentModel;
56

67
namespace Azure.Core;
@@ -27,6 +28,12 @@ protected ClientWorkspace(TokenCredential credential)
2728
/// <returns>The connection options for the specified client type and instance ID.</returns>
2829
public abstract ClientConnection GetConnectionOptions(string connectionId);
2930

31+
/// <summary>
32+
/// Retrieves all connection options.
33+
/// </summary>
34+
/// <returns></returns>
35+
public abstract IEnumerable<ClientConnection> GetAllConnectionOptions();
36+
3037
/// <summary>
3138
/// Gets the token credential.
3239
/// </summary>

sdk/cloudmachine/Azure.CloudMachine/src/CloudMachineClient.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.ComponentModel;
67
using Azure.Core;
78
using Azure.Identity;
@@ -75,7 +76,7 @@ public CloudMachineClient(IConfiguration configuration, TokenCredential credenti
7576
/// <param name="connections"></param>
7677
/// <param name="credential">The token credential.</param>
7778
// TODO: we need to combine the configuration and the connections into a single parameter.
78-
public CloudMachineClient(ConnectionCollection connections = default, TokenCredential credential = default)
79+
public CloudMachineClient(IEnumerable<ClientConnection> connections = default, TokenCredential credential = default)
7980
#pragma warning restore AZC0007 // DO provide a minimal constructor that takes only the parameters required to connect to the service.
8081
: base(BuildCredential(credential))
8182
{
@@ -144,4 +145,11 @@ private static TokenCredential BuildCredential(TokenCredential credential)
144145
/// <inheritdoc/>
145146
[EditorBrowsable(EditorBrowsableState.Never)]
146147
public override string ToString() => Id;
148+
149+
/// <summary>
150+
/// Retrieves all connection options.
151+
/// </summary>
152+
/// <returns></returns>
153+
public override IEnumerable<ClientConnection> GetAllConnectionOptions()
154+
=> Connections;
147155
}

0 commit comments

Comments
 (0)