Skip to content

Commit 8440e75

Browse files
authored
Merge pull request #10324 from hytao/hata1017
Add UrlRewriteAction and CacheKeyQueryStringAction to CDN rulesEngine
2 parents b89b63b + 0b55a02 commit 8440e75

18 files changed

+2498
-36
lines changed

src/Cdn/Cdn.Test/Cdn.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.Azure.Management.Cdn" Version="4.2.2-preview" />
14+
<PackageReference Include="Microsoft.Azure.Management.Cdn" Version="4.2.4-preview" />
1515
</ItemGroup>
1616

1717
</Project>

src/Cdn/Cdn.Test/ScenarioTests/EndpointTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ public void TestEndpointCrudAndAction()
3535
TestController.NewInstance.RunPowerShellTest(_logger, "Test-EndpointCrudAndAction");
3636
}
3737

38+
[Fact]
39+
[Trait(Category.AcceptanceType, Category.CheckIn)]
40+
public void TestEndpointCreateWithRulesEngine()
41+
{
42+
TestController.NewInstance.RunPowerShellTest(_logger, "Test-EndpointCreateWithRulesEngine");
43+
}
44+
3845
[Fact]
3946
[Trait(Category.AcceptanceType, Category.CheckIn)]
4047
public void TestEndpointCrudAndActionWithPiping()

src/Cdn/Cdn.Test/ScenarioTests/EndpointTests.ps1

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,53 @@ function Test-EndpointCrudAndAction
7070
Remove-AzResourceGroup -Name $resourceGroup.ResourceGroupName -Force
7171
}
7272

73+
<#
74+
.SYNOPSIS
75+
Create ENdpoint with RulesEngine config
76+
#>
77+
function Test-EndpointCreateWithRulesEngine
78+
{
79+
$profileName = getAssetName
80+
$resourceGroup = TestSetup-CreateResourceGroup
81+
$resourceLocation = "EastUS"
82+
$profileSku = "Standard_Microsoft"
83+
$createdProfile = New-AzCdnProfile -ProfileName $profileName -ResourceGroupName $resourceGroup.ResourceGroupName -Location $resourceLocation -Sku $profileSku
84+
85+
$endpointName = getAssetName
86+
$originName = getAssetName
87+
$originHostName = "www.microsoft.com"
88+
89+
$nameAvailability = Get-AzCdnEndpointNameAvailability -EndpointName $endpointName
90+
Assert-True{$nameAvailability.NameAvailable}
91+
92+
$description = 'Sample delivery policy'
93+
$cond1 = New-AzCdnDeliveryRuleCondition -MatchVariable IsDevice -Operator Equal -MatchValue "Desktop"
94+
$action1 = New-AzCdnDeliveryRuleAction -SourcePattern "/abc" -Destination "/def" -PreservePath
95+
$action2 = New-AzCdnDeliveryRuleAction -QueryStringBehavior ExcludeAll -QueryParameter "abc","def"
96+
$action3 = New-AzCdnDeliveryRuleAction -QueryStringBehavior IncludeAll
97+
$redirect = New-AzCdnDeliveryRuleAction -RedirectType Found -DestinationProtocol MatchRequest
98+
$rule0 = New-AzCdnDeliveryRule -Name "EmptyCondition" -Order 0 -Action $redirect,$action3
99+
$rule1 = New-AzCdnDeliveryRule -Name "Rule1" -Order 1 -Condition $cond1 -Action $action1,$action2
100+
$deliverypolicy = New-AzCdnDeliveryPolicy -Description $description -Rule $rule0,$rule1
101+
102+
$createdEndpoint = New-AzCdnEndpoint -EndpointName $endpointName -ProfileName $profileName -ResourceGroupName $resourceGroup.ResourceGroupName -Location $resourceLocation -OriginName $originName -OriginHostName $originHostName -DeliveryPolicy $deliveryPolicy
103+
Assert-AreEqual $description $createdEndpoint.DeliveryPolicy.Description
104+
Assert-AreEqual $endpointName $createdEndpoint.Name
105+
Assert-AreEqual $deliverypolicy.Count $createdEndpoint.DeliveryPolicy.Count
106+
Assert-AreEqual $profileName $createdEndpoint.ProfileName
107+
Assert-AreEqual $resourceGroup.ResourceGroupName $createdEndpoint.ResourceGroupName
108+
Assert-AreEqual $originName $createdEndpoint.Origins[0].Name
109+
Assert-AreEqual $originHostName $createdEndpoint.Origins[0].HostName
110+
111+
$endpointRemoved = Remove-AzCdnEndpoint -EndpointName $endpointName -ProfileName $profileName -ResourceGroupName $resourceGroup.ResourceGroupName -PassThru -Force
112+
Assert-True{$endpointRemoved}
113+
114+
Assert-ThrowsContains { Get-AzCdnEndpoint -EndpointName $endpointName -ProfileName $profileName -ResourceGroupName $resourceGroup.ResourceGroupName } "NotFound"
115+
116+
Remove-AzResourceGroup -Name $resourceGroup.ResourceGroupName -Force
117+
}
118+
119+
73120
<#
74121
.SYNOPSIS
75122
Endpoint cycle with piping

