Skip to content

Add UrlRewriteAction and CacheKeyQueryStringAction to CDN rulesEngine #10324

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 6 commits into from
Oct 24, 2019
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
2 changes: 1 addition & 1 deletion src/Cdn/Cdn.Test/Cdn.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.Cdn" Version="4.2.2-preview" />
<PackageReference Include="Microsoft.Azure.Management.Cdn" Version="4.2.4-preview" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions src/Cdn/Cdn.Test/ScenarioTests/EndpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public void TestEndpointCrudAndAction()
TestController.NewInstance.RunPowerShellTest(_logger, "Test-EndpointCrudAndAction");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestEndpointCreateWithRulesEngine()
{
TestController.NewInstance.RunPowerShellTest(_logger, "Test-EndpointCreateWithRulesEngine");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestEndpointCrudAndActionWithPiping()
Expand Down
47 changes: 47 additions & 0 deletions src/Cdn/Cdn.Test/ScenarioTests/EndpointTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,53 @@ function Test-EndpointCrudAndAction
Remove-AzResourceGroup -Name $resourceGroup.ResourceGroupName -Force
}

<#
.SYNOPSIS
Create ENdpoint with RulesEngine config
#>
function Test-EndpointCreateWithRulesEngine
{
$profileName = getAssetName
$resourceGroup = TestSetup-CreateResourceGroup
$resourceLocation = "EastUS"
$profileSku = "Standard_Microsoft"
$createdProfile = New-AzCdnProfile -ProfileName $profileName -ResourceGroupName $resourceGroup.ResourceGroupName -Location $resourceLocation -Sku $profileSku

$endpointName = getAssetName
$originName = getAssetName
$originHostName = "www.microsoft.com"

$nameAvailability = Get-AzCdnEndpointNameAvailability -EndpointName $endpointName
Assert-True{$nameAvailability.NameAvailable}

$description = 'Sample delivery policy'
$cond1 = New-AzCdnDeliveryRuleCondition -MatchVariable IsDevice -Operator Equal -MatchValue "Desktop"
$action1 = New-AzCdnDeliveryRuleAction -SourcePattern "/abc" -Destination "/def" -PreservePath
$action2 = New-AzCdnDeliveryRuleAction -QueryStringBehavior ExcludeAll -QueryParameter "abc","def"
$action3 = New-AzCdnDeliveryRuleAction -QueryStringBehavior IncludeAll
$redirect = New-AzCdnDeliveryRuleAction -RedirectType Found -DestinationProtocol MatchRequest
$rule0 = New-AzCdnDeliveryRule -Name "EmptyCondition" -Order 0 -Action $redirect,$action3
$rule1 = New-AzCdnDeliveryRule -Name "Rule1" -Order 1 -Condition $cond1 -Action $action1,$action2
$deliverypolicy = New-AzCdnDeliveryPolicy -Description $description -Rule $rule0,$rule1

$createdEndpoint = New-AzCdnEndpoint -EndpointName $endpointName -ProfileName $profileName -ResourceGroupName $resourceGroup.ResourceGroupName -Location $resourceLocation -OriginName $originName -OriginHostName $originHostName -DeliveryPolicy $deliveryPolicy
Assert-AreEqual $description $createdEndpoint.DeliveryPolicy.Description
Assert-AreEqual $endpointName $createdEndpoint.Name
Assert-AreEqual $deliverypolicy.Count $createdEndpoint.DeliveryPolicy.Count
Assert-AreEqual $profileName $createdEndpoint.ProfileName
Assert-AreEqual $resourceGroup.ResourceGroupName $createdEndpoint.ResourceGroupName
Assert-AreEqual $originName $createdEndpoint.Origins[0].Name
Assert-AreEqual $originHostName $createdEndpoint.Origins[0].HostName

$endpointRemoved = Remove-AzCdnEndpoint -EndpointName $endpointName -ProfileName $profileName -ResourceGroupName $resourceGroup.ResourceGroupName -PassThru -Force
Assert-True{$endpointRemoved}

Assert-ThrowsContains { Get-AzCdnEndpoint -EndpointName $endpointName -ProfileName $profileName -ResourceGroupName $resourceGroup.ResourceGroupName } "NotFound"

Remove-AzResourceGroup -Name $resourceGroup.ResourceGroupName -Force
}


