Skip to content

Commit 7429f73

Browse files
author
Adrian Hall
committed
(#244) Added 2 MongoDB instances (Community and Cosmos) and centralized connection strings.
1 parent 23af4f3 commit 7429f73

File tree

17 files changed

+149
-48
lines changed

17 files changed

+149
-48
lines changed

infra/main.bicep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module resources './resources.bicep' = {
5151
output AZSQL_CONNECTION_STRING string = resources.outputs.AZSQL_CONNECTIONSTRING
5252
output COSMOS_CONNECTION_STRING string = resources.outputs.COSMOS_CONNECTIONSTRING
5353
output MONGO_CONNECTION_STRING string = resources.outputs.MONGO_CONNECTIONSTRING
54+
output MONGOACI_CONNECTION_STRING string = resources.outputs.MONGOACI_CONNECTIONSTRING
5455
output MYSQL_CONNECTION_STRING string = resources.outputs.MYSQL_CONNECTIONSTRING
5556
output PGSQL_CONNECTION_STRING string = resources.outputs.PGSQL_CONNECTIONSTRING
5657
output SERVICE_ENDPOINT string = resources.outputs.SERVICE_ENDPOINT

infra/modules/aci-mongodb.bicep

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
targetScope = 'resourceGroup'
2+
3+
@secure()
4+
@description('The password for the administrator')
5+
param administratorPassword string
6+
7+
@description('The username for the administrator')
8+
param administratorUsername string = 'tester'
9+
10+
@description('The image URI to use.')
11+
param image string = 'mongo'
12+
13+
@minLength(1)
14+
@description('Primary location for all resources')
15+
param location string = resourceGroup().location
16+
17+
@description('The port # to expose in the Docker image')
18+
param port int = 27017
19+
20+
@description('The name of the Mongo Server to create.')
21+
param serverName string
22+
23+
@description('The list of tags to apply to all resources.')
24+
param tags object = {}
25+
26+
/*********************************************************************************/
27+
28+
resource containerGroup 'Microsoft.ContainerInstance/containerGroups@2023-05-01' = {
29+
name: toLower(serverName)
30+
location: location
31+
tags: tags
32+
properties: {
33+
containers: [
34+
{
35+
name: toLower(serverName)
36+
properties: {
37+
image: image
38+
environmentVariables: [
39+
{ name: 'MONGO_INITDB_ROOT_USERNAME', value: administratorUsername }
40+
{ name: 'MONGO_INITDB_ROOT_PASSWORD', secureValue: administratorPassword }
41+
]
42+
ports: [
43+
{
44+
port: port
45+
protocol: 'TCP'
46+
}
47+
]
48+
resources: {
49+
requests: {
50+
cpu: 2
51+
memoryInGB: 2
52+
}
53+
}
54+
}
55+
}
56+
]
57+
osType: 'Linux'
58+
restartPolicy: 'Always'
59+
ipAddress: {
60+
type: 'Public'
61+
ports: [
62+
{
63+
port: port
64+
protocol: 'TCP'
65+
}
66+
]
67+
}
68+
}
69+
}
70+
71+
/*********************************************************************************/
72+
73+
#disable-next-line outputs-should-not-contain-secrets
74+
output MONGO_CONNECTIONSTRING string = 'mongodb://${administratorUsername}:${administratorPassword}@${containerGroup.properties.ipAddress.ip}:27017/'

infra/modules/cosmos-mongodb.bicep

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,4 @@ resource collection 'Microsoft.DocumentDb/databaseAccounts/mongodbDatabases/coll
179179
/*********************************************************************************/
180180

181181
#disable-next-line outputs-should-not-contain-secrets
182-
output MONGODB_CONNECTIONSTRING string = 'mongodb://${account.properties.documentEndpoint}/${containerName}'
182+
output MONGODB_CONNECTIONSTRING string = account.listConnectionStrings().connectionStrings[1].connectionString

infra/resources.bicep

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var cosmosServerName = 'cosmos-${resourceToken}'
3636
var pgsqlServerName = 'pgsql-${resourceToken}'
3737
var mysqlServerName = 'mysql-${resourceToken}'
3838
var mongoServerName = 'mongo-${resourceToken}'
39+
var mongoaciServerName = 'mongoaci-${resourceToken}'
3940

4041
var testDatabaseName = 'unittests'
4142
var cosmosContainerName = 'Movies'
@@ -115,6 +116,17 @@ module mongodb './modules/cosmos-mongodb.bicep' = {
115116
}
116117
}
117118

119+
module mongoaci './modules/aci-mongodb.bicep' = {
120+
name: 'mongoaci-deployment-${resourceToken}'
121+
params: {
122+
location: location
123+
tags: tags
124+
serverName: mongoaciServerName
125+
administratorPassword: sqlAdminPassword
126+
127+
}
128+
}
129+
118130
module app_service './modules/appservice.bicep' = {
119131
name: 'appsvc-deployment-${resourceToken}'
120132
params: {
@@ -137,6 +149,7 @@ module app_service './modules/appservice.bicep' = {
137149
output AZSQL_CONNECTIONSTRING string = azuresql.outputs.AZSQL_CONNECTIONSTRING
138150
output COSMOS_CONNECTIONSTRING string = cosmos.outputs.COSMOS_CONNECTIONSTRING
139151
output MONGO_CONNECTIONSTRING string = mongodb.outputs.MONGODB_CONNECTIONSTRING
152+
output MONGOACI_CONNECTIONSTRING string = mongoaci.outputs.MONGO_CONNECTIONSTRING
140153
output MYSQL_CONNECTIONSTRING string = mysql.outputs.MYSQL_CONNECTIONSTRING
141154
output PGSQL_CONNECTIONSTRING string = pgsql.outputs.PGSQL_CONNECTIONSTRING
142155
output SERVICE_ENDPOINT string = app_service.outputs.SERVICE_ENDPOINT

infra/scripts/write-runsettings.ps1

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@
66
$outputs = (azd env get-values --output json | ConvertFrom-Json)
77
$outputFile = "tests\.runsettings"
88

9-
$fileContents = @"
9+
$prefix = @"
1010
<?xml version="1.0" encoding="utf-8"?>
1111
<RunSettings>
1212
<RunConfiguration>
1313
<EnvironmentVariables>
14-
<DATASYNC_AZSQL_CONNECTIONSTRING>$($outputs.AZSQL_CONNECTION_STRING)</DATASYNC_AZSQL_CONNECTIONSTRING>
15-
<DATASYNC_COSMOS_CONNECTIONSTRING>$($outputs.COSMOS_CONNECTION_STRING)</DATASYNC_COSMOS_CONNECTIONSTRING>
16-
<DATASYNC_MONGO_CONNECTIONSTRING>$($outputs.MONGO_CONNECTION_STRING)</DATASYNC_MONGO_CONNECTIONSTRING>
17-
<DATASYNC_MYSQL_CONNECTIONSTRING>$($outputs.MYSQL_CONNECTION_STRING)</DATASYNC_MYSQL_CONNECTIONSTRING>
18-
<DATASYNC_PGSQL_CONNECTIONSTRING>$($outputs.PGSQL_CONNECTION_STRING)</DATASYNC_PGSQL_CONNECTIONSTRING>
19-
<DATASYNC_SERVICE_ENDPOINT>$($outputs.SERVICE_ENDPOINT)</DATASYNC_SERVICE_ENDPOINT>
14+
15+
"@
16+
17+
$postfix = @"
2018
<ENABLE_SQL_LOGGING>true</ENABLE_SQL_LOGGING>
2119
</EnvironmentVariables>
2220
</RunConfiguration>
2321
</RunSettings>
2422
"@
2523

26-
$fileContents | Out-File -FilePath $outputFile
24+
$sb = New-Object System.Text.StringBuilder
25+
$outputs | Get-Member -MemberType Properties | Foreach-Object {
26+
$propertyName = $_.Name
27+
$propertyValue = [System.Security.SecurityElement]::Escape($outputs.$propertyName)
28+
$sb.AppendLine(" <$($propertyName)>$($propertyValue)</$($propertyName)>") | Out-Null
29+
}
30+
31+
$prefix + $sb.ToString() + $postfix | Out-File -FilePath $outputFile

tests/CommunityToolkit.Datasync.Client.Test/Live/SampleServerTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using CommunityToolkit.Datasync.TestCommon.Databases;
56
using System.Net;
67
using System.Net.Http.Json;
78

@@ -10,8 +11,8 @@ namespace CommunityToolkit.Datasync.Client.Test.Live;
1011
[ExcludeFromCodeCoverage]
1112
public class SampleServerTests
1213
{
13-
private readonly bool liveTestsAreEnabled = Environment.GetEnvironmentVariable("DATASYNC_SERVICE_ENDPOINT") is not null;
14-
private readonly string serviceEndpoint = Environment.GetEnvironmentVariable("DATASYNC_SERVICE_ENDPOINT");
14+
private readonly bool liveTestsAreEnabled = ConnectionStrings.Service is not null;
15+
private readonly string serviceEndpoint = ConnectionStrings.Service;
1516

1617
[SkippableFact]
1718
public async Task Metadata_GetsSetByServer()

tests/CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test/AzureSqlEntityTableRepository_Tests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ public class AzureSqlEntityTableRepository_Tests(DatabaseFixture fixture, ITestO
1717
{
1818
#region Setup
1919
private readonly Random random = new();
20-
private readonly string connectionString = Environment.GetEnvironmentVariable("DATASYNC_AZSQL_CONNECTIONSTRING");
2120
private List<AzureSqlEntityMovie> movies = [];
2221

2322
public async Task InitializeAsync()
2423
{
25-
if (!string.IsNullOrEmpty(this.connectionString))
24+
if (!string.IsNullOrEmpty(ConnectionStrings.AzureSql))
2625
{
27-
Context = await AzureSqlDbContext.CreateContextAsync(this.connectionString, output);
26+
Context = await AzureSqlDbContext.CreateContextAsync(ConnectionStrings.AzureSql, output);
2827
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
2928
}
3029
}
@@ -39,7 +38,7 @@ public async Task DisposeAsync()
3938

4039
private AzureSqlDbContext Context { get; set; }
4140

42-
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(this.connectionString);
41+
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(ConnectionStrings.AzureSql);
4342

4443
protected override async Task<AzureSqlEntityMovie> GetEntityAsync(string id)
4544
=> await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id);

tests/CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test/CosmosEntityTableRepository_Tests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ public class CosmosEntityTableRepository_Tests(DatabaseFixture fixture, ITestOut
1717
{
1818
#region Setup
1919
private readonly Random random = new();
20-
private readonly string connectionString = Environment.GetEnvironmentVariable("DATASYNC_COSMOS_CONNECTIONSTRING");
2120
private List<CosmosEntityMovie> movies = [];
2221

2322
public async Task InitializeAsync()
2423
{
25-
if (!string.IsNullOrEmpty(this.connectionString))
24+
if (!string.IsNullOrEmpty(ConnectionStrings.CosmosDb))
2625
{
27-
Context = await CosmosDbContext.CreateContextAsync(this.connectionString, output);
26+
Context = await CosmosDbContext.CreateContextAsync(ConnectionStrings.CosmosDb, output);
2827
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
2928
}
3029
}
@@ -39,7 +38,7 @@ public async Task DisposeAsync()
3938

4039
private CosmosDbContext Context { get; set; }
4140

42-
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(this.connectionString);
41+
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(ConnectionStrings.CosmosDb);
4342

4443
protected override async Task<CosmosEntityMovie> GetEntityAsync(string id)
4544
=> await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id);

tests/CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test/MySqlEntityTableRepository_Tests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ public class MysqlEntityTableRepository_Tests(DatabaseFixture fixture, ITestOutp
1717
{
1818
#region Setup
1919
private readonly Random random = new();
20-
private readonly string connectionString = Environment.GetEnvironmentVariable("DATASYNC_MYSQL_CONNECTIONSTRING");
2120
private List<MysqlEntityMovie> movies = [];
2221

2322
public async Task InitializeAsync()
2423
{
25-
if (!string.IsNullOrEmpty(this.connectionString))
24+
if (!string.IsNullOrEmpty(ConnectionStrings.MySql))
2625
{
27-
Context = await MysqlDbContext.CreateContextAsync(this.connectionString, output);
26+
Context = await MysqlDbContext.CreateContextAsync(ConnectionStrings.MySql, output);
2827
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
2928
}
3029
}
@@ -39,7 +38,7 @@ public async Task DisposeAsync()
3938

4039
private MysqlDbContext Context { get; set; }
4140

42-
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(this.connectionString);
41+
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(ConnectionStrings.MySql);
4342

4443
protected override async Task<MysqlEntityMovie> GetEntityAsync(string id)
4544
=> await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id);

tests/CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test/PgEntityTableRepository_Tests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ public class PgEntityTableRepository_Tests(DatabaseFixture fixture, ITestOutputH
1717
{
1818
#region Setup
1919
private readonly Random random = new();
20-
private readonly string connectionString = Environment.GetEnvironmentVariable("DATASYNC_PGSQL_CONNECTIONSTRING");
2120
private List<PgEntityMovie> movies = [];
2221

2322
public async Task InitializeAsync()
2423
{
25-
if (!string.IsNullOrEmpty(this.connectionString))
24+
if (!string.IsNullOrEmpty(ConnectionStrings.PgSql))
2625
{
27-
Context = await PgDbContext.CreateContextAsync(this.connectionString, output);
26+
Context = await PgDbContext.CreateContextAsync(ConnectionStrings.PgSql, output);
2827
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
2928
}
3029
}
@@ -39,7 +38,7 @@ public async Task DisposeAsync()
3938

4039
private PgDbContext Context { get; set; }
4140

42-
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(this.connectionString);
41+
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(ConnectionStrings.PgSql);
4342

4443
protected override async Task<PgEntityMovie> GetEntityAsync(string id)
4544
=> await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id);

0 commit comments

Comments
 (0)