Skip to content

Issue #16094 - Updated New-AzAppServicePlan to create an app service plan with host environment id. #16645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Websites/Websites/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
* Updated `New-AzAppServicePlan` to create an app service plan with host environment id #16094

## Version 2.9.0
* Updated the Microsoft.Azure.Management.Websites SDK to 3.1.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ public class NewAzureAppServicePlanCmdlet : AppServicePlanBaseCmdlet
[ValidateSet("Small", "Medium", "Large", "ExtraLarge", IgnoreCase = true)]
public string WorkerSize { get; set; }

[Parameter(Position = 6, Mandatory = false, HelpMessage = "Name of application service environment")]
[Parameter(Position = 6, Mandatory = false, HelpMessage = "Name of App Service Environment")]
[ValidateNotNullOrEmpty]
public string AseName { get; set; }

[Parameter(Position = 7, Mandatory = false, HelpMessage = "Name of the application service environment resource group")]
[Parameter(Position = 7, Mandatory = false, HelpMessage = "Name of the App Service Environment resource group")]
[ValidateNotNullOrEmpty]
public string AseResourceGroupName { get; set; }

[Parameter(Position = 8, Mandatory = false, HelpMessage = "Resource id of App Service Environment")]
[ValidateNotNullOrEmpty]
public string AseResourceId { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Whether or not to enable Per Site Scaling")]
[ValidateNotNullOrEmpty]
public bool PerSiteScaling { get; set; }
Expand All @@ -73,10 +77,10 @@ public class NewAzureAppServicePlanCmdlet : AppServicePlanBaseCmdlet

public override void ExecuteCmdlet()
{
if (HyperV.IsPresent &&
(Tier != "PremiumContainer" && Tier != "PremiumV3"))
if (HyperV.IsPresent &&
(Tier != "PremiumContainer" && Tier != "PremiumV3" && Tier != "IsolatedV2"))
{
throw new Exception("HyperV switch is only allowed for PremiumContainer or PremiumV3 tiers");
throw new Exception("HyperV switch is only allowed for PremiumContainer , PremiumV3 or IsolatedV2 tiers");
}
if (!HyperV.IsPresent && Tier == "PremiumContainer")
{
Expand Down Expand Up @@ -117,11 +121,21 @@ public override void ExecuteCmdlet()
Sku = sku,
PerSiteScaling = PerSiteScaling,
IsXenon = HyperV.IsPresent,
Tags= (IDictionary<string, string>)CmdletHelpers.ConvertToStringDictionary(Tag),
Tags = (IDictionary<string, string>)CmdletHelpers.ConvertToStringDictionary(Tag),
Reserved = Linux.IsPresent
};

AppServicePlan retPlan = WebsitesClient.CreateOrUpdateAppServicePlan(ResourceGroupName, Name, appServicePlan, AseName, aseResourceGroupName);
AppServicePlan retPlan = null;

if (!string.IsNullOrEmpty(AseResourceId))
{
retPlan = WebsitesClient.CreateOrUpdateAppServicePlan(ResourceGroupName, Name, appServicePlan, AseResourceId);
}
else
{
retPlan = WebsitesClient.CreateOrUpdateAppServicePlan(ResourceGroupName, Name, appServicePlan, AseName, aseResourceGroupName);
}

PSAppServicePlan psPlan = new PSAppServicePlan(retPlan);

WriteObject(psPlan, true);
Expand Down
35 changes: 28 additions & 7 deletions src/Websites/Websites/Utilities/CmdletHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ public static class CmdletHelpers
private static readonly Regex KeyVaultResourceIdRegex =
new Regex(@"^\/subscriptions\/(?<subscriptionName>[^\/]+)\/resourceGroups\/(?<resourceGroupName>[^\/]+)\/providers\/Microsoft.KeyVault\/vaults\/(?<vaultName>[^\/]+)$", RegexOptions.IgnoreCase);

private static readonly Regex AppServiceEnvironmentResourceIdRegex =
new Regex(@"^\/subscriptions\/(?<subscriptionName>[^\/]+)\/resourceGroups\/(?<resourceGroupName>[^\/]+)\/providers\/Microsoft.Web\/hostingEnvironments\/(?<aseName>[^\/]+)$", RegexOptions.IgnoreCase);

private static readonly Dictionary<string, int> WorkerSizes = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase) { { "Small", 1 }, { "Medium", 2 }, { "Large", 3 }, { "ExtraLarge", 4 } };

private const string ProductionSlotName = "Production";

private const string FmtSiteWithSlotName = "{0}({1})";
public const string ApplicationServiceEnvironmentResourcesName = "hostingEnvironments";
private const string ApplicationServiceEnvironmentResourceIdFormat =
public const string AppServiceEnvironmentResourcesName = "hostingEnvironments";
private const string AppServiceEnvironmentResourceIdFormat =
"/subscriptions/{0}/resourcegroups/{1}/providers/Microsoft.Web/{2}/{3}";

public const string DockerRegistryServerUrl = "DOCKER_REGISTRY_SERVER_URL";
Expand Down Expand Up @@ -180,10 +183,10 @@ internal static bool ShouldUseDeploymentSlot(string webSiteName, string slotName
internal static HostingEnvironmentProfile CreateHostingEnvironmentProfile(string subscriptionId, string resourceGroupName, string aseResourceGroupName, string aseName)
{
var rg = string.IsNullOrEmpty(aseResourceGroupName) ? resourceGroupName : aseResourceGroupName;
var aseResourceId = CmdletHelpers.GetApplicationServiceEnvironmentResourceId(subscriptionId, rg, aseName);
var aseResourceId = CmdletHelpers.GetAppServiceEnvironmentResourceId(subscriptionId, rg, aseName);
return new HostingEnvironmentProfile(
aseResourceId,
CmdletHelpers.ApplicationServiceEnvironmentResourcesName,
CmdletHelpers.AppServiceEnvironmentResourcesName,
aseName);
}

Expand Down Expand Up @@ -269,6 +272,24 @@ internal static bool TryParseAppServicePlanMetadataFromResourceId(string resourc
return false;
}

internal static bool TryParseAppServiceEnvironmentMetadataFromResourceId(string resourceId, out string resourceGroupName,
out string aseName)
{
var match = AppServiceEnvironmentResourceIdRegex.Match(resourceId);
if (match.Success)
{
resourceGroupName = match.Groups["resourceGroupName"].Value;
aseName = match.Groups["aseName"].Value;

return true;
}

resourceGroupName = null;
aseName = null;

return false;
}

internal static bool IsValidAKVResourceId(string resourceId)
{
return KeyVaultResourceIdRegex.Match(resourceId).Success;
Expand Down Expand Up @@ -345,10 +366,10 @@ public static string GenerateSiteWithSlotName(string siteName, string slotName)
return siteName;
}

internal static string GetApplicationServiceEnvironmentResourceId(string subscriptionId, string resourceGroupName, string applicationServiceEnvironmentName)
internal static string GetAppServiceEnvironmentResourceId(string subscriptionId, string resourceGroupName, string appServiceEnvironmentName)
{
return string.Format(ApplicationServiceEnvironmentResourceIdFormat, subscriptionId, resourceGroupName, ApplicationServiceEnvironmentResourcesName,
applicationServiceEnvironmentName);
return string.Format(AppServiceEnvironmentResourceIdFormat, subscriptionId, resourceGroupName, AppServiceEnvironmentResourcesName,
appServiceEnvironmentName);
}

internal static HostNameSslState[] GetHostNameSslStatesFromSiteResponse(Site site, string hostName = null)
Expand Down
20 changes: 18 additions & 2 deletions src/Websites/Websites/Utilities/WebsitesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,25 @@ public AppServicePlan CreateOrUpdateAppServicePlan(string resourceGroupName, str
&& !string.IsNullOrEmpty(aseResourceGroupName))
{
appServicePlan.HostingEnvironmentProfile = new HostingEnvironmentProfile(
id: CmdletHelpers.GetApplicationServiceEnvironmentResourceId(WrappedWebsitesClient.SubscriptionId, aseResourceGroupName, aseName),
id: CmdletHelpers.GetAppServiceEnvironmentResourceId(WrappedWebsitesClient.SubscriptionId, aseResourceGroupName, aseName),
name: aseName,
type: CmdletHelpers.ApplicationServiceEnvironmentResourcesName);
type: CmdletHelpers.AppServiceEnvironmentResourcesName);
}

return WrappedWebsitesClient.AppServicePlans().CreateOrUpdate(resourceGroupName, appServicePlanName, appServicePlan);
}

