Skip to content

Commit 0995d15

Browse files
Begun implementing DynamoDB support
1 parent 375562d commit 0995d15

22 files changed

+683
-14
lines changed

GuiStack/ApplicationInfo.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public record class ThirdPartyLicense(string Name, string LicenseText);
5858
/// </summary>
5959
public static readonly string License = GetStringResource("LICENSE.txt");
6060

61+
/// <summary>
62+
/// The version number of the application.
63+
/// </summary>
64+
public static readonly Version Version = ProgramClass.Assembly.GetName().Version;
65+
6166
/// <summary>
6267
/// The collection of copyright licenses of third-party resources used by the application.
6368
/// </summary>

GuiStack/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using System.Reflection;
1212
using System.Resources;
1313

14-
[assembly: AssemblyTitle("GuiStack - A GUI for your AWS or LocalStack environment")]
14+
[assembly: AssemblyTitle("GuiStack - A GUI for your AWS or LocalStack development environment")]
1515
[assembly: AssemblyProduct("GuiStack")]
1616
[assembly: AssemblyCopyright("Copyright © Vincent Bengtsson & Contributors 2022-2024")]
1717
[assembly: AssemblyVersion("1.2.0.0")]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright © Vincent Bengtsson & Contributors 2022-2024
7+
* https://github.com/Visual-Vincent/GuiStack
8+
*/
9+
10+
using System;
11+
using Amazon;
12+
using Amazon.DynamoDBv2;
13+
using Amazon.Runtime;
14+
15+
namespace GuiStack.Authentication.AWS
16+
{
17+
public class DynamoDBAuthenticator : Authenticator<AWSCredentials, AmazonDynamoDBClient>
18+
{
19+
public override AmazonDynamoDBClient Authenticate(AWSCredentials credentials)
20+
{
21+
var config = new AmazonDynamoDBConfig() {
22+
AuthenticationRegion = AWSConfigs.AWSRegion,
23+
MaxErrorRetry = 1
24+
};
25+
26+
string endpointUrl = Environment.GetEnvironmentVariable("AWS_DYNAMODB_ENDPOINT_URL");
27+
28+
if(!string.IsNullOrWhiteSpace(endpointUrl))
29+
config.ServiceURL = endpointUrl;
30+
31+
return new AmazonDynamoDBClient(credentials, config);
32+
}
33+
34+
public override AWSCredentials GetCredentials()
35+
{
36+
return new BasicAWSCredentials(
37+
Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID"),
38+
Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY")
39+
);
40+
}
41+
}
42+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright © Vincent Bengtsson & Contributors 2022-2024
7+
* https://github.com/Visual-Vincent/GuiStack
8+
*/
9+
10+
using System;
11+
using System.Net;
12+
using System.Threading.Tasks;
13+
using Amazon.DynamoDBv2;
14+
using GuiStack.Extensions;
15+
using GuiStack.Models;
16+
using GuiStack.Repositories;
17+
using Microsoft.AspNetCore.Mvc;
18+
19+
namespace GuiStack.Controllers.DynamoDB
20+
{
21+
[ApiController]
22+
[Route("api/" + nameof(DynamoDB) + "/[controller]")]
23+
public class TablesController : Controller
24+
{
25+
private IDynamoDBRepository dynamodbRepository;
26+
27+
public TablesController(IDynamoDBRepository dynamodbRepository)
28+
{
29+
this.dynamodbRepository = dynamodbRepository;
30+
}
31+
32+
private ActionResult HandleException(Exception ex)
33+
{
34+
if(ex == null)
35+
throw new ArgumentNullException(nameof(ex));
36+
37+
if(ex is AmazonDynamoDBException dynamodbEx)
38+
{
39+
if(dynamodbEx.StatusCode == HttpStatusCode.NotFound)
40+
return StatusCode((int)dynamodbEx.StatusCode, new { error = dynamodbEx.Message });
41+
42+
Console.Error.WriteLine(dynamodbEx);
43+
return StatusCode((int)HttpStatusCode.InternalServerError, new { error = ex.Message });
44+
}
45+
46+
Console.Error.WriteLine(ex);
47+
return StatusCode((int)HttpStatusCode.InternalServerError);
48+
}
49+
50+
[HttpPost]
51+
[Consumes("application/json")]
52+
public async Task<ActionResult> CreateTable([FromBody] DynamoDBCreateTableModel model)
53+
{
54+
if(string.IsNullOrWhiteSpace(model.TableName) || model.PartitionKey == null || string.IsNullOrEmpty(model.PartitionKey.Name))
55+
return StatusCode((int)HttpStatusCode.BadRequest);
56+
57+
try
58+
{
59+
await dynamodbRepository.CreateTableAsync(model);
60+
return Ok();
61+
}
62+
catch(Exception ex)
63+
{
64+
return HandleException(ex);
65+
}
66+
}
67+
68+
[HttpDelete("{tableName}")]
69+
public async Task<ActionResult> DeleteTable([FromRoute] string tableName)
70+
{
71+
if(string.IsNullOrWhiteSpace(tableName))
72+
return StatusCode((int)HttpStatusCode.BadRequest);
73+
74+
tableName = tableName.DecodeRouteParameter();
75+
76+
try
77+
{
78+
await dynamodbRepository.DeleteTableAsync(tableName);
79+
return Ok();
80+
}
81+
catch(Exception ex)
82+
{
83+
return HandleException(ex);
84+
}
85+
}
86+
}
87+
}

GuiStack/Extensions/ModelExtensions.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* License, v. 2.0. If a copy of the MPL was not distributed with this
44
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
55
*
6-
* Copyright © Vincent Bengtsson & Contributors 2022
6+
* Copyright © Vincent Bengtsson & Contributors 2022-2024
77
* https://github.com/Visual-Vincent/GuiStack
88
*/
99

@@ -13,10 +13,18 @@
1313
using System.Linq;
1414
using GuiStack.Models;
1515

16+
using ScalarAttributeType = Amazon.DynamoDBv2.ScalarAttributeType;
17+
1618
namespace GuiStack.Extensions
1719
{
1820
public static class ModelExtensions
1921
{
22+
private static readonly Dictionary<DynamoDBAttributeType, ScalarAttributeType> DynamoDBScalarAttributeMap = new Dictionary<DynamoDBAttributeType, ScalarAttributeType>() {
23+
{ DynamoDBAttributeType.String, ScalarAttributeType.S },
24+
{ DynamoDBAttributeType.Number, ScalarAttributeType.N },
25+
{ DynamoDBAttributeType.Binary, ScalarAttributeType.B }
26+
};
27+
2028
/// <summary>
2129
/// Converts the <see cref="IEnumerable"/>&lt;<see cref="S3Object"/>&gt; to <see cref="IEnumerable"/>&lt;<see cref="S3ObjectModel"/>&gt;.
2230
/// </summary>
@@ -29,5 +37,14 @@ public static IEnumerable<S3ObjectModel> ToObjectModel(this IEnumerable<S3Object
2937
Object = obj
3038
});
3139
}
40+
41+
/// <summary>
42+
/// Converts the <see cref="DynamoDBAttributeType"/> to <see cref="ScalarAttributeType"/>.
43+
/// </summary>
44+
/// <param name="attributeType">The <see cref="DynamoDBAttributeType"/> to convert.</param>
45+
public static ScalarAttributeType ToScalarAttributeType(this DynamoDBAttributeType attributeType)
46+
{
47+
return DynamoDBScalarAttributeMap.GetValueOrDefault(attributeType);
48+
}
3249
}
3350
}

