Skip to content

Commit

Permalink
#1793 Add AmazonIoTClient and AmazonIotDataClient configurations
Browse files Browse the repository at this point in the history
- Add "CloudProvider" in configuration
- Add AmazonIoTClient and AmazonIotDataClient configurations when CloudProvider is AWS
  • Loading branch information
delager committed Mar 23, 2023
1 parent bf69a5d commit 2ead108
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/AzureIoTHub.Portal.Domain/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public abstract class ConfigHandler
public abstract string MySQLConnectionString { get; }

public abstract string DbProvider { get; }
public abstract string CloudProvider { get; }
public abstract string AWSAccess { get; }
public abstract string AWSAccessSecret { get; }
public abstract string AWSRegion { get; }
Expand Down
13 changes: 13 additions & 0 deletions src/AzureIoTHub.Portal.Domain/Shared/Constants/CloudProviders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Domain.Shared.Constants
{

public static class CloudProviders
{
public const string Azure = "Azure";

public const string AWS = "AWS";
}
}
2 changes: 2 additions & 0 deletions src/AzureIoTHub.Portal.Infrastructure/ConfigHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ internal abstract class ConfigHandlerBase : ConfigHandler
internal const string IdeasAuthenticationHeaderKey = "Ideas:Authentication:Header";
internal const string IdeasAuthenticationTokenKey = "Ideas:Authentication:Token";

internal const string CloudProviderKey = "CloudProvider";

internal const string AWSAccessKey = "AWS:Access";
internal const string AWSAccessSecretKey = "AWS:AccessSecret";
internal const string AWSRegionKey = "AWS:Region";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ internal DevelopmentConfigHandler(IConfiguration config)

public override string DbProvider => this.config.GetValue(DbProviderKey, DbProviders.PostgreSQL);

public override string CloudProvider => this.config[CloudProviderKey];

public override string AWSAccess => this.config[AWSAccessKey];
public override string AWSAccessSecret => this.config[AWSAccessSecretKey];
public override string AWSRegion => this.config[AWSRegionKey];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ internal ProductionConfigHandler(IConfiguration config)
public override string IdeasAuthenticationHeader => this.config.GetValue(IdeasAuthenticationHeaderKey, "Ocp-Apim-Subscription-Key");
public override string IdeasAuthenticationToken => this.config.GetValue(IdeasAuthenticationTokenKey, string.Empty);

public override string CloudProvider => this.config[CloudProviderKey];

public override string AWSAccess => this.config[AWSAccessKey];
public override string AWSAccessSecret => this.config[AWSAccessSecretKey];
public override string AWSRegion => this.config[AWSRegionKey];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="AWSSDK.IoT" Version="3.7.105.14" />
<PackageReference Include="AWSSDK.IotData" Version="3.7.101.74" />
<PackageReference Include="Azure.Data.Tables" Version="12.8.0" />
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.8.1" />
<PackageReference Include="Azure.Messaging.EventHubs.Processor" Version="5.8.1" />
Expand Down
19 changes: 19 additions & 0 deletions src/AzureIoTHub.Portal.Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace AzureIoTHub.Portal.Server
using System;
using System.IO;
using System.Threading.Tasks;
using Amazon;
using Amazon.IoT;
using Amazon.IotData;
using AzureIoTHub.Portal.Application.Managers;
using AzureIoTHub.Portal.Application.Services;
using AzureIoTHub.Portal.Application.Startup;
Expand Down Expand Up @@ -77,6 +80,22 @@ public void ConfigureServices(IServiceCollection services)
_ = services.AddSingleton(new PortalMetric());
_ = services.AddSingleton(new LoRaGatewayIDList());

if (configuration.CloudProvider.Equals(CloudProviders.AWS))
{
var AWSIoTClient = new AmazonIoTClient(configuration.AWSAccess, configuration.AWSAccessSecret, RegionEndpoint.GetBySystemName(configuration.AWSRegion));
var endPoint = AWSIoTClient.DescribeEndpointAsync(new Amazon.IoT.Model.DescribeEndpointRequest
{
EndpointType = "iot:Data-ATS"
}).Result;
var AWSIoTDataClient = new AmazonIotDataClient(configuration.AWSAccess, configuration.AWSAccessSecret, new AmazonIotDataConfig
{
ServiceURL = $"https://{endPoint.EndpointAddress}"
});

_ = services.AddSingleton(AWSIoTClient);
_ = services.AddSingleton(AWSIoTDataClient);
}

_ = services.AddRazorPages();

_ = services.AddTransient<IExportManager, ExportManager>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private DevelopmentConfigHandler CreateDevelopmentConfigHandler()
[TestCase(ConfigHandlerBase.AWSAccessKey, nameof(ConfigHandlerBase.AWSAccess))]
[TestCase(ConfigHandlerBase.AWSAccessSecretKey, nameof(ConfigHandlerBase.AWSAccessSecret))]
[TestCase(ConfigHandlerBase.AWSRegionKey, nameof(ConfigHandlerBase.AWSRegion))]
[TestCase(ConfigHandlerBase.CloudProviderKey, nameof(ConfigHandlerBase.CloudProvider))]
public void SettingsShouldGetValueFromAppSettings(string configKey, string configPropertyName)
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public void SecretsShouldGetValueFromConnectionStrings(string configKey, string
[TestCase(ConfigHandlerBase.AWSAccessKey, nameof(ConfigHandlerBase.AWSAccess))]
[TestCase(ConfigHandlerBase.AWSAccessSecretKey, nameof(ConfigHandlerBase.AWSAccessSecret))]
[TestCase(ConfigHandlerBase.AWSRegionKey, nameof(ConfigHandlerBase.AWSRegion))]
[TestCase(ConfigHandlerBase.CloudProviderKey, nameof(ConfigHandlerBase.CloudProvider))]
public void SettingsShouldGetValueFromAppSettings(string configKey, string configPropertyName)
{
// Arrange
Expand Down

0 comments on commit 2ead108

Please sign in to comment.