<#
.SYNOPSIS
Endpoint cycle with piping
Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Cdn/Cdn/Cdn.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.Cdn" Version="4.2.2-preview" />
<PackageReference Include="Microsoft.Azure.Management.Cdn" Version="4.2.4-preview" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions src/Cdn/Cdn/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
-->

## Upcoming Release
* Introduced UrlRewriteAction and CacheKeyQueryStringAction to RulesEngine.
* Fixed several bugs like missing "Selector" Input in New-AzDeliveryRuleCondition cmdlet.

## Version 1.3.1
* Fixed miscellaneous typos across module
Expand Down
2 changes: 2 additions & 0 deletions src/Cdn/Cdn/Common/AzureCdnCmdletBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class AzureCdnCmdletBase : AzureRMCmdlet
public const string CacheExpirationActionParameterSet = "CacheExpirationActionParameterSet";
public const string HeaderActionParameterSet = "HeaderActionParameterSet";
public const string UrlRedirectActionParameterSet = "UrlRedirectActionParameterSet";
public const string CacheKeyQueryStringActionParameterSet = "CacheKeyQueryStringActionParameterSet";
public const string UrlRewriteActionParameterSet = "UrlRewriteActionParameterSet";


/// <summary>
Expand Down
6 changes: 5 additions & 1 deletion src/Cdn/Cdn/Endpoint/NewAzCdnDdliveryRuleCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ public class NewAzCdnDeliveryRuleCondition : AzureCdnCmdletBase
[Parameter(Mandatory = true, HelpMessage = "Match variable of the condition.")]
[ValidateNotNullOrEmpty]
[PSArgumentCompleter("RemoteAddress", "RequestMethod", "QueryString", "PostArgs", "RequestUri",
"RequestHeader", "RequestBody", "RequestScheme", "UrlPath", "UrlFileExtension", "UrlFileName", "IsDevice")]
"RequestHeader", "RequestBody", "RequestScheme", "UrlPath", "UrlFileExtension", "UrlFileName", "IsDevice", "Cookies", "HttpVersion")]
public string MatchVariable { get; set; }

