Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Commit

Permalink
Version 2.0 updates
Browse files Browse the repository at this point in the history
* Update MongoDb NuGet package version to 2.12.4
* Update readme instructions
* Add Copy Database functionality (closes #1)
* Set Dockerfile to not run as root (closes #3)
  • Loading branch information
TreyHendon committed Jul 12, 2021
1 parent 55461f0 commit da19a72
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 2 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,39 @@ JSON Body:
}
```

## Copy Database
POST: https://localhost:5001/copydatabase
Authentication: Disabled
JSON Body:
```
{
"SourceDatabase":{
"Host":"source.mongo.cosmos.azure.com",
"Port":10255,
"UseTls":true,
"User":"source",
"Password":"source_password",
"AuthDatabaseName":"mydb",
"ReplicaSet":"globaldb",
"ApplicationName":"@source@",
"MaxConnectionIdleTimeStringOfMs":"120000",
"DatabaseName":"mydb"
},
"TargetDatabase":{
"Host":"target.mongo.cosmos.azure.com",
"Port":10255,
"UseTls":true,
"User":"target",
"Password":"target_password",
"AuthDatabaseName":"mydb",
"ReplicaSet":"globaldb",
"ApplicationName":"@target@",
"MaxConnectionIdleTimeStringOfMs":"120000",
"DatabaseName":"mydb"
}
}
```

# Considerations

## Creating a Basic Database
Expand Down
1 change: 1 addition & 0 deletions src/MongoDbManagement/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ RUN dotnet publish -c Release -o /app/publish --no-restore --no-build
FROM mcr.microsoft.com/dotnet/aspnet:${DOTNET_VERSION} AS release
WORKDIR /app
COPY --from=publish /app/publish ./
USER user
ENTRYPOINT [ "dotnet", "MongoDbManagement.API.dll" ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// MIT License
//
// Copyright (c) 2020 RedSail Technologies
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDbManagement.API.Models;

namespace MongoDbManagement.API.Controllers
{
/// <summary>
/// Copy database controller.
/// </summary>
/// <seealso cref="ControllerBase" />
[ApiController]
[Route("[controller]")]
public class CopyDatabaseController : ControllerBase
{
/// <summary>
/// The _logger.
/// </summary>
private readonly ILogger<CopyDatabaseController> _logger;

/// <summary>
/// Initializes a new instance of the <see cref="CopyDatabaseController"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
public CopyDatabaseController(ILogger<CopyDatabaseController> logger)
{
_logger = logger;
}

/// <summary>
/// Copies a database.
/// </summary>
/// <param name="mongoDatabaseCopy">The mongo database copy model.</param>
/// <returns>
/// The 201 or 400.
/// </returns>
[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public ActionResult<object> CopyDatabase(MongoDatabaseCopy mongoDatabaseCopy)
{
try
{
MongoClient sourceClient = Helper.GetMongoClient(mongoDatabaseCopy.SourceDatabase);
var sourceDatabase = sourceClient.GetDatabase(mongoDatabaseCopy.SourceDatabase.DatabaseName);

MongoClient targetClient = Helper.GetMongoClient(mongoDatabaseCopy.TargetDatabase);
var targetDatabase = targetClient.GetDatabase(mongoDatabaseCopy.TargetDatabase.DatabaseName);

var sourceCollectionNames = sourceDatabase.ListCollectionNames().ToList();
foreach (var sourceCollectionName in sourceCollectionNames)
{
var sourceCollection = sourceDatabase.GetCollection<BsonDocument>(sourceCollectionName);
var sourceDocuments = sourceCollection.Find(_ => true).ToList();

targetDatabase.CreateCollection(sourceCollectionName);
var targetCollection = targetDatabase.GetCollection<BsonDocument>(sourceCollectionName);
targetCollection.InsertMany(sourceDocuments);

var sourceIndexes = sourceCollection.Indexes.List().ToList();
foreach (var sourceIndex in sourceIndexes)
{
var key = sourceIndex.GetElement("key");
var value = key.Value.AsBsonDocument;
var keyName = value.Elements.FirstOrDefault().Name;
var sortOrder = value.Elements.FirstOrDefault().Value.AsInt32;

IndexKeysDefinition<BsonDocument> keys;
if (sortOrder == 1)
{
keys = Builders<BsonDocument>.IndexKeys.Ascending(keyName);
}
else
{
keys = Builders<BsonDocument>.IndexKeys.Descending(keyName);
}

// TODO: FIND OPTIONS ELEMENT(?) AND DUPLICATE
var model = new CreateIndexModel<BsonDocument>(keys);
targetCollection.Indexes.CreateOne(model);
}
}
}
catch
{
return StatusCodes.Status400BadRequest;
}

return StatusCodes.Status201Created;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// MIT License
//
// Copyright (c) 2020 RedSail Technologies
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

namespace MongoDbManagement.API.Models
{
/// <summary>
/// Mongo database copy.
/// </summary>
public class MongoDatabaseCopy
{
/// <summary>
/// Gets or sets the source database.
/// </summary>
/// <value>
/// The source database.
/// </value>
public MongoDatabase SourceDatabase { get; set; }

/// <summary>
/// Gets or sets the target database.
/// </summary>
/// <value>
/// The target database.
/// </value>
public MongoDatabase TargetDatabase { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MongoDb.Bson" Version="2.12.3" />
<PackageReference Include="MongoDb.Driver" Version="2.12.3" />
<PackageReference Include="MongoDb.Bson" Version="2.12.4" />
<PackageReference Include="MongoDb.Driver" Version="2.12.4" />
</ItemGroup>

</Project>

0 comments on commit da19a72

Please sign in to comment.