Skip to content

Commit 16b9c5d

Browse files
author
Sam Lin
authored
Issue Azure#16094 - Updated New-AzAppServicePlan to create an app service plan with host environment id. (Azure#16645)
* Updated New-AzAppServicePlan to create an app service plan with host environment id Azure#16094 * Bugfix - Updated New-AzAppServicePlan to create an app service plan with host environment id (Azure#16094) * Remove comment - Updated New-AzAppServicePlan to create an app service plan with host environment id (Azure#16094)
1 parent e69e0c2 commit 16b9c5d

File tree

5 files changed

+89
-21
lines changed

5 files changed

+89
-21
lines changed

src/Websites/Websites/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Updated `New-AzAppServicePlan` to create an app service plan with host environment id #16094
2122

2223
## Version 2.9.0
2324
* Updated the Microsoft.Azure.Management.Websites SDK to 3.1.2

src/Websites/Websites/Cmdlets/AppServicePlans/NewAzureAppServicePlan.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ public class NewAzureAppServicePlanCmdlet : AppServicePlanBaseCmdlet
4747
[ValidateSet("Small", "Medium", "Large", "ExtraLarge", IgnoreCase = true)]
4848
public string WorkerSize { get; set; }
4949

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

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

58+
[Parameter(Position = 8, Mandatory = false, HelpMessage = "Resource id of App Service Environment")]
59+
[ValidateNotNullOrEmpty]
60+
public string AseResourceId { get; set; }
61+
5862
[Parameter(Mandatory = false, HelpMessage = "Whether or not to enable Per Site Scaling")]
5963
[ValidateNotNullOrEmpty]
6064
public bool PerSiteScaling { get; set; }
@@ -73,10 +77,10 @@ public class NewAzureAppServicePlanCmdlet : AppServicePlanBaseCmdlet
7377

7478
public override void ExecuteCmdlet()
7579
{
76-
if (HyperV.IsPresent &&
77-
(Tier != "PremiumContainer" && Tier != "PremiumV3"))
80+
if (HyperV.IsPresent &&
81+
(Tier != "PremiumContainer" && Tier != "PremiumV3" && Tier != "IsolatedV2"))
7882
{
79-
throw new Exception("HyperV switch is only allowed for PremiumContainer or PremiumV3 tiers");
83+
throw new Exception("HyperV switch is only allowed for PremiumContainer , PremiumV3 or IsolatedV2 tiers");
8084
}
8185
if (!HyperV.IsPresent && Tier == "PremiumContainer")
8286
{
@@ -117,11 +121,21 @@ public override void ExecuteCmdlet()
117121
Sku = sku,
118122
PerSiteScaling = PerSiteScaling,
119123
IsXenon = HyperV.IsPresent,
120-
Tags= (IDictionary<string, string>)CmdletHelpers.ConvertToStringDictionary(Tag),
124+
Tags = (IDictionary<string, string>)CmdletHelpers.ConvertToStringDictionary(Tag),
121125
Reserved = Linux.IsPresent
122126
};
123127

124-
AppServicePlan retPlan = WebsitesClient.CreateOrUpdateAppServicePlan(ResourceGroupName, Name, appServicePlan, AseName, aseResourceGroupName);
128+
AppServicePlan retPlan = null;
129+
130+
if (!string.IsNullOrEmpty(AseResourceId))
131+
{
132+
retPlan = WebsitesClient.CreateOrUpdateAppServicePlan(ResourceGroupName, Name, appServicePlan, AseResourceId);
133+
}
134+
else
135+
{
136+
retPlan = WebsitesClient.CreateOrUpdateAppServicePlan(ResourceGroupName, Name, appServicePlan, AseName, aseResourceGroupName);
137+
}
138+
125139
PSAppServicePlan psPlan = new PSAppServicePlan(retPlan);
126140

127141
WriteObject(psPlan, true);

src/Websites/Websites/Utilities/CmdletHelpers.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ public static class CmdletHelpers
5252
private static readonly Regex KeyVaultResourceIdRegex =
5353
new Regex(@"^\/subscriptions\/(?<subscriptionName>[^\/]+)\/resourceGroups\/(?<resourceGroupName>[^\/]+)\/providers\/Microsoft.KeyVault\/vaults\/(?<vaultName>[^\/]+)$", RegexOptions.IgnoreCase);
5454

55+
private static readonly Regex AppServiceEnvironmentResourceIdRegex =
56+
new Regex(@"^\/subscriptions\/(?<subscriptionName>[^\/]+)\/resourceGroups\/(?<resourceGroupName>[^\/]+)\/providers\/Microsoft.Web\/hostingEnvironments\/(?<aseName>[^\/]+)$", RegexOptions.IgnoreCase);
57+
5558
private static readonly Dictionary<string, int> WorkerSizes = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase) { { "Small", 1 }, { "Medium", 2 }, { "Large", 3 }, { "ExtraLarge", 4 } };
5659