GuiStack/GuiStack.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="AWSSDK.Core" Version="3.7.106.33" />
10+
<PackageReference Include="AWSSDK.Core" Version="3.7.108.3" />
11+
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.105.7" />
1112
<PackageReference Include="AWSSDK.S3" Version="3.7.104.12" />
1213
<PackageReference Include="AWSSDK.SimpleNotificationService" Version="3.7.101.62" />
1314
<PackageReference Include="AWSSDK.SQS" Version="3.7.102.1" />
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright © Vincent Bengtsson & Contributors 2022-2024
7+
* https://github.com/Visual-Vincent/GuiStack
8+
*/
9+
10+
using System;
11+
12+
namespace GuiStack.Models
13+
{
14+
public class DynamoDBAttribute
15+
{
16+
public string Name { get; set; }
17+
public DynamoDBAttributeType Type { get; set; }
18+
}
19+
20+
public enum DynamoDBAttributeType
21+
{
22+
String = 0,
23+
Number,
24+
Binary
25+
}
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright © Vincent Bengtsson & Contributors 2022-2024
7+
* https://github.com/Visual-Vincent/GuiStack
8+
*/
9+
10+
using System;
11+
12+
namespace GuiStack.Models
13+
{
14+
public class DynamoDBCreateTableModel
15+
{
16+
public string TableName { get; set; }
17+
public DynamoDBAttribute PartitionKey { get; set; }
18+
public DynamoDBAttribute SortKey { get; set; }
19+
}
20+
}

GuiStack/Pages/About.cshtml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
<h1 class="text-center" style="font-size: 30pt; margin-bottom: 0px">GuiStack</h1>
1818

19+
<p class="text-center" style="margin-top: 4px">
20+
Version @ApplicationInfo.Version
21+
</p>
22+
1923
<p class="text-center" style="font-size: 18pt; margin: 24px 0px">
2024
@AboutModel.Description
2125
</p>

0 commit comments

Comments
 (0)