src/Cdn/Cdn.Test/SessionRecords/Microsoft.Azure.Commands.Cdn.Test.ScenarioTests.ScenarioTest.EndpointTests/TestEndpointCreateWithRulesEngine.json

Lines changed: 2106 additions & 0 deletions
Large diffs are not rendered by default.

src/Cdn/Cdn/Cdn.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.Azure.Management.Cdn" Version="4.2.2-preview" />
14+
<PackageReference Include="Microsoft.Azure.Management.Cdn" Version="4.2.4-preview" />
1515
</ItemGroup>
1616

1717
</Project>

src/Cdn/Cdn/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
-->
2020

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

2325
## Version 1.3.1
2426
* Fixed miscellaneous typos across module

src/Cdn/Cdn/Common/AzureCdnCmdletBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class AzureCdnCmdletBase : AzureRMCmdlet
3636
public const string CacheExpirationActionParameterSet = "CacheExpirationActionParameterSet";
3737
public const string HeaderActionParameterSet = "HeaderActionParameterSet";
3838
public const string UrlRedirectActionParameterSet = "UrlRedirectActionParameterSet";
39+
public const string CacheKeyQueryStringActionParameterSet = "CacheKeyQueryStringActionParameterSet";
40+
public const string UrlRewriteActionParameterSet = "UrlRewriteActionParameterSet";
3941

4042

4143
/// <summary>

src/Cdn/Cdn/Endpoint/NewAzCdnDdliveryRuleCondition.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@ public class NewAzCdnDeliveryRuleCondition : AzureCdnCmdletBase
3434
[Parameter(Mandatory = true, HelpMessage = "Match variable of the condition.")]
3535
[ValidateNotNullOrEmpty]
3636
[PSArgumentCompleter("RemoteAddress", "RequestMethod", "QueryString", "PostArgs", "RequestUri",
37-
"RequestHeader", "RequestBody", "RequestScheme", "UrlPath", "UrlFileExtension", "UrlFileName", "IsDevice")]
37+
"RequestHeader", "RequestBody", "RequestScheme", "UrlPath", "UrlFileExtension", "UrlFileName", "IsDevice", "Cookies", "HttpVersion")]
3838
public string MatchVariable { get; set; }
3939

4040
[Parameter(Mandatory = true, HelpMessage = "Describes operator to be matched")]
4141
[ValidateNotNullOrEmpty]
4242
[PSArgumentCompleter("Any", "IPMatch", "GeoMatch", "Equal", "Contains","LessThan", "GreaterThan",
4343
"LessThanOrEqual", "GreaterThanOrEqual", "BeginsWith", "EndsWith", "Wildcard")]
4444
public string Operator { get; set; }
45+
46+
[Parameter(Mandatory = false, HelpMessage = "Name of Selector to be matched")]
47+
public string Selector { get; set; }
4548

