Skip to content

Commit

Permalink
Make mongodb connection string used for tests configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
kmorkos committed Jul 25, 2024
1 parent 5ae28a6 commit 13aef63
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions Tools/DeployApps/BaasClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public class FunctionReturn

private readonly HttpClient _client = new();

private readonly string? _mongodbConnString;

private readonly string? _clusterName;

private readonly TextWriter _output;
Expand Down Expand Up @@ -190,18 +192,24 @@ static BaasClient()
BsonSerializer.RegisterSerializer(new ObjectSerializer(_ => true));
}

private BaasClient(Uri baseUri, string differentiator, TextWriter output, string? clusterName = null)
private BaasClient(
Uri baseUri,
string differentiator,
TextWriter output,
string? clusterName = null,
string? mongodbConnString = null)
{
_client.BaseAddress = new Uri(baseUri, "api/admin/v3.0/");
_client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");
_clusterName = clusterName;
_mongodbConnString = mongodbConnString;
Differentiator = differentiator;
_output = output;
}

public static async Task<BaasClient> Docker(Uri baseUri, string differentiator, TextWriter output)
public static async Task<BaasClient> Docker(Uri baseUri, string differentiator, TextWriter output, string? mongodbConnString)
{
var result = new BaasClient(baseUri, differentiator, output);
var result = new BaasClient(baseUri, differentiator, output, mongodbConnString: mongodbConnString);

await result.Authenticate("local-userpass", new
{
Expand All @@ -217,7 +225,7 @@ public static async Task<BaasClient> Docker(Uri baseUri, string differentiator,

public static async Task<BaasClient> Atlas(Uri baseUri, string differentiator, TextWriter output, string clusterName, string apiKey, string privateApiKey, string groupId)
{
var result = new BaasClient(baseUri, differentiator, output, clusterName);
var result = new BaasClient(baseUri, differentiator, output, clusterName: clusterName);
await result.Authenticate("mongodb-cloud", new
{
username = apiKey,
Expand All @@ -235,6 +243,8 @@ private class BaasArgs

public string? BaasUrl { get; set; }

public string? MongoDBConnectionString { get; set; }

public string? BaasCluster { get; set; }

public string? BaasApiKey { get; set; }
Expand Down Expand Up @@ -266,7 +276,7 @@ private class BaasArgs
if (!string.IsNullOrEmpty(extracted.BaasaasApiKey))
{
baseUri = await GetOrDeployContainer(extracted.BaasaasApiKey!, differentiator, output);
client = await Docker(baseUri, differentiator, output);
client = await Docker(baseUri, differentiator, output, extracted.MongoDBConnectionString);
}
else
{
Expand All @@ -278,7 +288,7 @@ private class BaasArgs
baseUri = new Uri(extracted.BaasUrl!);

client = extracted.UseDocker
? await Docker(baseUri, differentiator, output)
? await Docker(baseUri, differentiator, output, extracted.MongoDBConnectionString)
: await Atlas(baseUri, differentiator, output, extracted.BaasCluster, extracted.BaasApiKey, extracted.BaasPrivateApiKey, extracted.BaasProjectId);
}

Expand Down Expand Up @@ -673,10 +683,12 @@ private async Task<string> CreateService(BaasApp app, string name, string type,

private async Task<string> CreateMongodbService(BaasApp app, object syncConfig)
{
var serviceName = _clusterName == null ? "mongodb" : "mongodb-atlas";
object mongoConfig = _clusterName == null ? new { uri = "mongodb://localhost:26000" } : new { clusterName = _clusterName };
var datasourceType = _clusterName == null ? "mongodb" : "mongodb-atlas";
object mongoConfig = _clusterName == null
? new { uri = _mongodbConnString ?? "mongodb://localhost:26000" }
: new { clusterName = _clusterName };

var mongoServiceId = await CreateService(app, "BackingDB", serviceName, mongoConfig);
var mongoServiceId = await CreateService(app, "BackingDB", datasourceType, mongoConfig);

// The cluster linking must be separated from enabling sync because Atlas
// takes a few seconds to provision a user for BaaS, meaning enabling sync
Expand Down

0 comments on commit 13aef63

Please sign in to comment.