From 3013a73aad6967609e6f0bc8c4baf8481ee75f40 Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Fri, 20 Oct 2023 15:15:11 +0200 Subject: [PATCH 01/24] support Logic App Standard for Disable-AzLogicApp and Enable-AzLogicApp --- .../powershell/azure-logic-apps.md | 70 ++++++++++------ .../Arcus.Scripting.LogicApps.psd1 | Bin 8168 -> 8328 bytes .../Arcus.Scripting.LogicApps.psm1 | 6 +- .../Scripts/Disable-AzLogicApp.ps1 | 44 ++++++---- .../Scripts/Enable-AzLogicApp.ps1 | 44 ++++++---- .../Arcus.Scripting.LogicApps.tests.ps1 | 76 +++++++++++++++++- 6 files changed, 179 insertions(+), 61 deletions(-) diff --git a/docs/preview/03-Features/powershell/azure-logic-apps.md b/docs/preview/03-Features/powershell/azure-logic-apps.md index 7e1ea30a..63c4d1f1 100644 --- a/docs/preview/03-Features/powershell/azure-logic-apps.md +++ b/docs/preview/03-Features/powershell/azure-logic-apps.md @@ -73,20 +73,21 @@ PS> Resubmit-FailedAzLogicAppRuns ` Use this script to enable a specific Azure Logic App. -| Parameter | Mandatory | Description | -| ----------------- | --------- | ------------------------------------------------------------------------------------------------------------------- | -| EnvironmentName | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | -| SubscriptionId | no | The Id of the subscription containing the Azure Logic App. | -| | | When not provided, it will be retrieved from the current context (Get-AzContext). | -| ResourceGroupName | yes | The resource group containing the Azure Logic Apps. | -| LogicAppName | yes | The name of the Azure Logic App to be disabled. | -| ApiVersion | no | The version of the management API to be used. (default: `2016-06-01`) | -| AccessToken | no | The access token to be used to disable the Azure Logic App. | -| | | When not provided, it will be retrieved from the current context (Get-AzContext). | +| Parameter | Mandatory | Description | +| ----------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------- | +| EnvironmentName | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | +| SubscriptionId | no | The Id of the subscription containing the Azure Logic App. | +| | | When not provided, it will be retrieved from the current context (Get-AzContext). | +| ResourceGroupName | yes | The resource group containing the Azure Logic Apps. | +| LogicAppName | yes | The name of the Azure Logic App to be disabled. | +| WorkflowName | no | The name of the workflow that needs to be disabled, this only needs to be supplied when disabling a workflow in Logic App Standard. | +| ApiVersion | no | The version of the management API to be used. (default: `2016-06-01`) | +| AccessToken | no | The access token to be used to disable the Azure Logic App. | +| | | When not provided, it will be retrieved from the current context (Get-AzContext). | **Example** -Taking an example in which a specific Azure Logic Apps (`"rcv-shopping-order-sftp"`) needs to be disabled, without providing the subscriptionId or accesstoken. +Taking an example in which a specific Azure Logic App Consumption (`"rcv-shopping-order-sftp"`) needs to be disabled, without providing the subscriptionId or accesstoken. ```powershell PS> Disable-AzLogicApp ` @@ -95,7 +96,7 @@ PS> Disable-AzLogicApp ` # Successfully disabled Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' ``` -Taking an example in which a specific Azure Logic Apps (`"rcv-shopping-order-sftp"`) needs to be disabled, with providing the subscriptionId or accesstoken. +Taking an example in which a specific Azure Logic App Consumption (`"rcv-shopping-order-sftp"`) needs to be disabled, with providing the subscriptionId or accesstoken. ```powershell PS> Disable-AzLogicApp ` @@ -106,24 +107,35 @@ PS> Disable-AzLogicApp ` # Successfully disabled Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' ``` +Taking an example in which a specific workflow (`ProcessMessage`) in an Azure Logic App Standard (`"rcv-shopping-order-sftp"`) needs to be disabled, without providing the subscriptionId or accesstoken. + +```powershell +PS> Disable-AzLogicApp ` +-ResourceGroupName "rg-common-dev" ` +-LogicAppName "rcv-shopping-order-sftp" ` +-WorkflowName "ProcessMessage" +# Successfully disabled workflow 'ProcessMessage' in Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' +``` + ## Enable an Azure Logic App Use this script to enable a specific Azure Logic App. -| Parameter | Mandatory | Description | -| ----------------- | --------- | ------------------------------------------------------------------------------------------------------------------- | -| EnvironmentName | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | -| SubscriptionId | no | The Id of the subscription containing the Azure Logic App. | -| | | When not provided, it will be retrieved from the current context (Get-AzContext). | -| ResourceGroupName | yes | The resource group containing the Azure Logic Apps. | -| LogicAppName | yes | The name of the Azure Logic App to be enabled. | -| ApiVersion | no | The version of the management API to be used. (default: `2016-06-01`) | -| AccessToken | no | The access token to be used to enable the Azure Logic App. | -| | | When not provided, it will be retrieved from the current context (Get-AzContext). | +| Parameter | Mandatory | Description | +| ----------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------- | +| EnvironmentName | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | +| SubscriptionId | no | The Id of the subscription containing the Azure Logic App. | +| | | When not provided, it will be retrieved from the current context (Get-AzContext). | +| ResourceGroupName | yes | The resource group containing the Azure Logic Apps. | +| LogicAppName | yes | The name of the Azure Logic App to be enabled. | +| WorkflowName | no | The name of the workflow that needs to be disabled, this only needs to be supplied when disabling a workflow in Logic App Standard. | +| ApiVersion | no | The version of the management API to be used. (default: `2016-06-01`) | +| AccessToken | no | The access token to be used to enable the Azure Logic App. | +| | | When not provided, it will be retrieved from the current context (Get-AzContext). | **Example** -Taking an example in which a specific Azure Logic Apps (`"rcv-shopping-order-sftp"`) needs to be enabled, without providing the subscriptionId or accesstoken. +Taking an example in which a specific Azure Logic App Consumption (`"rcv-shopping-order-sftp"`) needs to be enabled, without providing the subscriptionId or accesstoken. ```powershell PS> Enable-AzLogicApp ` @@ -132,7 +144,7 @@ PS> Enable-AzLogicApp ` # Successfully enabled Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' ``` -Taking an example in which a specific Azure Logic Apps (`"rcv-shopping-order-sftp"`) needs to be enabled, with providing the subscriptionId or accesstoken. +Taking an example in which a specific Azure Logic App Consumption (`"rcv-shopping-order-sftp"`) needs to be enabled, with providing the subscriptionId or accesstoken. ```powershell PS> Enable-AzLogicApp ` @@ -143,6 +155,16 @@ PS> Enable-AzLogicApp ` # Successfully enabled Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' ``` +Taking an example in which a specific workflow (`ProcessMessage`) in an Azure Logic App Standard (`"rcv-shopping-order-sftp"`) needs to be enabled, without providing the subscriptionId or accesstoken. + +```powershell +PS> Enable-AzLogicApp ` +-ResourceGroupName "rg-common-dev" ` +-LogicAppName "rcv-shopping-order-sftp" ` +-WorkflowName "ProcessMessage" +# Successfully enabled workflow 'ProcessMessage' in Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' +``` + ## Disabling Azure Logic Apps from configuration file Typically done the first task of the release pipeline, right before the deployment of the Logic Apps, will disable all specified Logic Apps in a specific order. diff --git a/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psd1 b/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psd1 index 95a27c1238a96751ab0618aba7809e8eee22f70f..784cb1f94fb28e688b6b6db0423e4c32b843e1cd 100644 GIT binary patch delta 36 qcmaE1-{H7HjAwEcx1F#fLje#5Go&&UF_bZ6G9)vkZa&VfC;|Y+HwoAP delta 12 TcmeBhd||&qjA!#Uo=d_2BN7E2 diff --git a/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psm1 b/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psm1 index 3739a483..bec3b003 100644 --- a/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psm1 +++ b/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psm1 @@ -100,11 +100,12 @@ function Disable-AzLogicApp { [Parameter(Mandatory = $false)][string] $SubscriptionId = "", [Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of the resource group is required"), [Parameter(Mandatory = $true)][string] $LogicAppName = $(throw "Name of the logic app is required"), + [Parameter(Mandatory = $false)][string] $WorkflowName = "", [Parameter(Mandatory = $false)][string] $ApiVersion = "2016-06-01", [Parameter(Mandatory = $false)][string] $AccessToken = "" ) - . $PSScriptRoot\Scripts\Disable-AzLogicApp.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -ApiVersion $ApiVersion -AccessToken $AccessToken + . $PSScriptRoot\Scripts\Disable-AzLogicApp.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -ApiVersion $ApiVersion -AccessToken $AccessToken } Export-ModuleMember -Function Disable-AzLogicApp @@ -178,11 +179,12 @@ function Enable-AzLogicApp { [Parameter(Mandatory = $false)][string] $SubscriptionId = "", [Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of resource group is reqiured"), [Parameter(Mandatory = $true)][string] $LogicAppName = $(throw "Name of logic app is required"), + [Parameter(Mandatory = $false)][string] $WorkflowName = "", [Parameter(Mandatory = $false)][string] $ApiVersion = "2016-06-01", [Parameter(Mandatory = $false)][string] $AccessToken = "" ) - . $PSScriptRoot\Scripts\Enable-AzLogicApp.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -ApiVersion $ApiVersion -AccessToken $AccessToken + . $PSScriptRoot\Scripts\Enable-AzLogicApp.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -ApiVersion $ApiVersion -AccessToken $AccessToken } Export-ModuleMember -Function Enable-AzLogicApp diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 index e9e2f337..3f9315a7 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 @@ -3,35 +3,45 @@ param( [Parameter(Mandatory = $false)][string] $SubscriptionId = "", [Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of the resource group is required"), [Parameter(Mandatory = $true)][string] $LogicAppName = $(throw "Name of the logic app is required"), + [Parameter(Mandatory = $false)][string] $WorkflowName = "", [Parameter(Mandatory = $false)][string] $ApiVersion = "2016-06-01", [Parameter(Mandatory = $false)][string] $AccessToken = "" ) try{ - if($SubscriptionId -eq "" -or $AccessToken -eq ""){ - # Request accessToken in case the script contains records - $token = Get-AzCachedAccessToken + if($WorkflowName -eq "") { + if($SubscriptionId -eq "" -or $AccessToken -eq ""){ + # Request accessToken in case the script contains records + $token = Get-AzCachedAccessToken - $AccessToken = $token.AccessToken - $SubscriptionId = $token.SubscriptionId - } + $AccessToken = $token.AccessToken + $SubscriptionId = $token.SubscriptionId + } - $fullUrl = . $PSScriptRoot\Get-AzLogicAppResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -ApiVersion $ApiVersion -Action "disable" + $fullUrl = . $PSScriptRoot\Get-AzLogicAppResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -ApiVersion $ApiVersion -Action "disable" - Write-Verbose "Attempting to disable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'..." - $params = @{ - Method = 'Post' - Headers = @{ - 'authorization'="Bearer $AccessToken" + Write-Verbose "Attempting to disable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'..." + $params = @{ + Method = 'Post' + Headers = @{ + 'authorization'="Bearer $AccessToken" + } + URI = $fullUrl } - URI = $fullUrl - } - $web = Invoke-WebRequest @params -ErrorAction Stop - Write-Host "Successfully disabled Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" -ForegroundColor Green + $web = Invoke-WebRequest @params -ErrorAction Stop + Write-Host "Successfully disabled Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" -ForegroundColor Green + } else { + Set-AzAppServiceSetting -ResourceGroupName $ResourceGroupName -AppServiceName $LogicAppName -AppServiceSettingName "Workflows.$WorkflowName.FlowState" -AppServiceSettingValue "Disabled" + Write-Host "Successfully disabled workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" -ForegroundColor Green + } } catch { - Write-Warning "Failed to disable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + if($WorkflowName -eq "") { + Write-Warning "Failed to disable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + } else { + Write-Warning "Failed to disable workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + } $ErrorMessage = $_.Exception.Message Write-Debug "Error: $ErrorMessage" } \ No newline at end of file diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 index c16df2cf..2af97b7e 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 @@ -3,35 +3,45 @@ param( [Parameter(Mandatory = $false)][string] $SubscriptionId = "", [Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of resource group is reqiured"), [Parameter(Mandatory = $true)][string] $LogicAppName = $(throw "Name of logic app is required"), + [Parameter(Mandatory = $false)][string] $WorkflowName = "", [Parameter(Mandatory = $false)][string] $ApiVersion = "2016-06-01", [Parameter(Mandatory = $false)][string] $AccessToken = "" ) try{ - if($SubscriptionId -eq "" -or $AccessToken -eq ""){ - # Request accessToken in case the script contains records - $token = Get-AzCachedAccessToken + if($WorkflowName -eq "") { + if($SubscriptionId -eq "" -or $AccessToken -eq ""){ + # Request accessToken in case the script contains records + $token = Get-AzCachedAccessToken - $AccessToken = $token.AccessToken - $SubscriptionId = $token.SubscriptionId - } + $AccessToken = $token.AccessToken + $SubscriptionId = $token.SubscriptionId + } - $fullUrl = . $PSScriptRoot\Get-AzLogicAppResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -ApiVersion $ApiVersion -Action enable + $fullUrl = . $PSScriptRoot\Get-AzLogicAppResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -ApiVersion $ApiVersion -Action enable - Write-Verbose "Attempting to enable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'..." - $params = @{ - Method = 'Post' - Headers = @{ - 'authorization'="Bearer $AccessToken" + Write-Verbose "Attempting to enable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'..." + $params = @{ + Method = 'Post' + Headers = @{ + 'authorization'="Bearer $AccessToken" + } + URI = $fullUrl } - URI = $fullUrl - } - $web = Invoke-WebRequest @params -ErrorAction Stop - Write-Host "Successfully enabled Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" -ForegroundColor Green + $web = Invoke-WebRequest @params -ErrorAction Stop + Write-Host "Successfully enabled Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" -ForegroundColor Green + } else { + Set-AzAppServiceSetting -ResourceGroupName $ResourceGroupName -AppServiceName $LogicAppName -AppServiceSettingName "Workflows.$WorkflowName.FlowState" -AppServiceSettingValue "Enabled" + Write-Host "Successfully enabled workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" -ForegroundColor Green + } } catch { - Write-Warning "Failed to enable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + if($WorkflowName -eq "") { + Write-Warning "Failed to enable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + } else { + Write-Warning "Failed to enable workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + } $ErrorMessage = $_.Exception.Message Write-Debug "Error: $ErrorMessage" } diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 index 243e72ac..11a70702 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 @@ -2,7 +2,7 @@ Import-Module -Name $PSScriptRoot\..\Arcus.Scripting.Security -ErrorAction Stop Import-Module -Name $PSScriptRoot\..\Arcus.Scripting.LogicApps -ErrorAction Stop InModuleScope Arcus.Scripting.LogicApps { - Describe "Arcus Azure Logic Apps unit tests" { + Describe "Arcus Azure Logic Apps Consumption unit tests" { Context "Enable Logic Apps without configuration" { It "Fails to enable an unknown Azure Logic App" { # Arrange @@ -678,4 +678,78 @@ InModuleScope Arcus.Scripting.LogicApps { } } } + Describe "Arcus Azure Logic Apps Standard unit tests" { + Context "Enable Logic Apps without configuration" { + It "Fails to enable an unknown Azure Logic App" { + # Arrange + $resourceGroupName = "codit-arcus-scripting" + $logicAppName = "arc-dev-we-rcv-unknown-http" + $workflowName = "test" + Mock Write-Warning -MockWith { } + Mock Set-AzAppServiceSetting { + Throw 'Not found error' + } + + # Act + Enable-AzLogicApp -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -WorkflowName $workflowName + + # Assert + Assert-VerifiableMock + Assert-MockCalled Set-AzAppServiceSetting -Times 1 + Assert-MockCalled Write-Warning -Scope It -Times 1 -ParameterFilter { $Message -contains "Failed to enable workflow '$workflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" } + } + It "Enabling an Azure Logic App should succeed" { + # Arrange + $resourceGroupName = "codit-arcus-scripting" + $logicAppName = "arc-dev-we-rcv-unknown-http" + $workflowName = "test" + Mock Write-Host -MockWith { } + Mock Set-AzAppServiceSetting { } + + # Act + Enable-AzLogicApp -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -WorkflowName $workflowName + + # Assert + Assert-VerifiableMock + Assert-MockCalled Set-AzAppServiceSetting -Times 1 + Assert-MockCalled Write-Host -Scope It -Times 1 -ParameterFilter { $Message -contains "Successfully enabled workflow '$workflowName' in Azure Logic App '$logicAppName' in resource group '$resourceGroupName'" } + } + } + Context "Disable Logic Apps without configuration" { + It "Fails to disable an unknown Azure Logic App" { + # Arrange + $resourceGroupName = "codit-arcus-scripting" + $logicAppName = "arc-dev-we-rcv-unknown-http" + $workflowName = "test" + Mock Write-Warning -MockWith { } + Mock Set-AzAppServiceSetting { + Throw 'Not found error' + } + + # Act + Disable-AzLogicApp -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -WorkflowName $workflowName + + # Assert + Assert-VerifiableMock + Assert-MockCalled Set-AzAppServiceSetting -Times 1 + Assert-MockCalled Write-Warning -Scope It -Times 1 -ParameterFilter { $Message -contains "Failed to disable workflow '$workflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" } + } + It "Disabling an Azure Logic App should succeed" { + # Arrange + $resourceGroupName = "codit-arcus-scripting" + $logicAppName = "arc-dev-we-rcv-unknown-http" + $workflowName = "test" + Mock Write-Host -MockWith { } + Mock Set-AzAppServiceSetting { } + + # Act + Disable-AzLogicApp -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -WorkflowName $workflowName + + # Assert + Assert-VerifiableMock + Assert-MockCalled Set-AzAppServiceSetting -Times 1 + Assert-MockCalled Write-Host -Scope It -Times 1 -ParameterFilter { $Message -contains "Successfully disabled workflow '$workflowName' in Azure Logic App '$logicAppName' in resource group '$resourceGroupName'" } + } + } + } } \ No newline at end of file From b2e325b947d92fb9d657e06734673b4fda347e3a Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Fri, 20 Oct 2023 16:14:29 +0200 Subject: [PATCH 02/24] fix docs --- docs/preview/03-Features/powershell/azure-logic-apps.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/preview/03-Features/powershell/azure-logic-apps.md b/docs/preview/03-Features/powershell/azure-logic-apps.md index 63c4d1f1..deb071ce 100644 --- a/docs/preview/03-Features/powershell/azure-logic-apps.md +++ b/docs/preview/03-Features/powershell/azure-logic-apps.md @@ -174,7 +174,7 @@ The order of the Azure Logic Apps in the configuration file (bottom to top) defi | Parameter | Mandatory | Description | | ----------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | | ResourceGroupName | yes | The resource group containing the Azure Logic Apps. | -| DeployFileName | no | If your solution consists of multiple interfaces, you can specify the flow-specific name of the configuration file. | +| DeployFileName | yes | If your solution consists of multiple interfaces, you can specify the flow-specific name of the configuration file. | | ResourcePrefix | no | In case the Azure Logic Apps all start with the same prefix, you can specify this prefix through this parameter instead of updating the configuration-file. | | EnvironmentName | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | | ApiVersion | no | The version of the management API to be used. (default: `2016-06-01`) | @@ -187,7 +187,7 @@ The schema of this configuration file is a JSON structure of an array with the f | MaximumFollowNextPageLink | `integer` | This sets the amount of pages (30 runs per page) of the Logic App run history (if any) that are retrieved. If not supplied the default value is 10. | | CheckType | `enum` | `None`: don't perform any additional checks. | | | | `NoWaitingOrRunningRuns`: waits until there are no more waiting or running Logic App instances. | -| StopType | `enum` | `None`: don't disable to given Logic Apps. | +| StopType | `enum` | `None`: don't disable the given Logic Apps. | | | | `Immediate`: disable the given Logic Apps. | | LogicApps | `string array` | Set of Logic App names to disable. | @@ -321,7 +321,7 @@ The order of the Azure Logic Apps in the configuration file (top to bottom) defi | Parameter | Mandatory | Description | | ----------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | | ResourceGroupName | yes | The resource group containing the Azure Logic Apps. | -| DeployFileName | no | If your solution consists of multiple interfaces, you can specify the flow-specific name of the configuration file. | +| DeployFileName | yes | If your solution consists of multiple interfaces, you can specify the flow-specific name of the configuration file. | | ResourcePrefix | no | In case the Azure Logic Apps all start with the same prefix, you can specify this prefix through this parameter instead of updating the configuration-file. | | EnvironmentName | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | | ApiVersion | no | The version of the management API to be used. (default: `2016-06-01`) | @@ -332,7 +332,7 @@ The schema of this configuration file is a JSON structure of an array with the f | ----------- | --------------- | --------------------------------------------------------------------------------------------------------------------- | | Description | `string` | Description of Azure Logic App set to enable. | | CheckType | `enum` | _Not taken into account for enabling Logic Apps._ | -| StopType | `enum` | `None`: don't enable to given Logic Apps. | +| StopType | `enum` | `None`: don't enable the given Logic Apps. | | | | `Immediate`: enable the given Logic Apps. | | LogicApps | `string array` | Set of Logic App names to enable. | **Example** From 8c9f44a4bab051b776b09fa6b9529696d259048d Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Thu, 2 Nov 2023 17:10:16 +0100 Subject: [PATCH 03/24] add logic app standard support to `Disable-AzLogicAppsFromConfig` and `Enable-AzLogicAppsFromConfig` --- .../Arcus.Scripting.LogicApps.pssproj | 3 +- .../Scripts/Disable-AzLogicApp.ps1 | 2 +- .../Scripts/Disable-AzLogicAppsFromConfig.ps1 | 175 ++++++++++++------ .../Scripts/Enable-AzLogicApp.ps1 | 2 +- .../Scripts/Enable-AzLogicAppsFromConfig.ps1 | 51 +++-- ...icAppConsumptionResourceManagementUrl.ps1} | 0 ...zLogicAppStandardResourceManagementUrl.ps1 | 37 ++++ 7 files changed, 199 insertions(+), 71 deletions(-) rename src/Arcus.Scripting.LogicApps/Scripts/{Get-AzLogicAppResourceManagementUrl.ps1 => Get-AzLogicAppConsumptionResourceManagementUrl.ps1} (100%) create mode 100644 src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 diff --git a/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.pssproj b/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.pssproj index 65d2e31e..8f986aec 100644 --- a/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.pssproj +++ b/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.pssproj @@ -31,13 +31,14 @@ + - + diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 index 3f9315a7..b5f23e9c 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 @@ -18,7 +18,7 @@ try{ $SubscriptionId = $token.SubscriptionId } - $fullUrl = . $PSScriptRoot\Get-AzLogicAppResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -ApiVersion $ApiVersion -Action "disable" + $fullUrl = . $PSScriptRoot\Get-AzLogicAppConsumptionResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -ApiVersion $ApiVersion -Action "disable" Write-Verbose "Attempting to disable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'..." $params = @{ diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 index c3e50685..ea288662 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 @@ -14,22 +14,27 @@ function ExecuteStopType() { param ( [string][parameter(Mandatory = $true)]$ResourceGroupName, + [string][parameter(Mandatory = $false)]$LogicAppType, [string][parameter(Mandatory = $true)]$LogicAppName, + [string][parameter(Mandatory = $false)]$WorkflowName, [string][parameter(Mandatory = $true)]$stopType ) BEGIN { - Write-Verbose "Executing StopType '$stopType' for Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'..." - If ($stopType -Match "Immediate") { + Write-Verbose "Executing stopType '$($stopType)' for Logic App $($LogicAppType) '$($LogicAppName)' in resource group '$ResourceGroupName'" + if ($stopType -Match "Immediate") { try { - Disable-AzLogicApp -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -ApiVersion $ApiVersion -AccessToken $Global:accessToken - } - catch { - Write-Warning "Failed to disable Azure Logic App '$LogicAppName'" + Disable-AzLogicApp -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -ApiVersion $ApiVersion -AccessToken $Global:accessToken + } catch { + if ($LogicAppType -match "Standard") { + Write-Warning "Failed to disable workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + } else { + Write-Warning "Failed to disable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + } $ErrorMessage = $_.Exception.Message Write-Debug "Error: $ErrorMessage" - } + } } - ElseIf ($stopType -Match "None") { + elseIf ($stopType -Match "None") { Write-Host "Executing Stop 'None' => performing no stop" } else { @@ -53,59 +58,123 @@ function ExecuteCheckType() { } if ($batch.checkType -Match "NoWaitingOrRunningRuns") { Write-Host "Executing Check 'NoWaitingOrRunningRuns'" - if ($batch.logicApps.Length -gt 0 ) { - $batch.logicApps | ForEach-Object { - $logicApp = $_; - if($ResourcePrefix.Length -gt 0){ - $logicApp = "$ResourcePrefix$_" - } + if ($batch.logicAppType -match "Standard") { + if ($batch.logicApps.Length -gt 0 ) { + $batch.logicApps | ForEach-Object { + $LogicAppName = $_.name; - try { - $runHistory = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $logicApp -FollowNextPageLink -MaximumFollowNextPageLink $maximumFollowNextPageLink -ErrorAction Stop - $RunningRunsCount = ($runHistory | Where-Object { $_.Status -eq "Running" }).Count - $WaitingRunsCount = ($runHistory | Where-Object { $_.Status -eq "Waiting" }).Count - if ($RunningRunsCount -ne 0 -and $WaitingRunsCount -ne 0) { - while ($RunningRunsCount -ne 0 -and $WaitingRunsCount -ne 0) { - Write-Verbose "Azure Logic App '$logicApp' has Running and/or Waiting Runs, waiting 10 seconds and checking again..." - Write-Debug "Number of running runs: $RunningRunsCount" - Write-Debug "Number of waiting runs: $WaitingRunsCount" - Start-Sleep -Second 10 - $runHistory = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $logicApp -FollowNextPageLink -MaximumFollowNextPageLink $maximumFollowNextPageLink -ErrorAction Stop - $RunningRunsCount = ($runHistory | Where-Object { $_.Status -eq "Running" }).Count - $WaitingRunsCount = ($runHistory | Where-Object { $_.Status -eq "Waiting" }).Count - if ($RunningRunsCount -eq 0 -and $WaitingRunsCount -eq 0) { - Write-Verbose "Found no more waiting or running runs for Azure Logic App '$logicApp', executing stopType for Logic App" - ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppName $logicApp -stopType $batch.stopType + if ($_.workflows.Length -gt 0 ) { + $_.workflows | ForEach-Object { + $WorkflowName = $_; + $fullUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName + $params = @{ + Method = 'Get' + Headers = @{ + 'authorization'="Bearer $Global:accessToken" + } + URI = $fullUrl } + + $runHistory = Invoke-WebRequest @params -ErrorAction Stop + $runHistoryContent = $runHistory.Content | ConvertFrom-Json + $runningRunsCount = ($runHistoryContent.value | Where-Object { $_.properties.status -eq "Running" }).Count + $waitingRunsCount = ($runHistoryContent.value | Where-Object { $_.properties.status -eq "Waiting" }).Count + + if ($runningRunsCount -ne 0 -or $waitingRunsCount -ne 0) { + while ($runningRunsCount -ne 0 -or $waitingRunsCount -ne 0) { + Write-Verbose "Workflow '$WorkflowName' in Azure Logic App '$LogicAppName' has Running and/or Waiting Runs, waiting 10 seconds and checking again..." + Write-Debug "Number of running runs: $runningRunsCount" + Write-Debug "Number of waiting runs: $waitingRunsCount" + Start-Sleep -Second 10 + $runHistory = Invoke-WebRequest @params -ErrorAction Stop + $runHistoryContent = $runHistory.Content | ConvertFrom-Json + $runningRunsCount = ($runHistoryContent.value | Where-Object { $_.properties.status -eq "Running" }).Count + $waitingRunsCount = ($runHistoryContent.value | Where-Object { $_.properties.status -eq "Waiting" }).Count + + if ($runningRunsCount -eq 0 -and $waitingRunsCount -eq 0) { + Write-Verbose "Found no more waiting or running runs for Workflow '$WorkflowName' in Azure Logic App '$LogicAppName', executing stopType for Logic App Workflow" + ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppType $batch.logicAppType -LogicAppName $LogicAppName -WorkflowName $WorkflowName -stopType $batch.stopType + } + } + } else { + Write-Verbose "Found no more waiting or running runs for Workflow '$WorkflowName' in Azure Logic App '$LogicAppName', executing stopType for Logic App Workflow" + ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppType $batch.logicAppType -LogicAppName $LogicAppName -WorkflowName $WorkflowName -stopType $batch.stopType + } + Write-Host "Check 'NoWaitingOrRunningRuns' executed successfully onWorkflow '$WorkflowName' in Azure Logic App '$LogicAppName'" -ForegroundColor Green } - } else{ - Write-Verbose "Found no more waiting or running runs for Azure Logic App '$logicApp', executing stopType for Logic App" - ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppName $logicApp -stopType $batch.stopType - } - Write-Host "Check 'NoWaitingOrRunningRuns' executed successfully on Azure Logic App '$logicApp'" -ForegroundColor Green + } else { + Write-Warning "No workflows specified to disable" + } } - catch { - Write-Warning "Failed to perform check 'NoWaitingOrRunningRuns' for Azure Logic App '$logicApp'" - $ErrorMessage = $_.Exception.Message - Write-Debug "Error: $ErrorMessage" + } else { + Write-Warning "No Azure Logic Apps specified to disable" + } + } else { + if ($batch.logicApps.Length -gt 0 ) { + $batch.logicApps | ForEach-Object { + $logicApp = $_; + if ($ResourcePrefix.Length -gt 0){ + $logicApp = "$ResourcePrefix$_" + } + + try { + $runHistory = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $logicApp -FollowNextPageLink -MaximumFollowNextPageLink $maximumFollowNextPageLink -ErrorAction Stop + $runningRunsCount = ($runHistory | Where-Object { $_.Status -eq "Running" }).Count + $waitingRunsCount = ($runHistory | Where-Object { $_.Status -eq "Waiting" }).Count + + if ($runningRunsCount -ne 0 -or $waitingRunsCount -ne 0) { + while ($runningRunsCount -ne 0 -or $waitingRunsCount -ne 0) { + Write-Verbose "Azure Logic App '$logicApp' has Running and/or Waiting Runs, waiting 10 seconds and checking again..." + Write-Debug "Number of running runs: $runningRunsCount" + Write-Debug "Number of waiting runs: $waitingRunsCount" + Start-Sleep -Second 10 + $runHistory = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $logicApp -FollowNextPageLink -MaximumFollowNextPageLink $maximumFollowNextPageLink -ErrorAction Stop + $runningRunsCount = ($runHistory | Where-Object { $_.Status -eq "Running" }).Count + $waitingRunsCount = ($runHistory | Where-Object { $_.Status -eq "Waiting" }).Count + if ($runningRunsCount -eq 0 -and $waitingRunsCount -eq 0) { + Write-Verbose "Found no more waiting or running runs for Azure Logic App '$logicApp', executing stopType for Logic App" + ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppType $batch.logicAppType -LogicAppName $logicApp -stopType $batch.stopType + } + } + } else { + Write-Verbose "Found no more waiting or running runs for Azure Logic App '$logicApp', executing stopType for Logic App" + ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppType $batch.logicAppType -LogicAppName $logicApp -stopType $batch.stopType + } + Write-Host "Check 'NoWaitingOrRunningRuns' executed successfully on Azure Logic App '$logicApp'" -ForegroundColor Green + } catch { + Write-Warning "Failed to perform check 'NoWaitingOrRunningRuns' for Azure Logic App '$logicApp'" + $ErrorMessage = $_.Exception.Message + Write-Debug "Error: $ErrorMessage" + } } + } else { + Write-Warning "No Azure Logic Apps specified to disable" } } - else { - Write-Warning "No Azure Logic Apps specified to disable" - } - } - elseIf ($batch.checkType -Match "None") { - Write-Host "Executing Check 'None' => peforming no check and executing stopType" - $batch.logicApps | ForEach-Object { - $logicApp = $_; - if($ResourcePrefix.Length -gt 0){ - $logicApp = "$ResourcePrefix$_" + } elseIf ($batch.checkType -Match "None") { + Write-Host "Executing Check 'None' => performing no check and executing stopType" + if ($batch.logicAppType -match "Standard") { + if ($batch.logicApps.Length -gt 0 ) { + $batch.logicApps | ForEach-Object { + $LogicAppName = $_.name; + + $_.workflows | ForEach-Object { + $WorkflowName = $_; + + ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppType $batch.logicAppType -LogicAppName $LogicAppName -WorkflowName $WorkflowName -stopType $batch.stopType + } + } } - ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppName $logicApp -stopType $batch.stopType - } - } - else { + } else { + $batch.logicApps | ForEach-Object { + $LogicAppName = $_; + if($ResourcePrefix.Length -gt 0){ + $LogicAppName = "$ResourcePrefix$_" + } + ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppType $batch.logicAppType -LogicAppName $LogicAppName -stopType $batch.stopType + } + } + } else { Write-Warning "CheckType '$batch.checkType' has no known implementation, performing no check or stop on the Azure Logic App '$logicApp' in resource group '$ResourceGroupName'..." } } diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 index 2af97b7e..978f956c 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 @@ -18,7 +18,7 @@ try{ $SubscriptionId = $token.SubscriptionId } - $fullUrl = . $PSScriptRoot\Get-AzLogicAppResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -ApiVersion $ApiVersion -Action enable + $fullUrl = . $PSScriptRoot\Get-AzLogicAppConsumptionResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -ApiVersion $ApiVersion -Action enable Write-Verbose "Attempting to enable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'..." $params = @{ diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicAppsFromConfig.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicAppsFromConfig.ps1 index 17c1ff4c..e2c8fd21 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicAppsFromConfig.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicAppsFromConfig.ps1 @@ -19,24 +19,45 @@ function ReverseStopType() { BEGIN { Write-Verbose "Reverting stopType '$($batch.stopType)' for batch '$($batch.description)' in resource group '$ResourceGroupName'" if ($batch.stopType -Match "Immediate") { - if ($batch.logicApps.Length -gt 0 ) { - $batch.logicApps | ForEach-Object { - $LogicAppName = $_; - if($ResourcePrefix.Length -gt 0){ - $LogicAppName = "$ResourcePrefix$_" - } - try { - Enable-AzLogicApp -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -ApiVersion $ApiVersion -AccessToken $Global:accessToken + if ($batch.logicAppType -match "Standard") { + if ($batch.logicApps.Length -gt 0 ) { + $batch.logicApps | ForEach-Object { + $LogicAppName = $_.name; + + $_.workflows | ForEach-Object { + $WorkflowName = $_; + + try { + Enable-AzLogicApp -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -ApiVersion $ApiVersion -AccessToken $Global:accessToken + } + catch { + Write-Warning "Failed to enable workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + $ErrorMessage = $_.Exception.Message + Write-Debug "Error: $ErrorMessage" + } + } } - catch { - Write-Warning "Failed to enable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" - $ErrorMessage = $_.Exception.Message - Write-Debug "Error: $ErrorMessage" + } + } else { + if ($batch.logicApps.Length -gt 0 ) { + $batch.logicApps | ForEach-Object { + $LogicAppName = $_; + if($ResourcePrefix.Length -gt 0){ + $LogicAppName = "$ResourcePrefix$_" + } + try { + Enable-AzLogicApp -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -ApiVersion $ApiVersion -AccessToken $Global:accessToken + } + catch { + Write-Warning "Failed to enable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + $ErrorMessage = $_.Exception.Message + Write-Debug "Error: $ErrorMessage" + } } } - } - else { - Write-Warning "No Azure Logic Apps specified to enable" + else { + Write-Warning "No Azure Logic Apps specified to enable" + } } } elseIf ($batch.stopType -Match "None") { diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppResourceManagementUrl.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppConsumptionResourceManagementUrl.ps1 similarity index 100% rename from src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppResourceManagementUrl.ps1 rename to src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppConsumptionResourceManagementUrl.ps1 diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 new file mode 100644 index 00000000..d6fdf6de --- /dev/null +++ b/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 @@ -0,0 +1,37 @@ +param( + [string][Parameter(Mandatory = $true)] $EnvironmentName, + [string][parameter(Mandatory = $true)] $SubscriptionId, + [string][parameter(Mandatory = $true)] $ResourceGroupName, + [string][parameter(Mandatory = $true)] $LogicAppName, + [string][parameter(Mandatory = $true)] $WorkflowName +) + +try { + $resourceManagerUrl = "" + + $environments = (Get-AzEnvironment).Name + if ($EnvironmentName -notin $environments) { + $supportedEnvironments = "" + + foreach ($env in $environments) { + if ($supportedEnvironments.Length -eq 0) { + $supportedEnvironments += $env + } + else { + $supportedEnvironments += ", " + $env + } + } + + Write-Error "Unrecognized environment specified. Supported values are: $supportedEnvironments" + } + + $resourceManagerUrl = (Get-AzEnvironment -Name $EnvironmentName).ResourceManagerUrl + + $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/hostruntime/runtime/webhooks/workflow/api/management/workflows/$WorkflowName/runs?api-version=2022-03-01" + return $fullUrl +} +catch { + Write-Warning "Failed to define the resource management endpoint (Environment: '$EnvironmentName', SubscriptionId: '$SubscriptionId', ResourceGroup: '$ResourceGroupName', LogicApp: '$LogicAppName', WorkflowName: '$WorkflowName')" + $ErrorMessage = $_.Exception.Message + Write-Debug "Error: $ErrorMessage" +} \ No newline at end of file From ffcf41bc571a071e396d7d76673062ef06c639c8 Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Thu, 2 Nov 2023 17:16:20 +0100 Subject: [PATCH 04/24] minor cleanup --- .../Scripts/Disable-AzLogicApp.ps1 | 9 ++++----- .../Scripts/Disable-AzLogicAppsFromConfig.ps1 | 13 ++++++------- .../Scripts/Enable-AzLogicApp.ps1 | 8 ++++---- .../Scripts/Enable-AzLogicAppsFromConfig.ps1 | 19 +++++++------------ ...gicAppConsumptionResourceManagementUrl.ps1 | 3 +-- ...zLogicAppStandardResourceManagementUrl.ps1 | 3 +-- 6 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 index b5f23e9c..8aac0af2 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 @@ -9,8 +9,8 @@ param( ) try{ - if($WorkflowName -eq "") { - if($SubscriptionId -eq "" -or $AccessToken -eq ""){ + if ($WorkflowName -eq "") { + if ($SubscriptionId -eq "" -or $AccessToken -eq ""){ # Request accessToken in case the script contains records $token = Get-AzCachedAccessToken @@ -35,9 +35,8 @@ try{ Set-AzAppServiceSetting -ResourceGroupName $ResourceGroupName -AppServiceName $LogicAppName -AppServiceSettingName "Workflows.$WorkflowName.FlowState" -AppServiceSettingValue "Disabled" Write-Host "Successfully disabled workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" -ForegroundColor Green } -} -catch { - if($WorkflowName -eq "") { +} catch { + if ($WorkflowName -eq "") { Write-Warning "Failed to disable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" } else { Write-Warning "Failed to disable workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 index ea288662..1a0b9531 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 @@ -36,10 +36,9 @@ function ExecuteStopType() { } elseIf ($stopType -Match "None") { Write-Host "Executing Stop 'None' => performing no stop" - } - else { + } else { Write-Warning "StopType '$stopType' has no known implementation, doing nothing.." - } + } } } @@ -99,12 +98,12 @@ function ExecuteCheckType() { } else { Write-Verbose "Found no more waiting or running runs for Workflow '$WorkflowName' in Azure Logic App '$LogicAppName', executing stopType for Logic App Workflow" ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppType $batch.logicAppType -LogicAppName $LogicAppName -WorkflowName $WorkflowName -stopType $batch.stopType - } + } Write-Host "Check 'NoWaitingOrRunningRuns' executed successfully onWorkflow '$WorkflowName' in Azure Logic App '$LogicAppName'" -ForegroundColor Green } } else { Write-Warning "No workflows specified to disable" - } + } } } else { Write-Warning "No Azure Logic Apps specified to disable" @@ -139,7 +138,7 @@ function ExecuteCheckType() { } else { Write-Verbose "Found no more waiting or running runs for Azure Logic App '$logicApp', executing stopType for Logic App" ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppType $batch.logicAppType -LogicAppName $logicApp -stopType $batch.stopType - } + } Write-Host "Check 'NoWaitingOrRunningRuns' executed successfully on Azure Logic App '$logicApp'" -ForegroundColor Green } catch { Write-Warning "Failed to perform check 'NoWaitingOrRunningRuns' for Azure Logic App '$logicApp'" @@ -173,7 +172,7 @@ function ExecuteCheckType() { } ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppType $batch.logicAppType -LogicAppName $LogicAppName -stopType $batch.stopType } - } + } } else { Write-Warning "CheckType '$batch.checkType' has no known implementation, performing no check or stop on the Azure Logic App '$logicApp' in resource group '$ResourceGroupName'..." } diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 index 978f956c..69f6b9ac 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 @@ -9,8 +9,8 @@ param( ) try{ - if($WorkflowName -eq "") { - if($SubscriptionId -eq "" -or $AccessToken -eq ""){ + if ($WorkflowName -eq "") { + if ($SubscriptionId -eq "" -or $AccessToken -eq ""){ # Request accessToken in case the script contains records $token = Get-AzCachedAccessToken @@ -37,11 +37,11 @@ try{ } } catch { - if($WorkflowName -eq "") { + if ($WorkflowName -eq "") { Write-Warning "Failed to enable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" } else { Write-Warning "Failed to enable workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" - } + } $ErrorMessage = $_.Exception.Message Write-Debug "Error: $ErrorMessage" } diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicAppsFromConfig.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicAppsFromConfig.ps1 index e2c8fd21..1621955a 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicAppsFromConfig.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicAppsFromConfig.ps1 @@ -29,8 +29,7 @@ function ReverseStopType() { try { Enable-AzLogicApp -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -ApiVersion $ApiVersion -AccessToken $Global:accessToken - } - catch { + } catch { Write-Warning "Failed to enable workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" $ErrorMessage = $_.Exception.Message Write-Debug "Error: $ErrorMessage" @@ -42,30 +41,26 @@ function ReverseStopType() { if ($batch.logicApps.Length -gt 0 ) { $batch.logicApps | ForEach-Object { $LogicAppName = $_; - if($ResourcePrefix.Length -gt 0){ + if ($ResourcePrefix.Length -gt 0){ $LogicAppName = "$ResourcePrefix$_" } try { Enable-AzLogicApp -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -ApiVersion $ApiVersion -AccessToken $Global:accessToken - } - catch { + } catch { Write-Warning "Failed to enable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" $ErrorMessage = $_.Exception.Message Write-Debug "Error: $ErrorMessage" } } - } - else { + } else { Write-Warning "No Azure Logic Apps specified to enable" } } - } - elseIf ($batch.stopType -Match "None") { + } elseIf ($batch.stopType -Match "None") { Write-Host "StopType equals 'None', performing no enable action on the Logic App(s)" - } - else { + } else { Write-Warning "StopType '$($batch.stopType)' has no known implementation, doing nothing" - } + } } } diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppConsumptionResourceManagementUrl.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppConsumptionResourceManagementUrl.ps1 index dd4a8c9b..2347949d 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppConsumptionResourceManagementUrl.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppConsumptionResourceManagementUrl.ps1 @@ -32,8 +32,7 @@ try { $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Logic/workflows/$LogicAppName/$Action" + "?api-version=$ApiVersion" return $fullUrl -} -catch { +} catch { Write-Warning "Failed to define the resource management endpoint (Environment: '$EnvironmentName', SubscriptionId: '$SubscriptionId', ResourceGroup: '$ResourceGroupName', LogicApp: '$LogicAppName')" $ErrorMessage = $_.Exception.Message Write-Debug "Error: $ErrorMessage" diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 index d6fdf6de..fab22226 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 @@ -29,8 +29,7 @@ try { $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/hostruntime/runtime/webhooks/workflow/api/management/workflows/$WorkflowName/runs?api-version=2022-03-01" return $fullUrl -} -catch { +} catch { Write-Warning "Failed to define the resource management endpoint (Environment: '$EnvironmentName', SubscriptionId: '$SubscriptionId', ResourceGroup: '$ResourceGroupName', LogicApp: '$LogicAppName', WorkflowName: '$WorkflowName')" $ErrorMessage = $_.Exception.Message Write-Debug "Error: $ErrorMessage" From 3e894622c90b1aa8ecc828840f7ff151a1606852 Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Fri, 3 Nov 2023 11:21:48 +0100 Subject: [PATCH 05/24] fixed unit tests --- .../Arcus.Scripting.LogicApps.tests.ps1 | 46 +++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 index 11a70702..13e1d40f 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 @@ -171,12 +171,22 @@ InModuleScope Arcus.Scripting.LogicApps { } It "Doesn't disable anything when checkType = NoWaitingOrRunningRuns but returns a zero count on the running runs for unknown stopType" { # Arrange + $script:i = 0 $resourceGroup = "my-resource-group" $logicAppNames = @("snd-async", "ord-sthp-harvest-order-doublechecker", "ord-sthp-harvest-order-doublechecker") - Mock Get-AzLogicAppRunHistory { + Mock Get-AzLogicAppRunHistory { $ResourceGroupName | Should -Be $resourceGroup - $Name | Should -BeIn $logicAppNames - return @([pscustomobject]@{ Status = "Waiting" }) + $Name | Should -Be "snd-async" + $script:i++ + if ($script:i -gt 2) { + Write-Host "Returning empty (no running, no waiting) runs" + return @() + } else { + Write-Host "Returning 1 running run" + return @( + [pscustomobject]@{ Status = "Running" } + ) + } } Mock Disable-AzLogicApp {} Mock Get-AzCachedAccessToken -MockWith { @@ -196,12 +206,22 @@ InModuleScope Arcus.Scripting.LogicApps { } It "Doesn't disable anything when checkType = NoWaitingOrRunningRuns but returns a zero count on an the waiting runs for stopType = None" { # Arrange + $script:i = 0 $resourceGroup = "my-resource-group" $logicAppNames = @("snd-async", "ord-sthp-harvest-order-doublechecker") Mock Get-AzLogicAppRunHistory { $ResourceGroupName | Should -Be $resourceGroup - $Name | Should -BeIn $logicAppNames - return @([pscustomobject]{ Status = "Running" }) + $Name | Should -Be "snd-async" + $script:i++ + if ($script:i -gt 2) { + Write-Host "Returning empty (no running, no waiting) runs" + return @() + } else { + Write-Host "Returning 1 waiting run" + return @( + [pscustomobject]@{ Status = "Waiting" } + ) + } } Mock Disable-AzLogicApp {} Mock Get-AzCachedAccessToken -MockWith { @@ -221,12 +241,22 @@ InModuleScope Arcus.Scripting.LogicApps { } It "Disables all logic apps when checkType = NoWaitingOrRunningRuns with found waiting and no running runs for stopType = Immediate" { # Arrange + $script:i = 0 $resourceGroup = "my-resource-group" $logicAppNames = @("snd-async", "ord-sthp-harvest-order-doublechecker", "rcv-sthp-harvest-order-af-ftp", "rcv-sthp-harvest-order-af-sft", "rcv-sthp-harvest-order-af-file") - Mock Get-AzLogicAppRunHistory { + Mock Get-AzLogicAppRunHistory { $ResourceGroupName | Should -Be $resourceGroup $Name | Should -BeIn $logicAppNames - return @([pscustomobject]@{ Status = "Waiting" }) + $script:i++ + if ($script:i -gt 2) { + Write-Host "Returning empty (no running, no waiting) runs" + return @() + } else { + Write-Host "Returning 1 waiting run" + return @( + [pscustomobject]@{ Status = "Waiting" } + ) + } } Mock Disable-AzLogicApp { $ResourceGroupName | Should -Be $resourceGroup @@ -250,7 +280,7 @@ InModuleScope Arcus.Scripting.LogicApps { It "Disables all logic apps when checkType = NoWaitingOrRunningRuns with found waiting and running runs for stopType = Immediate" { # Arrange $resourceGroup = "my-resource-group" - $i = 0 + $script:i = 0 Mock Get-AzLogicAppRunHistory { $ResourceGroupName | Should -Be $resourceGroup $Name | Should -Be "snd-async" From 8a6941a75b9d2a121161080e589603a5c99cbd50 Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Fri, 3 Nov 2023 13:50:25 +0100 Subject: [PATCH 06/24] added logic app standard unit tests --- .../Scripts/Disable-AzLogicAppsFromConfig.ps1 | 2 +- .../Arcus.Scripting.LogicApps.tests.ps1 | 276 +++++++++++++++++- .../Arcus.Scripting.Tests.Unit.pssproj | 27 +- ...rderControl-maximumFollowNextPageLink.json | Bin ...erControl-noMaximumFollowNextPageLink.json | Bin ...l-noWaitingOrRunningRunsWithImmediate.json | Bin ...oWaitingOrRunningRunsWithNoneStopType.json | Bin ...itingOrRunningRunsWithSingleImmediate.json | Bin ...itingOrRunningRunsWithunknownStopType.json | Bin .../deploy-orderControl-none.json | Bin .../deploy-orderControl-unknownCheckType.json | Bin .../deploy-orderControl-unknownStopType.json | Bin ...l-noWaitingOrRunningRunsWithImmediate.json | Bin 0 -> 2302 bytes ...oWaitingOrRunningRunsWithNoneStopType.json | Bin 0 -> 1526 bytes ...itingOrRunningRunsWithSingleImmediate.json | Bin 0 -> 566 bytes ...itingOrRunningRunsWithunknownStopType.json | Bin 0 -> 1942 bytes .../Standard/deploy-orderControl-none.json | Bin 0 -> 520 bytes .../deploy-orderControl-unknownCheckType.json | Bin 0 -> 544 bytes .../deploy-orderControl-unknownStopType.json | Bin 0 -> 576 bytes 19 files changed, 283 insertions(+), 22 deletions(-) rename src/Arcus.Scripting.Tests.Unit/Files/LogicApps/{ => Consumption}/deploy-orderControl-maximumFollowNextPageLink.json (100%) rename src/Arcus.Scripting.Tests.Unit/Files/LogicApps/{ => Consumption}/deploy-orderControl-noMaximumFollowNextPageLink.json (100%) rename src/Arcus.Scripting.Tests.Unit/Files/LogicApps/{ => Consumption}/deploy-orderControl-noWaitingOrRunningRunsWithImmediate.json (100%) rename src/Arcus.Scripting.Tests.Unit/Files/LogicApps/{ => Consumption}/deploy-orderControl-noWaitingOrRunningRunsWithNoneStopType.json (100%) rename src/Arcus.Scripting.Tests.Unit/Files/LogicApps/{ => Consumption}/deploy-orderControl-noWaitingOrRunningRunsWithSingleImmediate.json (100%) rename src/Arcus.Scripting.Tests.Unit/Files/LogicApps/{ => Consumption}/deploy-orderControl-noWaitingOrRunningRunsWithunknownStopType.json (100%) rename src/Arcus.Scripting.Tests.Unit/Files/LogicApps/{ => Consumption}/deploy-orderControl-none.json (100%) rename src/Arcus.Scripting.Tests.Unit/Files/LogicApps/{ => Consumption}/deploy-orderControl-unknownCheckType.json (100%) rename src/Arcus.Scripting.Tests.Unit/Files/LogicApps/{ => Consumption}/deploy-orderControl-unknownStopType.json (100%) create mode 100644 src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-noWaitingOrRunningRunsWithImmediate.json create mode 100644 src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-noWaitingOrRunningRunsWithNoneStopType.json create mode 100644 src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-noWaitingOrRunningRunsWithSingleImmediate.json create mode 100644 src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-noWaitingOrRunningRunsWithunknownStopType.json create mode 100644 src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-none.json create mode 100644 src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-unknownCheckType.json create mode 100644 src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-unknownStopType.json diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 index 1a0b9531..6ba0beca 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 @@ -65,7 +65,7 @@ function ExecuteCheckType() { if ($_.workflows.Length -gt 0 ) { $_.workflows | ForEach-Object { $WorkflowName = $_; - $fullUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName + $fullUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName $params = @{ Method = 'Get' Headers = @{ diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 index 13e1d40f..85b56b7b 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 @@ -50,7 +50,7 @@ InModuleScope Arcus.Scripting.LogicApps { } # Act - Enable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\deploy-orderControl-unknownStopType.json" -ResourceGroupName "ignored-resource-group" + Enable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Consumption\deploy-orderControl-unknownStopType.json" -ResourceGroupName "ignored-resource-group" # Assert Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 @@ -69,7 +69,7 @@ InModuleScope Arcus.Scripting.LogicApps { } # Act - Enable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\deploy-orderControl-none.json" -ResourceGroupName "ignored-resource-group" + Enable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Consumption\deploy-orderControl-none.json" -ResourceGroupName "ignored-resource-group" # Assert Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 @@ -124,7 +124,7 @@ InModuleScope Arcus.Scripting.LogicApps { } # Act - Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\deploy-orderControl-unknownCheckType.json" -ResourceGroup "ignored-resource-group" + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Consumption\deploy-orderControl-unknownCheckType.json" -ResourceGroup "ignored-resource-group" # Assert Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 @@ -143,7 +143,7 @@ InModuleScope Arcus.Scripting.LogicApps { } # Act - Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\deploy-orderControl-unknownStopType.json" -ResourceGroupName "ignored-resource-group" + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Consumption\deploy-orderControl-unknownStopType.json" -ResourceGroupName "ignored-resource-group" # Assert Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 @@ -162,7 +162,7 @@ InModuleScope Arcus.Scripting.LogicApps { } # Act - Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\deploy-orderControl-none.json" -ResourceGroupName "ignored-resource-group" + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Consumption\deploy-orderControl-none.json" -ResourceGroupName "ignored-resource-group" # Assert Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 @@ -197,7 +197,7 @@ InModuleScope Arcus.Scripting.LogicApps { } # Act - Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\deploy-orderControl-noWaitingOrRunningRunsWithunknownStopType.json" -ResourceGroupName $resourceGroup + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Consumption\deploy-orderControl-noWaitingOrRunningRunsWithunknownStopType.json" -ResourceGroupName $resourceGroup # Assert Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 @@ -232,7 +232,7 @@ InModuleScope Arcus.Scripting.LogicApps { } # Act - Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\deploy-orderControl-noWaitingOrRunningRunsWithNoneStopType.json" -ResourceGroupName $resourceGroup + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Consumption\deploy-orderControl-noWaitingOrRunningRunsWithNoneStopType.json" -ResourceGroupName $resourceGroup # Assert Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 @@ -270,7 +270,7 @@ InModuleScope Arcus.Scripting.LogicApps { } # Act - Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\deploy-orderControl-noWaitingOrRunningRunsWithImmediate.json" -ResourceGroupName $resourceGroup + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Consumption\deploy-orderControl-noWaitingOrRunningRunsWithImmediate.json" -ResourceGroupName $resourceGroup # Assert Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 @@ -308,7 +308,7 @@ InModuleScope Arcus.Scripting.LogicApps { } # Act - Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\deploy-orderControl-noWaitingOrRunningRunsWithSingleImmediate.json" -ResourceGroupName $resourceGroup + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Consumption\deploy-orderControl-noWaitingOrRunningRunsWithSingleImmediate.json" -ResourceGroupName $resourceGroup # Assert Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 @@ -336,7 +336,7 @@ InModuleScope Arcus.Scripting.LogicApps { } # Act - Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\deploy-orderControl-maximumFollowNextPageLink.json" -ResourceGroupName $resourceGroup + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Consumption\deploy-orderControl-maximumFollowNextPageLink.json" -ResourceGroupName $resourceGroup # Assert Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 @@ -364,7 +364,7 @@ InModuleScope Arcus.Scripting.LogicApps { } # Act - Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\deploy-orderControl-noMaximumFollowNextPageLink.json" -ResourceGroupName $resourceGroup + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Consumption\deploy-orderControl-noMaximumFollowNextPageLink.json" -ResourceGroupName $resourceGroup # Assert Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 @@ -710,7 +710,7 @@ InModuleScope Arcus.Scripting.LogicApps { } Describe "Arcus Azure Logic Apps Standard unit tests" { Context "Enable Logic Apps without configuration" { - It "Fails to enable an unknown Azure Logic App" { + It "Fails to enable an unknown Azure Logic App" { # Arrange $resourceGroupName = "codit-arcus-scripting" $logicAppName = "arc-dev-we-rcv-unknown-http" @@ -745,6 +745,46 @@ InModuleScope Arcus.Scripting.LogicApps { Assert-MockCalled Write-Host -Scope It -Times 1 -ParameterFilter { $Message -contains "Successfully enabled workflow '$workflowName' in Azure Logic App '$logicAppName' in resource group '$resourceGroupName'" } } } + Context "Enable Logic Apps with configuration" { + It "Doesn't enable anything when the stopType is not recognized" { + # Arrange + Mock Get-AzLogicAppRunHistory {} + Mock Enable-AzLogicApp {} + Mock Get-AzCachedAccessToken -MockWith { + return @{ + SubscriptionId = "123456" + AccessToken = "accessToken" + } + } + + # Act + Enable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Standard\deploy-orderControl-unknownStopType.json" -ResourceGroupName "ignored-resource-group" + + # Assert + Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 + Assert-MockCalled Get-AzLogicAppRunHistory -Scope It -Times 0 + Assert-MockCalled Enable-AzLogicApp -Scope It -Exactly 0 + } + It "Doesn't enable anything when both checkType & stopType is 'None'" { + # Arrange + Mock Get-AzLogicAppRunHistory {} + Mock Enable-AzLogicApp {} + Mock Get-AzCachedAccessToken -MockWith { + return @{ + SubscriptionId = "123456" + AccessToken = "accessToken" + } + } + + # Act + Enable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Standard\deploy-orderControl-none.json" -ResourceGroupName "ignored-resource-group" + + # Assert + Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 + Assert-MockCalled Get-AzLogicAppRunHistory -Scope It -Times 0 + Assert-MockCalled Enable-AzLogicApp -Scope It -Exactly 0 + } + } Context "Disable Logic Apps without configuration" { It "Fails to disable an unknown Azure Logic App" { # Arrange @@ -781,5 +821,217 @@ InModuleScope Arcus.Scripting.LogicApps { Assert-MockCalled Write-Host -Scope It -Times 1 -ParameterFilter { $Message -contains "Successfully disabled workflow '$workflowName' in Azure Logic App '$logicAppName' in resource group '$resourceGroupName'" } } } + Context "Disable Logic Apps with configuration" { + It "Doesn't disable anything when the checkType is not recognized" { + # Arrange + Mock Invoke-WebRequest {} + Mock Disable-AzLogicApp {} + Mock Get-AzCachedAccessToken -MockWith { + return @{ + SubscriptionId = "123456" + AccessToken = "accessToken" + } + } + + # Act + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Standard\deploy-orderControl-unknownCheckType.json" -ResourceGroup "ignored-resource-group" + + # Assert + Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 + Assert-MockCalled Invoke-WebRequest -Scope It -Times 0 + Assert-MockCalled Disable-AzLogicApp -Scope It -Exactly 0 + } + It "Doesn't disable anything when the stopType is not recognized" { + # Arrange + Mock Invoke-WebRequest {} + Mock Disable-AzLogicApp {} + Mock Get-AzCachedAccessToken -MockWith { + return @{ + SubscriptionId = "123456" + AccessToken = "accessToken" + } + } + + # Act + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Standard\deploy-orderControl-unknownStopType.json" -ResourceGroupName "ignored-resource-group" + + # Assert + Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 + Assert-MockCalled Invoke-WebRequest -Scope It -Times 0 + Assert-MockCalled Disable-AzLogicApp -Scope It -Exactly 0 + } + It "Doesn't disable anything when both checkType & stopType is 'None'" { + # Arrange + Mock Invoke-WebRequest {} + Mock Disable-AzLogicApp {} + Mock Get-AzCachedAccessToken -MockWith { + return @{ + SubscriptionId = "123456" + AccessToken = "accessToken" + } + } + + # Act + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Standard\deploy-orderControl-none.json" -ResourceGroupName "ignored-resource-group" + + # Assert + Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 + Assert-MockCalled Invoke-WebRequest -Scope It -Times 0 + Assert-MockCalled Disable-AzLogicApp -Scope It -Exactly 0 + } + It "Doesn't disable anything when checkType = NoWaitingOrRunningRuns but returns a running run for unknown stopType" { + # Arrange + $script:i = 0 + $resourceGroup = "my-resource-group" + $logicAppNames = @("snd-async", "ord-sthp-harvest-order-doublechecker", "ord-sthp-harvest-order-doublechecker") + + Mock Invoke-WebRequest -MockWith { + $script:i++ + if ($script:i -gt 2) { + Write-Host "Returning empty (no running, no waiting) runs" + return [pscustomobject] @{ + Content = '{"value":[{"properties":{"status":"Succeeded"}}]}' + } + } else { + Write-Host "Returning 1 running run" + return [pscustomobject] @{ + Content = '{"value":[{"properties":{"status":"Running"}}]}' + } + } + } + Mock Disable-AzLogicApp {} + Mock Get-AzCachedAccessToken -MockWith { + $Global:subscriptionId = '123456' + return @{ + SubscriptionId = "123456" + AccessToken = "accessToken" + } + } + + # Act + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Standard\deploy-orderControl-noWaitingOrRunningRunsWithunknownStopType.json" -ResourceGroupName $resourceGroup + + # Assert + Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 + Assert-MockCalled Invoke-WebRequest -Scope It -Exactly 5 + Assert-MockCalled Disable-AzLogicApp -Scope It -Exactly 0 + } + It "Doesn't disable anything when checkType = NoWaitingOrRunningRuns but returns a waiting run for stopType = None" { + # Arrange + $script:i = 0 + $resourceGroup = "my-resource-group" + $logicAppNames = @("snd-async", "ord-sthp-harvest-order-doublechecker") + + Mock Invoke-WebRequest -MockWith { + $script:i++ + if ($script:i -gt 2) { + Write-Host "Returning empty (no running, no waiting) runs" + return [pscustomobject] @{ + Content = '{"value":[{"properties":{"status":"Succeeded"}}]}' + } + } else { + Write-Host "Returning 1 waiting run" + return [pscustomobject] @{ + Content = '{"value":[{"properties":{"status":"Waiting"}}]}' + } + } + } + Mock Disable-AzLogicApp {} + Mock Get-AzCachedAccessToken -MockWith { + $Global:subscriptionId = '123456' + return @{ + SubscriptionId = "123456" + AccessToken = "accessToken" + } + } + + # Act + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Standard\deploy-orderControl-noWaitingOrRunningRunsWithNoneStopType.json" -ResourceGroupName $resourceGroup + + # Assert + Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 + Assert-MockCalled Invoke-WebRequest -Scope It -Exactly 4 + Assert-MockCalled Disable-AzLogicApp -Scope It -Exactly 0 + } + It "Disables all logic apps when checkType = NoWaitingOrRunningRuns with found waiting and no running runs for stopType = Immediate" { + # Arrange + $script:i = 0 + $resourceGroup = "my-resource-group" + $logicAppNames = @("snd-async", "ord-sthp-harvest-order-doublechecker", "rcv-sthp-harvest-order-af-ftp", "rcv-sthp-harvest-order-af-sft", "rcv-sthp-harvest-order-af-file") + + Mock Invoke-WebRequest -MockWith { + $script:i++ + if ($script:i -gt 2) { + Write-Host "Returning empty (no running, no waiting) runs" + return [pscustomobject] @{ + Content = '{"value":[{"properties":{"status":"Succeeded"}}]}' + } + } else { + Write-Host "Returning 1 waiting run" + return [pscustomobject] @{ + Content = '{"value":[{"properties":{"status":"Waiting"}}]}' + } + } + } + Mock Disable-AzLogicApp { + $ResourceGroupName | Should -Be $resourceGroup + $LogicAppName | Should -BeIn $logicAppNames + } + Mock Get-AzCachedAccessToken -MockWith { + $Global:subscriptionId = '123456' + return @{ + SubscriptionId = "123456" + AccessToken = "accessToken" + } + } + + # Act + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Standard\deploy-orderControl-noWaitingOrRunningRunsWithImmediate.json" -ResourceGroupName $resourceGroup + + # Assert + Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 + Assert-MockCalled Invoke-WebRequest -Scope It -Exactly 7 + Assert-MockCalled Disable-AzLogicApp -Scope It -Exactly 5 -ParameterFilter { $resourceGroupName -eq $resourceGroup } + } + It "Disables all logic apps when checkType = NoWaitingOrRunningRuns with found waiting and running runs for stopType = Immediate" { + # Arrange + $resourceGroup = "my-resource-group" + $script:i = 0 + + Mock Invoke-WebRequest -MockWith { + $script:i++ + if ($script:i -gt 2) { + Write-Host "Returning empty (no running, no waiting) runs" + return [pscustomobject] @{ + Content = '{"value":[{"properties":{"status":"Succeeded"}},{"properties":{"status":"Succeeded"}}]}' + } + } else { + Write-Host "Returning 1 waiting run and 1 running run" + return [pscustomobject] @{ + Content = '{"value":[{"properties":{"status":"Waiting"}},{"properties":{"status":"Running"}}]}' + } + } + } + Mock Disable-AzLogicApp { + $ResourceGroupName | Should -Be $resourceGroup + $LogicAppName | Should -Be "snd-async" + } + Mock Get-AzCachedAccessToken -MockWith { + $Global:subscriptionId = '123456' + return @{ + SubscriptionId = "123456" + AccessToken = "accessToken" + } + } + + # Act + Disable-AzLogicAppsFromConfig -DeployFileName "$PSScriptRoot\Files\LogicApps\Standard\deploy-orderControl-noWaitingOrRunningRunsWithSingleImmediate.json" -ResourceGroupName $resourceGroup + + # Assert + Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 + Assert-MockCalled Invoke-WebRequest -Scope It -Exactly 3 + Assert-MockCalled Disable-AzLogicApp -Scope It -Exactly 1 -ParameterFilter { $ResourceGroupName -eq $resourceGroup -and $LogicAppName -eq "snd-async" } + } + } } } \ No newline at end of file diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.Tests.Unit.pssproj b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.Tests.Unit.pssproj index f8ff3b99..87304831 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.Tests.Unit.pssproj +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.Tests.Unit.pssproj @@ -90,25 +90,34 @@ - - + + + + + + + + + - - - - - - - + + + + + + + + + diff --git a/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-maximumFollowNextPageLink.json b/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-maximumFollowNextPageLink.json similarity index 100% rename from src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-maximumFollowNextPageLink.json rename to src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-maximumFollowNextPageLink.json diff --git a/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-noMaximumFollowNextPageLink.json b/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-noMaximumFollowNextPageLink.json similarity index 100% rename from src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-noMaximumFollowNextPageLink.json rename to src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-noMaximumFollowNextPageLink.json diff --git a/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-noWaitingOrRunningRunsWithImmediate.json b/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-noWaitingOrRunningRunsWithImmediate.json similarity index 100% rename from src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-noWaitingOrRunningRunsWithImmediate.json rename to src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-noWaitingOrRunningRunsWithImmediate.json diff --git a/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-noWaitingOrRunningRunsWithNoneStopType.json b/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-noWaitingOrRunningRunsWithNoneStopType.json similarity index 100% rename from src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-noWaitingOrRunningRunsWithNoneStopType.json rename to src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-noWaitingOrRunningRunsWithNoneStopType.json diff --git a/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-noWaitingOrRunningRunsWithSingleImmediate.json b/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-noWaitingOrRunningRunsWithSingleImmediate.json similarity index 100% rename from src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-noWaitingOrRunningRunsWithSingleImmediate.json rename to src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-noWaitingOrRunningRunsWithSingleImmediate.json diff --git a/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-noWaitingOrRunningRunsWithunknownStopType.json b/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-noWaitingOrRunningRunsWithunknownStopType.json similarity index 100% rename from src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-noWaitingOrRunningRunsWithunknownStopType.json rename to src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-noWaitingOrRunningRunsWithunknownStopType.json diff --git a/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-none.json b/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-none.json similarity index 100% rename from src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-none.json rename to src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-none.json diff --git a/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-unknownCheckType.json b/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-unknownCheckType.json similarity index 100% rename from src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-unknownCheckType.json rename to src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-unknownCheckType.json diff --git a/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-unknownStopType.json b/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-unknownStopType.json similarity index 100% rename from src/Arcus.Scripting.Tests.Unit/Files/LogicApps/deploy-orderControl-unknownStopType.json rename to src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Consumption/deploy-orderControl-unknownStopType.json diff --git a/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-noWaitingOrRunningRunsWithImmediate.json b/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-noWaitingOrRunningRunsWithImmediate.json new file mode 100644 index 0000000000000000000000000000000000000000..c18d82f3b341a5cd088731cb3f6427268a5352a2 GIT binary patch literal 2302 zcmeHJO-sW-5PfIC|4?#LaQ}cek6u));6ZwbN&10xBiXb?q<>v~Z_{m>qy4wGa}F zxhlaE?(j;CY#n_?tb4@76t2Yx*WBk8Gv3>krFwE(a2=y^;P}8WBDAc?K?>-_OwM$dhGmZ7%EB z1@kY+=n%?d!H8YE*D+#3oJEcE3I0LHx@=f#|8nDmzo!=ET&Y2M6#jH7Jsq_3Gwq1k z6vm(Ln0hwsIVpF(I&+A|f9n1_!jN#YSs)1pj9=M{}WOt`~ zy1J*jdcMEr=-~=|KA)#QtrFxY;9=nKgg0#XUc=IU9lTR|ZP?>5;JzU}qXvS(in|P+ zagPsT6sPDhv1Z7KDO`&s?wDtSE&uJ&a#;BeT+gUA@w!lL$kMFjh5MJpHMn71J90|I zESx7}9eGF2CW7h`6D5CQt<7V!CPdHhTH(Nld?~G->{!;;WU!3gv3^G$AE7#SjQA~k z8M9$7F_qN168(#obzifT{(Ysd=V?SWA2gsk98Vfm^skuM2Yu_TMo(KY;xk7Yy1Jr@ zQ_h$4`hM(m8dRdWb@b(2ZSg?GXa?gX%6DlxyX6Tr_qwSJ`J99k7hH*%zkWYeCs}v> F0zW2B%v}Hg literal 0 HcmV?d00001 diff --git a/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-noWaitingOrRunningRunsWithSingleImmediate.json b/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-noWaitingOrRunningRunsWithSingleImmediate.json new file mode 100644 index 0000000000000000000000000000000000000000..a1ec028f75dd7ea2dd2b508006390fcf2ef66bc2 GIT binary patch literal 566 zcmZ`$!3x4K6w6ugA7m#5{Q++ty(l=rgX~bcIfvTGIvFDVx|()wWt~zeZPMgr+55G} z5Ce?39_`880>nt5AyFX16(#o^iul~JAz5YL6d3c}gv``)kT~*{#0gv6>5;VR6}@&4 z^fbN-Db}2`!HsA2Me3XW;j5|2>F2vHap6jsDx-I4=lPk=RHo!57If%)gwzT9WrB&~ zAJcsYlhAbHnl%0O8*R{l?!xA5y3}^wjHqT$irM@wowj2Bf{h+va#U3Oo9;BG3Da57 R`9BRG_0VSraeR$O_yF}2NiqNc literal 0 HcmV?d00001 diff --git a/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-noWaitingOrRunningRunsWithunknownStopType.json b/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-noWaitingOrRunningRunsWithunknownStopType.json new file mode 100644 index 0000000000000000000000000000000000000000..bcf16e704f617f790245588eedca8edef54ef79f GIT binary patch literal 1942 zcmds2O-sW-5PfIC{}6gnq4@*e1i_nvE%YEg#3Y)cZe){MRQlJ|_cq;p>Ol~UQo7xp z*?BWN^Je<~c7=WHA>ik=-sv%ZXMS+9t4LVrV#GulQ(PqvJK}c!AxD8|Ldu?ntjNdZK75Z` zin-#BYGnCR&%UuRZC=h-~$tpAdMcHjO)Kc2D1#?@VuFyNe4%~Q@%abrC5f8iLb d`BlXgFhX^udUWc3{mK7BjGzZWP&lwl;e-ut)F^xQ+H1Lg z%(0wQvG;$*nf-gZQ&^FgGBYYl?^SY_;>XNb!G3&(Y+NE~Mja}sE^+zV&-!@_qL!O$ pdW$y=+=l)QGupxVXvk%~>ufF-W|t=Lz~4AfevkIo^^VKj{Df literal 0 HcmV?d00001 diff --git a/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-unknownCheckType.json b/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-unknownCheckType.json new file mode 100644 index 0000000000000000000000000000000000000000..81b48fc93a02943f95cb490e6b1d22f9dfac09e5 GIT binary patch literal 544 zcmah`K@Ng25S+7#e=wXh(GPg@176UR;Xn!^L8)m;8e{yrIxS+QJs84<&g{%?hu8Cf zDJGclxqrXxDM5h}8UhM6uIN}LTHcb5_BsSpPfd&;C9$n^c_L zfF^X$-93lYw41jgs+ma3-NLC}Yq-B+CP&zt7%F;~c)Hhg%+w~8jQ+&HXA$}sA)fE? F3~yYQME3vy literal 0 HcmV?d00001 diff --git a/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-unknownStopType.json b/src/Arcus.Scripting.Tests.Unit/Files/LogicApps/Standard/deploy-orderControl-unknownStopType.json new file mode 100644 index 0000000000000000000000000000000000000000..0b2f40a17a04a08799683a8ed86896630b388a36 GIT binary patch literal 576 zcmZuuK@Ng25S+7#e=wXh(GPg@10KYi;Xn!^Q466>h#J4HPK(gc5)#7B?#%2C-_Hqi z%&_3|8oo>wAwv!Yfd(aNXuc~*;#=E|*_C#t!IE<;cGjMPz=cx+R~+%6Mn16jR?E~r zN71R`tj~yv-l?FeS<<{J$9KAsUyvg>Ur#z}r1r*?8DtZ? z`U>iQpUc!F8D`s~5!F+BRiCP|ejB2SiAnC}C*-&-_ivc(8D@``j7}wVcO1=pOv2IV QPxbvIK;I+8^*cPl51~~{J^%m! literal 0 HcmV?d00001 From f7267e828a18bb796781a14995049355816355bf Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Fri, 3 Nov 2023 15:01:32 +0100 Subject: [PATCH 07/24] use filter in getting running and waiting runs --- .../Scripts/Disable-AzLogicAppsFromConfig.ps1 | 36 +++++++++++++------ ...zLogicAppStandardResourceManagementUrl.ps1 | 13 +++++-- .../Arcus.Scripting.LogicApps.tests.ps1 | 16 ++++----- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 index 6ba0beca..82f74e5e 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 @@ -65,19 +65,31 @@ function ExecuteCheckType() { if ($_.workflows.Length -gt 0 ) { $_.workflows | ForEach-Object { $WorkflowName = $_; - $fullUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName - $params = @{ + + $listRunningUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -Action 'listRunning' + $listRunningParams = @{ Method = 'Get' Headers = @{ 'authorization'="Bearer $Global:accessToken" } - URI = $fullUrl + URI = $listRunningUrl } + $runHistoryRunning = Invoke-WebRequest @$listRunningParams -ErrorAction Stop + $runHistoryRunningContent = $runHistoryRunning.Content | ConvertFrom-Json - $runHistory = Invoke-WebRequest @params -ErrorAction Stop - $runHistoryContent = $runHistory.Content | ConvertFrom-Json - $runningRunsCount = ($runHistoryContent.value | Where-Object { $_.properties.status -eq "Running" }).Count - $waitingRunsCount = ($runHistoryContent.value | Where-Object { $_.properties.status -eq "Waiting" }).Count + $listWaitingUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -Action 'listWaiting' + $lisWaitingParams = @{ + Method = 'Get' + Headers = @{ + 'authorization'="Bearer $Global:accessToken" + } + URI = $lisWaitingParams + } + $runHistoryWaiting = Invoke-WebRequest @$lisWaitingParams -ErrorAction Stop + $runHistoryWaitingContent = $runHistoryWaiting.Content | ConvertFrom-Json + + $runningRunsCount = ($runHistoryRunningContent.value).Count + $waitingRunsCount = ($runHistoryWaitingContent.value).Count if ($runningRunsCount -ne 0 -or $waitingRunsCount -ne 0) { while ($runningRunsCount -ne 0 -or $waitingRunsCount -ne 0) { @@ -85,10 +97,12 @@ function ExecuteCheckType() { Write-Debug "Number of running runs: $runningRunsCount" Write-Debug "Number of waiting runs: $waitingRunsCount" Start-Sleep -Second 10 - $runHistory = Invoke-WebRequest @params -ErrorAction Stop - $runHistoryContent = $runHistory.Content | ConvertFrom-Json - $runningRunsCount = ($runHistoryContent.value | Where-Object { $_.properties.status -eq "Running" }).Count - $waitingRunsCount = ($runHistoryContent.value | Where-Object { $_.properties.status -eq "Waiting" }).Count + $runHistoryRunning = Invoke-WebRequest @$listRunningParams -ErrorAction Stop + $runHistoryRunningContent = $runHistoryRunning.Content | ConvertFrom-Json + $runHistoryWaiting = Invoke-WebRequest @$lisWaitingParams -ErrorAction Stop + $runHistoryWaitingContent = $runHistoryWaiting.Content | ConvertFrom-Json + $runningRunsCount = ($runHistoryRunningContent.value).Count + $waitingRunsCount = ($runHistoryWaitingContent.value).Count if ($runningRunsCount -eq 0 -and $waitingRunsCount -eq 0) { Write-Verbose "Found no more waiting or running runs for Workflow '$WorkflowName' in Azure Logic App '$LogicAppName', executing stopType for Logic App Workflow" diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 index fab22226..e6e82adf 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 @@ -3,7 +3,9 @@ param( [string][parameter(Mandatory = $true)] $SubscriptionId, [string][parameter(Mandatory = $true)] $ResourceGroupName, [string][parameter(Mandatory = $true)] $LogicAppName, - [string][parameter(Mandatory = $true)] $WorkflowName + [string][parameter(Mandatory = $true)] $WorkflowName, + [string][parameter(Mandatory = $false)] $RunName, + [string][Parameter(Mandatory = $true)][ValidateSet('listWaiting', 'listRunning','cancel')] $Action ) try { @@ -27,7 +29,14 @@ try { $resourceManagerUrl = (Get-AzEnvironment -Name $EnvironmentName).ResourceManagerUrl - $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/hostruntime/runtime/webhooks/workflow/api/management/workflows/$WorkflowName/runs?api-version=2022-03-01" + if ($Action -eq 'listWaiting') { + $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/hostruntime/runtime/webhooks/workflow/api/management/workflows/$WorkflowName/runs?api-version=2022-03-01&%24filter=Status%20eq%20'Waiting'" + } elseIf ($Action -eq 'listRunning') { + $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/hostruntime/runtime/webhooks/workflow/api/management/workflows/$WorkflowName/runs?api-version=2022-03-01&%24filter=Status%20eq%20'Running'" + } elseIf ($Action -eq 'cancel') { + $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/hostruntime/runtime/webhooks/workflow/api/management/workflows/$WorkflowName/runs/$RunName/cancel?api-version=2022-03-01" + } + return $fullUrl } catch { Write-Warning "Failed to define the resource management endpoint (Environment: '$EnvironmentName', SubscriptionId: '$SubscriptionId', ResourceGroup: '$ResourceGroupName', LogicApp: '$LogicAppName', WorkflowName: '$WorkflowName')" diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 index 85b56b7b..b20dae8d 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 @@ -890,7 +890,7 @@ InModuleScope Arcus.Scripting.LogicApps { if ($script:i -gt 2) { Write-Host "Returning empty (no running, no waiting) runs" return [pscustomobject] @{ - Content = '{"value":[{"properties":{"status":"Succeeded"}}]}' + Content = '{"value":[]}' } } else { Write-Host "Returning 1 running run" @@ -913,7 +913,7 @@ InModuleScope Arcus.Scripting.LogicApps { # Assert Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 - Assert-MockCalled Invoke-WebRequest -Scope It -Exactly 5 + Assert-MockCalled Invoke-WebRequest -Scope It -Exactly 8 Assert-MockCalled Disable-AzLogicApp -Scope It -Exactly 0 } It "Doesn't disable anything when checkType = NoWaitingOrRunningRuns but returns a waiting run for stopType = None" { @@ -927,7 +927,7 @@ InModuleScope Arcus.Scripting.LogicApps { if ($script:i -gt 2) { Write-Host "Returning empty (no running, no waiting) runs" return [pscustomobject] @{ - Content = '{"value":[{"properties":{"status":"Succeeded"}}]}' + Content = '{"value":[]}' } } else { Write-Host "Returning 1 waiting run" @@ -950,7 +950,7 @@ InModuleScope Arcus.Scripting.LogicApps { # Assert Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 - Assert-MockCalled Invoke-WebRequest -Scope It -Exactly 4 + Assert-MockCalled Invoke-WebRequest -Scope It -Exactly 6 Assert-MockCalled Disable-AzLogicApp -Scope It -Exactly 0 } It "Disables all logic apps when checkType = NoWaitingOrRunningRuns with found waiting and no running runs for stopType = Immediate" { @@ -964,7 +964,7 @@ InModuleScope Arcus.Scripting.LogicApps { if ($script:i -gt 2) { Write-Host "Returning empty (no running, no waiting) runs" return [pscustomobject] @{ - Content = '{"value":[{"properties":{"status":"Succeeded"}}]}' + Content = '{"value":[]}' } } else { Write-Host "Returning 1 waiting run" @@ -990,7 +990,7 @@ InModuleScope Arcus.Scripting.LogicApps { # Assert Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 - Assert-MockCalled Invoke-WebRequest -Scope It -Exactly 7 + Assert-MockCalled Invoke-WebRequest -Scope It -Exactly 12 Assert-MockCalled Disable-AzLogicApp -Scope It -Exactly 5 -ParameterFilter { $resourceGroupName -eq $resourceGroup } } It "Disables all logic apps when checkType = NoWaitingOrRunningRuns with found waiting and running runs for stopType = Immediate" { @@ -1003,7 +1003,7 @@ InModuleScope Arcus.Scripting.LogicApps { if ($script:i -gt 2) { Write-Host "Returning empty (no running, no waiting) runs" return [pscustomobject] @{ - Content = '{"value":[{"properties":{"status":"Succeeded"}},{"properties":{"status":"Succeeded"}}]}' + Content = '{"value":[]}' } } else { Write-Host "Returning 1 waiting run and 1 running run" @@ -1029,7 +1029,7 @@ InModuleScope Arcus.Scripting.LogicApps { # Assert Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 - Assert-MockCalled Invoke-WebRequest -Scope It -Exactly 3 + Assert-MockCalled Invoke-WebRequest -Scope It -Exactly 4 Assert-MockCalled Disable-AzLogicApp -Scope It -Exactly 1 -ParameterFilter { $ResourceGroupName -eq $resourceGroup -and $LogicAppName -eq "snd-async" } } } From 44a7f768fb0814b4a7d2ad7ef35f761a0c0ef64d Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Fri, 3 Nov 2023 15:13:35 +0100 Subject: [PATCH 08/24] fix url params --- .../Scripts/Disable-AzLogicAppsFromConfig.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 index 82f74e5e..9be03896 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 @@ -74,7 +74,7 @@ function ExecuteCheckType() { } URI = $listRunningUrl } - $runHistoryRunning = Invoke-WebRequest @$listRunningParams -ErrorAction Stop + $runHistoryRunning = Invoke-WebRequest @listRunningParams -ErrorAction Stop $runHistoryRunningContent = $runHistoryRunning.Content | ConvertFrom-Json $listWaitingUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -Action 'listWaiting' @@ -85,7 +85,7 @@ function ExecuteCheckType() { } URI = $lisWaitingParams } - $runHistoryWaiting = Invoke-WebRequest @$lisWaitingParams -ErrorAction Stop + $runHistoryWaiting = Invoke-WebRequest @lisWaitingParams -ErrorAction Stop $runHistoryWaitingContent = $runHistoryWaiting.Content | ConvertFrom-Json $runningRunsCount = ($runHistoryRunningContent.value).Count @@ -97,9 +97,9 @@ function ExecuteCheckType() { Write-Debug "Number of running runs: $runningRunsCount" Write-Debug "Number of waiting runs: $waitingRunsCount" Start-Sleep -Second 10 - $runHistoryRunning = Invoke-WebRequest @$listRunningParams -ErrorAction Stop + $runHistoryRunning = Invoke-WebRequest @listRunningParams -ErrorAction Stop $runHistoryRunningContent = $runHistoryRunning.Content | ConvertFrom-Json - $runHistoryWaiting = Invoke-WebRequest @$lisWaitingParams -ErrorAction Stop + $runHistoryWaiting = Invoke-WebRequest @lisWaitingParams -ErrorAction Stop $runHistoryWaitingContent = $runHistoryWaiting.Content | ConvertFrom-Json $runningRunsCount = ($runHistoryRunningContent.value).Count $waitingRunsCount = ($runHistoryWaitingContent.value).Count From 3f5a7b9d61a2047d1411b4b40256178013dd2965 Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Fri, 3 Nov 2023 15:17:57 +0100 Subject: [PATCH 09/24] added Logic App Standard support for `Cancel-AzLogicAppRuns` --- .../Scripts/Cancel-AzLogicAppRuns.ps1 | 50 ++++++++++++++++--- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 index 3ba9502d..bef74f38 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 @@ -1,19 +1,55 @@ param( [Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of the resource group is required"), [Parameter(Mandatory = $true)][string] $LogicAppName = $(throw "Name of the logic app is required"), + [Parameter(Mandatory = $false)][string] $WorkflowName = "", + [Parameter(Mandatory = $false)][string] $EnvironmentName = "AzureCloud", [Parameter(Mandatory = $false)][int] $MaximumFollowNextPageLink = 10 ) +$Global:accessToken = ""; +$Global:subscriptionId = ""; + try { - $runs = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $LogicAppName -FollowNextPageLink -MaximumFollowNextPageLink $MaximumFollowNextPageLink | - Where-Object {$_.Status -eq 'Running'} + if ($WorkflowName -eq "") { + $runs = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $LogicAppName -FollowNextPageLink -MaximumFollowNextPageLink $MaximumFollowNextPageLink | + Where-Object {$_.Status -eq 'Running'} - foreach ($run in $runs) { - Stop-AzLogicAppRun -ResourceGroupName $ResourceGroupName -Name $LogicAppName -RunName $run.Name -Force - Write-Verbose "Cancelled run $run.Name for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" - } + foreach ($run in $runs) { + Stop-AzLogicAppRun -ResourceGroupName $ResourceGroupName -Name $LogicAppName -RunName $run.Name -Force + Write-Verbose "Cancelled run $run.Name for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + } + + Write-Host "Successfully cancelled all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" -ForegroundColor Green + } else { + $token = Get-AzCachedAccessToken -AssignGlobalVariables + + $listRunningUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -Action 'listRunning' + $listRunningParams = @{ + Method = 'Get' + Headers = @{ + 'authorization'="Bearer $Global:accessToken" + } + URI = $listRunningUrl + } - Write-Host "Successfully cancelled all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" -ForegroundColor Green + $runs = Invoke-WebRequest @listRunningParams -ErrorAction Stop + $runsContent = $runs.Content | ConvertFrom-Json + + foreach ($run in $runsContent.value) { + $runName = $run.name + + $cancelUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -RunName $runName -Action 'cancel' + $cancelParams = @{ + Method = 'Post' + Headers = @{ + 'authorization'="Bearer $Global:accessToken" + } + URI = $cancelUrl + } + $cancel = Invoke-WebRequest @cancelParams -ErrorAction Stop + Write-Verbose "Cancelled run '$runName' for the workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + } + } } catch { throw "Failed to cancel all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: $($_.Exception.Message)" } \ No newline at end of file From 361363fa39ffca26597d046fd6bb11cc4cb79b70 Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Fri, 3 Nov 2023 15:27:21 +0100 Subject: [PATCH 10/24] fixes --- .../Scripts/Disable-AzLogicAppsFromConfig.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 index 9be03896..2831a9df 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 @@ -78,14 +78,14 @@ function ExecuteCheckType() { $runHistoryRunningContent = $runHistoryRunning.Content | ConvertFrom-Json $listWaitingUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -Action 'listWaiting' - $lisWaitingParams = @{ + $listWaitingParams = @{ Method = 'Get' Headers = @{ 'authorization'="Bearer $Global:accessToken" } - URI = $lisWaitingParams + URI = $listWaitingUrl } - $runHistoryWaiting = Invoke-WebRequest @lisWaitingParams -ErrorAction Stop + $runHistoryWaiting = Invoke-WebRequest @listWaitingParams -ErrorAction Stop $runHistoryWaitingContent = $runHistoryWaiting.Content | ConvertFrom-Json $runningRunsCount = ($runHistoryRunningContent.value).Count @@ -99,7 +99,7 @@ function ExecuteCheckType() { Start-Sleep -Second 10 $runHistoryRunning = Invoke-WebRequest @listRunningParams -ErrorAction Stop $runHistoryRunningContent = $runHistoryRunning.Content | ConvertFrom-Json - $runHistoryWaiting = Invoke-WebRequest @lisWaitingParams -ErrorAction Stop + $runHistoryWaiting = Invoke-WebRequest @listWaitingParams -ErrorAction Stop $runHistoryWaitingContent = $runHistoryWaiting.Content | ConvertFrom-Json $runningRunsCount = ($runHistoryRunningContent.value).Count $waitingRunsCount = ($runHistoryWaitingContent.value).Count From 73d720a82108a6bc5d141d1a0d83185c8e7985d7 Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Fri, 3 Nov 2023 15:45:11 +0100 Subject: [PATCH 11/24] added unittests for `Cancel-AzLogicAppRuns` for Logic App Standard --- .../Arcus.Scripting.LogicApps.psm1 | 16 +++++++- .../Arcus.Scripting.LogicApps.tests.ps1 | 38 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psm1 b/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psm1 index bec3b003..47cdc0f0 100644 --- a/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psm1 +++ b/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psm1 @@ -10,6 +10,12 @@ .Parameter LogicAppName The name of the Azure Logic App. + + .Parameter WorkflowName + The name of the workflow in the Azure Logic App. + + .Parameter EnvironmentName + [Optional] The Azure Cloud environment in which the Azure Logic App resides. .Parameter MaximumFollowNextPageLink The number of times the script should retrieve the next page of Logic App runs, if not specified the entire run history of the Logic App will be retrieved. @@ -19,10 +25,12 @@ function Cancel-AzLogicAppRuns { param( [Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of the resource group is required"), [Parameter(Mandatory = $true)][string] $LogicAppName = $(throw "Name of the logic app is required"), + [Parameter(Mandatory = $false)][string] $WorkflowName = "", + [Parameter(Mandatory = $false)][string] $EnvironmentName = "AzureCloud", [Parameter(Mandatory = $false)][int] $MaximumFollowNextPageLink = 10 ) - . $PSScriptRoot\Scripts\Cancel-AzLogicAppRuns.ps1 -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -MaximumFollowNextPageLink $MaximumFollowNextPageLink + . $PSScriptRoot\Scripts\Cancel-AzLogicAppRuns.ps1 -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -EnvironmentName $EnvironmentName -MaximumFollowNextPageLink $MaximumFollowNextPageLink } Export-ModuleMember -Function Cancel-AzLogicAppRuns @@ -86,6 +94,9 @@ Export-ModuleMember -Function Resubmit-FailedAzLogicAppRuns .Parameter LogicAppName The name of the Azure Logic App to be enabled. + + .Parameter WorkflowName + The name of the workflow in the Azure Logic App. .Parameter ApiVersion [Optional] The version of the api to be used to disable the Azure Logic App. @@ -164,6 +175,9 @@ Export-ModuleMember -Function Disable-AzLogicAppsFromConfig .Parameter LogicAppName The name of the Azure Logic App to be enabled. + + .Parameter WorkflowName + The name of the workflow in the Azure Logic App. .Parameter ApiVersion [Optional] The version of the api to be used to enable the Azure Logic App. diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 index b20dae8d..2f44b70f 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 @@ -1033,5 +1033,43 @@ InModuleScope Arcus.Scripting.LogicApps { Assert-MockCalled Disable-AzLogicApp -Scope It -Exactly 1 -ParameterFilter { $ResourceGroupName -eq $resourceGroup -and $LogicAppName -eq "snd-async" } } } + Context "Cancel Logic Apps runs" { + It "Cancelling all runs from Logic App history should succeed" { + # Arrange + $resourceGroupName = "codit-arcus-scripting" + $logicAppName = "arc-dev-we-rcv-unknown-http" + $workflowName = "test" + + Mock Invoke-WebRequest -MockWith { + return [pscustomobject] @{ + Content = '{"value":[{"properties":{"status":"Running"}},{"properties":{"status":"Running"}}]}' + } + } + + # Act + { Cancel-AzLogicAppRuns -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -WorkflowName $workflowName } | + Should -Not -Throw + + # Assert + Assert-VerifiableMock + Assert-MockCalled Invoke-WebRequest -Scope It -Times 3 + } + It "Cancelling all runs should fail when retrieving Logic App history fails" { + # Arrange + $resourceGroupName = "codit-arcus-scripting" + $logicAppName = "arc-dev-we-rcv-unknown-http" + $workflowName = "test" + + Mock Invoke-WebRequest -MockWith { throw 'some error' } + + # Act + { Cancel-AzLogicAppRuns -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -WorkflowName $workflowName } | + Should -Throw -ExpectedMessage "Failed to cancel all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: some error" + + # Assert + Assert-VerifiableMock + Assert-MockCalled Invoke-WebRequest -Scope It -Times 1 + } + } } } \ No newline at end of file From 679e51f882636dc1a6fe46907b7a2ab25ad801c1 Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Wed, 15 Nov 2023 08:54:58 +0100 Subject: [PATCH 12/24] aligned error handling --- .../Scripts/Cancel-AzLogicAppRuns.ps1 | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 index bef74f38..de7f6cf4 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 @@ -52,4 +52,13 @@ try { } } catch { throw "Failed to cancel all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: $($_.Exception.Message)" +} +} catch { + if ($WorkflowName -eq "") { + Write-Warning "Failed to cancel all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + } else { + Write-Warning "Failed to cancel all running instances for the workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + } + $ErrorMessage = $_.Exception.Message + Write-Debug "Error: $ErrorMessage" } \ No newline at end of file From be418e10e8bc5fc43404eaa8209c66bc5314cd88 Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Wed, 15 Nov 2023 08:59:53 +0100 Subject: [PATCH 13/24] add logic app standard support for `Resubmit-FailedAzLogicAppRuns` --- .../Arcus.Scripting.LogicApps.psm1 | 28 ++++-- ...zLogicAppStandardResourceManagementUrl.ps1 | 7 +- .../Scripts/Resubmit-FailedAzLogicAppRuns.ps1 | 99 +++++++++++++------ 3 files changed, 93 insertions(+), 41 deletions(-) diff --git a/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psm1 b/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psm1 index 47cdc0f0..0e41ff53 100644 --- a/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psm1 +++ b/src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psm1 @@ -12,13 +12,13 @@ The name of the Azure Logic App. .Parameter WorkflowName - The name of the workflow in the Azure Logic App. + [Optional] The name of the workflow in the Azure Logic App Standard Site. .Parameter EnvironmentName [Optional] The Azure Cloud environment in which the Azure Logic App resides. .Parameter MaximumFollowNextPageLink - The number of times the script should retrieve the next page of Logic App runs, if not specified the entire run history of the Logic App will be retrieved. + [Optional] The number of times the script should retrieve the next page of Logic App runs, if not specified the entire run history of the Logic App will be retrieved. #> function Cancel-AzLogicAppRuns { @@ -48,29 +48,37 @@ Export-ModuleMember -Function Cancel-AzLogicAppRuns .Parameter LogicAppName The name of the Azure Logic App. + .Parameter WorkflowName + [Optional] The name of the workflow in the Azure Logic App Standard Site. + + .Parameter EnvironmentName + [Optional] The Azure Cloud environment in which the Azure Logic App resides. + .Parameter StartTime The start time of the failed instances of the Azure Logic App. .Parameter EndTime - The end time of the failed instances of the Azure Logic App. + [Optional] The end time of the failed instances of the Azure Logic App. .Parameter MaximumFollowNextPageLink - The number of times the script should retrieve the next page of Logic App runs, if not specified the entire run history of the Logic App will be retrieved. + [Optional] The number of times the script should retrieve the next page of Logic App runs, if not specified the entire run history of the Logic App will be retrieved. #> function Resubmit-FailedAzLogicAppRuns { param( [Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of the resource group is required"), [Parameter(Mandatory = $true)][string] $LogicAppName = $(throw "Name of the logic app is required"), + [Parameter(Mandatory = $false)][string] $WorkflowName = "", [Parameter(Mandatory = $true)][datetime] $StartTime = $(throw "Start time is required"), [Parameter(Mandatory = $false)][datetime] $EndTime, + [Parameter(Mandatory = $false)][string] $EnvironmentName = "AzureCloud", [Parameter(Mandatory = $false)][int] $MaximumFollowNextPageLink = 10 ) if ($EndTime) { - . $PSScriptRoot\Scripts\Resubmit-FailedAzLogicAppRuns.ps1 -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -StartTime $StartTime -EndTime $EndTime -MaximumFollowNextPageLink $MaximumFollowNextPageLink + . $PSScriptRoot\Scripts\Resubmit-FailedAzLogicAppRuns.ps1 -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -EnvironmentName $EnvironmentName -StartTime $StartTime -EndTime $EndTime -MaximumFollowNextPageLink $MaximumFollowNextPageLink } else { - . $PSScriptRoot\Scripts\Resubmit-FailedAzLogicAppRuns.ps1 -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -StartTime $StartTime -MaximumFollowNextPageLink $MaximumFollowNextPageLink + . $PSScriptRoot\Scripts\Resubmit-FailedAzLogicAppRuns.ps1 -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -EnvironmentName $EnvironmentName -StartTime $StartTime -MaximumFollowNextPageLink $MaximumFollowNextPageLink } } @@ -96,7 +104,7 @@ Export-ModuleMember -Function Resubmit-FailedAzLogicAppRuns The name of the Azure Logic App to be enabled. .Parameter WorkflowName - The name of the workflow in the Azure Logic App. + [Optional] The name of the workflow in the Azure Logic App Standard Site. .Parameter ApiVersion [Optional] The version of the api to be used to disable the Azure Logic App. @@ -135,7 +143,7 @@ Export-ModuleMember -Function Disable-AzLogicApp If your solution consists of multiple interfaces, you can specify the flow-specific name of the configuration file, if not, the script will look for a file named 'deploy-orderControl.json' by default. .Parameter ResourcePrefix - The prefix assigned to all Azure Logic Apps, which can differ per environment. + [Optional] The prefix assigned to all Azure Logic Apps, which can differ per environment. .Parameter EnvironmentName [Optional] The Azure Cloud environment in which the Azure Logic App resides. @@ -177,7 +185,7 @@ Export-ModuleMember -Function Disable-AzLogicAppsFromConfig The name of the Azure Logic App to be enabled. .Parameter WorkflowName - The name of the workflow in the Azure Logic App. + [Optional] The name of the workflow in the Azure Logic App Standard Site. .Parameter ApiVersion [Optional] The version of the api to be used to enable the Azure Logic App. @@ -217,7 +225,7 @@ Export-ModuleMember -Function Enable-AzLogicApp If your solution consists of multiple interfaces, you can specify the flow-specific name of the configuration file, if not, the script will look for a file named 'deploy-orderControl.json' by default. .Parameter ResourcePrefix - The prefix assigned to all Azure Logic Apps, which can differ per environment. + [Optional] The prefix assigned to all Azure Logic Apps, which can differ per environment. .Parameter EnvironmentName [Optional] The Azure Cloud environment in which the Azure Logic App resides. diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 index e6e82adf..dbc15493 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 @@ -5,7 +5,8 @@ param( [string][parameter(Mandatory = $true)] $LogicAppName, [string][parameter(Mandatory = $true)] $WorkflowName, [string][parameter(Mandatory = $false)] $RunName, - [string][Parameter(Mandatory = $true)][ValidateSet('listWaiting', 'listRunning','cancel')] $Action + [string][parameter(Mandatory = $false)] $TriggerName, + [string][Parameter(Mandatory = $true)][ValidateSet('listWaiting', 'listRunning', 'listFailed', 'cancel', 'resubmit')] $Action ) try { @@ -33,8 +34,12 @@ try { $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/hostruntime/runtime/webhooks/workflow/api/management/workflows/$WorkflowName/runs?api-version=2022-03-01&%24filter=Status%20eq%20'Waiting'" } elseIf ($Action -eq 'listRunning') { $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/hostruntime/runtime/webhooks/workflow/api/management/workflows/$WorkflowName/runs?api-version=2022-03-01&%24filter=Status%20eq%20'Running'" + } elseIf ($Action -eq 'listFailed') { + $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/hostruntime/runtime/webhooks/workflow/api/management/workflows/$WorkflowName/runs?api-version=2022-03-01&%24filter=Status%20eq%20'Failed'" } elseIf ($Action -eq 'cancel') { $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/hostruntime/runtime/webhooks/workflow/api/management/workflows/$WorkflowName/runs/$RunName/cancel?api-version=2022-03-01" + } elseIf ($Action -eq 'resubmit') { + $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/hostruntime/runtime/webhooks/workflow/api/management/workflows/$WorkflowName/triggers/$TriggerName/histories/$RunName/resubmit?api-version=2022-03-01" } return $fullUrl diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 index b72e633c..42dd136c 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 @@ -1,51 +1,90 @@ param( [Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of the resource group is required"), [Parameter(Mandatory = $true)][string] $LogicAppName = $(throw "Name of the logic app is required"), + [Parameter(Mandatory = $false)][string] $WorkflowName = "", [Parameter(Mandatory = $true)][datetime] $StartTime = $(throw "Start time is required"), [Parameter(Mandatory = $false)][datetime] $EndTime, - [Parameter(Mandatory = $false)][int] $MaximumFollowNextPageLink = 10 + [Parameter(Mandatory = $false)][int] $MaximumFollowNextPageLink = 10, + [Parameter(Mandatory = $false)][string] $EnvironmentName = "AzureCloud" ) +$Global:accessToken = ""; +$Global:subscriptionId = ""; + try{ - if ($EndTime) { - $runs = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $LogicAppName -FollowNextPageLink -MaximumFollowNextPageLink $MaximumFollowNextPageLink | - Where-Object {$_.Status -eq 'Failed' -and $_.StartTime -ge $StartTime -and $_.EndTime -le $EndTime} - } else { - $runs = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $LogicAppName -FollowNextPageLink -MaximumFollowNextPageLink $MaximumFollowNextPageLink | - Where-Object {$_.Status -eq 'Failed' -and $_.StartTime -ge $StartTime} - } + $token = Get-AzCachedAccessToken -AssignGlobalVariables - $token = Get-AzCachedAccessToken - $accessToken = $token.AccessToken - $subscriptionId = $token.SubscriptionId + if ($WorkflowName -eq "") { + if ($EndTime) { + $runs = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $LogicAppName -FollowNextPageLink -MaximumFollowNextPageLink $MaximumFollowNextPageLink | + Where-Object {$_.Status -eq 'Failed' -and $_.StartTime -ge $StartTime -and $_.EndTime -le $EndTime} + } else { + $runs = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $LogicAppName -FollowNextPageLink -MaximumFollowNextPageLink $MaximumFollowNextPageLink | + Where-Object {$_.Status -eq 'Failed' -and $_.StartTime -ge $StartTime} + } + + foreach ($run in $runs) { + $triggerName = $run.Trigger.Name + $runId = $run.Name + $resubmitUrl = "https://management.azure.com/subscriptions/$Global:subscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Logic/workflows/$LogicAppName/triggers/$triggerName/histories/$runId/resubmit?api-version=2016-06-01" + + $params = @{ + Method = 'Post' + Headers = @{ + 'authorization'="Bearer $Global:accessToken" + } + URI = $resubmitUrl + } - foreach ($run in $runs) { - $triggerName = $run.Trigger.Name - $runId = $run.Name - $resubmitUrl = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Logic/workflows/$LogicAppName/triggers/$triggerName/histories/$runId/resubmit?api-version=2016-06-01" + $web = Invoke-WebRequest @params -ErrorAction Stop - $params = @{ - Method = 'Post' + Write-Verbose "Resubmitted run $runId for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + } + + if ($EndTime) { + Write-Host "Successfully resubmitted all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime' and until '$EndTime'" -ForegroundColor Green + } else { + Write-Host "Successfully resubmitted all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'" -ForegroundColor Green + } + } else { + $listFailedUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -Action 'listFailed' + $listFailedParams = @{ + Method = 'Get' Headers = @{ - 'authorization'="Bearer $accessToken" + 'authorization'="Bearer $Global:accessToken" } - URI = $resubmitUrl + URI = $listFailedUrl } - $web = Invoke-WebRequest @params -ErrorAction Stop - - Write-Verbose "Resubmitted run $runId for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" - } + $failedRuns = Invoke-WebRequest @listFailedParams -ErrorAction Stop + $failedRunsContent = $failedRuns.Content | ConvertFrom-Json - if ($EndTime) { - Write-Host "Successfully resubmitted all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime' and until '$EndTime'" -ForegroundColor Green - } else { - Write-Host "Successfully resubmitted all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'" -ForegroundColor Green + foreach ($failedRun in $failedRunsContent.value) { + $runName = $failedRun.name + $triggerName = $failedRun.properties.trigger.name + + $resubmitUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -TriggerName $triggerName -RunName $runName -Action 'resubmit' + $resubmitParams = @{ + Method = 'Post' + Headers = @{ + 'authorization'="Bearer $Global:accessToken" + } + URI = $resubmitUrl + } + $resubmit = Invoke-WebRequest @resubmitParams -ErrorAction Stop + Write-Verbose "Resubmit run '$runName' for the workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + } } } catch { - if ($EndTime) { - throw "Failed to resubmit all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime' and until '$EndTime'. Details: $($_.Exception.Message)" + if ($WorkflowName -eq "") { + if ($EndTime) { + Write-Warning "Failed to resubmit all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime' and until '$EndTime'. Details: $($_.Exception.Message)" + } else { + Write-Warning "Failed to resubmit all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'. Details: $($_.Exception.Message)" + } } else { - throw "Failed to resubmit all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'. Details: $($_.Exception.Message)" + Write-Warning "Failed to resubmit all failed instances for the workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'. Details: $($_.Exception.Message)" } + $ErrorMessage = $_.Exception.Message + Write-Debug "Error: $ErrorMessage" } \ No newline at end of file From 78b98970b4f49a3cecc55a68f1f6b0b93e9bf61e Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Wed, 15 Nov 2023 09:36:55 +0100 Subject: [PATCH 14/24] added support for nextlink --- ...zLogicAppStandardResourceManagementUrl.ps1 | 4 ++- .../Scripts/Resubmit-FailedAzLogicAppRuns.ps1 | 27 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 index dbc15493..6cd629e1 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Get-AzLogicAppStandardResourceManagementUrl.ps1 @@ -6,6 +6,7 @@ param( [string][parameter(Mandatory = $true)] $WorkflowName, [string][parameter(Mandatory = $false)] $RunName, [string][parameter(Mandatory = $false)] $TriggerName, + [datetime][parameter(Mandatory = $false)] $StartTime, [string][Parameter(Mandatory = $true)][ValidateSet('listWaiting', 'listRunning', 'listFailed', 'cancel', 'resubmit')] $Action ) @@ -35,7 +36,8 @@ try { } elseIf ($Action -eq 'listRunning') { $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/hostruntime/runtime/webhooks/workflow/api/management/workflows/$WorkflowName/runs?api-version=2022-03-01&%24filter=Status%20eq%20'Running'" } elseIf ($Action -eq 'listFailed') { - $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/hostruntime/runtime/webhooks/workflow/api/management/workflows/$WorkflowName/runs?api-version=2022-03-01&%24filter=Status%20eq%20'Failed'" + $StartTimeFormatted = $StartTime.ToString("yyyy-MM-ddTHH:mm:ssZ") + $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/hostruntime/runtime/webhooks/workflow/api/management/workflows/$WorkflowName/runs?api-version=2022-03-01&%24filter=Status%20eq%20'Failed'%20and%20startTime%20ge%20$StartTimeFormatted" } elseIf ($Action -eq 'cancel') { $fullUrl = "$resourceManagerUrl" + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/hostruntime/runtime/webhooks/workflow/api/management/workflows/$WorkflowName/runs/$RunName/cancel?api-version=2022-03-01" } elseIf ($Action -eq 'resubmit') { diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 index 42dd136c..62ab0490 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 @@ -47,7 +47,7 @@ try{ Write-Host "Successfully resubmitted all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'" -ForegroundColor Green } } else { - $listFailedUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -Action 'listFailed' + $listFailedUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -StartTime $StartTime -Action 'listFailed' $listFailedParams = @{ Method = 'Get' Headers = @{ @@ -58,8 +58,29 @@ try{ $failedRuns = Invoke-WebRequest @listFailedParams -ErrorAction Stop $failedRunsContent = $failedRuns.Content | ConvertFrom-Json + $allFailedRuns = $failedRunsContent.value - foreach ($failedRun in $failedRunsContent.value) { + if ($failedRunsContent.nextLink -ne $null) { + $nextPageCounter = 1 + $nextPageUrl = $failedRunsContent.nextLink + while ($nextPageUrl -ne $null -and $nextPageCounter -le $MaximumFollowNextPageLink) { + $nextPageCounter = $nextPageCounter + 1 + $listFailedParams = @{ + Method = 'Get' + Headers = @{ + 'authorization'="Bearer $Global:accessToken" + } + URI = $nextPageUrl + } + + $failedRunsNextPage = Invoke-WebRequest @listFailedParams -ErrorAction Stop + $failedRunsNextPageContent = $failedRunsNextPage.Content | ConvertFrom-Json + $nextPageUrl = $failedRunsNextPageContent.nextLink + $allFailedRuns = $allFailedRuns + $failedRunsNextPageContent.value + } + } + + foreach ($failedRun in $allFailedRuns) { $runName = $failedRun.name $triggerName = $failedRun.properties.trigger.name @@ -74,6 +95,8 @@ try{ $resubmit = Invoke-WebRequest @resubmitParams -ErrorAction Stop Write-Verbose "Resubmit run '$runName' for the workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" } + + Write-Host "Successfully resubmitted all failed instances for the workflow '$WorkflowName' in the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'" -ForegroundColor Green } } catch { if ($WorkflowName -eq "") { From 5984114e54cd9f39e06cca29c05136b16c138dad Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Wed, 15 Nov 2023 09:49:16 +0100 Subject: [PATCH 15/24] added nextlink functionality to `Cancel-AzLogicAppRuns` --- .../Scripts/Cancel-AzLogicAppRuns.ps1 | 28 ++++++++++++++++--- .../Scripts/Resubmit-FailedAzLogicAppRuns.ps1 | 2 +- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 index de7f6cf4..d4811a46 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 @@ -34,8 +34,29 @@ try { $runs = Invoke-WebRequest @listRunningParams -ErrorAction Stop $runsContent = $runs.Content | ConvertFrom-Json + $allRuns = $runsContent.value - foreach ($run in $runsContent.value) { + if ($runsContent.nextLink -ne $null) { + $nextPageCounter = 1 + $nextPageUrl = $runsContent.nextLink + while ($nextPageUrl -ne $null -and $nextPageCounter -le $MaximumFollowNextPageLink) { + $nextPageCounter = $nextPageCounter + 1 + $listRunningParams = @{ + Method = 'Get' + Headers = @{ + 'authorization'="Bearer $Global:accessToken" + } + URI = $nextPageUrl + } + + $runsNextPage = Invoke-WebRequest @listRunningParams -ErrorAction Stop + $runsNextPageContent = $runsNextPage.Content | ConvertFrom-Json + $nextPageUrl = $runsNextPageContent.nextLink + $allRuns = $allRuns + $runsNextPageContent.value + } + } + + foreach ($run in $allRuns) { $runName = $run.name $cancelUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -RunName $runName -Action 'cancel' @@ -49,10 +70,9 @@ try { $cancel = Invoke-WebRequest @cancelParams -ErrorAction Stop Write-Verbose "Cancelled run '$runName' for the workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" } + + Write-Host "Successfully cancelled all running instances for the workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" -ForegroundColor Green } -} catch { - throw "Failed to cancel all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: $($_.Exception.Message)" -} } catch { if ($WorkflowName -eq "") { Write-Warning "Failed to cancel all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 index 62ab0490..f2009e91 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 @@ -58,7 +58,7 @@ try{ $failedRuns = Invoke-WebRequest @listFailedParams -ErrorAction Stop $failedRunsContent = $failedRuns.Content | ConvertFrom-Json - $allFailedRuns = $failedRunsContent.value + $allFailedRuns = $failedRunsContent.value if ($failedRunsContent.nextLink -ne $null) { $nextPageCounter = 1 From 85dca7aff5e1fc7071d19da3c0187157f2bf31fb Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Wed, 15 Nov 2023 10:26:53 +0100 Subject: [PATCH 16/24] align error handling and unit tests --- .../Scripts/Cancel-AzLogicAppRuns.ps1 | 6 ++-- .../Scripts/Disable-AzLogicApp.ps1 | 6 ++-- .../Scripts/Enable-AzLogicApp.ps1 | 6 ++-- .../Scripts/Resubmit-FailedAzLogicAppRuns.ps1 | 8 ++---- .../Arcus.Scripting.LogicApps.tests.ps1 | 28 +++++++------------ 5 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 index d4811a46..c75e8270 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 @@ -75,10 +75,8 @@ try { } } catch { if ($WorkflowName -eq "") { - Write-Warning "Failed to cancel all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + throw "Failed to cancel all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: $($_.Exception.Message)" } else { - Write-Warning "Failed to cancel all running instances for the workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + throw "Failed to cancel all running instances for the workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: $($_.Exception.Message)" } - $ErrorMessage = $_.Exception.Message - Write-Debug "Error: $ErrorMessage" } \ No newline at end of file diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 index 8aac0af2..ecd57f86 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 @@ -37,10 +37,8 @@ try{ } } catch { if ($WorkflowName -eq "") { - Write-Warning "Failed to disable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + throw "Failed to disable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: $($_.Exception.Message)" } else { - Write-Warning "Failed to disable workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + throw "Failed to disable workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: $($_.Exception.Message)" } - $ErrorMessage = $_.Exception.Message - Write-Debug "Error: $ErrorMessage" } \ No newline at end of file diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 index 69f6b9ac..00e3bb6f 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Enable-AzLogicApp.ps1 @@ -38,10 +38,8 @@ try{ } catch { if ($WorkflowName -eq "") { - Write-Warning "Failed to enable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + throw "Failed to enable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: $($_.Exception.Message)" } else { - Write-Warning "Failed to enable workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + throw "Failed to enable workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: $($_.Exception.Message)" } - $ErrorMessage = $_.Exception.Message - Write-Debug "Error: $ErrorMessage" } diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 index f2009e91..9b6f9f67 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 @@ -101,13 +101,11 @@ try{ } catch { if ($WorkflowName -eq "") { if ($EndTime) { - Write-Warning "Failed to resubmit all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime' and until '$EndTime'. Details: $($_.Exception.Message)" + throw "Failed to resubmit all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime' and until '$EndTime'. Details: $($_.Exception.Message)" } else { - Write-Warning "Failed to resubmit all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'. Details: $($_.Exception.Message)" + throw "Failed to resubmit all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'. Details: $($_.Exception.Message)" } } else { - Write-Warning "Failed to resubmit all failed instances for the workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'. Details: $($_.Exception.Message)" + throw "Failed to resubmit all failed instances for the workflow '$WorkflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'. Details: $($_.Exception.Message)" } - $ErrorMessage = $_.Exception.Message - Write-Debug "Error: $ErrorMessage" } \ No newline at end of file diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 index 2f44b70f..ec952053 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 @@ -9,8 +9,6 @@ InModuleScope Arcus.Scripting.LogicApps { $resourceGroupName = "codit-arcus-scripting" $logicAppName = "arc-dev-we-rcv-unknown-http" $errorContent = "{""error"":{""code"":""ResourceNotFound"",""message"":""Unable to find the resource Microsoft.Logic/workflows/$logicAppName within resourcegroup codit-arcus-scripting.""}}" - Mock Write-Warning -MockWith { } - Mock Write-Debug -MockWith { } -ParameterFilter {$Message -like "Error: $errorContent" } Mock Invoke-WebRequest -MockWith { $status = [System.Net.WebExceptionStatus]::ConnectionClosed $response = New-Object -type 'System.Net.HttpWebResponse' @@ -26,15 +24,13 @@ InModuleScope Arcus.Scripting.LogicApps { } } - # Act - Enable-AzLogicApp -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName - + # Act + { Enable-AzLogicApp -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName } | + Should -Throw -ExpectedMessage "Failed to enable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: $errorContent" # Assert Assert-VerifiableMock Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 - Assert-MockCalled Write-Warning -Scope It -Times 1 -ParameterFilter { $Message -contains "Failed to enable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" } - Assert-MockCalled Write-Debug -Scope It -Times 1 -ParameterFilter { $Message -eq "Error: $errorContent" } } } Context "Enable Logic Apps with configuration" { @@ -101,14 +97,12 @@ InModuleScope Arcus.Scripting.LogicApps { } # Act - Disable-AzLogicApp -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName - + { Disable-AzLogicApp -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName } | + Should -Throw -ExpectedMessage "Failed to disable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: $errorContent" # Assert Assert-VerifiableMock Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 - Assert-MockCalled Write-Warning -Scope It -Times 1 -ParameterFilter { $Message -eq "Failed to disable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" } - Assert-MockCalled Write-Debug -Scope It -Times 1 -ParameterFilter { $Message -eq "Error: $errorContent" } } } Context "Disable Logic Apps with configuration" { @@ -715,18 +709,17 @@ InModuleScope Arcus.Scripting.LogicApps { $resourceGroupName = "codit-arcus-scripting" $logicAppName = "arc-dev-we-rcv-unknown-http" $workflowName = "test" - Mock Write-Warning -MockWith { } Mock Set-AzAppServiceSetting { Throw 'Not found error' } # Act - Enable-AzLogicApp -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -WorkflowName $workflowName + { Enable-AzLogicApp -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -WorkflowName $workflowName } | + Should -Throw -ExpectedMessage "Failed to enable workflow '$workflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: Not found error" # Assert Assert-VerifiableMock Assert-MockCalled Set-AzAppServiceSetting -Times 1 - Assert-MockCalled Write-Warning -Scope It -Times 1 -ParameterFilter { $Message -contains "Failed to enable workflow '$workflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" } } It "Enabling an Azure Logic App should succeed" { # Arrange @@ -791,18 +784,17 @@ InModuleScope Arcus.Scripting.LogicApps { $resourceGroupName = "codit-arcus-scripting" $logicAppName = "arc-dev-we-rcv-unknown-http" $workflowName = "test" - Mock Write-Warning -MockWith { } Mock Set-AzAppServiceSetting { Throw 'Not found error' } # Act - Disable-AzLogicApp -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -WorkflowName $workflowName + { Disable-AzLogicApp -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -WorkflowName $workflowName } | + Should -Throw -ExpectedMessage "Failed to disable workflow '$workflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: Not found error" # Assert Assert-VerifiableMock Assert-MockCalled Set-AzAppServiceSetting -Times 1 - Assert-MockCalled Write-Warning -Scope It -Times 1 -ParameterFilter { $Message -contains "Failed to disable workflow '$workflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" } } It "Disabling an Azure Logic App should succeed" { # Arrange @@ -1064,7 +1056,7 @@ InModuleScope Arcus.Scripting.LogicApps { # Act { Cancel-AzLogicAppRuns -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -WorkflowName $workflowName } | - Should -Throw -ExpectedMessage "Failed to cancel all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: some error" + Should -Throw -ExpectedMessage "Failed to cancel all running instances for the workflow '$workflowName' in Azure Logic App '$logicAppName' in resource group '$ResourceGroupName'. Details: some error" # Assert Assert-VerifiableMock From ea340e44db6e7e70fc7d50665db9ffb021294aae Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Wed, 15 Nov 2023 10:46:57 +0100 Subject: [PATCH 17/24] added unit tests for `Resubmit-FailedAzLogicAppRuns` --- .../Arcus.Scripting.LogicApps.tests.ps1 | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 index ec952053..6d92d34a 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 @@ -1063,5 +1063,66 @@ InModuleScope Arcus.Scripting.LogicApps { Assert-MockCalled Invoke-WebRequest -Scope It -Times 1 } } + Context "Resubmitting failed Logic Apps runs" { + It "Resubmitting a single failed run from Logic App history should succeed" { + # Arrange + $resourceGroupName = "codit-arcus-scripting" + $logicAppName = "arc-dev-we-rcv-unknown-http" + $workflowName = "test" + $startTime = '2023-01-01 00:00:00' + + Mock Invoke-WebRequest -MockWith { + return [pscustomobject] @{ + Content = '{"value":[{"properties":{"status":"Failed"}}]}' + } + } + + # Act + { Resubmit-FailedAzLogicAppRuns -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -WorkflowName $workflowName -StartTime $startTime } | + Should -Not -Throw + + # Assert + Assert-VerifiableMock + Assert-MockCalled Invoke-WebRequest -Scope It -Times 2 + } + It "Resubmitting multiple failed runs from Logic App history should succeed" { + # Arrange + $resourceGroupName = "codit-arcus-scripting" + $logicAppName = "arc-dev-we-rcv-unknown-http" + $workflowName = "test" + $startTime = '2023-01-01 00:00:00' + + Mock Invoke-WebRequest -MockWith { + return [pscustomobject] @{ + Content = '{"value":[{"properties":{"status":"Failed"}},{"properties":{"status":"Failed"}}]}' + } + } + + # Act + { Resubmit-FailedAzLogicAppRuns -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -WorkflowName $workflowName -StartTime $startTime } | + Should -Not -Throw + + # Assert + Assert-VerifiableMock + Assert-MockCalled Invoke-WebRequest -Scope It -Times 3 + } + It "Resubmitting failed runs should fail when retrieving Logic App history fails" { + # Arrange + $resourceGroupName = "codit-arcus-scripting" + $logicAppName = "arc-dev-we-rcv-unknown-http" + $workflowName = "test" + $startTime = '01/01/2023 00:00:00' + + Mock Invoke-WebRequest { throw 'some error' } + + # Act + { Resubmit-FailedAzLogicAppRuns -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -WorkflowName $workflowName -StartTime $startTime } | + Should -Throw -ExpectedMessage "Failed to resubmit all failed instances for the workflow '$workflowName' in Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$startTime'. Details: some error" + + # Assert + Assert-VerifiableMock + Assert-MockCalled Invoke-WebRequest -Scope It -Times 1 + } + } } } \ No newline at end of file From d597ccf85df14726799d9fcb0cfc514f4206c07f Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Thu, 16 Nov 2023 12:23:24 +0100 Subject: [PATCH 18/24] updated docs --- ...pps.md => azure-logic-apps-consumption.md} | 116 +++--- .../powershell/azure-logic-apps-standard.md | 354 ++++++++++++++++++ .../Scripts/Disable-AzLogicAppsFromConfig.ps1 | 2 +- 3 files changed, 402 insertions(+), 70 deletions(-) rename docs/preview/03-Features/powershell/{azure-logic-apps.md => azure-logic-apps-consumption.md} (70%) create mode 100644 docs/preview/03-Features/powershell/azure-logic-apps-standard.md diff --git a/docs/preview/03-Features/powershell/azure-logic-apps.md b/docs/preview/03-Features/powershell/azure-logic-apps-consumption.md similarity index 70% rename from docs/preview/03-Features/powershell/azure-logic-apps.md rename to docs/preview/03-Features/powershell/azure-logic-apps-consumption.md index deb071ce..3615d1dd 100644 --- a/docs/preview/03-Features/powershell/azure-logic-apps.md +++ b/docs/preview/03-Features/powershell/azure-logic-apps-consumption.md @@ -1,9 +1,9 @@ --- -title: " Azure Logic Apps" +title: "Azure Logic Apps - Consumption" layout: default --- -# Azure Logic Apps +# Azure Logic Apps - Consumption ## Installation @@ -73,21 +73,20 @@ PS> Resubmit-FailedAzLogicAppRuns ` Use this script to enable a specific Azure Logic App. -| Parameter | Mandatory | Description | -| ----------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------- | -| EnvironmentName | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | -| SubscriptionId | no | The Id of the subscription containing the Azure Logic App. | -| | | When not provided, it will be retrieved from the current context (Get-AzContext). | -| ResourceGroupName | yes | The resource group containing the Azure Logic Apps. | -| LogicAppName | yes | The name of the Azure Logic App to be disabled. | -| WorkflowName | no | The name of the workflow that needs to be disabled, this only needs to be supplied when disabling a workflow in Logic App Standard. | -| ApiVersion | no | The version of the management API to be used. (default: `2016-06-01`) | -| AccessToken | no | The access token to be used to disable the Azure Logic App. | -| | | When not provided, it will be retrieved from the current context (Get-AzContext). | +| Parameter | Mandatory | Description | +| ------------------- | --------- | ------------------------------------------------------------------------------------------------------------------- | +| `EnvironmentName` | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | +| `SubscriptionId` | no | The Id of the subscription containing the Azure Logic App. | +| | | When not provided, it will be retrieved from the current context (Get-AzContext). | +| `ResourceGroupName` | yes | The resource group containing the Azure Logic Apps. | +| `LogicAppName` | yes | The name of the Azure Logic App to be disabled. | +| `ApiVersion` | no | The version of the management API to be used. (default: `2016-06-01`) | +| `AccessToken` | no | The access token to be used to disable the Azure Logic App. | +| | | When not provided, it will be retrieved from the current context (Get-AzContext). | **Example** -Taking an example in which a specific Azure Logic App Consumption (`"rcv-shopping-order-sftp"`) needs to be disabled, without providing the subscriptionId or accesstoken. +Taking an example in which a specific Azure Logic App (`"rcv-shopping-order-sftp"`) needs to be disabled, without providing the subscriptionId or accesstoken. ```powershell PS> Disable-AzLogicApp ` @@ -96,7 +95,7 @@ PS> Disable-AzLogicApp ` # Successfully disabled Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' ``` -Taking an example in which a specific Azure Logic App Consumption (`"rcv-shopping-order-sftp"`) needs to be disabled, with providing the subscriptionId or accesstoken. +Taking an example in which a specific Azure Logic Apps (`"rcv-shopping-order-sftp"`) needs to be disabled, with providing the subscriptionId or accesstoken. ```powershell PS> Disable-AzLogicApp ` @@ -107,35 +106,24 @@ PS> Disable-AzLogicApp ` # Successfully disabled Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' ``` -Taking an example in which a specific workflow (`ProcessMessage`) in an Azure Logic App Standard (`"rcv-shopping-order-sftp"`) needs to be disabled, without providing the subscriptionId or accesstoken. - -```powershell -PS> Disable-AzLogicApp ` --ResourceGroupName "rg-common-dev" ` --LogicAppName "rcv-shopping-order-sftp" ` --WorkflowName "ProcessMessage" -# Successfully disabled workflow 'ProcessMessage' in Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' -``` - ## Enable an Azure Logic App Use this script to enable a specific Azure Logic App. -| Parameter | Mandatory | Description | -| ----------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------- | -| EnvironmentName | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | -| SubscriptionId | no | The Id of the subscription containing the Azure Logic App. | -| | | When not provided, it will be retrieved from the current context (Get-AzContext). | -| ResourceGroupName | yes | The resource group containing the Azure Logic Apps. | -| LogicAppName | yes | The name of the Azure Logic App to be enabled. | -| WorkflowName | no | The name of the workflow that needs to be disabled, this only needs to be supplied when disabling a workflow in Logic App Standard. | -| ApiVersion | no | The version of the management API to be used. (default: `2016-06-01`) | -| AccessToken | no | The access token to be used to enable the Azure Logic App. | -| | | When not provided, it will be retrieved from the current context (Get-AzContext). | +| Parameter | Mandatory | Description | +| ------------------- | --------- | ------------------------------------------------------------------------------------------------------------------- | +| `EnvironmentName` | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | +| `SubscriptionId` | no | The Id of the subscription containing the Azure Logic App. | +| | | When not provided, it will be retrieved from the current context (Get-AzContext). | +| `ResourceGroupName` | yes | The resource group containing the Azure Logic Apps. | +| `LogicAppName` | yes | The name of the Azure Logic App to be enabled. | +| `ApiVersion` | no | The version of the management API to be used. (default: `2016-06-01`) | +| `AccessToken` | no | The access token to be used to enable the Azure Logic App. | +| | | When not provided, it will be retrieved from the current context (Get-AzContext). | **Example** -Taking an example in which a specific Azure Logic App Consumption (`"rcv-shopping-order-sftp"`) needs to be enabled, without providing the subscriptionId or accesstoken. +Taking an example in which a specific Azure Logic Apps (`"rcv-shopping-order-sftp"`) needs to be enabled, without providing the subscriptionId or accesstoken. ```powershell PS> Enable-AzLogicApp ` @@ -144,7 +132,7 @@ PS> Enable-AzLogicApp ` # Successfully enabled Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' ``` -Taking an example in which a specific Azure Logic App Consumption (`"rcv-shopping-order-sftp"`) needs to be enabled, with providing the subscriptionId or accesstoken. +Taking an example in which a specific Azure Logic App (`"rcv-shopping-order-sftp"`) needs to be enabled, with providing the subscriptionId or accesstoken. ```powershell PS> Enable-AzLogicApp ` @@ -155,29 +143,19 @@ PS> Enable-AzLogicApp ` # Successfully enabled Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' ``` -Taking an example in which a specific workflow (`ProcessMessage`) in an Azure Logic App Standard (`"rcv-shopping-order-sftp"`) needs to be enabled, without providing the subscriptionId or accesstoken. - -```powershell -PS> Enable-AzLogicApp ` --ResourceGroupName "rg-common-dev" ` --LogicAppName "rcv-shopping-order-sftp" ` --WorkflowName "ProcessMessage" -# Successfully enabled workflow 'ProcessMessage' in Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' -``` - ## Disabling Azure Logic Apps from configuration file Typically done the first task of the release pipeline, right before the deployment of the Logic Apps, will disable all specified Logic Apps in a specific order. The Azure Logic Apps to be disabled and the order in which this will be done, will be defined in the provided configuration file. The order of the Azure Logic Apps in the configuration file (bottom to top) defines the order in which they will be disabled by the script. The counterpart of this script used to enable the Azure Logic Apps, will take the order as specified (top to bottom) in the file. -| Parameter | Mandatory | Description | -| ----------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | -| ResourceGroupName | yes | The resource group containing the Azure Logic Apps. | -| DeployFileName | yes | If your solution consists of multiple interfaces, you can specify the flow-specific name of the configuration file. | -| ResourcePrefix | no | In case the Azure Logic Apps all start with the same prefix, you can specify this prefix through this parameter instead of updating the configuration-file. | -| EnvironmentName | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | -| ApiVersion | no | The version of the management API to be used. (default: `2016-06-01`) | +| Parameter | Mandatory | Description | +| ------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `ResourceGroupName` | yes | The resource group containing the Azure Logic Apps. | +| `DeployFileName` | yes | If your solution consists of multiple interfaces, you can specify the flow-specific name of the configuration file. | +| `ResourcePrefix` | no | In case the Azure Logic Apps all start with the same prefix, you can specify this prefix through this parameter instead of updating the configuration-file. | +| `EnvironmentName` | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | +| `ApiVersion` | no | The version of the management API to be used. (default: `2016-06-01`) | The schema of this configuration file is a JSON structure of an array with the following inputs: @@ -318,23 +296,23 @@ Typically done as the last task of the release pipeline, right after the deploym The Azure Logic Apps to be enabled and the order in which this will be done, will be defined in the provided configuration file. The order of the Azure Logic Apps in the configuration file (top to bottom) defines the order in which they will be enabled by the script. The counterpart of this script used to disable the Azure Logic Apps, will take the reversed order as specified (bottom to top) in the file. -| Parameter | Mandatory | Description | -| ----------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | -| ResourceGroupName | yes | The resource group containing the Azure Logic Apps. | -| DeployFileName | yes | If your solution consists of multiple interfaces, you can specify the flow-specific name of the configuration file. | -| ResourcePrefix | no | In case the Azure Logic Apps all start with the same prefix, you can specify this prefix through this parameter instead of updating the configuration-file. | -| EnvironmentName | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | -| ApiVersion | no | The version of the management API to be used. (default: `2016-06-01`) | +| Parameter | Mandatory | Description | +| ------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `ResourceGroupName` | yes | The resource group containing the Azure Logic Apps. | +| `DeployFileName` | yes | If your solution consists of multiple interfaces, you can specify the flow-specific name of the configuration file. | +| `ResourcePrefix` | no | In case the Azure Logic Apps all start with the same prefix, you can specify this prefix through this parameter instead of updating the configuration-file. | +| `EnvironmentName` | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | +| `ApiVersion` | no | The version of the management API to be used. (default: `2016-06-01`) | The schema of this configuration file is a JSON structure of an array with the following inputs: -| Node | Type | Description | -| ----------- | --------------- | --------------------------------------------------------------------------------------------------------------------- | -| Description | `string` | Description of Azure Logic App set to enable. | -| CheckType | `enum` | _Not taken into account for enabling Logic Apps._ | -| StopType | `enum` | `None`: don't enable the given Logic Apps. | -| | | `Immediate`: enable the given Logic Apps. | -| LogicApps | `string array` | Set of Logic App names to enable. | +| Node | Type | Description | +| ------------- | --------------- | --------------------------------------------------------------------------------------------------------------------- | +| `Description` | `string` | Description of Azure Logic App set to enable. | +| `CheckType` | `enum` | _Not taken into account for enabling Logic Apps._ | +| `StopType` | `enum` | `None`: don't enable the given Logic Apps. | +| | | `Immediate`: enable the given Logic Apps. | +| `LogicApps` | `string array` | Set of Logic App names to enable. | **Example** Taking an example in which a set of Azure Logic Apps (`"rcv-shopping-order-*"`) need to be enabled, the following configuration will ignore the `checkType`, as this is only used for disabling the Logic Apps, and will simply enable them (`stopType = Immediate`), starting with the _sender_ instances and working its way down to the _receive protocol_ Logic Apps. diff --git a/docs/preview/03-Features/powershell/azure-logic-apps-standard.md b/docs/preview/03-Features/powershell/azure-logic-apps-standard.md new file mode 100644 index 00000000..49d45161 --- /dev/null +++ b/docs/preview/03-Features/powershell/azure-logic-apps-standard.md @@ -0,0 +1,354 @@ +--- +title: "Azure Logic Apps - Standard" +layout: default +--- + +# Azure Logic Apps - Standard + +## Installation + +To have access to the following features, you have to import the module: + +```powershell +PS> Install-Module -Name Arcus.Scripting.LogicApps +``` + +## Cancel running instances for a Azure Logic App workflow + +Use this script to cancel all running instances for a specific Azure Logic App. + +| Parameter | Mandatory | Description | +| --------------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | +| `EnvironmentName` | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | +| `ResourceGroupName` | yes | The resource group containing the Azure Logic App. | +| `LogicAppName` | yes | The name of the Azure Logic App containing the workflow for which the runs will be cancelled. | +| `WorkflowName` | yes | The name of the workflow within the Azure Logic App for which the runs will be cancelled. | +| `MaximumFollowNextPageLink` | no | This sets the amount of pages (30 runs per page) of the Logic App run history (if any) that are retrieved. If not supplied the default value is 10. | + +**Example** + +Taking an example in which a specific Azure Logic App (`"rcv-shopping-order-sftp"`) needs to have all its runs cancelled for the workflow 'process'. + +```powershell +PS> Cancel-AzLogicAppRuns ` +-ResourceGroupName "rg-common-dev" ` +-LogicAppName "rcv-shopping-order-sftp" ` +-WorkflowName "process" + +# Successfully cancelled all running instances for the workflow 'process' in Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' +``` + +## Resubmitting failed instances for a Azure Logic App workflow + +Use this script to re-run a failed Azure Logic App run. + +| Parameter | Mandatory | Description | +| --------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `EnvironmentName` | no | The name of the Azure environment where the Azure Logic App resides. (default: `AzureCloud`) | +| `ResourceGroupName` | yes | The resource group containing the Azure Logic App. | +| `LogicAppName` | yes | The name of the Azure Logic App containing the workflow for which the failed runs will be resubmitted. | +| `WorkflowName` | yes | The name of the workflow within the Azure Logic App for which the runs will be resubmitted. | +| `StartTime` | yes | The start time in UTC for retrieving the failed instances. | +| `MaximumFollowNextPageLink` | no | This sets the amount of pages (30 runs per page) of the Logic App workflow run history (if any) that are retrieved. If not supplied the default value is 10. | + +**Example** + +Taking an example in which a specific Azure Logic App (`"rcv-shopping-order-sftp"`) needs to have all its failed runs resubmitted from 2023-05-01 00:00:00 for the workflow 'process'. + +```powershell +PS> Resubmit-FailedAzLogicAppRuns ` +-ResourceGroupName "rg-common-dev" ` +-LogicAppName "rcv-shopping-order-sftp" ` +-WorkflowName "process" ` +-StartTime "2023-05-01 00:00:00" +# Successfully resubmitted all failed instances for the workflow 'process' in the Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' from '2023-05-01 00:00:00' +``` + +## Disable a Azure Logic App workflow + +Use this script to enable a specific Azure Logic App. + +| Parameter | Mandatory | Description | +| ------------------- | --------- | ------------------------------------------------------------------------------------------------------------------- | +| `ResourceGroupName` | yes | The resource group containing the Azure Logic Apps. | +| `LogicAppName` | yes | The name of the Azure Logic App containing the workflow to be disabled. | +| `WorkflowName` | yes | The name of the workflow within the Azure Logic App that needs to be disabled. | + +**Example** + +Taking an example in which a specific workflow (`"process"`) in an Azure Logic App (`"rcv-shopping-order-sftp"`) needs to be disabled. + +```powershell +PS> Disable-AzLogicApp ` +-ResourceGroupName "rg-common-dev" ` +-LogicAppName "rcv-shopping-order-sftp" ` +-WorkflowName "process" +# Successfully disabled workflow 'process' in Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' +``` + +## Enable a Azure Logic App workflow + +Use this script to enable a specific Azure Logic App. + +| Parameter | Mandatory | Description | +| ------------------- | --------- | ------------------------------------------------------------------------------------------------------------------- | +| `ResourceGroupName` | yes | The resource group containing the Azure Logic Apps. | +| `LogicAppName` | yes | The name of the Azure Logic App to be enabled. | +| `WorkflowName` | yes | The name of the workflow within the Azure Logic App that needs to be enabled. | + +**Example** + +Taking an example in which a specific workflow (`"process"`) in an Azure Logic App (`"rcv-shopping-order-sftp"`) needs to be enabled. + +```powershell +PS> Enable-AzLogicApp ` +-ResourceGroupName "rg-common-dev" ` +-LogicAppName "rcv-shopping-order-sftp" ` +-WorkflowName "process" +# Successfully enabled workflow 'process' in Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' +``` + +## Disabling Azure Logic App workflows from configuration file + +Typically done the first task of the release pipeline, right before the deployment of the Logic Apps, will disable all specified Logic App workflows in a specific order. +The Azure Logic App workflows to be disabled and the order in which this will be done, will be defined in the provided configuration file. +The order of the Azure Logic App workflows in the configuration file (bottom to top) defines the order in which they will be disabled by the script. The counterpart of this script is used to enable the Azure Logic App workflows, will take the order as specified (top to bottom) in the file. + +| Parameter | Mandatory | Description | +| ------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `ResourceGroupName` | yes | The resource group containing the Azure Logic Apps. | +| `DeployFileName` | yes | If your solution consists of multiple interfaces, you can specify the flow-specific name of the configuration file. | + +The schema of this configuration file is a JSON structure of an array with the following inputs: + +| Node | Type | Description | +| ------------------------- | --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | +| Description | `string` | Description of Azure Logic App set to disable. | +| CheckType | `enum` | `None`: don't perform any additional checks. | +| | | `NoWaitingOrRunningRuns`: waits until there are no more waiting or running Logic App instances. | +| StopType | `enum` | `None`: don't disable the given Logic Apps. | +| | | `Immediate`: disable the given Logic Apps. | +| LogicApps | `array` | Set of Logic App names and workflow to disable. | + +**Example** + +Taking an example in which a set of Azure Logic App workflows need to be disabled, the following configuration will not take into account any active Logic Apps runs (`checkType = None`) and will immediately disable them (`stopType = Immediate`), starting with the _receive protocol_ instances and working its way up to the _sender_ Logic App. + +```json +[ + { + "description": "Sender(s)", + "checkType": "None", + "stopType": "Immediate", + "logicApps": [ + { + "name": "snd-shopping-order-confirmation-smtp", + "workflows": [ + "send" + ] + } + ] + }, + { + "description": "Orchestrator(s)", + "checkType": "None", + "stopType": "Immediate", + "logicApps": [ + { + "name": "orc-shopping-order-processing", + "workflows": [ + "process" + ] + } + ] + }, + { + "description": "Generic Receiver(s)", + "checkType": "None", + "stopType": "Immediate", + "logicApps": [ + { + "name": "rcv-shopping-order-generic", + "workflows": [ + "process" + ] + } + ] + }, + { + "description": "Protocol Receiver(s)", + "checkType": "None", + "stopType": "Immediate", + "logicApps": [ + { + "name": "rcv-shopping-order-protocol", + "workflows": [ + "receive-ftp", + "receive-sftp", + "receive-file" + ] + } + ] + } +] +``` + +**Example** + +Disables all the Logic Apps based on the `./deploy-orderControl.json` configuration file. +Uses the sample configuration file here above. + +```powershell +PS> Disable-AzLogicAppsFromConfig ` +-DeployFilename "./deploy-orderControl" ` +-ResourceGroupName "my-resource-group" +# Executing batch: Protocol Receiver(s) +# Executing CheckType 'None' for batch 'Protocol Receiver(s)' in resource group 'my-resource-group'" +# Executing Check 'None' => performing no check and executing stopType + +# Executing StopType 'Immediate' for Logic App 'rcv-shopping-order-protocol' in resource group 'my-resource-group' +# Successfully disabled workflow 'receive-file' in Azure Logic App 'rcv-shopping-order-protocol' in resource group 'my-resource-group' + +# Executing StopType 'Immediate' for Logic App 'rcv-shopping-order-protocol' in resource group 'my-resource-group' +# Successfully disabled workflow 'receive-sftp' in Azure Logic App 'rcv-shopping-order-protocol' in resource group 'my-resource-group' + +# Executing StopType 'Immediate' for Logic App 'rcv-shopping-order-protocol' in resource group 'my-resource-group' +# Successfully disabled workflow 'receive-ftp' in Azure Logic App 'rcv-shopping-order-protocol' in resource group 'my-resource-group' +# Batch: 'Protocol Receiver(s)' has been executed + +# Executing batch: 'Generic Receiver(s)' +# Executing StopType 'Immediate' for Logic App 'rcv-shopping-order-generic' in resource group 'my-resource-group' +# Successfully disabled workflow 'process' in Azure Logic App 'rcv-shopping-order-generic' in resource group 'my-resource-group' +# Batch: 'Generic Receiver(s)' has been executed + +# Executing batch: 'Orchestrator(s)' +# Executing StopType 'Immediate' for Logic App 'orc-shopping-order-processing' in resource group 'my-resource-group' +# Successfully disabled workflow 'process' in Azure Logic App 'orc-shopping-order-processing' in resource group 'my-resource-group' +# Batch: 'Orchestrator(s)' has been executed + +# Executing batch: 'Sender(s)' +# Executing StopType 'Immediate' for Logic App 'snd-shopping-order-confirmation-smtp' in resource group 'my-resource-group' +# Successfully disabled workflow 'send' in Azure Logic App 'snd-shopping-order-smtp' in resource group 'my-resource-group' +# Batch: 'Sender(s)' has been executed +``` + +## Enabling Azure Logic App workflows from configuration file + +Typically done as the last task of the release pipeline, right after the deployment of the Logic Apps, as this will enable all specified Logic Apps in a specific order. +The Azure Logic Apps to be enabled and the order in which this will be done, will be defined in the provided configuration file. +The order of the Azure Logic Apps in the configuration file (top to bottom) defines the order in which they will be enabled by the script. The counterpart of this script is used to disable the Azure Logic App workflows, will take the reversed order as specified (bottom to top) in the file. + +| Parameter | Mandatory | Description | +| ------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `ResourceGroupName` | yes | The resource group containing the Azure Logic Apps. | +| `DeployFileName` | yes | If your solution consists of multiple interfaces, you can specify the flow-specific name of the configuration file. | + +The schema of this configuration file is a JSON structure of an array with the following inputs: + +| Node | Type | Description | +| ------------- | --------------- | --------------------------------------------------------------------------------------------------------------------- | +| `Description` | `string` | Description of Azure Logic App set to enable. | +| `CheckType` | `enum` | _Not taken into account for enabling Logic Apps._ | +| `StopType` | `enum` | `None`: don't enable the given Logic Apps. | +| | | `Immediate`: enable the given Logic Apps. | +| LogicApps | `array` | Set of Logic App names and workflow to enable. | + +**Example** + +Taking an example in which a set of Azure Logic App workflows need to be enabled, the following configuration will ignore the `checkType`, as this is only used for disabling the Logic Apps, and will simply enable them (`stopType = Immediate`), starting with the _sender_ instances and working its way down to the _receive protocol_ Logic App workflows. +This ensures that all of the down-stream Logic App workflows are enabled by the time the initial/trigger Logic App workflows have been activated. + +```json +[ + { + "description": "Sender(s)", + "checkType": "None", + "stopType": "Immediate", + "logicApps": [ + { + "name": "snd-shopping-order-confirmation-smtp", + "workflows": [ + "send" + ] + } + ] + }, + { + "description": "Orchestrator(s)", + "checkType": "None", + "stopType": "Immediate", + "logicApps": [ + { + "name": "orc-shopping-order-processing", + "workflows": [ + "process" + ] + } + ] + }, + { + "description": "Generic Receiver(s)", + "checkType": "None", + "stopType": "Immediate", + "logicApps": [ + { + "name": "rcv-shopping-order-generic", + "workflows": [ + "receive" + ] + } + ] + }, + { + "description": "Protocol Receiver(s)", + "checkType": "None", + "stopType": "Immediate", + "logicApps": [ + { + "name": "rcv-shopping-order-protocol", + "workflows": [ + "receive-ftp", + "receive-sftp", + "receive-file" + ] + } + ] + } +] +``` + +**Example** + +Enables all the Logic Apps based on the `./deploy-orderControl.json` configuration file. +Uses the sample configuration file here above. + +```powershell +PS> Enable-AzLogicAppsFromConfig ` +-DeployFilename "./deploy-orderControl" ` +-ResourceGroupName "my-resource-group" +# Executing batch: 'Sender(s)' +# Reverting StopType 'Immediate' for Logic App 'snd-shopping-order-confirmation-smtp' in resource group 'my-resource-group' +# Successfully enabled workflow 'send' in Azure Logic App 'snd-shopping-order-confirmation-smtp' in resource group 'my-resource-group' +# Batch: 'Sender(s)' has been executed + +# Executing batch: 'Orchestrator(s)' +# Reverting StopType 'Immediate' for Logic App 'orc-shopping-order-processing' in resource group 'my-resource-group' +# Successfully enabled workflow 'process' in Azure Logic App 'orc-shopping-order-processing' in resource group 'my-resource-group' +# Batch: 'Orchestrator(s)' has been executed + +# Executing batch: 'Generic Receiver(s)' +# Reverting StopType 'Immediate' for Logic App 'rcv-shopping-order-generic' in resource group 'my-resource-group' +# Successfully enabled workflow 'receive' in Azure Logic App 'rcv-shopping-order-generic' in resource group 'my-resource-group' +# Batch: 'Generic Receiver(s)' has been executed + +# Executing batch: Protocol Receiver(s) +# Reverting StopType 'Immediate' for Logic App 'rcv-shopping-order-protocol' in resource group 'my-resource-group' +# Successfully enabled workflow 'receive-ftp' in Azure Logic App 'rcv-shopping-order-protocol' in resource group 'my-resource-group' + +# Reverting StopType 'Immediate' for Logic App 'rcv-shopping-order-protocol' in resource group 'my-resource-group' +# Successfully enabled workflow 'receive-sftp' in Azure Logic App 'rcv-shopping-order-protocol' in resource group 'my-resource-group' + +# Reverting StopType 'Immediate' for Logic App 'rcv-shopping-order-protocol' in resource group 'my-resource-group' +# Successfully enabled workflow 'receive-file' in Azure Logic App 'rcv-shopping-order-protocol' in resource group 'my-resource-group' +# Batch: 'Protocol Receiver(s)' has been executed +``` diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 index 2831a9df..42cbc7b6 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicAppsFromConfig.ps1 @@ -113,7 +113,7 @@ function ExecuteCheckType() { Write-Verbose "Found no more waiting or running runs for Workflow '$WorkflowName' in Azure Logic App '$LogicAppName', executing stopType for Logic App Workflow" ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppType $batch.logicAppType -LogicAppName $LogicAppName -WorkflowName $WorkflowName -stopType $batch.stopType } - Write-Host "Check 'NoWaitingOrRunningRuns' executed successfully onWorkflow '$WorkflowName' in Azure Logic App '$LogicAppName'" -ForegroundColor Green + Write-Host "Check 'NoWaitingOrRunningRuns' executed successfully on Workflow '$WorkflowName' in Azure Logic App '$LogicAppName'" -ForegroundColor Green } } else { Write-Warning "No workflows specified to disable" From e4a6e0ce26f6cb5700863944ae504939645e852f Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Thu, 16 Nov 2023 12:26:51 +0100 Subject: [PATCH 19/24] add Mock Get-AzCachedAccessToken --- .../Arcus.Scripting.LogicApps.tests.ps1 | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 index 6d92d34a..060cd47e 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 @@ -1032,6 +1032,13 @@ InModuleScope Arcus.Scripting.LogicApps { $logicAppName = "arc-dev-we-rcv-unknown-http" $workflowName = "test" + Mock Get-AzCachedAccessToken -MockWith { + $Global:subscriptionId = '123456' + return @{ + SubscriptionId = "123456" + AccessToken = "accessToken" + } + } Mock Invoke-WebRequest -MockWith { return [pscustomobject] @{ Content = '{"value":[{"properties":{"status":"Running"}},{"properties":{"status":"Running"}}]}' @@ -1044,6 +1051,7 @@ InModuleScope Arcus.Scripting.LogicApps { # Assert Assert-VerifiableMock + Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 Assert-MockCalled Invoke-WebRequest -Scope It -Times 3 } It "Cancelling all runs should fail when retrieving Logic App history fails" { @@ -1052,6 +1060,13 @@ InModuleScope Arcus.Scripting.LogicApps { $logicAppName = "arc-dev-we-rcv-unknown-http" $workflowName = "test" + Mock Get-AzCachedAccessToken -MockWith { + $Global:subscriptionId = '123456' + return @{ + SubscriptionId = "123456" + AccessToken = "accessToken" + } + } Mock Invoke-WebRequest -MockWith { throw 'some error' } # Act @@ -1060,6 +1075,7 @@ InModuleScope Arcus.Scripting.LogicApps { # Assert Assert-VerifiableMock + Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 Assert-MockCalled Invoke-WebRequest -Scope It -Times 1 } } @@ -1071,6 +1087,13 @@ InModuleScope Arcus.Scripting.LogicApps { $workflowName = "test" $startTime = '2023-01-01 00:00:00' + Mock Get-AzCachedAccessToken -MockWith { + $Global:subscriptionId = '123456' + return @{ + SubscriptionId = "123456" + AccessToken = "accessToken" + } + } Mock Invoke-WebRequest -MockWith { return [pscustomobject] @{ Content = '{"value":[{"properties":{"status":"Failed"}}]}' @@ -1083,6 +1106,7 @@ InModuleScope Arcus.Scripting.LogicApps { # Assert Assert-VerifiableMock + Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 Assert-MockCalled Invoke-WebRequest -Scope It -Times 2 } It "Resubmitting multiple failed runs from Logic App history should succeed" { @@ -1092,6 +1116,13 @@ InModuleScope Arcus.Scripting.LogicApps { $workflowName = "test" $startTime = '2023-01-01 00:00:00' + Mock Get-AzCachedAccessToken -MockWith { + $Global:subscriptionId = '123456' + return @{ + SubscriptionId = "123456" + AccessToken = "accessToken" + } + } Mock Invoke-WebRequest -MockWith { return [pscustomobject] @{ Content = '{"value":[{"properties":{"status":"Failed"}},{"properties":{"status":"Failed"}}]}' @@ -1104,6 +1135,7 @@ InModuleScope Arcus.Scripting.LogicApps { # Assert Assert-VerifiableMock + Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 Assert-MockCalled Invoke-WebRequest -Scope It -Times 3 } It "Resubmitting failed runs should fail when retrieving Logic App history fails" { @@ -1113,6 +1145,13 @@ InModuleScope Arcus.Scripting.LogicApps { $workflowName = "test" $startTime = '01/01/2023 00:00:00' + Mock Get-AzCachedAccessToken -MockWith { + $Global:subscriptionId = '123456' + return @{ + SubscriptionId = "123456" + AccessToken = "accessToken" + } + } Mock Invoke-WebRequest { throw 'some error' } # Act @@ -1121,6 +1160,7 @@ InModuleScope Arcus.Scripting.LogicApps { # Assert Assert-VerifiableMock + Assert-MockCalled Get-AzCachedAccessToken -Scope It -Times 1 Assert-MockCalled Invoke-WebRequest -Scope It -Times 1 } } From 3299474771aaf3d8ab403f74de593f4c09f22d72 Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Thu, 16 Nov 2023 12:36:29 +0100 Subject: [PATCH 20/24] Add Mock Get-AzCachedAccessToken --- .../Arcus.Scripting.LogicApps.tests.ps1 | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 index 060cd47e..ead5fe7e 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 @@ -613,6 +613,13 @@ InModuleScope Arcus.Scripting.LogicApps { $logicAppName = "arc-dev-we-rcv-unknown-http" $startTime = '01/01/2023 00:00:00' + Mock Get-AzCachedAccessToken -MockWith { + return @{ + SubscriptionId = "123456" + AccessToken = "accessToken" + } + } + Mock Get-AzLogicAppRunHistory { throw 'some error' } Mock Invoke-WebRequest -MockWith { @@ -625,6 +632,7 @@ InModuleScope Arcus.Scripting.LogicApps { # Assert Assert-VerifiableMock + Assert-MockCalled Get-AzCachedAccessToken -Times 1 Assert-MockCalled Get-AzLogicAppRunHistory -Scope It -Times 1 Assert-MockCalled Invoke-WebRequest -Scope It -Times 0 } From aee1f81057c3e9187dea7db8187ea41b90fefe44 Mon Sep 17 00:00:00 2001 From: Pim Simons <32359437+pim-simons@users.noreply.github.com> Date: Fri, 17 Nov 2023 14:31:16 +0100 Subject: [PATCH 21/24] Apply suggestions from code review Co-authored-by: Stijn Moreels <9039753+stijnmoreels@users.noreply.github.com> --- src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 | 2 +- .../Arcus.Scripting.LogicApps.tests.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 index ecd57f86..90f16992 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Disable-AzLogicApp.ps1 @@ -10,7 +10,7 @@ param( try{ if ($WorkflowName -eq "") { - if ($SubscriptionId -eq "" -or $AccessToken -eq ""){ + if ($SubscriptionId -eq "" -or $AccessToken -eq "") { # Request accessToken in case the script contains records $token = Get-AzCachedAccessToken diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 index ead5fe7e..5aac4b79 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 @@ -24,7 +24,7 @@ InModuleScope Arcus.Scripting.LogicApps { } } - # Act + # Act { Enable-AzLogicApp -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName } | Should -Throw -ExpectedMessage "Failed to enable Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'. Details: $errorContent" From f901fcd105ec31ffd9944f338e7e9ba730abbcdb Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Mon, 20 Nov 2023 09:41:39 +0100 Subject: [PATCH 22/24] aligned logging --- .../Scripts/Cancel-AzLogicAppRuns.ps1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 index c75e8270..272cd253 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 @@ -15,8 +15,9 @@ try { Where-Object {$_.Status -eq 'Running'} foreach ($run in $runs) { - Stop-AzLogicAppRun -ResourceGroupName $ResourceGroupName -Name $LogicAppName -RunName $run.Name -Force - Write-Verbose "Cancelled run $run.Name for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" + $runName = $run.name + Stop-AzLogicAppRun -ResourceGroupName $ResourceGroupName -Name $LogicAppName -RunName $runName -Force + Write-Verbose "Cancelled run '$runName' for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" } Write-Host "Successfully cancelled all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" -ForegroundColor Green From b0a253d438e194a9e9336e4c822b176566b842dd Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Mon, 20 Nov 2023 12:47:12 +0100 Subject: [PATCH 23/24] do not use global variables --- .../Scripts/Cancel-AzLogicAppRuns.ps1 | 17 +++++++-------- .../Scripts/Resubmit-FailedAzLogicAppRuns.ps1 | 21 +++++++++---------- .../Arcus.Scripting.LogicApps.tests.ps1 | 9 -------- 3 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 index 272cd253..01d55515 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Cancel-AzLogicAppRuns.ps1 @@ -6,9 +6,6 @@ param( [Parameter(Mandatory = $false)][int] $MaximumFollowNextPageLink = 10 ) -$Global:accessToken = ""; -$Global:subscriptionId = ""; - try { if ($WorkflowName -eq "") { $runs = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $LogicAppName -FollowNextPageLink -MaximumFollowNextPageLink $MaximumFollowNextPageLink | @@ -22,13 +19,15 @@ try { Write-Host "Successfully cancelled all running instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'" -ForegroundColor Green } else { - $token = Get-AzCachedAccessToken -AssignGlobalVariables + $token = Get-AzCachedAccessToken + $accessToken = $token.AccessToken + $subscriptionId = $token.SubscriptionId - $listRunningUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -Action 'listRunning' + $listRunningUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -Action 'listRunning' $listRunningParams = @{ Method = 'Get' Headers = @{ - 'authorization'="Bearer $Global:accessToken" + 'authorization'="Bearer $accessToken" } URI = $listRunningUrl } @@ -45,7 +44,7 @@ try { $listRunningParams = @{ Method = 'Get' Headers = @{ - 'authorization'="Bearer $Global:accessToken" + 'authorization'="Bearer $accessToken" } URI = $nextPageUrl } @@ -60,11 +59,11 @@ try { foreach ($run in $allRuns) { $runName = $run.name - $cancelUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -RunName $runName -Action 'cancel' + $cancelUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -RunName $runName -Action 'cancel' $cancelParams = @{ Method = 'Post' Headers = @{ - 'authorization'="Bearer $Global:accessToken" + 'authorization'="Bearer $accessToken" } URI = $cancelUrl } diff --git a/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 b/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 index 9b6f9f67..9c2cc4ec 100644 --- a/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 +++ b/src/Arcus.Scripting.LogicApps/Scripts/Resubmit-FailedAzLogicAppRuns.ps1 @@ -8,11 +8,10 @@ param( [Parameter(Mandatory = $false)][string] $EnvironmentName = "AzureCloud" ) -$Global:accessToken = ""; -$Global:subscriptionId = ""; - try{ - $token = Get-AzCachedAccessToken -AssignGlobalVariables + $token = Get-AzCachedAccessToken + $accessToken = $token.AccessToken + $subscriptionId = $token.SubscriptionId if ($WorkflowName -eq "") { if ($EndTime) { @@ -26,12 +25,12 @@ try{ foreach ($run in $runs) { $triggerName = $run.Trigger.Name $runId = $run.Name - $resubmitUrl = "https://management.azure.com/subscriptions/$Global:subscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Logic/workflows/$LogicAppName/triggers/$triggerName/histories/$runId/resubmit?api-version=2016-06-01" + $resubmitUrl = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Logic/workflows/$LogicAppName/triggers/$triggerName/histories/$runId/resubmit?api-version=2016-06-01" $params = @{ Method = 'Post' Headers = @{ - 'authorization'="Bearer $Global:accessToken" + 'authorization'="Bearer $accessToken" } URI = $resubmitUrl } @@ -47,11 +46,11 @@ try{ Write-Host "Successfully resubmitted all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'" -ForegroundColor Green } } else { - $listFailedUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -StartTime $StartTime -Action 'listFailed' + $listFailedUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -StartTime $StartTime -Action 'listFailed' $listFailedParams = @{ Method = 'Get' Headers = @{ - 'authorization'="Bearer $Global:accessToken" + 'authorization'="Bearer $accessToken" } URI = $listFailedUrl } @@ -68,7 +67,7 @@ try{ $listFailedParams = @{ Method = 'Get' Headers = @{ - 'authorization'="Bearer $Global:accessToken" + 'authorization'="Bearer $accessToken" } URI = $nextPageUrl } @@ -84,11 +83,11 @@ try{ $runName = $failedRun.name $triggerName = $failedRun.properties.trigger.name - $resubmitUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -TriggerName $triggerName -RunName $runName -Action 'resubmit' + $resubmitUrl = . $PSScriptRoot\Get-AzLogicAppStandardResourceManagementUrl.ps1 -EnvironmentName $EnvironmentName -SubscriptionId $subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -WorkflowName $WorkflowName -TriggerName $triggerName -RunName $runName -Action 'resubmit' $resubmitParams = @{ Method = 'Post' Headers = @{ - 'authorization'="Bearer $Global:accessToken" + 'authorization'="Bearer $accessToken" } URI = $resubmitUrl } diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 index 5aac4b79..141519f4 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 @@ -901,7 +901,6 @@ InModuleScope Arcus.Scripting.LogicApps { } Mock Disable-AzLogicApp {} Mock Get-AzCachedAccessToken -MockWith { - $Global:subscriptionId = '123456' return @{ SubscriptionId = "123456" AccessToken = "accessToken" @@ -938,7 +937,6 @@ InModuleScope Arcus.Scripting.LogicApps { } Mock Disable-AzLogicApp {} Mock Get-AzCachedAccessToken -MockWith { - $Global:subscriptionId = '123456' return @{ SubscriptionId = "123456" AccessToken = "accessToken" @@ -978,7 +976,6 @@ InModuleScope Arcus.Scripting.LogicApps { $LogicAppName | Should -BeIn $logicAppNames } Mock Get-AzCachedAccessToken -MockWith { - $Global:subscriptionId = '123456' return @{ SubscriptionId = "123456" AccessToken = "accessToken" @@ -1017,7 +1014,6 @@ InModuleScope Arcus.Scripting.LogicApps { $LogicAppName | Should -Be "snd-async" } Mock Get-AzCachedAccessToken -MockWith { - $Global:subscriptionId = '123456' return @{ SubscriptionId = "123456" AccessToken = "accessToken" @@ -1041,7 +1037,6 @@ InModuleScope Arcus.Scripting.LogicApps { $workflowName = "test" Mock Get-AzCachedAccessToken -MockWith { - $Global:subscriptionId = '123456' return @{ SubscriptionId = "123456" AccessToken = "accessToken" @@ -1069,7 +1064,6 @@ InModuleScope Arcus.Scripting.LogicApps { $workflowName = "test" Mock Get-AzCachedAccessToken -MockWith { - $Global:subscriptionId = '123456' return @{ SubscriptionId = "123456" AccessToken = "accessToken" @@ -1096,7 +1090,6 @@ InModuleScope Arcus.Scripting.LogicApps { $startTime = '2023-01-01 00:00:00' Mock Get-AzCachedAccessToken -MockWith { - $Global:subscriptionId = '123456' return @{ SubscriptionId = "123456" AccessToken = "accessToken" @@ -1125,7 +1118,6 @@ InModuleScope Arcus.Scripting.LogicApps { $startTime = '2023-01-01 00:00:00' Mock Get-AzCachedAccessToken -MockWith { - $Global:subscriptionId = '123456' return @{ SubscriptionId = "123456" AccessToken = "accessToken" @@ -1154,7 +1146,6 @@ InModuleScope Arcus.Scripting.LogicApps { $startTime = '01/01/2023 00:00:00' Mock Get-AzCachedAccessToken -MockWith { - $Global:subscriptionId = '123456' return @{ SubscriptionId = "123456" AccessToken = "accessToken" From b0c7728324edf15562e541a8fd7e2593c2be028b Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Mon, 20 Nov 2023 14:07:35 +0100 Subject: [PATCH 24/24] fix tests --- .../Arcus.Scripting.LogicApps.tests.ps1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 index 141519f4..1b90eec9 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.LogicApps.tests.ps1 @@ -901,6 +901,7 @@ InModuleScope Arcus.Scripting.LogicApps { } Mock Disable-AzLogicApp {} Mock Get-AzCachedAccessToken -MockWith { + $Global:subscriptionId = '123456' return @{ SubscriptionId = "123456" AccessToken = "accessToken" @@ -937,6 +938,7 @@ InModuleScope Arcus.Scripting.LogicApps { } Mock Disable-AzLogicApp {} Mock Get-AzCachedAccessToken -MockWith { + $Global:subscriptionId = '123456' return @{ SubscriptionId = "123456" AccessToken = "accessToken" @@ -976,6 +978,7 @@ InModuleScope Arcus.Scripting.LogicApps { $LogicAppName | Should -BeIn $logicAppNames } Mock Get-AzCachedAccessToken -MockWith { + $Global:subscriptionId = '123456' return @{ SubscriptionId = "123456" AccessToken = "accessToken" @@ -1014,6 +1017,7 @@ InModuleScope Arcus.Scripting.LogicApps { $LogicAppName | Should -Be "snd-async" } Mock Get-AzCachedAccessToken -MockWith { + $Global:subscriptionId = '123456' return @{ SubscriptionId = "123456" AccessToken = "accessToken"