-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding hosting env check for dapr ext Signed-off-by: MD Ashique <[email protected]> * Adding hosting plan check Signed-off-by: MD Ashique <[email protected]> * Adding check before loading the extension Signed-off-by: MD Ashique <[email protected]> * Adding check before loading the extension Signed-off-by: MD Ashique <[email protected]> * Adding sku check for consumption plan Signed-off-by: MD Ashique <[email protected]> --------- Signed-off-by: MD Ashique <[email protected]>
- Loading branch information
Showing
6 changed files
with
360 additions
and
1 deletion.
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
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
78 changes: 78 additions & 0 deletions
78
src/Microsoft.Azure.WebJobs.Extensions.Dapr/Utils/EnvironmentUtils.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,78 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
// ------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.Dapr.Utils | ||
{ | ||
using System; | ||
using Microsoft.Azure.WebJobs; | ||
|
||
internal static class EnvironmentUtils | ||
{ | ||
public static bool IsAppService(INameResolver nameResolver) | ||
{ | ||
return !string.IsNullOrEmpty(nameResolver.Resolve(Constants.EnvironmentKeys.AzureWebsiteInstanceId)); | ||
} | ||
|
||
public static bool IsWindowsElasticPremium(INameResolver nameResolver) | ||
{ | ||
string value = nameResolver.Resolve(Constants.EnvironmentKeys.AzureWebsiteSku); | ||
return string.Equals(value, Constants.HostingPlanSkuConstants.ElasticPremiumSku, StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
public static bool IsLinuxConsumptionOnLegion(INameResolver nameResolver) | ||
{ | ||
return !IsAppService(nameResolver) && | ||
(!string.IsNullOrEmpty(nameResolver.Resolve(Constants.EnvironmentKeys.ContainerName)) || | ||
!string.IsNullOrEmpty(nameResolver.Resolve(Constants.EnvironmentKeys.WebsitePodName))) && | ||
!string.IsNullOrEmpty(nameResolver.Resolve(Constants.EnvironmentKeys.LegionServiceHost)); | ||
} | ||
|
||
public static bool IsFlexConsumptionSku(INameResolver nameResolver) | ||
{ | ||
string value = nameResolver.Resolve(Constants.EnvironmentKeys.AzureWebsiteSku); | ||
if (string.Equals(value, Constants.HostingPlanSkuConstants.FlexConsumptionSku, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return true; | ||
} | ||
|
||
// when in placeholder mode, site settings like SKU are not available | ||
// to enable this check to run in both modes, we check additional settings | ||
return IsLinuxConsumptionOnLegion(nameResolver); | ||
} | ||
|
||
public static bool IsLinuxConsumptionOnAtlas(INameResolver nameResolver) | ||
{ | ||
return !IsAppService(nameResolver) && | ||
!string.IsNullOrEmpty(nameResolver.Resolve(Constants.EnvironmentKeys.ContainerName)) && | ||
string.IsNullOrEmpty(nameResolver.Resolve(Constants.EnvironmentKeys.LegionServiceHost)); | ||
} | ||
|
||
public static bool IsManagedAppEnvironment(INameResolver nameResolver) | ||
{ | ||
return !string.IsNullOrEmpty(nameResolver.Resolve(Constants.EnvironmentKeys.ManagedEnvironment)); | ||
} | ||
|
||
public static bool IsAnyLinuxConsumption(INameResolver nameResolver) | ||
{ | ||
return (IsLinuxConsumptionOnAtlas(nameResolver) || IsFlexConsumptionSku(nameResolver)) && !IsManagedAppEnvironment(nameResolver); | ||
} | ||
|
||
public static bool IsWindowsConsumption(INameResolver nameResolver) | ||
{ | ||
string value = nameResolver.Resolve(Constants.EnvironmentKeys.AzureWebsiteSku); | ||
return string.Equals(value, Constants.HostingPlanSkuConstants.DynamicSku, StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
public static bool IsConsumptionSku(INameResolver nameResolver) | ||
{ | ||
return IsWindowsConsumption(nameResolver) || IsAnyLinuxConsumption(nameResolver) || IsFlexConsumptionSku(nameResolver); | ||
} | ||
|
||
public static bool ShouldRegisterDaprExtension(INameResolver nameResolver) | ||
{ | ||
return !IsWindowsElasticPremium(nameResolver) && !IsAppService(nameResolver) && !IsConsumptionSku(nameResolver); | ||
} | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
test/DaprExtensionTests/Tests/ExtensionUnsupportedHostingPlanTests.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,43 @@ | ||
namespace DaprExtensionTests.Tests | ||
{ | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.WebJobs.Extensions.Dapr; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
public class ExtensionUnsupportedHostingPlanTests : DaprTestBase | ||
{ | ||
private static readonly IDictionary<string, string> EnvironmentVariables = new Dictionary<string, string>() | ||
{ | ||
{ Constants.EnvironmentKeys.AzureWebsiteInstanceId, "someValue" } | ||
}; | ||
|
||
public ExtensionUnsupportedHostingPlanTests(ITestOutputHelper output) | ||
: base(output, EnvironmentVariables) | ||
{ | ||
this.AddFunctions(typeof(Functions)); | ||
} | ||
|
||
public static IEnumerable<object[]> GetTheoryDataInputs() => new List<object[]> | ||
{ | ||
new object[] { nameof(Functions.ReturnInt), 42 } | ||
}; | ||
|
||
[Theory] | ||
[MemberData(nameof(GetTheoryDataInputs))] | ||
public async Task ValidateDaprExtensionNotSupportedOnAppServiceHostingPlan(string methodName, object input) | ||
{ | ||
await Assert.ThrowsAsync<HttpRequestException>(async () => await this.SendRequestAsync( | ||
HttpMethod.Post, | ||
$"http://localhost:3001/{methodName}", | ||
jsonContent: input)); | ||
} | ||
} | ||
|
||
static class Functions | ||
{ | ||
public static int ReturnInt([DaprBindingTrigger] int input) => input; | ||
} | ||
} |
Oops, something went wrong.