Skip to content

Commit

Permalink
Merge pull request 'feature/get-region' (#169) from feature/get-regio…
Browse files Browse the repository at this point in the history
…n into develop
  • Loading branch information
pavelbannov committed Mar 3, 2025
2 parents fac7018 + dd61446 commit c808901
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
8 changes: 7 additions & 1 deletion web/ASC.Web.Api/Api/PortalController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ public async Task<TenantDto> Get()
return new TenantDto { TenantId = tenant.Id };
}

return mapper.Map<TenantDto>(tenant);
var dto = mapper.Map<TenantDto>(tenant);

if (!coreBaseSettings.Standalone && apiSystemHelper.ApiCacheEnable)
{
dto.Region = await apiSystemHelper.GetTenantRegionAsync(dto.Name);
}
return dto;
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions web/ASC.Web.Api/ApiModels/ResponseDto/TenantDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ public class TenantDto : IMapFrom<Tenant>
/// </summary>
public DateTime VersionChanged { get; set; }

/// <summary>
/// AWS region
/// </summary>
public string Region { get; set; }

public void Mapping(Profile profile)
{
profile.CreateMap<Tenant, TenantDto>()
Expand Down
33 changes: 33 additions & 0 deletions web/ASC.Web.Core/Helpers/ApiSystemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public bool ApiCacheEnable
private const string TenantRegionKey = "tenant_region";
private const string TenantDomainKey = "tenant_domain";
private readonly string _regionTableName;
private readonly Dictionary<string, string> _regions = new Dictionary<string, string> { { "us-west-2", "US" }, { "eu-central-1", "DEU" } };

public ApiSystemHelper(
IConfiguration configuration,
Expand Down Expand Up @@ -173,6 +174,38 @@ public async Task RemoveTenantFromCacheAsync(string tenantDomain)
await awsDynamoDbClient.DeleteItemAsync(request);
}

public async Task<string> GetTenantRegionAsync(string portalName)
{
using var awsDynamoDbClient = GetDynamoDBClient();

portalName = portalName.Trim().ToLowerInvariant();

var tenantDomain = $"{portalName}.{_coreBaseSettings.Basedomain}";

var getItemRequest = new GetItemRequest
{
TableName = _regionTableName,
Key = new Dictionary<string, AttributeValue>
{
{ TenantDomainKey, new AttributeValue { S = tenantDomain } }
},
ProjectionExpression = TenantRegionKey,
ConsistentRead = true
};

var getItemResponse = await awsDynamoDbClient.GetItemAsync(getItemRequest);

if (getItemResponse.Item.TryGetValue(TenantRegionKey, out var region))
{
if(_regions.TryGetValue(region.S, out var value))
{
return value;
}
}

return null;
}

public async Task<IEnumerable<string>> FindTenantsInCacheAsync(string portalName)
{
using var awsDynamoDbClient = GetDynamoDBClient();
Expand Down

0 comments on commit c808901

Please sign in to comment.