From cc473d039c283fa54fa502de03a73f448015946b Mon Sep 17 00:00:00 2001 From: Michael Flanakin Date: Wed, 27 Mar 2024 08:06:39 -0700 Subject: [PATCH 1/2] Extract New-ResourceGroup private command for reuse --- src/powershell/Private/New-ResourceGroup.ps1 | 46 +++++++++++++ src/powershell/Public/Deploy-FinOpsHub.ps1 | 9 +-- .../Tests/Unit/Deploy-FinOpsHub.Tests.ps1 | 30 --------- .../Tests/Unit/New-ResourceGroup.Tests.ps1 | 67 +++++++++++++++++++ 4 files changed, 114 insertions(+), 38 deletions(-) create mode 100644 src/powershell/Private/New-ResourceGroup.ps1 create mode 100644 src/powershell/Tests/Unit/New-ResourceGroup.Tests.ps1 diff --git a/src/powershell/Private/New-ResourceGroup.ps1 b/src/powershell/Private/New-ResourceGroup.ps1 new file mode 100644 index 000000000..fb13f5990 --- /dev/null +++ b/src/powershell/Private/New-ResourceGroup.ps1 @@ -0,0 +1,46 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +<# + .SYNOPSIS + Creates a new resource group. + + .EXAMPLE + New-ResourceGroup -WhatIf + + Shows what would happen if the command runs without actually running it. + + .DESCRIPTION + The New-ResourceGroup command performs any initialization tasks required for a resource group contributor to be able to deploy a FinOps hub instance in Azure, like registering resource providers. To view the full list of tasks performed, run the command with the -WhatIf option. + + .LINK + https://aka.ms/ftk/New-ResourceGroup +#> +function New-ResourceGroup +{ + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")] + [CmdletBinding(SupportsShouldProcess)] + param + ( + [Parameter(Mandatory = $true)] + [string] + $Name, + + [Parameter(Mandatory = $true)] + [string] + $Location, + + [Parameter()] + [hashtable] + $Tags + ) + + $resourceGroupObject = Get-AzResourceGroup -Name $Name -ErrorAction 'SilentlyContinue' + if (-not $resourceGroupObject) + { + if (Test-ShouldProcess $PSCmdlet $Name 'CreateResourceGroup') + { + $resourceGroupObject = New-AzResourceGroup -Name $Name -Location $Location -Tags $Tags + } + } +} diff --git a/src/powershell/Public/Deploy-FinOpsHub.ps1 b/src/powershell/Public/Deploy-FinOpsHub.ps1 index 0d67d9714..064ca5164 100644 --- a/src/powershell/Public/Deploy-FinOpsHub.ps1 +++ b/src/powershell/Public/Deploy-FinOpsHub.ps1 @@ -82,14 +82,7 @@ function Deploy-FinOpsHub try { - $resourceGroupObject = Get-AzResourceGroup -Name $ResourceGroupName -ErrorAction 'SilentlyContinue' - if (-not $resourceGroupObject) - { - if (Test-ShouldProcess $PSCmdlet $ResourceGroupName 'CreateResourceGroup') - { - $resourceGroupObject = New-AzResourceGroup -Name $ResourceGroupName -Location $Location - } - } + New-ResourceGroup -Name $ResourceGroupName -Location $Location -Tags $Tags -WhatIf:$WhatIfPreference $toolkitPath = Join-Path $env:temp -ChildPath 'FinOpsToolkit' if (Test-ShouldProcess $PSCmdlet $toolkitPath 'CreateTempDirectory') diff --git a/src/powershell/Tests/Unit/Deploy-FinOpsHub.Tests.ps1 b/src/powershell/Tests/Unit/Deploy-FinOpsHub.Tests.ps1 index 593e72608..60223b639 100644 --- a/src/powershell/Tests/Unit/Deploy-FinOpsHub.Tests.ps1 +++ b/src/powershell/Tests/Unit/Deploy-FinOpsHub.Tests.ps1 @@ -38,36 +38,6 @@ InModuleScope 'FinOpsToolkit' { } } - Context 'Resource groups' { - It 'Should create RG if it does not exist' { - # Arrange - Mock -CommandName 'Get-AzResourceGroup' -MockWith { return $null } - Mock -CommandName 'New-AzResourceGroup' -MockWith { } - Mock -CommandName 'Test-ShouldProcess' -MockWith { return $Action -eq 'CreateResourceGroup' } - - # Act - Deploy-FinOpsHub -Name $hubName -ResourceGroup $rgName -Location $location - - # Assert - Assert-MockCalled -CommandName 'Get-AzResourceGroup' -Times 1 - Assert-MockCalled -CommandName 'New-AzResourceGroup' -Times 1 - } - - It 'Should use RG if it exists' { - # Arrange - Mock -CommandName 'Get-AzResourceGroup' -MockWith { return $rgName } - Mock -CommandName 'New-AzResourceGroup' -MockWith { } - Mock -CommandName 'Test-ShouldProcess' -MockWith { return $Action -eq 'CreateResourceGroup' } - - # Act - Deploy-FinOpsHub -Name $hubName -ResourceGroup $rgName -Location $location - - # Assert - Assert-MockCalled -CommandName 'Get-AzResourceGroup' -Times 1 - Assert-MockCalled -CommandName 'New-AzResourceGroup' -Times 0 - } - } - Context 'Initialize' { It 'Should call Initialize-FinOpsHubDeployment' { # Arrange diff --git a/src/powershell/Tests/Unit/New-ResourceGroup.Tests.ps1 b/src/powershell/Tests/Unit/New-ResourceGroup.Tests.ps1 new file mode 100644 index 000000000..f193348fd --- /dev/null +++ b/src/powershell/Tests/Unit/New-ResourceGroup.Tests.ps1 @@ -0,0 +1,67 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +& "$PSScriptRoot/../Initialize-Tests.ps1" + +InModuleScope 'FinOpsToolkit' { + Describe 'New-ResourceGroup' { + BeforeAll { + function Get-AzResourceGroup {} + function New-AzResourceGroup {} + + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")] + $rgName = 'ftk-test' + + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")] + $location = 'eastus' + + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")] + $tags = @{ Foo = 'Bar' } + } + + Context "WhatIf" { + It 'Should run without error' { + # Arrange + Mock -CommandName 'Test-ShouldProcess' { return $false } + Mock -CommandName 'Get-AzResourceGroup' { return $null } + + # Act + New-ResourceGroup -WhatIf -Name $rgName -Location $location -Tags $tags + + # Assert + Assert-MockCalled -CommandName 'Get-AzResourceGroup' -Times 1 + Assert-MockCalled -CommandName 'Test-ShouldProcess' -Times 1 -ParameterFilter { $Action -eq 'CreateResourceGroup' } + } + } + + Context 'Resource groups' { + It 'Should create RG if it does not exist' { + # Arrange + Mock -CommandName 'Get-AzResourceGroup' -MockWith { return $null } + Mock -CommandName 'New-AzResourceGroup' -MockWith { } + Mock -CommandName 'Test-ShouldProcess' -MockWith { return $Action -eq 'CreateResourceGroup' } + + # Act + New-ResourceGroup -Name $rgName -Location $location -Tags $tags + + # Assert + Assert-MockCalled -CommandName 'Get-AzResourceGroup' -Times 1 + Assert-MockCalled -CommandName 'New-AzResourceGroup' -Times 1 + } + + It 'Should use RG if it exists' { + # Arrange + Mock -CommandName 'Get-AzResourceGroup' -MockWith { return $rgName } + Mock -CommandName 'New-AzResourceGroup' -MockWith { } + Mock -CommandName 'Test-ShouldProcess' -MockWith { return $Action -eq 'CreateResourceGroup' } + + # Act + New-ResourceGroup -Name $hubName -ResourceGroup $rgName -Location $location + + # Assert + Assert-MockCalled -CommandName 'Get-AzResourceGroup' -Times 1 + Assert-MockCalled -CommandName 'New-AzResourceGroup' -Times 0 + } + } + } +} \ No newline at end of file From bfe427fff4fd943579030a1f2fc53ee8b7aaa235 Mon Sep 17 00:00:00 2001 From: Michael Flanakin Date: Wed, 22 May 2024 09:53:14 -0700 Subject: [PATCH 2/2] Remove aka.ms link --- src/powershell/Private/New-ResourceGroup.ps1 | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/powershell/Private/New-ResourceGroup.ps1 b/src/powershell/Private/New-ResourceGroup.ps1 index fb13f5990..11254e299 100644 --- a/src/powershell/Private/New-ResourceGroup.ps1 +++ b/src/powershell/Private/New-ResourceGroup.ps1 @@ -12,9 +12,6 @@ .DESCRIPTION The New-ResourceGroup command performs any initialization tasks required for a resource group contributor to be able to deploy a FinOps hub instance in Azure, like registering resource providers. To view the full list of tasks performed, run the command with the -WhatIf option. - - .LINK - https://aka.ms/ftk/New-ResourceGroup #> function New-ResourceGroup {