5760
private const string ProductionSlotName = "Production";
5861

5962
private const string FmtSiteWithSlotName = "{0}({1})";
60-
public const string ApplicationServiceEnvironmentResourcesName = "hostingEnvironments";
61-
private const string ApplicationServiceEnvironmentResourceIdFormat =
63+
public const string AppServiceEnvironmentResourcesName = "hostingEnvironments";
64+
private const string AppServiceEnvironmentResourceIdFormat =
6265
"/subscriptions/{0}/resourcegroups/{1}/providers/Microsoft.Web/{2}/{3}";
6366

6467
public const string DockerRegistryServerUrl = "DOCKER_REGISTRY_SERVER_URL";
@@ -180,10 +183,10 @@ internal static bool ShouldUseDeploymentSlot(string webSiteName, string slotName
180183
internal static HostingEnvironmentProfile CreateHostingEnvironmentProfile(string subscriptionId, string resourceGroupName, string aseResourceGroupName, string aseName)
181184
{
182185
var rg = string.IsNullOrEmpty(aseResourceGroupName) ? resourceGroupName : aseResourceGroupName;
183-
var aseResourceId = CmdletHelpers.GetApplicationServiceEnvironmentResourceId(subscriptionId, rg, aseName);
186+
var aseResourceId = CmdletHelpers.GetAppServiceEnvironmentResourceId(subscriptionId, rg, aseName);
184187
return new HostingEnvironmentProfile(
185188
aseResourceId,
186-
CmdletHelpers.ApplicationServiceEnvironmentResourcesName,
189+
CmdletHelpers.AppServiceEnvironmentResourcesName,
187190
aseName);
188191
}
189192

@@ -269,6 +272,24 @@ internal static bool TryParseAppServicePlanMetadataFromResourceId(string resourc
269272
return false;
270273
}
271274

275+
internal static bool TryParseAppServiceEnvironmentMetadataFromResourceId(string resourceId, out string resourceGroupName,
276+
out string aseName)
277+
{
278+
var match = AppServiceEnvironmentResourceIdRegex.Match(resourceId);
279+
if (match.Success)
280+
{
281+
resourceGroupName = match.Groups["resourceGroupName"].Value;
282+
aseName = match.Groups["aseName"].Value;
283+
284+
return true;
285+
}
286+
287+
resourceGroupName = null;
288+
aseName = null;
289+
290+
return false;
291+
}
292+
272293
internal static bool IsValidAKVResourceId(string resourceId)
273294
{
274295
return KeyVaultResourceIdRegex.Match(resourceId).Success;
@@ -345,10 +366,10 @@ public static string GenerateSiteWithSlotName(string siteName, string slotName)
345366
return siteName;
346367
}
347368

348-
internal static string GetApplicationServiceEnvironmentResourceId(string subscriptionId, string resourceGroupName, string applicationServiceEnvironmentName)
369+
internal static string GetAppServiceEnvironmentResourceId(string subscriptionId, string resourceGroupName, string appServiceEnvironmentName)
349370
{
350-
return string.Format(ApplicationServiceEnvironmentResourceIdFormat, subscriptionId, resourceGroupName, ApplicationServiceEnvironmentResourcesName,
351-
applicationServiceEnvironmentName);
371+
return string.Format(AppServiceEnvironmentResourceIdFormat, subscriptionId, resourceGroupName, AppServiceEnvironmentResourcesName,
372+
appServiceEnvironmentName);
352373
}
353374

354375
internal static HostNameSslState[] GetHostNameSslStatesFromSiteResponse(Site site, string hostName = null)

src/Websites/Websites/Utilities/WebsitesClient.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,25 @@ public AppServicePlan CreateOrUpdateAppServicePlan(string resourceGroupName, str
481481
&& !string.IsNullOrEmpty(aseResourceGroupName))
482482
{
483483
appServicePlan.HostingEnvironmentProfile = new HostingEnvironmentProfile(
484-
id: CmdletHelpers.GetApplicationServiceEnvironmentResourceId(WrappedWebsitesClient.SubscriptionId, aseResourceGroupName, aseName),
484+
id: CmdletHelpers.GetAppServiceEnvironmentResourceId(WrappedWebsitesClient.SubscriptionId, aseResourceGroupName, aseName),
485485
name: aseName,
486-
type: CmdletHelpers.ApplicationServiceEnvironmentResourcesName);
486+
type: CmdletHelpers.AppServiceEnvironmentResourcesName);
487+
}
488+
489+
return WrappedWebsitesClient.AppServicePlans().CreateOrUpdate(resourceGroupName, appServicePlanName, appServicePlan);
490+
}
491+
492+
public AppServicePlan CreateOrUpdateAppServicePlan(string resourceGroupName, string appServicePlanName, AppServicePlan appServicePlan, string aseRecourceId)
493+
{
494+
if (!string.IsNullOrEmpty(aseRecourceId))
495+
{
496+
string aseResourceGroupName, aseName;
497+
if (!CmdletHelpers.TryParseAppServiceEnvironmentMetadataFromResourceId(aseRecourceId, out aseResourceGroupName, out aseName))
498+
throw new ArgumentException(string.Format("AseResourceId format is invalid"));
499+
appServicePlan.HostingEnvironmentProfile = new HostingEnvironmentProfile(
500+
id: aseRecourceId,
501+
name: aseName,
502+
type: CmdletHelpers.AppServiceEnvironmentResourcesName);
487503
}
488504

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

src/Websites/Websites/help/New-AzAppServicePlan.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ Creates an Azure App Service plan in a given Geo location.
1616
### S1
1717
```
1818
New-AzAppServicePlan [-Location] <String> [[-Tier] <String>] [[-NumberofWorkers] <Int32>]
19-
[[-WorkerSize] <String>] [[-AseName] <String>] [[-AseResourceGroupName] <String>] [-PerSiteScaling <Boolean>]
20-
[-HyperV] [-AsJob] [-Tag <Hashtable>] [-Linux] [-ResourceGroupName] <String> [-Name] <String>
21-
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
19+
[[-WorkerSize] <String>] [[-AseName] <String>] [[-AseResourceGroupName] <String>] [-AseResourceId <String>]
20+
[-PerSiteScaling <Boolean>] [-HyperV] [-AsJob] [-Tag <Hashtable>] [-Linux] [-ResourceGroupName] <String>
21+
[-Name] <String> [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
2222
```
2323

2424
### S2
2525
```
2626
New-AzAppServicePlan [-Location] <String> [[-Tier] <String>] [[-NumberofWorkers] <Int32>]
27-
[[-WorkerSize] <String>] [[-AseName] <String>] [[-AseResourceGroupName] <String>] [-PerSiteScaling <Boolean>]
28-
[-AsJob] [-AppServicePlan] <PSAppServicePlan> [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
27+
[[-WorkerSize] <String>] [[-AseName] <String>] [[-AseResourceGroupName] <String>] [-AseResourceId <String>]
28+
[-PerSiteScaling <Boolean>] [-AsJob] [-AppServicePlan] <PSAppServicePlan>
29+
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
2930
```
3031

3132
## DESCRIPTION
@@ -88,6 +89,21 @@ Accept pipeline input: False
8889
Accept wildcard characters: False
8990
```
9091
92+
### -AseResourceId
93+
Resource id of App Service Environment
94+
95+
```yaml
96+
Type: System.String
97+
Parameter Sets: (All)
98+
Aliases:
99+
100+
Required: False
101+
Position: Named
102+
Default value: None
103+
Accept pipeline input: False
104+
Accept wildcard characters: False
105+
```
106+
91107
### -AsJob
92108
Run cmdlet in the background
93109

0 commit comments

Comments
 (0)