Skip to content

Commit b81158b

Browse files
authored
Merge pull request Azure#11189 from Kotasudhakarreddy/master
4 issues of Az.AppService
2 parents 94de5e1 + bb888f1 commit b81158b

File tree

12 files changed

+8776
-1926
lines changed

12 files changed

+8776
-1926
lines changed

src/Websites/Websites.Test/ScenarioTests/AppServicePlanTests.ps1

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ function Test-CreateNewAppServicePlan
2424
$location = Get-Location
2525
$capacity = 2
2626
$skuName = "S2"
27-
27+
$tag= @{"TagKey" = "TagValue"}
2828
try
2929
{
3030
#Setup
3131
New-AzResourceGroup -Name $rgname -Location $location
3232

3333
# Test
34-
$job = New-AzAppServicePlan -ResourceGroupName $rgname -Name $whpName -Location $location -Tier "Standard" -WorkerSize Medium -NumberOfWorkers $capacity -AsJob
34+
$job = New-AzAppServicePlan -ResourceGroupName $rgname -Name $whpName -Location $location -Tier "Standard" -WorkerSize Medium -NumberOfWorkers $capacity -Tag $tag -AsJob
3535
$job | Wait-Job
3636
$createResult = $job | Receive-Job
3737

@@ -40,7 +40,8 @@ function Test-CreateNewAppServicePlan
4040
Assert-AreEqual "Standard" $createResult.Sku.Tier
4141
Assert-AreEqual $skuName $createResult.Sku.Name
4242
Assert-AreEqual $capacity $createResult.Sku.Capacity
43-
43+
Assert-AreEqual $tag.Keys $createResult.Tags.Keys
44+
Assert-AreEqual $tag.Values $createResult.Tags.Values
4445
# Assert
4546

4647
$getResult = Get-AzAppServicePlan -ResourceGroupName $rgname -Name $whpName
@@ -127,7 +128,7 @@ function Test-SetAppServicePlan
127128
$newWorkerSize = "Medium"
128129
$newCapacity = 2
129130
$newPerSiteScaling = $true;
130-
131+
$tag= @{"TagKey" = "TagValue"}
131132

132133
try
133134
{
@@ -173,6 +174,11 @@ function Test-SetAppServicePlan
173174
Assert-AreEqual $skuName $newresult.Sku.Name
174175
Assert-AreEqual $perSiteScaling $newresult.PerSiteScaling
175176

177+
#Set Tags
178+
$tagsResult= Set-AzAppServicePlan -ResourceGroupName $rgname -Name $whpName -Tag $tag
179+
# Assert
180+
Assert-AreEqual $tag.Keys $tagsResult.Tags.Keys
181+
Assert-AreEqual $tag.Values $tagsResult.Tags.Values
176182
}
177183
finally
178184
{

src/Websites/Websites.Test/ScenarioTests/WebAppTests.ps1

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,15 +1064,18 @@ function Test-SetWebApp
10641064
{
10651065
# Setup
10661066
$rgname = Get-ResourceGroupName
1067+
$rgname1 = Get-ResourceGroupName
10671068
$webAppName = Get-WebsiteName
10681069
$location = Get-WebLocation
10691070
$appServicePlanName1 = Get-WebHostPlanName
10701071
$appServicePlanName2 = Get-WebHostPlanName
1072+
$appServicePlanName3 = Get-WebHostPlanName
10711073
$tier1 = "Shared"
10721074
$tier2 = "Standard"
10731075
$apiversion = "2015-08-01"
10741076
$resourceType = "Microsoft.Web/sites"
10751077
$capacity = 2
1078+
$HN="custom.domain.com"
10761079

10771080
try
10781081
{
@@ -1159,6 +1162,49 @@ function Test-SetWebApp
11591162
Assert-AreEqual "" $webApp.SiteConfig.PhpVersion
11601163
Assert-AreEqual "1.2" $webApp.SiteConfig.MinTlsVersion
11611164

1165+
# set Custom Host Name(s)- Failed Scenario
1166+
$oldWebApp= Get-AzWebApp -ResourceGroupName $rgname -Name $webAppName
1167+
$CurrentWebApp = Set-AzWebApp -ResourceGroupName $rgname -Name $webAppName -HostNames $HN
1168+
#Assert
1169+
$status
1170+
foreach($oldHN in $oldWebApp.HostNames)
1171+
{
1172+
Assert-True { $CurrentWebApp.HostNames -contains $oldHN }
1173+
}
1174+
1175+
#Set-AzWebApp errors on operations for App Services not in the same resource group as the App Service Plan
1176+
#setup
1177+
## Create a Resource Group.
1178+
New-AzResourceGroup -Name $rgname1 -Location $location
1179+
1180+
## Create the App Service Plan in $rgname.
1181+
$asp = New-AzAppServicePlan -Location $location -Tier Standard -NumberofWorkers 1 -WorkerSize Small -ResourceGroupName $rgname -Name $appServicePlanName3
1182+
1183+
## Create a Web App in each Resource Group.
1184+
$app1 = Get-WebsiteName
1185+
$app2 = Get-WebsiteName
1186+
1187+
New-AzWebApp -ResourceGroupName $rgname -Name $app1 -Location $location -AppServicePlan $asp.Id
1188+
New-AzWebApp -ResourceGroupName $rgname1 -Name $app2 -Location $location -AppServicePlan $asp.Id
1189+
1190+
## Get the two Web Apps.
1191+
$wa1 = Get-AzWebApp -ResourceGroupName $rgname -Name $app1
1192+
$wa2 = Get-AzWebApp -ResourceGroupName $rgname1 -Name $app2
1193+
1194+
## Change a setting on the first Web App (which is in the same Resource Group as the App Service Plan).
1195+
$currentWa1ClientAffinityEnabled=$wa1.ClientAffinityEnabled
1196+
$wa1.ClientAffinityEnabled = !$wa1.ClientAffinityEnabled
1197+
$wa1 | Set-AzWebApp
1198+
1199+
#Assert
1200+
Assert-AreNotEqual $currentWa1ClientAffinityEnabled $wa1.ClientAffinityEnabled
1201+
## Change a setting on the first Web App (which is in the same Resource Group as the App Service Plan).
1202+
$currentWa2ClientAffinityEnabled=$wa2.ClientAffinityEnabled
1203+
$wa2.ClientAffinityEnabled = !$wa2.ClientAffinityEnabled
1204+
$wa2 | Set-AzWebApp
1205+
1206+
#Assert
1207+
Assert-AreNotEqual $currentWa2ClientAffinityEnabled $wa2.ClientAffinityEnabled
11621208
}
11631209
finally
11641210
{
@@ -1167,6 +1213,7 @@ function Test-SetWebApp
11671213
Remove-AzAppServicePlan -ResourceGroupName $rgname -Name $appServicePlanName1 -Force
11681214
Remove-AzAppServicePlan -ResourceGroupName $rgname -Name $appServicePlanName2 -Force
11691215
Remove-AzResourceGroup -Name $rgname -Force
1216+
Remove-AzResourceGroup -Name $rgname1 -Force
11701217
}
11711218
}
11721219

src/Websites/Websites.Test/SessionRecords/Microsoft.Azure.Commands.Websites.Test.ScenarioTests.AppServicePlanTests/TestCreateNewAppServicePlan.json

Lines changed: 138 additions & 195 deletions
Large diffs are not rendered by default.

src/Websites/Websites.Test/SessionRecords/Microsoft.Azure.Commands.Websites.Test.ScenarioTests.AppServicePlanTests/TestSetAppServicePlan.json

Lines changed: 385 additions & 184 deletions
Large diffs are not rendered by default.

src/Websites/Websites.Test/SessionRecords/Microsoft.Azure.Commands.Websites.Test.ScenarioTests.WebAppTests/TestSetWebApp.json

Lines changed: 8146 additions & 1533 deletions
Large diffs are not rendered by default.

src/Websites/Websites/ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
*Adding Tag parameter for New-AzAppServicePlan and Set-AzAppServicePlan.
22+
*Stop cmdlt execution if an exception is thrown when adding a custom domain to a website.
23+
*Adding support to perform operations for App Services not in the same resource group as the App Service Plan.
24+
*Apply access restriction to WebApp/Function in different resource groups
2125
*Fixing issue to set custom hostnames for WebAppSlots
2226

2327
## Version 1.6.0

src/Websites/Websites/Cmdlets/AccessRestriction/AddAzureWebAppAccessRestrictionRule.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,14 @@ public override void ExecuteCmdlet()
149149
case SubnetNameParameterSet:
150150
case SubnetIdParameterSet:
151151
var Subnet = ParameterSetName == SubnetNameParameterSet ? SubnetName : SubnetId;
152-
var subnetResourceId = CmdletHelpers.ValidateSubnet(Subnet, VirtualNetworkName, ResourceGroupName, DefaultContext.Subscription.Id);
152+
//Fetch RG of given SubNet
153+
var subNetResourceGroupName = CmdletHelpers.GetSubnetResourceGroupName(DefaultContext, Subnet, VirtualNetworkName);
154+
//If unble to fetch SubNet rg from above step, use the input RG to get validation error from api call.
155+
subNetResourceGroupName = !String.IsNullOrEmpty(subNetResourceGroupName) ? subNetResourceGroupName : ResourceGroupName;
156+
var subnetResourceId = CmdletHelpers.ValidateSubnet(Subnet, VirtualNetworkName, subNetResourceGroupName, DefaultContext.Subscription.Id);
153157
if (!IgnoreMissingServiceEndpoint)
154158
{
155-
CmdletHelpers.VerifySubnetDelegation(DefaultContext, subnetResourceId);
159+
CmdletHelpers.VerifySubnetDelegation(subnetResourceId);
156160
}
157161
foreach (var accessRestriction in accessRestrictionList)
158162
{

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
using Microsoft.Azure.Commands.WebApps.Utilities;
1919
using Microsoft.Azure.Management.WebSites.Models;
2020
using System;
21+
using System.Collections;
22+
using System.Collections.Generic;
2123
using System.Management.Automation;
2224

2325
namespace Microsoft.Azure.Commands.WebApps.Cmdlets.AppServicePlans
@@ -63,6 +65,9 @@ public class NewAzureAppServicePlanCmdlet : AppServicePlanBaseCmdlet
6365
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
6466
public SwitchParameter AsJob { get; set; }
6567

68+
[Parameter(ParameterSetName = ParameterSet1Name, Mandatory = false, HelpMessage = "Tags are name/value pairs that enable you to categorize resources")]
69+
public Hashtable Tag { get; set; }
70+
6671
public override void ExecuteCmdlet()
6772
{
6873
if (HyperV.IsPresent && Tier != "PremiumContainer")
@@ -107,7 +112,8 @@ public override void ExecuteCmdlet()
107112
Location = Location,
108113
Sku = sku,
109114
PerSiteScaling = PerSiteScaling,
110-
IsXenon = HyperV.IsPresent
115+
IsXenon = HyperV.IsPresent,
116+
Tags= (IDictionary<string, string>)CmdletHelpers.ConvertToStringDictionary(Tag)
111117
};
112118

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

src/Websites/Websites/Cmdlets/AppServicePlans/SetAzureAppServicePlan.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
using System.Management.Automation;
1919
using System.Text.RegularExpressions;
2020
using Microsoft.Azure.Commands.WebApps.Models.WebApp;
21+
using System.Collections;
22+
using System.Collections.Generic;
2123

2224
namespace Microsoft.Azure.Commands.WebApps.Cmdlets.AppServicePlans
2325
{
@@ -48,7 +50,8 @@ public class SetAzureAppServicePlanCmdlet : AppServicePlanBaseCmdlet
4850

4951
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
5052
public SwitchParameter AsJob { get; set; }
51-
53+
[Parameter(ParameterSetName = ParameterSet1Name, Mandatory = false, HelpMessage = "Tags are name/value pairs that enable you to categorize resources")]
54+
public Hashtable Tag { get; set; }
5255
public override void ExecuteCmdlet()
5356
{
5457
base.ExecuteCmdlet();
@@ -62,6 +65,7 @@ public override void ExecuteCmdlet()
6265
int.TryParse(Regex.Match(AppServicePlan.Sku.Name, @"\d+").Value, out workerSizeAsNumber);
6366
AppServicePlan.Sku.Name = string.IsNullOrWhiteSpace(WorkerSize) ? CmdletHelpers.GetSkuName(AppServicePlan.Sku.Tier, workerSizeAsNumber) : CmdletHelpers.GetSkuName(AppServicePlan.Sku.Tier, WorkerSize);
6467
AppServicePlan.PerSiteScaling = PerSiteScaling;
68+
AppServicePlan.Tags = (IDictionary<string, string>)CmdletHelpers.ConvertToStringDictionary(Tag);
6569
break;
6670
}
6771

src/Websites/Websites/Cmdlets/WebApps/SetAzureWebApp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public override void ExecuteCmdlet()
312312
CmdletHelpers.TryParseAppServicePlanMetadataFromResourceId(WebApp.ServerFarmId, out rg, out servicePlanName);
313313
// AzureStorage path is not a part of the back end siteObject, but if the PSSite Object is given as an input, we will some value for this
314314
WebApp.AzureStoragePath = null;
315-
WebsitesClient.UpdateWebApp(ResourceGroupName, location, Name, null, servicePlanName, WebApp);
315+
WebsitesClient.UpdateWebApp(ResourceGroupName, location, Name, null, servicePlanName, WebApp,rg);
316316
WebsitesClient.AddCustomHostNames(ResourceGroupName, location, Name, WebApp.HostNames.ToArray());
317317
break;
318318
}

src/Websites/Websites/Utilities/CmdletHelpers.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ namespace Microsoft.Azure.Commands.WebApps.Utilities
1616
{
1717
public static class CmdletHelpers
1818
{
19+
public static NetworkManagementClient networkClient
20+
{
21+
get;
22+
private set;
23+
}
1924
public static HashSet<string> SiteConfigParameters = new HashSet<string>
2025
{
2126
"DefaultDocuments",
@@ -559,14 +564,13 @@ internal static string ValidateSubnet(string subnet, string virtualNetworkName,
559564
return subnetResourceId.ToString();
560565
}
561566

562-
internal static void VerifySubnetDelegation(IAzureContext context, string subnet)
567+
internal static void VerifySubnetDelegation(string subnet)
563568
{
564569
var subnetResourceId = new ResourceIdentifier(subnet);
565570
var resourceGroupName = subnetResourceId.ResourceGroupName;
566571
var virtualNetworkName = subnetResourceId.ParentResource.Substring(subnetResourceId.ParentResource.IndexOf('/') + 1);
567572
var subnetName = subnetResourceId.ResourceName;
568573

569-
var networkClient = AzureSession.Instance.ClientFactory.CreateArmClient<NetworkManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager);
570574
Subnet subnetObj = networkClient.Subnets.Get(resourceGroupName, virtualNetworkName, subnetName);
571575
var serviceEndpointServiceName = "Microsoft.Web";
572576
var serviceEndpointLocations = new List<string>() { "*" };
@@ -594,5 +598,22 @@ internal static void VerifySubnetDelegation(IAzureContext context, string subnet
594598
}
595599
}
596600
}
601+
602+
internal static string GetSubnetResourceGroupName(IAzureContext context, string Subnet, string VirtualNetworkName)
603+
{
604+
networkClient = AzureSession.Instance.ClientFactory.CreateArmClient<NetworkManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager);
605+
var matchedVNetwork = networkClient.VirtualNetworks.ListAll().FirstOrDefault(item => item.Name == VirtualNetworkName);
606+
if (matchedVNetwork != null)
607+
{
608+
var subNets = matchedVNetwork.Subnets.ToList();
609+
Subnet matchedSubnet = matchedVNetwork.Subnets.FirstOrDefault(sItem => sItem.Name == Subnet || sItem.Id == Subnet);
610+
if (matchedSubnet != null)
611+
{
612+
var subnetResourceId = new ResourceIdentifier(matchedSubnet.Id);
613+
return subnetResourceId.ResourceGroupName;
614+
}
615+
}
616+
return null;
617+
}
597618
}
598619
}

src/Websites/Websites/Utilities/WebsitesClient.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ public HostingEnvironmentProfile CreateHostingEnvironmentProfile(string resource
9797
return CmdletHelpers.CreateHostingEnvironmentProfile(WrappedWebsitesClient.SubscriptionId, resourceGroupName, aseResourceGroupName, aseName);
9898
}
9999

100-
public void UpdateWebApp(string resourceGroupName, string location, string webAppName, string slotName, string appServicePlan, Site siteEnvelope =null)
100+
public void UpdateWebApp(string resourceGroupName, string location, string webAppName, string slotName, string appServicePlan, Site siteEnvelope =null, string appServicePlanRg = null)
101101
{
102102
var webSiteToUpdate = new Site()
103103
{
104-
ServerFarmId = appServicePlan,
104+
ServerFarmId = (string.IsNullOrEmpty(appServicePlanRg) && resourceGroupName != appServicePlanRg) ? appServicePlan : siteEnvelope.ServerFarmId,
105105
Location = location,
106106
Tags = siteEnvelope?.Tags
107107
};
@@ -112,7 +112,7 @@ public void UpdateWebApp(string resourceGroupName, string location, string webAp
112112
}
113113

114114
// make sure the serverfarm ID is nt overwritten to the old value
115-
if (appServicePlan != null)
115+
if (appServicePlan != null && (string.IsNullOrEmpty(appServicePlanRg) && resourceGroupName != appServicePlanRg))
116116
{
117117
webSiteToUpdate.ServerFarmId = appServicePlan;
118118
}
@@ -163,6 +163,7 @@ public void AddCustomHostNames(string resourceGroupName, string location, string
163163
catch (Exception e)
164164
{
165165
WriteWarning("Could not set custom hostname '{0}'. Details: {1}", hostName, e.ToString());
166+
return;
166167
}
167168
}
168169

0 commit comments

Comments
 (0)