diff --git a/src/powershell/Private/New-ResourceGroup.ps1 b/src/powershell/Private/New-ResourceGroup.ps1 new file mode 100644 index 000000000..11254e299 --- /dev/null +++ b/src/powershell/Private/New-ResourceGroup.ps1 @@ -0,0 +1,43 @@ +# 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. +#> +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