[Parameter(Mandatory = true, HelpMessage = "Describes operator to be matched")]
[ValidateNotNullOrEmpty]
[PSArgumentCompleter("Any", "IPMatch", "GeoMatch", "Equal", "Contains","LessThan", "GreaterThan",
"LessThanOrEqual", "GreaterThanOrEqual", "BeginsWith", "EndsWith", "Wildcard")]
public string Operator { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Name of Selector to be matched")]
public string Selector { get; set; }

[Parameter(Mandatory = true, HelpMessage = "List of possible match values.")]
public string[] MatchValue { get; set; }
Expand All @@ -59,6 +62,7 @@ public override void ExecuteCmdlet()
{
MatchVariable = MatchVariable,
Operator = Operator,
Selector = Selector,
MatchValue = MatchValue,
NegateCondition = NegateCondition,
Transfroms = Transform == null? null : new List<string> { Transform }
Expand Down
42 changes: 42 additions & 0 deletions src/Cdn/Cdn/Endpoint/NewAzCdnDeliveryRuleAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ public class NewAzCdnDeliveryRuleAction : AzureCdnCmdletBase
[ValidateNotNullOrEmpty]
public string CustomFragment { get; set; }

[Parameter(Mandatory = true, HelpMessage = "QueryString behavior for the requests", ParameterSetName = CacheKeyQueryStringActionParameterSet)]
[PSArgumentCompleter("Include", "IncludeAll", "Exclude", "ExcludeAll")]
[ValidateNotNullOrEmpty]
public string QueryStringBehavior { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Query parameters to include or exclude (comma separated).", ParameterSetName = CacheKeyQueryStringActionParameterSet)]
public string[] QueryParameter { get; set; }

[Parameter(Mandatory = true, HelpMessage = "Define a request URI pattern that identifies the type of requests that may be rewritten. If value is blank, all strings are matched.", ParameterSetName = UrlRewriteActionParameterSet)]
public string SourcePattern { get; set; }

[Parameter(Mandatory = true, HelpMessage = "Define the relative URL to which the above requests will be rewritten by.", ParameterSetName = UrlRewriteActionParameterSet)]
public string Destination { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Whether to preserve unmatched path.", ParameterSetName = UrlRewriteActionParameterSet)]
public SwitchParameter PreservePath { get; set; }

public override void ExecuteCmdlet()
{
Expand Down Expand Up @@ -120,6 +136,32 @@ public override void ExecuteCmdlet()
CustomFragment = CustomFragment
};
}
else if (ParameterSetName == CacheKeyQueryStringActionParameterSet)
{
if ((QueryStringBehavior == "Exclude" || QueryStringBehavior == "Include") && QueryParameter == null)
{
throw new PSArgumentException("QueryParameter cannot be empty when QueryStringBehavior is {0}", QueryStringBehavior);
}
else if (QueryStringBehavior == "ExcludeAll" || QueryStringBehavior == "IncludeAll")
{
QueryParameter = null;
}

deliveryRuleAction = new PSDeliveryRuleCacheKeyQueryStringAction
{
QueryStringBehavior = QueryStringBehavior,
QueryParameter = QueryParameter == null? string.Empty : string.Join(",", QueryParameter)
};
}
else if (ParameterSetName == UrlRewriteActionParameterSet)
{
deliveryRuleAction = new PSDeliveryRuleUrlRewriteAction
{
SourcePattern = SourcePattern,
Destination = Destination,
PreservePath = PreservePath
};
}
else
{
deliveryRuleAction = new PSDeliveryRuleAction();
Expand Down
47 changes: 27 additions & 20 deletions src/Cdn/Cdn/Endpoint/SetAzureRmCdnEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,34 @@ private void SetEndpoint()
{
var resourceGroupName = CdnEndpoint.ResourceGroupName;
var profileName = CdnEndpoint.ProfileName;
try
{
var endpoint = CdnManagementClient.Endpoints.Update(
resourceGroupName,
profileName,
CdnEndpoint.Name,
new EndpointUpdateParameters(
CdnEndpoint.Tags.ToDictionaryTags(),
CdnEndpoint.OriginHostHeader,
CdnEndpoint.OriginPath,
CdnEndpoint.ContentTypesToCompress.ToList(),
CdnEndpoint.IsCompressionEnabled,
CdnEndpoint.IsHttpAllowed,
CdnEndpoint.IsHttpsAllowed,
(QueryStringCachingBehavior)Enum.Parse(typeof(QueryStringCachingBehavior), CdnEndpoint.QueryStringCachingBehavior.ToString()),
CdnEndpoint.OptimizationType,
CdnEndpoint.ProbePath,
CdnEndpoint.GeoFilters.Select(g => g.ToSdkGeoFilter()).ToList(),
CdnEndpoint.DeliveryPolicy?.ToSdkDeliveryPolicy()));

var endpoint = CdnManagementClient.Endpoints.Update(
resourceGroupName,
profileName,
CdnEndpoint.Name,
new EndpointUpdateParameters(
CdnEndpoint.Tags.ToDictionaryTags(),
CdnEndpoint.OriginHostHeader,
CdnEndpoint.OriginPath,
CdnEndpoint.ContentTypesToCompress.ToList(),
CdnEndpoint.IsCompressionEnabled,
CdnEndpoint.IsHttpAllowed,
CdnEndpoint.IsHttpsAllowed,
(QueryStringCachingBehavior)Enum.Parse(typeof(QueryStringCachingBehavior), CdnEndpoint.QueryStringCachingBehavior.ToString()),
CdnEndpoint.OptimizationType,
CdnEndpoint.ProbePath,
CdnEndpoint.GeoFilters.Select(g => g.ToSdkGeoFilter()).ToList(),
CdnEndpoint.DeliveryPolicy?.ToSdkDeliveryPolicy()));

WriteVerbose(Resources.Success);
WriteObject(endpoint.ToPsEndpoint());
WriteVerbose(Resources.Success);
WriteObject(endpoint.ToPsEndpoint());
}
catch (Microsoft.Azure.Management.Cdn.Models.ErrorResponseException e)
{
throw new PSArgumentException(string.Format("Error response received.Error Message: '{0}'",
e.Response.Content));
}
}
}
}
94 changes: 91 additions & 3 deletions src/Cdn/Cdn/Helpers/ModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,29 @@ public static PSDeliveryRuleAction ToPsDeliveryRuleAction(this DeliveryRuleActio
{
return new PSDeliveryRuleUrlRedirectAction
{
RedirectType = urlRedirectAction.Parameters.RedirectType
RedirectType = urlRedirectAction.Parameters.RedirectType,
DestinationProtocol = urlRedirectAction.Parameters.DestinationProtocol,
CustomHostname = urlRedirectAction.Parameters.CustomHostname,
CustomPath = urlRedirectAction.Parameters.CustomPath,
CustomFragment = urlRedirectAction.Parameters.CustomFragment,
CustomQueryString = urlRedirectAction.Parameters.CustomQueryString
};
}
else if (deliveryRuleAction is UrlRewriteAction urlRewriteAction)
{
return new PSDeliveryRuleUrlRewriteAction
{
SourcePattern = urlRewriteAction.Parameters.SourcePattern,
Destination = urlRewriteAction.Parameters.Destination,
PreservePath = urlRewriteAction.Parameters.PreserveUnmatchedPath ?? true
};
}
else if (deliveryRuleAction is DeliveryRuleCacheKeyQueryStringAction deliveryRuleCacheKeyQueryStringAction)
{
return new PSDeliveryRuleCacheKeyQueryStringAction
{
QueryStringBehavior = deliveryRuleCacheKeyQueryStringAction.Parameters.QueryStringBehavior,
QueryParameter = deliveryRuleCacheKeyQueryStringAction.Parameters.QueryParameters
};
}
else
Expand Down Expand Up @@ -342,6 +364,26 @@ public static PSDeliveryRuleCondition ToPsDeliveryRuleCondition(this DeliveryRul
MatchValue = deliveryRuleIsDeviceCondition.Parameters.MatchValues
};
}
else if (deliveryRuleCondition is DeliveryRuleHttpVersionCondition deliveryRuleHttpVersionCondition)
{
return new PSDeliveryRuleCondition
{
MatchVariable = "HttpVersion",
NegateCondition = deliveryRuleHttpVersionCondition.Parameters.NegateCondition,
MatchValue = deliveryRuleHttpVersionCondition.Parameters.MatchValues
};
}
else if (deliveryRuleCondition is DeliveryRuleCookiesCondition deliveryRuleCookiesCondition)
{
return new PSDeliveryRuleCondition
{
MatchVariable = "Cookies",
NegateCondition = deliveryRuleCookiesCondition.Parameters.NegateCondition,
Selector = deliveryRuleCookiesCondition.Parameters.Selector,
MatchValue = deliveryRuleCookiesCondition.Parameters.MatchValues,
Transfroms = deliveryRuleCookiesCondition.Parameters.Transforms
};
}
else
{
return new PSDeliveryRuleCondition();
Expand Down Expand Up @@ -519,6 +561,27 @@ public static DeliveryRuleCondition ToDeliveryRuleCondition(
Transforms = psDeliveryRuleCondition.Transfroms
}
};
case "HttpVersion":
return new DeliveryRuleHttpVersionCondition
{
Parameters = new HttpVersionMatchConditionParameters
{
MatchValues = psDeliveryRuleCondition.MatchValue,
NegateCondition = psDeliveryRuleCondition.NegateCondition
}
};
case "Cookies":
return new DeliveryRuleCookiesCondition
{
Parameters = new CookiesMatchConditionParameters
{
MatchValues = psDeliveryRuleCondition.MatchValue,
NegateCondition = psDeliveryRuleCondition.NegateCondition,
OperatorProperty = psDeliveryRuleCondition.Operator,
Selector = psDeliveryRuleCondition.Selector,
Transforms = psDeliveryRuleCondition.Transfroms
}
};
default:
return new DeliveryRuleCondition();
}
Expand Down Expand Up @@ -579,6 +642,29 @@ public static DeliveryRuleAction ToDeliveryRuleAction(this PSDeliveryRuleAction
}
};
}
else if (psDeliveryRuleAction is PSDeliveryRuleCacheKeyQueryStringAction psDeliveryRuleCacheKeyQueryStringAction)
{
return new DeliveryRuleCacheKeyQueryStringAction
{
Parameters = new CacheKeyQueryStringActionParameters
{
QueryStringBehavior = psDeliveryRuleCacheKeyQueryStringAction.QueryStringBehavior,
QueryParameters = psDeliveryRuleCacheKeyQueryStringAction.QueryParameter
}
};
}
else if (psDeliveryRuleAction is PSDeliveryRuleUrlRewriteAction psDeliveryRuleUrlRewriteAction)
{
return new UrlRewriteAction
{
Parameters = new UrlRewriteActionParameters
{
SourcePattern = psDeliveryRuleUrlRewriteAction.SourcePattern,
Destination = psDeliveryRuleUrlRewriteAction.Destination,
PreserveUnmatchedPath = psDeliveryRuleUrlRewriteAction.PreservePath
}
};
}
return new DeliveryRuleAction();
}

