Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract New-ResourceGroup private command for reuse #656

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/powershell/Private/New-ResourceGroup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

<#
.SYNOPSIS
Creates a new resource group.

Copy link
Member

@helderpinto helderpinto Jul 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding parameters...

Suggested change
.PARAMETER Name
Specifies a name for the resource group. The resource group name must be unique in the subscription. If a resource group that has that name already exists, the command prompts you for confirmation before replacing the existing resource group.
.PARAMETER Location
Specifies the location of the resource group.
.PARAMETER Tags
Key-value pairs in the form of a hash table. For example: @{key0="value0";key1=$null;key2="value2"}

.EXAMPLE
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add parameters to the docs

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.
Comment on lines +8 to +14
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.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.
.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.
.EXAMPLE
New-ResourceGroup -WhatIf
Shows what would happen if the command runs without actually running it.

#>
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
}
}
}
9 changes: 1 addition & 8 deletions src/powershell/Public/Deploy-FinOpsHub.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
30 changes: 0 additions & 30 deletions src/powershell/Tests/Unit/Deploy-FinOpsHub.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 67 additions & 0 deletions src/powershell/Tests/Unit/New-ResourceGroup.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
Loading