Skip to content

Commit c97d6dc

Browse files
feat: Add script to cancel running instances for a Logic App (#376)
* initial commit * add unit tests * added integration test * log run names, added integration test * added documentation * Apply suggestions from code review Co-authored-by: Stijn Moreels <9039753+stijnmoreels@users.noreply.github.com> * include `Set-AzLogicApp` in try-catch --------- Co-authored-by: Pim Simons <pim.simons@codit.eu> Co-authored-by: Stijn Moreels <9039753+stijnmoreels@users.noreply.github.com>
1 parent 281dd0c commit c97d6dc

7 files changed

Lines changed: 177 additions & 2 deletions

File tree

docs/preview/03-Features/powershell/azure-logic-apps.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,30 @@ To have access to the following features, you have to import the module:
1313
PS> Install-Module -Name Arcus.Scripting.LogicApps
1414
```
1515

16+
## Cancel running instances for an Azure Logic App
17+
18+
Use this script to cancel all running instances for a specific Azure Logic App.
19+
20+
| Parameter | Mandatory | Description |
21+
| ------------------- | --------- | -------------------------------------------------- |
22+
| `ResourceGroupName` | yes | The resource group containing the Azure Logic App. |
23+
| `LogicAppName` | yes | The name of the Azure Logic App to be disabled. |
24+
25+
**Example**
26+
27+
Taking an example in which a specific Azure Logic App (`"rcv-shopping-order-sftp"`) needs to have all its runs cancelled.
28+
29+
```powershell
30+
PS> Cancel-AzLogicAppRuns `
31+
-ResourceGroupName "rg-common-dev" `
32+
-LogicAppName "rcv-shopping-order-sftp"
33+
# Successfully cancelled all running instances for the Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev'
34+
```
35+
36+
1637
## Disable an Azure Logic App
1738

18-
Using this script to enable a specific Azure Logic App.
39+
Use this script to enable a specific Azure Logic App.
1940

2041
| Parameter | Mandatory | Description |
2142
| ----------------- | --------- | ------------------------------------------------------------------------------------------------------------------- |
@@ -52,7 +73,7 @@ PS> Disable-AzLogicApp `
5273

5374
## Enable an Azure Logic App
5475

55-
Using this script to enable a specific Azure Logic App.
76+
Use this script to enable a specific Azure Logic App.
5677

5778
| Parameter | Mandatory | Description |
5879
| ----------------- | --------- | ------------------------------------------------------------------------------------------------------------------- |
60 Bytes
Binary file not shown.

src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psm1

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
<#
2+
.Synopsis
3+
Cancel all running instances of a specific Logic App.
4+
5+
.Description
6+
Cancel all running instances of a specific Logic App.
7+
8+
.Parameter ResourceGroupName
9+
The resource group containing the Azure Logic App.
10+
11+
.Parameter LogicAppName
12+
The name of the Azure Logic App.
13+
14+
#>
15+
function Cancel-AzLogicAppRuns {
16+
param(
17+
[Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of the resource group is required"),
18+
[Parameter(Mandatory = $true)][string] $LogicAppName = $(throw "Name of the logic app is required")
19+
)
20+
21+
. $PSScriptRoot\Scripts\Cancel-AzLogicAppRuns.ps1 -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName
22+
}
23+
24+
Export-ModuleMember -Function Cancel-AzLogicAppRuns
25+
126
<#
227
.Synopsis
328
Disable a specific Logic App.

src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.pssproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<ItemGroup>
3232
<Compile Include="Arcus.Scripting.LogicApps.psd1" />
3333
<Compile Include="Arcus.Scripting.LogicApps.psm1" />
34+
<Compile Include="Scripts\Cancel-AzLogicAppRuns.ps1" />
3435
<Compile Include="Scripts\Disable-AzLogicApp.ps1" />
3536
<Compile Include="Scripts\Disable-AzLogicAppsFromConfig.ps1" />
3637
<Compile Include="Scripts\Enable-AzLogicApp.ps1" />
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
param(
2+
[Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of the resource group is required"),
3+
[Parameter(Mandatory = $true)][string] $LogicAppName = $(throw "Name of the logic app is required")
4+
)
5+
6+
try{
7+
$runs = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $LogicAppName |
8+
Where-Object {$_.Status -eq 'Running'}
9+
10+
foreach ($run in $runs) {
11+
Stop-AzLogicAppRun -ResourceGroupName $ResourceGroupName -Name $LogicAppName -RunName $run.Name -Force
12+
Write-Verbose "Cancelled run $run.Name for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'"
13+
}
14+
15+
Write-Host "Successfully cancelled all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" -ForegroundColor Green
16+
} catch {
17+
throw "Failed to cancel all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: $($_.Exception.Message)"
18+
}

src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.LogicApps.tests.ps1

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,68 @@ InModuleScope Arcus.Scripting.LogicApps {
164164
}
165165
}
166166
}
167+
Context "Cancel Logic Apps runs" {
168+
It "Cancel all running instances for a Logic App"{
169+
# Arrange
170+
$resourceGroupName = $config.Arcus.ResourceGroupName
171+
$logicAppName = Create-AzLogicAppName
172+
$workflowDefinition = '{
173+
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
174+
"actions": {
175+
"Delay": {
176+
"inputs": {
177+
"interval": {
178+
"count": 5,
179+
"unit": "Minute"
180+
}
181+
},
182+
"runAfter": {},
183+
"type": "wait"
184+
}
185+
},
186+
"outputs": {},
187+
"parameters": {},
188+
"triggers": {
189+
"Recurrence": {
190+
"recurrence": {
191+
"frequency": "Second",
192+
"interval": 1
193+
},
194+
"type": "recurrence"
195+
}
196+
},
197+
"contentVersion": "1.0.0.0"
198+
}'
199+
200+
New-AzLogicApp `
201+
-ResourceGroupName $resourceGroupName `
202+
-Location westeurope `
203+
-Name $logicAppName `
204+
-Definition $workflowDefinition `
205+
-State Enabled
206+
207+
Start-Sleep -Seconds 5
208+
209+
try {
210+
Set-AzLogicApp `
211+
-ResourceGroupName $resourceGroupName `
212+
-Name $logicAppName `
213+
-State Disabled `
214+
-Force
215+
216+
# Act
217+
Cancel-AzLogicAppRuns -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName
218+
219+
# Assert
220+
$runs = Get-AzLogicAppRunHistory -ResourceGroupName $resourceGroupName -Name $logicAppName |
221+
Where-Object {$_.Status -eq 'Cancelled'} | measure
222+
223+
$runs.Count | Should -BeGreaterThan 0
224+
225+
} finally {
226+
Remove-AzLogicApp -ResourceGroupName $resourceGroupName -Name $logicAppName -Force
227+
}
228+
}
229+
}
167230
}
168231
}

src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,5 +285,52 @@ InModuleScope Arcus.Scripting.LogicApps {
285285
Assert-MockCalled Disable-AzLogicApp -Scope It -Exactly 1 -ParameterFilter { $ResourceGroupName -eq $resourceGroup -and $LogicAppName -eq "snd-async" }
286286
}
287287
}
288+
Context "Cancel Logic Apps runs" {
289+
It "Cancelling all runs from Logic App history should succeed" {
290+
# Arrange
291+
$resourceGroupName = "codit-arcus-scripting"
292+
$logicAppName = "arc-dev-we-rcv-unknown-http"
293+
294+
Mock Get-AzLogicAppRunHistory -MockWith {
295+
return @{
296+
Name = "test"
297+
Status = "Running"
298+
}
299+
}
300+
301+
Mock Stop-AzLogicAppRun -MockWith {
302+
return $null
303+
}
304+
305+
# Act
306+
{ Cancel-AzLogicAppRuns -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName } |
307+
Should -Not -Throw
308+
309+
# Assert
310+
Assert-VerifiableMock
311+
Assert-MockCalled Get-AzLogicAppRunHistory -Scope It -Times 1
312+
Assert-MockCalled Stop-AzLogicAppRun -Scope It -Times 1
313+
}
314+
It "Cancelling all runs should fail when retrieving Logic App history fails" {
315+
# Arrange
316+
$resourceGroupName = "codit-arcus-scripting"
317+
$logicAppName = "arc-dev-we-rcv-unknown-http"
318+
319+
Mock Get-AzLogicAppRunHistory { throw 'some error' }
320+
321+
Mock Stop-AzLogicAppRun -MockWith {
322+
return $null
323+
}
324+
325+
# Act
326+
{ Cancel-AzLogicAppRuns -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName } |
327+
Should -Throw -ExpectedMessage "Failed to cancel all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: some error"
328+
329+
# Assert
330+
Assert-VerifiableMock
331+
Assert-MockCalled Get-AzLogicAppRunHistory -Scope It -Times 1
332+
Assert-MockCalled Stop-AzLogicAppRun -Scope It -Times 0
333+
}
334+
}
288335
}
289336
}

0 commit comments

Comments
 (0)