Expand All @@ -589,7 +675,7 @@ public static DeliveryRule ToDeliveryRule(this PSDeliveryRule psDeliveryRule)
Name = psDeliveryRule.Name,
Order = psDeliveryRule.Order,
Actions = psDeliveryRule.Actions.Select(action => action.ToDeliveryRuleAction()).ToList(),
Conditions = psDeliveryRule.Conditions.Select(condition => condition.ToDeliveryRuleCondition()).ToList()
Conditions = psDeliveryRule.Conditions?.Select(condition => condition.ToDeliveryRuleCondition()).ToList()
};
}

Expand Down Expand Up @@ -758,12 +844,13 @@ public static void ValidateDeliveryRuleCondition(this PSDeliveryRuleCondition co
condition.Operator != "LessThanOrEqual" && condition.Operator != "GreaterThan" && condition.Operator != "GreaterThanOrEqual")
{
throw new PSArgumentException(string.Format(
"Invalid Operator {0} found for {1} match condition. Valid operators are IPMatch, Any, GeoMatch", condition.Operator, condition.MatchVariable
"Invalid Operator {0} found for {1} match condition. Valid operators are Any, Equal, Contains, BeginsWith, EndsWith, LessThan, LessThanOrEqual, GreaterThan, GreaterThanOrEqual.", condition.Operator, condition.MatchVariable
));
}
break;
case "RequestHeader":
case "PostArgs":
case "Cookies":
if (condition.Selector == null)
{
throw new PSArgumentException(string.Format(
Expand All @@ -782,6 +869,7 @@ public static void ValidateDeliveryRuleCondition(this PSDeliveryRuleCondition co
case "RequestScheme":
case "IsDevice":
case "RequestMethod":
case "HttpVersion":
if (condition.Operator !="Equal")
{
throw new PSArgumentException(string.Format(
Expand Down
Loading