-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #589 from dolittle/7.3.0-legolas
Legolas - Tenants and Resources
- Loading branch information
Showing
21 changed files
with
530 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) Dolittle. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Dolittle.Runtime.DependencyInversion; | ||
|
||
namespace Dolittle.Runtime.Resources.MongoDB | ||
{ | ||
/// <summary> | ||
/// Represents <see cref="ICanProvideBindings">bindings</see> for the resources system. | ||
/// </summary> | ||
public class Bindings : ICanProvideBindings | ||
{ | ||
/// <inheritdoc /> | ||
public void Provide(IBindingProviderBuilder builder) | ||
{ | ||
builder.Bind<IKnowTheConnectionString>().To<ConnectionStringFromResourceConfiguration>(); | ||
builder.Bind<ICanGetResourceForTenant>().To<ResourceForTenantGetter>(); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
Source/Resources/MongoDB/ConnectionStringFromResourceConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) Dolittle. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Dolittle.Runtime.Lifecycle; | ||
using Dolittle.Runtime.ResourceTypes.Configuration; | ||
using MongoDB.Driver; | ||
|
||
namespace Dolittle.Runtime.Resources.MongoDB | ||
{ | ||
/// <summary> | ||
/// Represents an implementation of <see cref="IKnowTheConnectionString"/>. | ||
/// </summary> | ||
[SingletonPerTenant] | ||
public class ConnectionStringFromResourceConfiguration : IKnowTheConnectionString | ||
{ | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ConnectionStringFromResourceConfiguration"/> class. | ||
/// </summary> | ||
/// <param name="configuration">The <see cref="IConfigurationFor{T}"/> of type <see cref="ResourceConfiguration"/> to use.</param> | ||
public ConnectionStringFromResourceConfiguration(IConfigurationFor<ResourceConfiguration> configuration) | ||
{ | ||
ConnectionString = BuildConnectionString(configuration.Instance); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public MongoUrl ConnectionString { get; } | ||
|
||
MongoUrl BuildConnectionString(ResourceConfiguration configuration) | ||
{ | ||
var builder = new MongoUrlBuilder(configuration.Host); | ||
builder.UseTls = configuration.UseSSL; | ||
builder.DatabaseName = configuration.Database; | ||
return builder.ToMongoUrl(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) Dolittle. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Dolittle.Runtime.Execution; | ||
using Dolittle.Runtime.Resources.Contracts; | ||
|
||
namespace Dolittle.Runtime.Resources.MongoDB | ||
{ | ||
/// <summary> | ||
/// Defines a system that can get the MongoDB resource details for a specific tenant. | ||
/// </summary> | ||
public interface ICanGetResourceForTenant | ||
{ | ||
/// <summary> | ||
/// Gets the MongoDB resource for a specific tenant. | ||
/// </summary> | ||
/// <param name="executionContext">The <see cref="ExecutionContext"/> that specifies the tenant..</param> | ||
/// <returns>The <see cref="GetMongoDBResponse"/> with the details for using the MongoDB resource.</returns> | ||
GetMongoDBResponse GetResource(ExecutionContext executionContext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Dolittle. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using MongoDB.Driver; | ||
|
||
namespace Dolittle.Runtime.Resources.MongoDB | ||
{ | ||
/// <summary> | ||
/// Defines a system that knows about the connection string to a MongoDB resource for the current tenant. | ||
/// </summary> | ||
public interface IKnowTheConnectionString | ||
{ | ||
/// <summary> | ||
/// Gets the <see cref="MongoUrl">connection string</see> for the MongoDB resource for the current tenant. | ||
/// </summary> | ||
MongoUrl ConnectionString { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) Dolittle. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using Dolittle.Runtime.ApplicationModel; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Dolittle.Runtime.Resources.MongoDB | ||
{ | ||
/// <summary> | ||
/// Represents a extensions for <see cref="Grpc.Core.Logging.ILogger" />. | ||
/// </summary> | ||
static class LoggerExtensions | ||
{ | ||
static readonly Action<ILogger, Guid, Exception> _getResourceCalled = LoggerMessage | ||
.Define<Guid>( | ||
LogLevel.Information, | ||
new EventId(1231142, nameof(GetResourceCalled)), | ||
"Getting MongoDB resource for {Tenant}"); | ||
|
||
static readonly Action<ILogger, Guid, Exception> _failedToGetResource = LoggerMessage | ||
.Define<Guid>( | ||
LogLevel.Information, | ||
new EventId(1231143, nameof(FailedToGetResource)), | ||
"Failed to get MongoDB resource for {Tenant}"); | ||
|
||
internal static void GetResourceCalled(this ILogger logger, TenantId tenantId) | ||
=> _getResourceCalled(logger, tenantId, null); | ||
|
||
internal static void FailedToGetResource(this ILogger logger, TenantId tenantId, Exception ex) | ||
=> _failedToGetResource(logger, tenantId, ex); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) Dolittle. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Dolittle.Runtime.Resources.MongoDB | ||
{ | ||
/// <summary> | ||
/// Represents the resource configuration for a MongoDB resource. | ||
/// </summary> | ||
public class ResourceConfiguration | ||
{ | ||
/// <summary> | ||
/// Gets or sets the MongoDB host. | ||
/// </summary> | ||
public string Host { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the database name. | ||
/// </summary> | ||
public string Database { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the value indicating whether or not to use SSL. | ||
/// </summary> | ||
public bool UseSSL { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) Dolittle. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using Dolittle.Runtime.DependencyInversion; | ||
using Dolittle.Runtime.Execution; | ||
using Dolittle.Runtime.Protobuf; | ||
using Dolittle.Runtime.Resources.Contracts; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Dolittle.Runtime.Resources.MongoDB | ||
{ | ||
/// <summary> | ||
/// Represents the implementation of <see cref="ICanGetResourceForTenant"/>. | ||
/// </summary> | ||
public class ResourceForTenantGetter : ICanGetResourceForTenant | ||
{ | ||
readonly FactoryFor<IKnowTheConnectionString> _getConnectionString; | ||
readonly IExecutionContextManager _executionContextManager; | ||
readonly ILogger _logger; | ||
|
||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ResourceForTenantGetter"/> class. | ||
/// </summary> | ||
/// <param name="getConnectionString">The <see cref="FactoryFor{T}"/> of type <see cref="IKnowTheConnectionString"/> to use to get connection strings after setting the execution context.</param> | ||
/// <param name="executionContextManager">The <see cref="IExecutionContextManager"/> to use to set the execution context.</param> | ||
/// <param name="logger">The <see cref="ILogger"/> to use for logging.</param> | ||
public ResourceForTenantGetter(FactoryFor<IKnowTheConnectionString> getConnectionString, IExecutionContextManager executionContextManager, ILogger logger) | ||
{ | ||
_getConnectionString = getConnectionString; | ||
_executionContextManager = executionContextManager; | ||
_logger = logger; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public GetMongoDBResponse GetResource(ExecutionContext executionContext) | ||
{ | ||
try | ||
{ | ||
_logger.GetResourceCalled(executionContext.Tenant); | ||
_executionContextManager.CurrentFor(executionContext); | ||
|
||
var mongoUrl = _getConnectionString().ConnectionString; | ||
return new GetMongoDBResponse | ||
{ | ||
ConnectionString = mongoUrl.ToString(), | ||
}; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.FailedToGetResource(executionContext.Tenant, ex); | ||
return new GetMongoDBResponse {Failure = new Failure(ex.Message) }; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Dolittle. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Dolittle.Runtime.ResourceTypes; | ||
|
||
namespace Dolittle.Runtime.Resources.MongoDB | ||
{ | ||
/// <summary> | ||
/// Represents a <see cref="IAmAResourceType">resource type</see> for the MongoDB resource. | ||
/// </summary> | ||
/// <remarks>The name is currently readModels to support for legacy cases.</remarks> | ||
public class ResourceType : IAmAResourceType | ||
{ | ||
/// <summary> | ||
/// The <see cref="ResourceTypes.ResourceType"/> name. | ||
/// </summary> | ||
/// <remarks> | ||
/// Although this resource type is a MongoDB specific type that cannot be swapped out with another implementation | ||
/// like the historical "Read Models" storage could, we're keeping the implementation for the configuration the | ||
/// same to make the Runtime compatible with the previous configuration files style. | ||
/// </remarks> | ||
public static ResourceTypes.ResourceType ResourceTypeName => "readModels"; | ||
|
||
/// <inheritdoc/> | ||
public ResourceTypes.ResourceType Name => ResourceTypeName; | ||
|
||
/// <inheritdoc/> | ||
public IEnumerable<Type> Services { get; } = new[] { typeof(IKnowTheConnectionString) }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) Dolittle. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Dolittle.Runtime.ResourceTypes; | ||
|
||
namespace Dolittle.Runtime.Resources.MongoDB | ||
{ | ||
/// <inheritdoc/> | ||
public class ResourceTypeRepresentation : IRepresentAResourceType | ||
{ | ||
static readonly IDictionary<Type, Type> _bindings = new Dictionary<Type, Type> | ||
{ | ||
{ typeof(IKnowTheConnectionString), typeof(ConnectionStringFromResourceConfiguration) } | ||
}; | ||
|
||
/// <inheritdoc/> | ||
public ResourceTypes.ResourceType Type => ResourceType.ResourceTypeName; | ||
|
||
/// <inheritdoc/> | ||
public ResourceTypeImplementation ImplementationName => "MongoDB"; | ||
|
||
/// <inheritdoc/> | ||
public Type ConfigurationObjectType => typeof(ResourceConfiguration); | ||
|
||
/// <inheritdoc/> | ||
public IDictionary<Type, Type> Bindings => _bindings; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="../../default.props" /> | ||
|
||
<PropertyGroup> | ||
<RootNamespace>Dolittle.Runtime.Resources</RootNamespace> | ||
<AssemblyName>Dolittle.Runtime.Resources</AssemblyName> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="MongoDB.Driver" Version="$(MongoDBDriverVersion)" /> | ||
<PackageReference Include="Dolittle.Runtime.Contracts" Version="$(ContractsVersion)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../Tenancy/Tenancy.csproj" /> | ||
<ProjectReference Include="../Rudimentary/Rudimentary.csproj" /> | ||
<ProjectReference Include="../ResourceTypes/ResourceTypes.csproj" /> | ||
<ProjectReference Include="../ResourceTypes.Configuration/ResourceTypes.Configuration.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) Dolittle. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Threading.Tasks; | ||
using Dolittle.Runtime.Protobuf; | ||
using Dolittle.Runtime.Resources.Contracts; | ||
using Grpc.Core; | ||
using static Dolittle.Runtime.Resources.Contracts.Resources; | ||
|
||
namespace Dolittle.Runtime.Resources | ||
{ | ||
/// <summary> | ||
/// Represents an implementation of <see cref="ResourcesBase"/>. | ||
/// </summary> | ||
public class ResourcesService : ResourcesBase | ||
{ | ||
readonly MongoDB.ICanGetResourceForTenant _mongodb; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ResourcesService"/> class. | ||
/// </summary> | ||
public ResourcesService(MongoDB.ICanGetResourceForTenant mongodbService) | ||
{ | ||
_mongodb = mongodbService; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override Task<GetMongoDBResponse> GetMongoDB(GetRequest request, ServerCallContext context) | ||
=> Task.FromResult(_mongodb.GetResource(request.CallContext.ExecutionContext.ToExecutionContext())); | ||
} | ||
} |
Oops, something went wrong.