public AppServicePlan CreateOrUpdateAppServicePlan(string resourceGroupName, string appServicePlanName, AppServicePlan appServicePlan, string aseRecourceId)
{
if (!string.IsNullOrEmpty(aseRecourceId))
{
string aseResourceGroupName, aseName;
if (!CmdletHelpers.TryParseAppServiceEnvironmentMetadataFromResourceId(aseRecourceId, out aseResourceGroupName, out aseName))
throw new ArgumentException(string.Format("AseResourceId format is invalid"));
appServicePlan.HostingEnvironmentProfile = new HostingEnvironmentProfile(
id: aseRecourceId,
name: aseName,
type: CmdletHelpers.AppServiceEnvironmentResourcesName);
}

return WrappedWebsitesClient.AppServicePlans().CreateOrUpdate(resourceGroupName, appServicePlanName, appServicePlan);
Expand Down
26 changes: 21 additions & 5 deletions src/Websites/Websites/help/New-AzAppServicePlan.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ Creates an Azure App Service plan in a given Geo location.
### S1
```
New-AzAppServicePlan [-Location] <String> [[-Tier] <String>] [[-NumberofWorkers] <Int32>]
[[-WorkerSize] <String>] [[-AseName] <String>] [[-AseResourceGroupName] <String>] [-PerSiteScaling <Boolean>]
[-HyperV] [-AsJob] [-Tag <Hashtable>] [-Linux] [-ResourceGroupName] <String> [-Name] <String>
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
[[-WorkerSize] <String>] [[-AseName] <String>] [[-AseResourceGroupName] <String>] [-AseResourceId <String>]
[-PerSiteScaling <Boolean>] [-HyperV] [-AsJob] [-Tag <Hashtable>] [-Linux] [-ResourceGroupName] <String>
[-Name] <String> [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
```

### S2
```
New-AzAppServicePlan [-Location] <String> [[-Tier] <String>] [[-NumberofWorkers] <Int32>]
[[-WorkerSize] <String>] [[-AseName] <String>] [[-AseResourceGroupName] <String>] [-PerSiteScaling <Boolean>]
[-AsJob] [-AppServicePlan] <PSAppServicePlan> [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
[[-WorkerSize] <String>] [[-AseName] <String>] [[-AseResourceGroupName] <String>] [-AseResourceId <String>]
[-PerSiteScaling <Boolean>] [-AsJob] [-AppServicePlan] <PSAppServicePlan>
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -88,6 +89,21 @@ Accept pipeline input: False
Accept wildcard characters: False
```

### -AseResourceId
Resource id of App Service Environment

```yaml
Type: System.String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -AsJob
Run cmdlet in the background

Expand Down