Skip to content

Commit

Permalink
Allowing proxy api info to save in ApiClientManager (#421)
Browse files Browse the repository at this point in the history
* Allowing proxy api info to flow downstream

* Adding apiClientManager
  • Loading branch information
nchapagain001 authored Jan 15, 2025
1 parent 5a11998 commit ece8949
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace VirtualClient
{
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using VirtualClient.Contracts;
using VirtualClient.Contracts.Proxy;

[TestFixture]
[Category("Unit")]
Expand Down Expand Up @@ -107,5 +110,23 @@ public void ApiClientManagerCreatesTheExpectedApiClientWhenRolesExist()
actualClient = clientManager.GetOrCreateApiClient("AnyServer", new ClientInstance("AnyServer", "1.2.3.5", ClientRole.Server));
Assert.AreEqual($"http://1.2.3.5:4502/", actualClient.BaseUri.ToString());
}

[Test]
public void ApiClientManagerCreatesTheExpectedProxyApiClient()
{
Uri uri = new Uri("http://1.2.3.5:4502/");
ApiClientManager clientManager = new ApiClientManager();

Assert.IsEmpty(clientManager.GetProxyApiClients());

clientManager.GetOrCreateProxyApiClient("1", uri);
IProxyApiClient proxyClient = clientManager.GetProxyApiClient("1");
Assert.AreEqual(proxyClient.BaseUri, uri);

IEnumerable<IProxyApiClient> proxyList = clientManager.GetProxyApiClients();
Assert.IsNotEmpty(proxyList);

Assert.AreEqual(proxyList.FirstOrDefault().BaseUri, uri);
}
}
}
17 changes: 17 additions & 0 deletions src/VirtualClient/VirtualClient.Core/ApiClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ public IEnumerable<IApiClient> GetApiClients()
return clients;
}

/// <summary>
/// Get all existing/cached Proxy API clients.
/// </summary>
/// <returns>
/// A set of <see cref="IProxyApiClient"/> that can be used to interface with Proxy API Endpoints.
/// </returns>
public IEnumerable<IProxyApiClient> GetProxyApiClients()
{
List<IProxyApiClient> clients = new List<IProxyApiClient>();
if (this.proxyApiClients.Any())
{
clients.AddRange(this.proxyApiClients.Values);
}

return clients;
}

/// <summary>
/// Returns the effective port to use for hosting the API service. The port can be defined/overridden
/// on the command line.
Expand Down
9 changes: 9 additions & 0 deletions src/VirtualClient/VirtualClient.Core/IApiClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public interface IApiClientManager
/// <returns>The port on which the REST API service will be hosted.</returns>
int GetApiPort(ClientInstance instance = null);

/// <summary>
/// Get all an existing/cached proxy API client.
/// </summary>
/// <returns>
/// List of <see cref="IProxyApiClient"/> that can be used to interface with a target
/// Virtual Client proxy API service.
/// </returns>
IEnumerable<IProxyApiClient> GetProxyApiClients();

/// <summary>
/// Gets an existing/cached proxy API client or creates a new one adding it to the cache.
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion src/VirtualClient/VirtualClient.Main/CommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ protected virtual IServiceCollection InitializeDependencies(string[] args)
IProfileManager profileManager = new ProfileManager();
List <IBlobManager> blobStores = new List<IBlobManager>();

ApiClientManager apiClientManager = new ApiClientManager(this.ApiPorts);

// The Virtual Client supports a proxy API interface. When a proxy API is used, all dependencies/blobs will be download
// through the proxy endpoint. All content/files will be uploaded through the proxy endpoint. All telemetry will be uploaded
// the proxy endpoint (with the exception of file logging which remains as-is). This enables Virtual Client to support disconnected
Expand All @@ -341,6 +343,9 @@ protected virtual IServiceCollection InitializeDependencies(string[] args)

blobStores.Add(DependencyFactory.CreateProxyBlobManager(new DependencyProxyStore(DependencyBlobStore.Content, this.ProxyApiUri), contentSource?.ToString(), debugLogger));
blobStores.Add(DependencyFactory.CreateProxyBlobManager(new DependencyProxyStore(DependencyBlobStore.Packages, this.ProxyApiUri), packageSource?.ToString(), debugLogger));

// Enabling ApiClientManager to save Proxy API will allow downstream to access proxy endpoints as required.
apiClientManager.GetOrCreateProxyApiClient(Guid.NewGuid().ToString(), this.ProxyApiUri);
}
else
{
Expand All @@ -355,10 +360,12 @@ protected virtual IServiceCollection InitializeDependencies(string[] args)
}
}



IServiceCollection dependencies = new ServiceCollection();
dependencies.AddSingleton<PlatformSpecifics>(platformSpecifics);
dependencies.AddSingleton<IApiManager>(apiManager);
dependencies.AddSingleton<IApiClientManager>(new ApiClientManager(this.ApiPorts));
dependencies.AddSingleton<IApiClientManager>(apiClientManager);
dependencies.AddSingleton<IConfiguration>(configuration);
dependencies.AddSingleton<IDiskManager>(systemManagement.DiskManager);
dependencies.AddSingleton<IExpressionEvaluator>(ProfileExpressionEvaluator.Instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ public IProxyApiClient GetOrCreateProxyApiClient(string id, Uri uri)
throw new NotImplementedException();
}

/// <summary>
/// Not implemented.
/// </summary>
public IEnumerable<IProxyApiClient> GetProxyApiClients()
{
throw new NotImplementedException();
}

/// <summary>
/// Creates API clients from the ones that are already cached.
/// </summary>
Expand Down

0 comments on commit ece8949

Please sign in to comment.