4649
[Parameter(Mandatory = true, HelpMessage = "List of possible match values.")]
4750
public string[] MatchValue { get; set; }
@@ -59,6 +62,7 @@ public override void ExecuteCmdlet()
5962
{
6063
MatchVariable = MatchVariable,
6164
Operator = Operator,
65+
Selector = Selector,
6266
MatchValue = MatchValue,
6367
NegateCondition = NegateCondition,
6468
Transfroms = Transform == null? null : new List<string> { Transform }

src/Cdn/Cdn/Endpoint/NewAzCdnDeliveryRuleAction.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ public class NewAzCdnDeliveryRuleAction : AzureCdnCmdletBase
8383
[ValidateNotNullOrEmpty]
8484
public string CustomFragment { get; set; }
8585

86+
[Parameter(Mandatory = true, HelpMessage = "QueryString behavior for the requests", ParameterSetName = CacheKeyQueryStringActionParameterSet)]
87+
[PSArgumentCompleter("Include", "IncludeAll", "Exclude", "ExcludeAll")]
88+
[ValidateNotNullOrEmpty]
89+
public string QueryStringBehavior { get; set; }
90+
91+
[Parameter(Mandatory = false, HelpMessage = "Query parameters to include or exclude (comma separated).", ParameterSetName = CacheKeyQueryStringActionParameterSet)]
92+
public string[] QueryParameter { get; set; }
93+
94+
[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)]
95+
public string SourcePattern { get; set; }
96+
97+
[Parameter(Mandatory = true, HelpMessage = "Define the relative URL to which the above requests will be rewritten by.", ParameterSetName = UrlRewriteActionParameterSet)]
98+
public string Destination { get; set; }
99+
100+
[Parameter(Mandatory = false, HelpMessage = "Whether to preserve unmatched path.", ParameterSetName = UrlRewriteActionParameterSet)]
101+
public SwitchParameter PreservePath { get; set; }
86102

87103
public override void ExecuteCmdlet()
88104
{
@@ -120,6 +136,32 @@ public override void ExecuteCmdlet()
120136
CustomFragment = CustomFragment
121137
};
122138
}
139+
else if (ParameterSetName == CacheKeyQueryStringActionParameterSet)
140+
{
141+
if ((QueryStringBehavior == "Exclude" || QueryStringBehavior == "Include") && QueryParameter == null)
142+
{
143+
throw new PSArgumentException("QueryParameter cannot be empty when QueryStringBehavior is {0}", QueryStringBehavior);
144+
}
145+
else if (QueryStringBehavior == "ExcludeAll" || QueryStringBehavior == "IncludeAll")
146+
{
147+
QueryParameter = null;
148+
}
149+
150+
deliveryRuleAction = new PSDeliveryRuleCacheKeyQueryStringAction
151+
{
152+
QueryStringBehavior = QueryStringBehavior,
153+
QueryParameter = QueryParameter == null? string.Empty : string.Join(",", QueryParameter)
154+
};
155+
}
156+
else if (ParameterSetName == UrlRewriteActionParameterSet)
157+
{
158+
deliveryRuleAction = new PSDeliveryRuleUrlRewriteAction
159+
{
160+
SourcePattern = SourcePattern,
161+
Destination = Destination,
162+
PreservePath = PreservePath
163+
};
164+
}
123165
else
124166
{
125167
deliveryRuleAction = new PSDeliveryRuleAction();

src/Cdn/Cdn/Endpoint/SetAzureRmCdnEndpoint.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,34 @@ private void SetEndpoint()
4343
{
4444
var resourceGroupName = CdnEndpoint.ResourceGroupName;
4545
var profileName = CdnEndpoint.ProfileName;
46+
try
47+
{
48+
var endpoint = CdnManagementClient.Endpoints.Update(
49+
resourceGroupName,
50+
profileName,
51+
CdnEndpoint.Name,
52+
new EndpointUpdateParameters(
53+
CdnEndpoint.Tags.ToDictionaryTags(),
54+
CdnEndpoint.OriginHostHeader,
55+
CdnEndpoint.OriginPath,
56+
CdnEndpoint.ContentTypesToCompress.ToList(),
57+
CdnEndpoint.IsCompressionEnabled,
58+
CdnEndpoint.IsHttpAllowed,
59+
CdnEndpoint.IsHttpsAllowed,
60+
(QueryStringCachingBehavior)Enum.Parse(typeof(QueryStringCachingBehavior), CdnEndpoint.QueryStringCachingBehavior.ToString()),
61+
CdnEndpoint.OptimizationType,
62+
CdnEndpoint.ProbePath,
63+
CdnEndpoint.GeoFilters.Select(g => g.ToSdkGeoFilter()).ToList(),
64+
CdnEndpoint.DeliveryPolicy?.ToSdkDeliveryPolicy()));
4665

47-
var endpoint = CdnManagementClient.Endpoints.Update(
48-
resourceGroupName,
49-
profileName,
50-
CdnEndpoint.Name,
51-
new EndpointUpdateParameters(
52-
CdnEndpoint.Tags.ToDictionaryTags(),
53-
CdnEndpoint.OriginHostHeader,
54-
CdnEndpoint.OriginPath,
55-
CdnEndpoint.ContentTypesToCompress.ToList(),
56-
CdnEndpoint.IsCompressionEnabled,
57-
CdnEndpoint.IsHttpAllowed,
58-
CdnEndpoint.IsHttpsAllowed,
59-
(QueryStringCachingBehavior)Enum.Parse(typeof(QueryStringCachingBehavior), CdnEndpoint.QueryStringCachingBehavior.ToString()),
60-
CdnEndpoint.OptimizationType,
61-
CdnEndpoint.ProbePath,
62-
CdnEndpoint.GeoFilters.Select(g => g.ToSdkGeoFilter()).ToList(),
63-
CdnEndpoint.DeliveryPolicy?.ToSdkDeliveryPolicy()));
64-
65-
WriteVerbose(Resources.Success);
66-
WriteObject(endpoint.ToPsEndpoint());
66+
WriteVerbose(Resources.Success);
67+
WriteObject(endpoint.ToPsEndpoint());
68+
}
69+
catch (Microsoft.Azure.Management.Cdn.Models.ErrorResponseException e)
70+
{
71+
throw new PSArgumentException(string.Format("Error response received.Error Message: '{0}'",
72+
e.Response.Content));
73+
}
6774
}
6875
}
6976
}

