Skip to content

4 issues of Az.AppService #11189

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 1 commit into from
Feb 26, 2020
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
14 changes: 10 additions & 4 deletions src/Websites/Websites.Test/ScenarioTests/AppServicePlanTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ function Test-CreateNewAppServicePlan
$location = Get-Location
$capacity = 2
$skuName = "S2"

$tag= @{"TagKey" = "TagValue"}
try
{
#Setup
New-AzResourceGroup -Name $rgname -Location $location

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

Expand All @@ -40,7 +40,8 @@ function Test-CreateNewAppServicePlan
Assert-AreEqual "Standard" $createResult.Sku.Tier
Assert-AreEqual $skuName $createResult.Sku.Name
Assert-AreEqual $capacity $createResult.Sku.Capacity

Assert-AreEqual $tag.Keys $createResult.Tags.Keys
Assert-AreEqual $tag.Values $createResult.Tags.Values
# Assert

$getResult = Get-AzAppServicePlan -ResourceGroupName $rgname -Name $whpName
Expand Down Expand Up @@ -127,7 +128,7 @@ function Test-SetAppServicePlan
$newWorkerSize = "Medium"
$newCapacity = 2
$newPerSiteScaling = $true;

$tag= @{"TagKey" = "TagValue"}

try
{
Expand Down Expand Up @@ -173,6 +174,11 @@ function Test-SetAppServicePlan
Assert-AreEqual $skuName $newresult.Sku.Name
Assert-AreEqual $perSiteScaling $newresult.PerSiteScaling

#Set Tags
$tagsResult= Set-AzAppServicePlan -ResourceGroupName $rgname -Name $whpName -Tag $tag
# Assert
Assert-AreEqual $tag.Keys $tagsResult.Tags.Keys
Assert-AreEqual $tag.Values $tagsResult.Tags.Values
}
finally
{
Expand Down
47 changes: 47 additions & 0 deletions src/Websites/Websites.Test/ScenarioTests/WebAppTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1064,15 +1064,18 @@ function Test-SetWebApp
{
# Setup
$rgname = Get-ResourceGroupName
$rgname1 = Get-ResourceGroupName
$webAppName = Get-WebsiteName
$location = Get-WebLocation
$appServicePlanName1 = Get-WebHostPlanName
$appServicePlanName2 = Get-WebHostPlanName
$appServicePlanName3 = Get-WebHostPlanName
$tier1 = "Shared"
$tier2 = "Standard"
$apiversion = "2015-08-01"
$resourceType = "Microsoft.Web/sites"
$capacity = 2
$HN="custom.domain.com"

try
{
Expand Down Expand Up @@ -1159,6 +1162,49 @@ function Test-SetWebApp
Assert-AreEqual "" $webApp.SiteConfig.PhpVersion
Assert-AreEqual "1.2" $webApp.SiteConfig.MinTlsVersion

# set Custom Host Name(s)- Failed Scenario
$oldWebApp= Get-AzWebApp -ResourceGroupName $rgname -Name $webAppName
$CurrentWebApp = Set-AzWebApp -ResourceGroupName $rgname -Name $webAppName -HostNames $HN
#Assert
$status
foreach($oldHN in $oldWebApp.HostNames)
{
Assert-True { $CurrentWebApp.HostNames -contains $oldHN }
}

#Set-AzWebApp errors on operations for App Services not in the same resource group as the App Service Plan
#setup
## Create a Resource Group.
New-AzResourceGroup -Name $rgname1 -Location $location

## Create the App Service Plan in $rgname.
$asp = New-AzAppServicePlan -Location $location -Tier Standard -NumberofWorkers 1 -WorkerSize Small -ResourceGroupName $rgname -Name $appServicePlanName3

## Create a Web App in each Resource Group.
$app1 = Get-WebsiteName
$app2 = Get-WebsiteName

New-AzWebApp -ResourceGroupName $rgname -Name $app1 -Location $location -AppServicePlan $asp.Id
New-AzWebApp -ResourceGroupName $rgname1 -Name $app2 -Location $location -AppServicePlan $asp.Id

## Get the two Web Apps.
$wa1 = Get-AzWebApp -ResourceGroupName $rgname -Name $app1
$wa2 = Get-AzWebApp -ResourceGroupName $rgname1 -Name $app2

## Change a setting on the first Web App (which is in the same Resource Group as the App Service Plan).
$currentWa1ClientAffinityEnabled=$wa1.ClientAffinityEnabled
$wa1.ClientAffinityEnabled = !$wa1.ClientAffinityEnabled
$wa1 | Set-AzWebApp

#Assert
Assert-AreNotEqual $currentWa1ClientAffinityEnabled $wa1.ClientAffinityEnabled
## Change a setting on the first Web App (which is in the same Resource Group as the App Service Plan).
$currentWa2ClientAffinityEnabled=$wa2.ClientAffinityEnabled
$wa2.ClientAffinityEnabled = !$wa2.ClientAffinityEnabled
$wa2 | Set-AzWebApp

#Assert
Assert-AreNotEqual $currentWa2ClientAffinityEnabled $wa2.ClientAffinityEnabled
}
finally
{
Expand All @@ -1167,6 +1213,7 @@ function Test-SetWebApp
Remove-AzAppServicePlan -ResourceGroupName $rgname -Name $appServicePlanName1 -Force
Remove-AzAppServicePlan -ResourceGroupName $rgname -Name $appServicePlanName2 -Force
Remove-AzResourceGroup -Name $rgname -Force
Remove-AzResourceGroup -Name $rgname1 -Force
}
}

Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/Websites/Websites/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
- Additional information about change #1
-->
## Upcoming Release
*Adding Tag parameter for New-AzAppServicePlan and Set-AzAppServicePlan.
*Stop cmdlt execution if an exception is thrown when adding a custom domain to a website.
*Adding support to perform operations for App Services not in the same resource group as the App Service Plan.
*Apply access restriction to WebApp/Function in different resource groups
*Fixing issue to set custom hostnames for WebAppSlots

## Version 1.6.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,14 @@ public override void ExecuteCmdlet()
case SubnetNameParameterSet:
case SubnetIdParameterSet:
var Subnet = ParameterSetName == SubnetNameParameterSet ? SubnetName : SubnetId;
var subnetResourceId = CmdletHelpers.ValidateSubnet(Subnet, VirtualNetworkName, ResourceGroupName, DefaultContext.Subscription.Id);
//Fetch RG of given SubNet
var subNetResourceGroupName = CmdletHelpers.GetSubnetResourceGroupName(DefaultContext, Subnet, VirtualNetworkName);
//If unble to fetch SubNet rg from above step, use the input RG to get validation error from api call.
subNetResourceGroupName = !String.IsNullOrEmpty(subNetResourceGroupName) ? subNetResourceGroupName : ResourceGroupName;
var subnetResourceId = CmdletHelpers.ValidateSubnet(Subnet, VirtualNetworkName, subNetResourceGroupName, DefaultContext.Subscription.Id);
if (!IgnoreMissingServiceEndpoint)
{
CmdletHelpers.VerifySubnetDelegation(DefaultContext, subnetResourceId);
CmdletHelpers.VerifySubnetDelegation(subnetResourceId);
}
foreach (var accessRestriction in accessRestrictionList)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
using Microsoft.Azure.Commands.WebApps.Utilities;
using Microsoft.Azure.Management.WebSites.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;

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

[Parameter(ParameterSetName = ParameterSet1Name, Mandatory = false, HelpMessage = "Tags are name/value pairs that enable you to categorize resources")]
public Hashtable Tag { get; set; }

public override void ExecuteCmdlet()
{
if (HyperV.IsPresent && Tier != "PremiumContainer")
Expand Down Expand Up @@ -107,7 +112,8 @@ public override void ExecuteCmdlet()
Location = Location,
Sku = sku,
PerSiteScaling = PerSiteScaling,
IsXenon = HyperV.IsPresent
IsXenon = HyperV.IsPresent,
Tags= (IDictionary<string, string>)CmdletHelpers.ConvertToStringDictionary(Tag)
};

AppServicePlan retPlan = WebsitesClient.CreateOrUpdateAppServicePlan(ResourceGroupName, Name, appServicePlan, AseName, aseResourceGroupName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
using System.Management.Automation;
using System.Text.RegularExpressions;
using Microsoft.Azure.Commands.WebApps.Models.WebApp;
using System.Collections;
using System.Collections.Generic;

namespace Microsoft.Azure.Commands.WebApps.Cmdlets.AppServicePlans
{
Expand Down Expand Up @@ -48,7 +50,8 @@ public class SetAzureAppServicePlanCmdlet : AppServicePlanBaseCmdlet

[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
public SwitchParameter AsJob { get; set; }

[Parameter(ParameterSetName = ParameterSet1Name, Mandatory = false, HelpMessage = "Tags are name/value pairs that enable you to categorize resources")]
public Hashtable Tag { get; set; }
public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();
Expand All @@ -62,6 +65,7 @@ public override void ExecuteCmdlet()
int.TryParse(Regex.Match(AppServicePlan.Sku.Name, @"\d+").Value, out workerSizeAsNumber);
AppServicePlan.Sku.Name = string.IsNullOrWhiteSpace(WorkerSize) ? CmdletHelpers.GetSkuName(AppServicePlan.Sku.Tier, workerSizeAsNumber) : CmdletHelpers.GetSkuName(AppServicePlan.Sku.Tier, WorkerSize);
AppServicePlan.PerSiteScaling = PerSiteScaling;
AppServicePlan.Tags = (IDictionary<string, string>)CmdletHelpers.ConvertToStringDictionary(Tag);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Websites/Websites/Cmdlets/WebApps/SetAzureWebApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public override void ExecuteCmdlet()
CmdletHelpers.TryParseAppServicePlanMetadataFromResourceId(WebApp.ServerFarmId, out rg, out servicePlanName);
// 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
WebApp.AzureStoragePath = null;
WebsitesClient.UpdateWebApp(ResourceGroupName, location, Name, null, servicePlanName, WebApp);
WebsitesClient.UpdateWebApp(ResourceGroupName, location, Name, null, servicePlanName, WebApp,rg);
WebsitesClient.AddCustomHostNames(ResourceGroupName, location, Name, WebApp.HostNames.ToArray());
break;
}
Expand Down
25 changes: 23 additions & 2 deletions src/Websites/Websites/Utilities/CmdletHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ namespace Microsoft.Azure.Commands.WebApps.Utilities
{
public static class CmdletHelpers
{
public static NetworkManagementClient networkClient
{
get;
private set;
}
public static HashSet<string> SiteConfigParameters = new HashSet<string>
{
"DefaultDocuments",
Expand Down Expand Up @@ -559,14 +564,13 @@ internal static string ValidateSubnet(string subnet, string virtualNetworkName,
return subnetResourceId.ToString();
}

internal static void VerifySubnetDelegation(IAzureContext context, string subnet)
internal static void VerifySubnetDelegation(string subnet)
{
var subnetResourceId = new ResourceIdentifier(subnet);
var resourceGroupName = subnetResourceId.ResourceGroupName;
var virtualNetworkName = subnetResourceId.ParentResource.Substring(subnetResourceId.ParentResource.IndexOf('/') + 1);
var subnetName = subnetResourceId.ResourceName;

var networkClient = AzureSession.Instance.ClientFactory.CreateArmClient<NetworkManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager);
Subnet subnetObj = networkClient.Subnets.Get(resourceGroupName, virtualNetworkName, subnetName);
var serviceEndpointServiceName = "Microsoft.Web";
var serviceEndpointLocations = new List<string>() { "*" };
Expand Down Expand Up @@ -594,5 +598,22 @@ internal static void VerifySubnetDelegation(IAzureContext context, string subnet
}
}
}

internal static string GetSubnetResourceGroupName(IAzureContext context, string Subnet, string VirtualNetworkName)
{
networkClient = AzureSession.Instance.ClientFactory.CreateArmClient<NetworkManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager);
var matchedVNetwork = networkClient.VirtualNetworks.ListAll().FirstOrDefault(item => item.Name == VirtualNetworkName);
if (matchedVNetwork != null)
{
var subNets = matchedVNetwork.Subnets.ToList();
Subnet matchedSubnet = matchedVNetwork.Subnets.FirstOrDefault(sItem => sItem.Name == Subnet || sItem.Id == Subnet);
if (matchedSubnet != null)
{
var subnetResourceId = new ResourceIdentifier(matchedSubnet.Id);
return subnetResourceId.ResourceGroupName;
}
}
return null;
}
}
}
7 changes: 4 additions & 3 deletions src/Websites/Websites/Utilities/WebsitesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ public HostingEnvironmentProfile CreateHostingEnvironmentProfile(string resource
return CmdletHelpers.CreateHostingEnvironmentProfile(WrappedWebsitesClient.SubscriptionId, resourceGroupName, aseResourceGroupName, aseName);
}

public void UpdateWebApp(string resourceGroupName, string location, string webAppName, string slotName, string appServicePlan, Site siteEnvelope =null)
public void UpdateWebApp(string resourceGroupName, string location, string webAppName, string slotName, string appServicePlan, Site siteEnvelope =null, string appServicePlanRg = null)
{
var webSiteToUpdate = new Site()
{
ServerFarmId = appServicePlan,
ServerFarmId = (string.IsNullOrEmpty(appServicePlanRg) && resourceGroupName != appServicePlanRg) ? appServicePlan : siteEnvelope.ServerFarmId,
Location = location,
Tags = siteEnvelope?.Tags
};
Expand All @@ -112,7 +112,7 @@ public void UpdateWebApp(string resourceGroupName, string location, string webAp
}

// make sure the serverfarm ID is nt overwritten to the old value
if (appServicePlan != null)
if (appServicePlan != null && (string.IsNullOrEmpty(appServicePlanRg) && resourceGroupName != appServicePlanRg))
{
webSiteToUpdate.ServerFarmId = appServicePlan;
}
Expand Down Expand Up @@ -163,6 +163,7 @@ public void AddCustomHostNames(string resourceGroupName, string location, string
catch (Exception e)
{
WriteWarning("Could not set custom hostname '{0}'. Details: {1}", hostName, e.ToString());
return;
}
}

Expand Down