Skip to content

Commit

Permalink
added content purge script for afd
Browse files Browse the repository at this point in the history
  • Loading branch information
calumrees99 committed Jan 14, 2025
1 parent 24bc3d8 commit 0cfaca1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
52 changes: 52 additions & 0 deletions Infrastructure-Scripts/Invoke-AfdContentPurge.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<#
.SYNOPSIS
Purges the content from Azure Front Door (AFD)
.DESCRIPTION
Purges the content from Azure Front Door (AFD)
.PARAMETER AFDProfileResourceGroup
The Resource Group of the AFD
.PARAMETER AFDProfileName
The AFD Profile Name
.PARAMETER AFDEndPointName
The AFD EndPoint Name
.PARAMETER PurgeContent
The assest you wish to purge from the edge nodes
Single URL Purge: Purge individual asset by specifying the full URL, e.g., "/pictures/image1.png" or "/pictures/image1"
Wildcard purge: Purge all folders, sub-folders, and files under an endpoint with "/*" e.g. "/* " or "/pictures/*"
Root domain purge: Purge the root of the endpoint with "/" in the path
.EXAMPLE
Invoke-AfdContentPurge.ps1 -AFDProfileResourceGroup aResourceGroup -AFDProfileName aAfdProfile -AFDEndPointName aAfdEndpoint -PurgeContent "/*"
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[String]$AFDProfileResourceGroup,
[Parameter(Mandatory = $true)]
[String]$AFDProfileName,
[Parameter(Mandatory = $true)]
[String]$AFDEndPointName,
[Parameter(Mandatory = $true)]
[String]$PurgeContent
)
try {
if ( $PurgeContent.Trim() -eq "" ) {
throw "Purge Content blank will not run purge"
}
# --- Set AFD EndPoint
$AFDEndpoint = Get-AzFrontDoorCdnEndpoint -ResourceGroupName $AFDProfileResourceGroup -ProfileName $AFDProfileName -EndpointName $AFDEndpointName
if (!$AFDEndpoint) {
throw "AFD Endpoint does not exist"
}
# --- Purging AFD EndPoint
$AFDEndpoint | Clear-AzFrontDoorCdnEndpointContent -ContentPath $PurgeContent
}
catch {
throw "$_"
}

32 changes: 32 additions & 0 deletions Tests/UT.Invoke-AfdContentPurge.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
$Config = Get-Content $PSScriptRoot\..\Tests\Configuration\Unit.Tests.Config.json -Raw | ConvertFrom-Json
Set-Location $PSScriptRoot\..\Infrastructure-Scripts\

Describe "Invoke-AfdContentPurge Unit Tests" -Tags @("Unit") {

Context "Purge Content Parameter Blank" {
It "Should throw an exception to warn that no content purge will be run" {
{ ./Invoke-AfdContentPurge -AFDProfileResourceGroup $Config.resourceGroupName -AFDProfileName $Config.CdnProfileName -AFDEndPointName $Config.CDNEndPointName -PurgeContent $Config.blankPurge } | Should throw "Purge Content blank will not run purge"
}
}

Context "Resource Group or CDN Profile does not exist" {
It "Should throw an expection to warn that the the specified Resource Group or CDN Profile was not found in the subscription" {
Mock Get-AzFrontDoorCdnEndpoint -MockWith { Return $null }
{ ./Invoke-AfdContentPurge -AFDProfileResourceGroup $Config.resourceGroupName -AFDProfileName $Config.CdnProfileName -AFDEndPointName $Config.CDNEndPointName -PurgeContent $Config.purgeContent } | Should throw "AFD Endpoint does not exist"
Assert-MockCalled -CommandName 'Get-AzFrontDoorCdnEndpoint' -Times 1 -Scope It
}
}

Context "Parameters are ok" {
It "Should call Clear-AzFrontDoorCdnEndpointContent" {
Mock Get-AzFrontDoorCdnEndpoint -MockWith {
$cdnEndpointExists = [Microsoft.Azure.PowerShell.Cmdlets.Cdn.Models.Api20240201.Endpoint]::new()
return $cdnEndpointExists
}
Mock Clear-AzFrontDoorCdnEndpointContent -MockWith { Return $null }
{ ./Invoke-AfdContentPurge -AFDProfileResourceGroup $Config.resourceGroupName -AFDProfileName $Config.CdnProfileName -AFDEndPointName $Config.CDNEndPointName -PurgeContent $Config.purgeContent } | Should Not Throw
Assert-MockCalled -CommandName 'Get-AzFrontDoorCdnEndpoint' -Times 1 -Scope It
Assert-MockCalled -CommandName 'Clear-AzFrontDoorCdnEndpointContent' -Times 1 -Scope It
}
}
}
2 changes: 1 addition & 1 deletion Tests/UT.Invoke-CdnContentPurge.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Describe "Invoke-CdnContentPurge Unit Tests" -Tags @("Unit") {
}

Context "Resource Group or CDN Profile does not exist" {
It "The specified Resource Group or CDN Profile was not found in the subscription, throw an error" {
It "Should throw an expection to warn that the the specified Resource Group or CDN Profile was not found in the subscription" {
Mock Get-AzCdnEndpoint -MockWith { Return $null }
{ ./Invoke-CdnContentPurge -CDNProfileResourceGroup $Config.resourceGroupName -CDNProfileName $Config.CdnProfileName -CDNEndPointName $Config.CDNEndPointName -PurgeContent $Config.purgeContent } | Should throw "CDN Endpoint does not exist"
Assert-MockCalled -CommandName 'Get-AzCdnEndpoint' -Times 1 -Scope It
Expand Down

0 comments on commit 0cfaca1

Please sign in to comment.