src/Cdn/Cdn/Helpers/ModelExtensions.cs

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,29 @@ public static PSDeliveryRuleAction ToPsDeliveryRuleAction(this DeliveryRuleActio
202202
{
203203
return new PSDeliveryRuleUrlRedirectAction
204204
{
205-
RedirectType = urlRedirectAction.Parameters.RedirectType
205+
RedirectType = urlRedirectAction.Parameters.RedirectType,
206+
DestinationProtocol = urlRedirectAction.Parameters.DestinationProtocol,
207+
CustomHostname = urlRedirectAction.Parameters.CustomHostname,
208+
CustomPath = urlRedirectAction.Parameters.CustomPath,
209+
CustomFragment = urlRedirectAction.Parameters.CustomFragment,
210+
CustomQueryString = urlRedirectAction.Parameters.CustomQueryString
211+
};
212+
}
213+
else if (deliveryRuleAction is UrlRewriteAction urlRewriteAction)
214+
{
215+
return new PSDeliveryRuleUrlRewriteAction
216+
{
217+
SourcePattern = urlRewriteAction.Parameters.SourcePattern,
218+
Destination = urlRewriteAction.Parameters.Destination,
219+
PreservePath = urlRewriteAction.Parameters.PreserveUnmatchedPath ?? true
220+
};
221+
}
222+
else if (deliveryRuleAction is DeliveryRuleCacheKeyQueryStringAction deliveryRuleCacheKeyQueryStringAction)
223+
{
224+
return new PSDeliveryRuleCacheKeyQueryStringAction
225+
{
226+
QueryStringBehavior = deliveryRuleCacheKeyQueryStringAction.Parameters.QueryStringBehavior,
227+
QueryParameter = deliveryRuleCacheKeyQueryStringAction.Parameters.QueryParameters
206228
};
207229
}
208230
else
@@ -342,6 +364,26 @@ public static PSDeliveryRuleCondition ToPsDeliveryRuleCondition(this DeliveryRul
342364
MatchValue = deliveryRuleIsDeviceCondition.Parameters.MatchValues
343365
};
344366
}
367+
else if (deliveryRuleCondition is DeliveryRuleHttpVersionCondition deliveryRuleHttpVersionCondition)
368+
{
369+
return new PSDeliveryRuleCondition
370+
{
371+
MatchVariable = "HttpVersion",
372+
NegateCondition = deliveryRuleHttpVersionCondition.Parameters.NegateCondition,
373+
MatchValue = deliveryRuleHttpVersionCondition.Parameters.MatchValues
374+
};
375+
}
376+
else if (deliveryRuleCondition is DeliveryRuleCookiesCondition deliveryRuleCookiesCondition)
377+
{
378+
return new PSDeliveryRuleCondition
379+
{
380+
MatchVariable = "Cookies",
381+
NegateCondition = deliveryRuleCookiesCondition.Parameters.NegateCondition,
382+
Selector = deliveryRuleCookiesCondition.Parameters.Selector,
383+
MatchValue = deliveryRuleCookiesCondition.Parameters.MatchValues,
384+
Transfroms = deliveryRuleCookiesCondition.Parameters.Transforms
385+
};
386+
}
345387
else
346388
{
347389
return new PSDeliveryRuleCondition();
@@ -519,6 +561,27 @@ public static DeliveryRuleCondition ToDeliveryRuleCondition(
519561
Transforms = psDeliveryRuleCondition.Transfroms
520562
}
521563
};
564+
case "HttpVersion":
565+
return new DeliveryRuleHttpVersionCondition
566+
{
567+
Parameters = new HttpVersionMatchConditionParameters
568+
{
569+
MatchValues = psDeliveryRuleCondition.MatchValue,
570+
NegateCondition = psDeliveryRuleCondition.NegateCondition
571+
}
572+
};
573+
case "Cookies":
574+
return new DeliveryRuleCookiesCondition
575+
{
576+
Parameters = new CookiesMatchConditionParameters
577+
{
578+
MatchValues = psDeliveryRuleCondition.MatchValue,
579+
NegateCondition = psDeliveryRuleCondition.NegateCondition,
580+
OperatorProperty = psDeliveryRuleCondition.Operator,
581+
Selector = psDeliveryRuleCondition.Selector,
582+
Transforms = psDeliveryRuleCondition.Transfroms
583+
}
584+
};
522585
default:
523586
return new DeliveryRuleCondition();
524587
}
@@ -579,6 +642,29 @@ public static DeliveryRuleAction ToDeliveryRuleAction(this PSDeliveryRuleAction
579642
}
580643
};
581644
}
645+
else if (psDeliveryRuleAction is PSDeliveryRuleCacheKeyQueryStringAction psDeliveryRuleCacheKeyQueryStringAction)
646+
{
647+
return new DeliveryRuleCacheKeyQueryStringAction
648+
{
649+
Parameters = new CacheKeyQueryStringActionParameters
650+
{
651+
QueryStringBehavior = psDeliveryRuleCacheKeyQueryStringAction.QueryStringBehavior,
652+
QueryParameters = psDeliveryRuleCacheKeyQueryStringAction.QueryParameter
653+
}
654+
};
655+
}
656+
else if (psDeliveryRuleAction is PSDeliveryRuleUrlRewriteAction psDeliveryRuleUrlRewriteAction)
657+
{
658+
return new UrlRewriteAction
659+
{
660+
Parameters = new UrlRewriteActionParameters
661+
{
662+
SourcePattern = psDeliveryRuleUrlRewriteAction.SourcePattern,
663+
Destination = psDeliveryRuleUrlRewriteAction.Destination,
664+
PreserveUnmatchedPath = psDeliveryRuleUrlRewriteAction.PreservePath
665+
}
666+
};
667+
}
582668
return new DeliveryRuleAction();
583669
}
584670

