diff --git a/Acl/src/EnvironmentOptions.cs b/Acl/src/EnvironmentOptions.cs index cda9dba..1dc6fec 100644 --- a/Acl/src/EnvironmentOptions.cs +++ b/Acl/src/EnvironmentOptions.cs @@ -10,5 +10,5 @@ public class EnvironmentOptions public string Environment { get; set; } = EnvironmentType.Test; - public string Cloud { get; set; } = CloudType.Public; + public string Cloud { get; set; } = CloudType.AzureCloud; } diff --git a/Acl/src/EnvironmentOptionsExts/CloudType.cs b/Acl/src/EnvironmentOptionsExts/CloudType.cs index 59b1cc6..f051f8f 100644 --- a/Acl/src/EnvironmentOptionsExts/CloudType.cs +++ b/Acl/src/EnvironmentOptionsExts/CloudType.cs @@ -8,10 +8,20 @@ public static class CloudType /// /// Represents the public cloud. /// - public const string Public = "public"; + public const string AzureCloud = nameof(AzureCloud); /// - /// Represents the Fairfax (US Government) cloud. + /// Represents the US Government cloud. /// - public const string Fairfax = "fairfax"; + public const string AzureUSGovernment = nameof(AzureUSGovernment); + + /// + /// Represents the Chine cloud. + /// + public const string AzureChinaCloud = nameof(AzureChinaCloud); + + /// + /// Represents the German cloud. + /// + public const string AzureGermanCloud = nameof(AzureGermanCloud); } diff --git a/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsCosmosDbExt.cs b/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsCosmosDbExt.cs index 5f13edd..d76e740 100644 --- a/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsCosmosDbExt.cs +++ b/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsCosmosDbExt.cs @@ -6,9 +6,13 @@ public static class EnvironmentOptionsCosmosDbExt public static string GetCosmosDbNameSharedUrl(this EnvironmentOptions settings) { - if (settings.Cloud == CloudType.Public) - return $"https://{GetCosmosDbNameShared(settings)}.documents.azure.com:443/"; - - throw new NotSupportedException($"Cloud type '{settings.Cloud}' is not supported."); + return settings.Cloud switch + { + CloudType.AzureCloud => $"https://{GetCosmosDbNameShared(settings)}.documents.azure.com:443/", + CloudType.AzureUSGovernment => $"https://{GetCosmosDbNameShared(settings)}.documents.azure.us:443/", + CloudType.AzureChinaCloud => $"https://{GetCosmosDbNameShared(settings)}.documents.azure.cn:443/", + CloudType.AzureGermanCloud => $"https://{GetCosmosDbNameShared(settings)}.documents.azure.com:443/", + _ => throw new NotSupportedException($"Cloud type '{settings.Cloud}' is not supported.") + }; } } diff --git a/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsExt.cs b/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsExt.cs index 80d7ecb..3210ff5 100644 --- a/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsExt.cs +++ b/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsExt.cs @@ -2,6 +2,6 @@ public static class EnvironmentOptionsExt { - public static string GetResourceName(this EnvironmentOptions settings, string name) => $"{settings.Environment}-{settings.RegionShortName}-{name}-{settings.ServiceName}".ToLowerInvariant(); - public static string GetResourceNameShared(this EnvironmentOptions settings, string name) => $"{settings.Environment}-{name}-{settings.ServiceName}".ToLowerInvariant(); + public static string GetResourceName(this EnvironmentOptions settings, string resourceType) => $"{settings.Environment}-{settings.RegionShortName}-{resourceType}-{settings.ServiceName}".ToLowerInvariant(); + public static string GetResourceNameShared(this EnvironmentOptions settings, string resourceType) => $"{settings.Environment}-{resourceType}-{settings.ServiceName}".ToLowerInvariant(); } diff --git a/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsKeyVaultExt.cs b/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsKeyVaultExt.cs index 48470c8..e6f54a7 100644 --- a/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsKeyVaultExt.cs +++ b/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsKeyVaultExt.cs @@ -9,14 +9,17 @@ public static string GetKeyVaultUri(this EnvironmentOptions settings) ArgumentNullException.ThrowIfNull(settings); var keyVaultName = settings.GetKeyVaultName(); - switch (settings.Cloud) + + // Map cloud to the known Key Vault endpoint suffix + var dnsSuffix = settings.Cloud switch { - case CloudType.Public: - return $"https://{keyVaultName}.vault.azure.net/"; - case CloudType.Fairfax: - return $"https://{keyVaultName}.vault.usgovcloudapi.net/"; - default: - throw new NotSupportedException($"Cloud type '{settings.Cloud}' is not supported for Key Vault URI generation."); - } + CloudType.AzureCloud => "vault.azure.net", + CloudType.AzureUSGovernment => "vault.usgovcloudapi.net", + CloudType.AzureChinaCloud => "vault.azure.cn", + CloudType.AzureGermanCloud => "vault.microsoftazure.de", + _ => throw new NotSupportedException($"Cloud type '{settings.Cloud}' is not supported for Key Vault URI generation.") + }; + + return $"https://{keyVaultName}.{dnsSuffix}/"; } } diff --git a/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsServiceBusExt.cs b/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsServiceBusExt.cs index f8e6fd9..c91df2d 100644 --- a/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsServiceBusExt.cs +++ b/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsServiceBusExt.cs @@ -4,13 +4,13 @@ public static class EnvironmentOptionsServiceBusExt { public static string GetServiceBusName(this EnvironmentOptions settings) => settings.GetResourceNameShared("sbns"); - public static string GetServiceBusNamespace(this EnvironmentOptions settings) - { - if (settings.Cloud == CloudType.Public) - return $"{GetServiceBusName(settings)}.servicebus.windows.net"; - if (settings.Cloud == CloudType.Fairfax) - return $"{GetServiceBusName(settings)}.servicebus.usgovcloudapi.net"; - - throw new NotSupportedException($"Cloud type '{settings.Cloud}' is not supported."); - } + public static string GetServiceBusNamespace(this EnvironmentOptions settings) => + settings.Cloud switch + { + CloudType.AzureCloud => $"{settings.GetServiceBusName()}.servicebus.windows.net", + CloudType.AzureUSGovernment => $"{settings.GetServiceBusName()}.servicebus.usgovcloudapi.net", + CloudType.AzureChinaCloud => $"{settings.GetServiceBusName()}.servicebus.chinacloudapi.cn", + CloudType.AzureGermanCloud => $"{settings.GetServiceBusName()}.servicebus.cloudapi.de", + _ => throw new NotSupportedException($"Cloud type '{settings.Cloud}' is not supported.") + }; } diff --git a/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsStorageExt.cs b/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsStorageExt.cs index a9fa1f7..087acc9 100644 --- a/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsStorageExt.cs +++ b/Acl/src/EnvironmentOptionsExts/EnvironmentOptionsStorageExt.cs @@ -36,8 +36,10 @@ public static string GetBlobStorageSharedUrl(this EnvironmentOptions settings) return settings.Cloud switch { - CloudType.Public => $"https://{GetStorageNameShared(settings)}.blob.core.windows.net", - CloudType.Fairfax => $"https://{GetStorageNameShared(settings)}.blob.core.usgovcloudapi.net", + CloudType.AzureCloud => $"https://{GetStorageNameShared(settings)}.blob.core.windows.net", + CloudType.AzureUSGovernment => $"https://{GetStorageNameShared(settings)}.blob.core.usgovcloudapi.net", + CloudType.AzureChinaCloud => $"https://{GetStorageNameShared(settings)}.blob.core.chinacloud.cn", + CloudType.AzureGermanCloud => $"https://{GetStorageNameShared(settings)}.blob.core.cloudapi.de", _ => throw new NotSupportedException( $"Cloud type '{settings.Cloud}' is not supported for Blob Storage URL generation.") }; diff --git a/Acl/tests/CosmosDbTests.cs b/Acl/tests/CosmosDbTests.cs new file mode 100644 index 0000000..1f3dadd --- /dev/null +++ b/Acl/tests/CosmosDbTests.cs @@ -0,0 +1,51 @@ +namespace AntiCorruptionLayer.Tests; +using BestWeatherForecast.AntiCorruptionLayer; + +public class CosmosDbTests +{ + [Theory] + [InlineData("local")] + [InlineData("test")] + public void Will_get_CosmosDb_URL(string env) + { + // Arrange + EnvironmentOptions environmentOptions = new() + { + Environment = env, + RegionShortName = "usw2", + ServiceName = "bwf" + }; + + var expected = $"{env}-cosno-bwf"; + + // Act + var actual = environmentOptions.GetCosmosDbNameShared(); + + // Assert + actual.Should().Be(expected); + } + + [Theory] + [InlineData(CloudType.AzureCloud, "https://ppe-cosno-bwf.documents.azure.com:443/")] + [InlineData(CloudType.AzureUSGovernment, "https://ppe-cosno-bwf.documents.azure.us:443/")] + [InlineData(CloudType.AzureChinaCloud, "https://ppe-cosno-bwf.documents.azure.cn:443/")] + [InlineData(CloudType.AzureGermanCloud, "https://ppe-cosno-bwf.documents.azure.com:443/")] + public void Will_get_url_for_Cloud(string cloudType, string expectedUrl) + { + // Arrange + EnvironmentOptions environmentOptions = new() + { + Environment = EnvironmentType.Ppe, + RegionShortName = "usw2", + ServiceName = "bwf", + Cloud = cloudType + }; + + // Act + var actualUrl = environmentOptions.GetCosmosDbNameSharedUrl(); + + // Assert + actualUrl.Should().Be(expectedUrl); + + } +} diff --git a/Acl/tests/KeyVaultTests.cs b/Acl/tests/KeyVaultTests.cs new file mode 100644 index 0000000..5414433 --- /dev/null +++ b/Acl/tests/KeyVaultTests.cs @@ -0,0 +1,50 @@ +namespace AntiCorruptionLayer.Tests; +using BestWeatherForecast.AntiCorruptionLayer; + +public class KeyVaultTests +{ + [Theory] + [InlineData("local")] + [InlineData("test")] + public void Will_get_KeyVault_name(string env) + { + // Arrange + EnvironmentOptions environmentOptions = new() + { + Environment = env, + RegionShortName = "usw2", + ServiceName = "bwf" + }; + + var expected = $"{env}-usw2-kv-bwf"; + + // Act + var actual = environmentOptions.GetKeyVaultName(); + + // Assert + actual.Should().Be(expected); + } + + [Theory] + [InlineData(CloudType.AzureCloud, "https://ppe-usw2-kv-bwf.vault.azure.net/")] + [InlineData(CloudType.AzureUSGovernment, "https://ppe-usw2-kv-bwf.vault.usgovcloudapi.net/")] + [InlineData(CloudType.AzureChinaCloud, "https://ppe-usw2-kv-bwf.vault.azure.cn/")] + [InlineData(CloudType.AzureGermanCloud, "https://ppe-usw2-kv-bwf.vault.microsoftazure.de/")] + public void Will_get_keyvault_uri_for_Cloud(string cloudType, string expectedUri) + { + // Arrange + EnvironmentOptions environmentOptions = new() + { + Environment = EnvironmentType.Ppe, + RegionShortName = "usw2", + ServiceName = "bwf", + Cloud = cloudType + }; + + // Act + var actualUri = environmentOptions.GetKeyVaultUri(); + + // Assert + actualUri.Should().Be(expectedUri); + } +} diff --git a/Acl/tests/ServiceBusTests.cs b/Acl/tests/ServiceBusTests.cs new file mode 100644 index 0000000..e15c74a --- /dev/null +++ b/Acl/tests/ServiceBusTests.cs @@ -0,0 +1,51 @@ +namespace AntiCorruptionLayer.Tests; + +using BestWeatherForecast.AntiCorruptionLayer; + +public class ServiceBusTests +{ + [Theory] + [InlineData("local")] + [InlineData("test")] + public void Will_get_ServiceBus_name(string env) + { + // Arrange + EnvironmentOptions environmentOptions = new() + { + Environment = env, + RegionShortName = "usw2", + ServiceName = "bwf" + }; + + var expected = $"{env}-sbns-bwf"; + + // Act + var actual = environmentOptions.GetServiceBusName(); + + // Assert + actual.Should().Be(expected); + } + + [Theory] + [InlineData(CloudType.AzureCloud, "ppe-sbns-bwf.servicebus.windows.net")] + [InlineData(CloudType.AzureUSGovernment, "ppe-sbns-bwf.servicebus.usgovcloudapi.net")] + [InlineData(CloudType.AzureChinaCloud, "ppe-sbns-bwf.servicebus.chinacloudapi.cn")] + [InlineData(CloudType.AzureGermanCloud, "ppe-sbns-bwf.servicebus.cloudapi.de")] + public void Will_get_namespace_for_Cloud(string cloudType, string expectedNamespace) + { + // Arrange + EnvironmentOptions environmentOptions = new() + { + Environment = EnvironmentType.Ppe, + RegionShortName = "usw2", + ServiceName = "bwf", + Cloud = cloudType + }; + + // Act + var actualNamespace = environmentOptions.GetServiceBusNamespace(); + + // Assert + actualNamespace.Should().Be(expectedNamespace); + } +} diff --git a/Acl/tests/StorageNameTests.cs b/Acl/tests/StorageNameTests.cs index 5164d55..b7df29e 100644 --- a/Acl/tests/StorageNameTests.cs +++ b/Acl/tests/StorageNameTests.cs @@ -27,26 +27,6 @@ public void Will_get_storage_name(string env) actual.Should().Be(expected); } - [Fact] - public void Will_get_blob_storage_url() - { - // Arrange - EnvironmentOptions environmentOptions = new() - { - Environment = EnvironmentType.Test, - RegionShortName = "usw2", - ServiceName = "bwf" - }; - - var expectedUrl = $"https://teststbwf.blob.core.windows.net"; - - // Act - var actualUrl = environmentOptions.GetBlobStorageSharedUrl(); - - // Assert - actualUrl.Should().Be(expectedUrl); - } - [Fact] public void Will_throw_exception_name_too_long() { @@ -65,8 +45,12 @@ public void Will_throw_exception_name_too_long() act.Should().Throw(); } - [Fact] - public void Will_get_blob_url_for_fairfax() + [Theory] + [InlineData(CloudType.AzureCloud, "https://ppestbwf.blob.core.windows.net")] + [InlineData(CloudType.AzureUSGovernment, "https://ppestbwf.blob.core.usgovcloudapi.net")] + [InlineData(CloudType.AzureChinaCloud, "https://ppestbwf.blob.core.chinacloud.cn")] + [InlineData(CloudType.AzureGermanCloud, "https://ppestbwf.blob.core.cloudapi.de")] + public void Will_get_blob_url_for_Cloud(string cloudType, string expectedUrl) { // Arrange EnvironmentOptions environmentOptions = new() @@ -74,11 +58,9 @@ public void Will_get_blob_url_for_fairfax() Environment = EnvironmentType.Ppe, RegionShortName = "usw2", ServiceName = "bwf", - Cloud = CloudType.Fairfax + Cloud = cloudType }; - var expectedUrl = $"https://ppestbwf.blob.core.usgovcloudapi.net"; - // Act var actualUrl = environmentOptions.GetBlobStorageSharedUrl();