-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add regional datastore service (#2180)
* add regional database service * update unit tests * update s3 bucket name for prod * update unit tests * reference to prod s3 bucket * remove unused constants * call http client if s3 client calls didn't succeed * add stage to s3 bucket name
- Loading branch information
Showing
11 changed files
with
146 additions
and
28 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
11 changes: 11 additions & 0 deletions
11
src/PortingAssistant.Compatibility.Common/Interface/IRegionalDatastoreService.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,11 @@ | ||
namespace PortingAssistant.Compatibility.Common.Interface | ||
{ | ||
public interface IRegionalDatastoreService | ||
{ | ||
public Task<Stream?> DownloadRegionalS3FileAsync(string fileToDownload, bool isRegionalCall = false); | ||
|
||
public Task<HashSet<string>> ListRegionalNamespacesObjectAsync(bool isRegionalCall = false); | ||
|
||
public Task<Stream> DownloadGitHubFileAsync(string fileToDownload); | ||
} | ||
} |
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
90 changes: 90 additions & 0 deletions
90
src/PortingAssistant.Compatibility.Common/Utils/RegionalDatastoreService.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,90 @@ | ||
using Amazon; | ||
using Amazon.S3; | ||
using PortingAssistant.Compatibility.Common.Interface; | ||
using Amazon.S3.Model; | ||
using Microsoft.Extensions.Logging; | ||
using System.Net; | ||
|
||
namespace PortingAssistant.Compatibility.Common.Utils | ||
{ | ||
public class RegionalDatastoreService : IRegionalDatastoreService | ||
{ | ||
private readonly IHttpService _httpService; | ||
private readonly AmazonS3Client _s3Client; | ||
private readonly bool _isLambdaEnvSetup; | ||
private readonly string _regionaS3BucketName; | ||
private readonly ILogger<RegionalDatastoreService> _logger; | ||
|
||
public RegionalDatastoreService( | ||
IHttpService httpService, | ||
ILogger<RegionalDatastoreService> logger | ||
) | ||
{ | ||
_httpService = httpService; | ||
_logger = logger; | ||
string region = Environment.GetEnvironmentVariable("AWS_REGION"); | ||
string stage = Environment.GetEnvironmentVariable("stage"); | ||
|
||
if (!string.IsNullOrEmpty(region) && (stage == Constants.BetaStageName || stage == Constants.GammaStageName || stage == Constants.ProdStageName)) | ||
{ | ||
_isLambdaEnvSetup = true; | ||
_regionaS3BucketName = stage == Constants.ProdStageName ? | ||
$"portingassistant-datastore-{region}" : $"portingassistant-datastore-{stage}-{region}"; | ||
_logger.LogInformation($"Read stage, region from environment: {stage}, {region}, set S3 bucket name: {_regionaS3BucketName}"); | ||
_s3Client = new AmazonS3Client(RegionEndpoint.GetBySystemName(region)); | ||
} | ||
} | ||
|
||
public async Task<Stream> DownloadGitHubFileAsync(string fileToDownload) | ||
{ | ||
return await _httpService.DownloadGitHubFileAsync(fileToDownload); | ||
} | ||
|
||
public async Task<Stream?> DownloadRegionalS3FileAsync(string fileToDownload, bool isRegionalCall = false) | ||
{ | ||
try | ||
{ | ||
_logger.LogInformation($"Downloading {fileToDownload} from regional S3 " + _regionaS3BucketName); | ||
if (isRegionalCall && _isLambdaEnvSetup) | ||
{ | ||
GetObjectRequest request = new GetObjectRequest | ||
{ | ||
BucketName = _regionaS3BucketName, | ||
Key = fileToDownload | ||
}; | ||
using (GetObjectResponse response = await _s3Client.GetObjectAsync(request)) | ||
{ | ||
if (response.HttpStatusCode == HttpStatusCode.OK) | ||
{ | ||
_logger.LogInformation($"Downloaded {fileToDownload} from " + _regionaS3BucketName); | ||
return response.ResponseStream; | ||
} | ||
else | ||
{ | ||
_logger.LogWarning($"Issues during downloading through S3 client from " + _regionaS3BucketName); | ||
_logger.LogInformation($"Downloading file through Http client..."); | ||
return await _httpService.DownloadS3FileAsync(fileToDownload); | ||
} | ||
} | ||
|
||
} | ||
else | ||
{ | ||
return await _httpService.DownloadS3FileAsync(fileToDownload); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError($"fail to download {fileToDownload}. " + ex.Message); | ||
return null; | ||
} | ||
|
||
} | ||
|
||
// TODO: This method could be deprecated since sdk namespaces won't change after each feature release | ||
public Task<HashSet<string>> ListRegionalNamespacesObjectAsync(bool isRegionalCall = false) | ||
{ | ||
return _httpService.ListNamespacesObjectAsync(); | ||
} | ||
} | ||
} |
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
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
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