@@ -589,7 +675,7 @@ public static DeliveryRule ToDeliveryRule(this PSDeliveryRule psDeliveryRule)
589675
Name = psDeliveryRule.Name,
590676
Order = psDeliveryRule.Order,
591677
Actions = psDeliveryRule.Actions.Select(action => action.ToDeliveryRuleAction()).ToList(),
592-
Conditions = psDeliveryRule.Conditions.Select(condition => condition.ToDeliveryRuleCondition()).ToList()
678+
Conditions = psDeliveryRule.Conditions?.Select(condition => condition.ToDeliveryRuleCondition()).ToList()
593679
};
594680
}
595681

@@ -758,12 +844,13 @@ public static void ValidateDeliveryRuleCondition(this PSDeliveryRuleCondition co
758844
condition.Operator != "LessThanOrEqual" && condition.Operator != "GreaterThan" && condition.Operator != "GreaterThanOrEqual")
759845
{
760846
throw new PSArgumentException(string.Format(
761-
"Invalid Operator {0} found for {1} match condition. Valid operators are IPMatch, Any, GeoMatch", condition.Operator, condition.MatchVariable
847+
"Invalid Operator {0} found for {1} match condition. Valid operators are Any, Equal, Contains, BeginsWith, EndsWith, LessThan, LessThanOrEqual, GreaterThan, GreaterThanOrEqual.", condition.Operator, condition.MatchVariable
762848
));
763849
}
764850
break;
765851
case "RequestHeader":
766852
case "PostArgs":
853+
case "Cookies":
767854
if (condition.Selector == null)
768855
{
769856
throw new PSArgumentException(string.Format(
@@ -782,6 +869,7 @@ public static void ValidateDeliveryRuleCondition(this PSDeliveryRuleCondition co
782869
case "RequestScheme":
783870
case "IsDevice":
784871
case "RequestMethod":
872+
case "HttpVersion":
785873
if (condition.Operator !="Equal")
786874
{
787875
throw new PSArgumentException(string.Format(

0 commit comments

Comments
 (0)