Skip to content

Commit

Permalink
Add DynamoDb integration
Browse files Browse the repository at this point in the history
  • Loading branch information
engineering87 committed Nov 17, 2024
1 parent 44d1b9f commit 2e55f8d
Show file tree
Hide file tree
Showing 20 changed files with 599 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/SharpConnector.Api/SharpConnector.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<Content Remove="appsettings.couchbase.json" />
<Content Remove="appsettings.dynamodb.json" />
<Content Remove="appsettings.litedb.json" />
<Content Remove="appsettings.memcached.json" />
<Content Remove="appsettings.mongodb.json" />
Expand All @@ -17,6 +18,9 @@
</ItemGroup>

<ItemGroup>
<None Include="appsettings.dynamodb.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.couchbase.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
3 changes: 2 additions & 1 deletion src/SharpConnector.Api/appsettings.couchbase.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
"SearchTimeout": 5000,
"ManagementTimeout": 10000
}
}
}
}
11 changes: 11 additions & 0 deletions src/SharpConnector.Api/appsettings.dynamodb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"ConnectorConfig": {
"Instance": "DynamoDb",
"AccessKey": "your-access-key-here",
"SecretKey": "your-secret-key-here",
"Region": "us-west-2",
"ServiceUrl": "https://dynamodb.us-west-2.amazonaws.com",
"UseHttp": false,
"TableName": "MyTableName"
}
}
20 changes: 7 additions & 13 deletions src/SharpConnector.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
{
"ConnectorConfig": {
"Instance": "Couchbase",
"ConnectionString": "couchbase://localhost",
"Username": "username",
"Password": "password",
"BucketName": "example_bucket",
"Options": {
"EnableTls": false,
"KvTimeout": 2500,
"QueryTimeout": 7500,
"AnalyticsTimeout": 10000,
"SearchTimeout": 5000,
"ManagementTimeout": 10000
}
"Instance": "DynamoDb",
"AccessKey": "your-access-key-here",
"SecretKey": "your-secret-key-here",
"Region": "us-west-2",
"ServiceUrl": "https://dynamodb.us-west-2.amazonaws.com",
"UseHttp": false,
"TableName": "MyTableName"
},
"Logging": {
"LogLevel": {
Expand Down
6 changes: 6 additions & 0 deletions src/SharpConnector/Configuration/CouchbaseConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public class CouchbaseConfig : IConnectorConfig
public int DatabaseNumber { get; private set; }
public string DatabaseName { get; private set; }
public string CollectionName { get; private set; }
public string AccessKey { get; private set; }
public string SecretKey { get; private set; }
public string Region { get; private set; }
public string ServiceUrl { get; private set; }
public bool UseHttp { get; private set; }
public string TableName { get; private set; }

public CouchbaseConfig(IConfiguration section)
{
Expand Down
53 changes: 53 additions & 0 deletions src/SharpConnector/Configuration/DynamoDbConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// (c) 2024 Francesco Del Re <[email protected]>
// This code is licensed under MIT license (see LICENSE.txt for details)
using Microsoft.Extensions.Configuration;
using SharpConnector.Enums;
using SharpConnector.Interfaces;
using System.Linq;

namespace SharpConnector.Configuration
{
public class DynamoDbConfig : IConnectorConfig
{
public string AccessKey { get; }
public string SecretKey { get; }
public string Region { get; }
public string ServiceUrl { get; }
public bool UseHttp { get; }
public string TableName { get; }

// Not used
public string ConnectionString { get; private set; }
public int DatabaseNumber { get; private set; }
public string DatabaseName { get; private set; }
public string CollectionName { get; private set; }
public string Username { get; private set; }
public string Password { get; private set; }
public string BucketName { get; private set; }

public DynamoDbConfig(IConfiguration section)
{
var sectionChildren = section.GetChildren();
var configurationSections = sectionChildren.ToList();

var sectionChildrenAccessKey = configurationSections.FirstOrDefault(s => s.Key.ToLower() == AppConfigParameterEnums.accesskey.ToString());
AccessKey = sectionChildrenAccessKey?.Value;

var sectionChildrenSecretKey = configurationSections.FirstOrDefault(s => s.Key.ToLower() == AppConfigParameterEnums.secretkey.ToString());
SecretKey = sectionChildrenSecretKey?.Value;

var sectionChildrenRegion = configurationSections.FirstOrDefault(s => s.Key.ToLower() == AppConfigParameterEnums.region.ToString());
Region = sectionChildrenRegion?.Value;

var sectionChildrenServiceUrl = configurationSections.FirstOrDefault(s => s.Key.ToLower() == AppConfigParameterEnums.serviceurl.ToString());
ServiceUrl = sectionChildrenServiceUrl?.Value;

var sectionChildrenUseHttp = configurationSections.FirstOrDefault(s => s.Key.ToLower() == AppConfigParameterEnums.usehttp.ToString());
bool.TryParse(sectionChildrenUseHttp?.Value, out var useHttp);
UseHttp = useHttp;

var sectionChildrenTableName = configurationSections.FirstOrDefault(s => s.Key.ToLower() == AppConfigParameterEnums.tablename.ToString());
TableName = sectionChildrenTableName?.Value;
}
}
}
6 changes: 6 additions & 0 deletions src/SharpConnector/Configuration/LiteDbConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public class LiteDbConfig : IConnectorConfig
public string Username { get; private set; }
public string Password { get; private set; }
public string BucketName { get; private set; }
public string AccessKey { get; private set; }
public string SecretKey { get; private set; }
public string Region { get; private set; }
public string ServiceUrl { get; private set; }
public bool UseHttp { get; private set; }
public string TableName { get; private set; }

public LiteDbConfig(IConfiguration section)
{
Expand Down
6 changes: 6 additions & 0 deletions src/SharpConnector/Configuration/MemcachedConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public class MemcachedConfig : IConnectorConfig
public string Username { get; private set; }
public string Password { get; private set; }
public string BucketName { get; private set; }
public string AccessKey { get; private set; }
public string SecretKey { get; private set; }
public string Region { get; private set; }
public string ServiceUrl { get; private set; }
public bool UseHttp { get; private set; }
public string TableName { get; private set; }

public MemcachedConfig(IConfiguration section)
{
Expand Down
6 changes: 6 additions & 0 deletions src/SharpConnector/Configuration/MongoDbConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public class MongoDbConfig : IConnectorConfig
public string Username { get; private set; }
public string Password { get; private set; }
public string BucketName { get; private set; }
public string AccessKey { get; private set; }
public string SecretKey { get; private set; }
public string Region { get; private set; }
public string ServiceUrl { get; private set; }
public bool UseHttp { get; private set; }
public string TableName { get; private set; }

public MongoDbConfig(IConfiguration section)
{
Expand Down
6 changes: 6 additions & 0 deletions src/SharpConnector/Configuration/RavenDbConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public class RavenDbConfig : IConnectorConfig
public string Username { get; private set; }
public string Password { get; private set; }
public string BucketName { get; private set; }
public string AccessKey { get; private set; }
public string SecretKey { get; private set; }
public string Region { get; private set; }
public string ServiceUrl { get; private set; }
public bool UseHttp { get; private set; }
public string TableName { get; private set; }

public RavenDbConfig(IConfiguration section)
{
Expand Down
6 changes: 6 additions & 0 deletions src/SharpConnector/Configuration/RedisConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public class RedisConfig : IConnectorConfig
public string Username { get; private set; }
public string Password { get; private set; }
public string BucketName { get; private set; }
public string AccessKey { get; private set; }
public string SecretKey { get; private set; }
public string Region { get; private set; }
public string ServiceUrl { get; private set; }
public bool UseHttp { get; private set; }
public string TableName { get; private set; }

public RedisConfig(IConfiguration section)
{
Expand Down
40 changes: 40 additions & 0 deletions src/SharpConnector/Connectors/DynamoDB/DynamoDBAccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// (c) 2024 Francesco Del Re <[email protected]>
// This code is licensed under MIT license (see LICENSE.txt for details)
using Amazon.DynamoDBv2;
using Amazon.Runtime;
using SharpConnector.Configuration;
using System;

namespace SharpConnector.Connectors.DynamoDb
{
internal class DynamoDbAccess : IDisposable
{
private readonly Lazy<AmazonDynamoDBClient> _client;
public AmazonDynamoDBClient GetClient() => _client.Value;

/// <summary>
/// Create a new DynamoDbAccess instance.
/// </summary>
/// <param name="connectorConfig">The DynamoDB configuration.</param>
public DynamoDbAccess(DynamoDbConfig connectorConfig)
{
var awsCredentials = new BasicAWSCredentials(connectorConfig.AccessKey, connectorConfig.SecretKey);
var config = new AmazonDynamoDBConfig
{
RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName(connectorConfig.Region),
ServiceURL = connectorConfig.ServiceUrl,
UseHttp = connectorConfig.UseHttp
};

_client = new Lazy<AmazonDynamoDBClient>(() => new AmazonDynamoDBClient(awsCredentials, config));
}

public void Dispose()
{
if (_client.IsValueCreated)
{
_client.Value.Dispose();
}
}
}
}
Loading

0 comments on commit 2e55f8d

Please sign in to comment.