PSSA: Fix PSUseShouldProcessForStateChangingFunctions

Function New-/Set-... has verb that could change system state. Therefore, the function has to support 'ShouldProcess'
This commit is contained in:
Alexis La Goutte 2021-07-23 21:20:20 +02:00
parent 1ca534b3a4
commit 0d28d3ef01
5 changed files with 52 additions and 36 deletions

View file

@ -13,7 +13,8 @@
function New-NetboxDCIMDevice {
[CmdletBinding()]
[CmdletBinding(ConfirmImpact = 'low',
SupportsShouldProcess = $true)]
[OutputType([pscustomobject])]
#region Parameters
param
@ -64,21 +65,21 @@ function New-NetboxDCIMDevice {
)
#endregion Parameters
# if ($null -ne $Device_Role) {
# # Validate device role?
# }
# if ($null -ne $Device_Role) {
# # Validate device role?
# }
# if ($null -ne $Device_Type) {
# # Validate device type?
# }
# if ($null -ne $Device_Type) {
# # Validate device type?
# }
# if ($null -ne $Status) {
# $PSBoundParameters.Status = ValidateDCIMChoice -ProvidedValue $Status -DeviceStatus
# }
# if ($null -ne $Status) {
# $PSBoundParameters.Status = ValidateDCIMChoice -ProvidedValue $Status -DeviceStatus
# }
# if ($null -ne $Face) {
# $PSBoundParameters.Face = ValidateDCIMChoice -ProvidedValue $Face -DeviceFace
# }
# if ($null -ne $Face) {
# $PSBoundParameters.Face = ValidateDCIMChoice -ProvidedValue $Face -DeviceFace
# }
$Segments = [System.Collections.ArrayList]::new(@('dcim', 'devices'))
@ -86,5 +87,7 @@ function New-NetboxDCIMDevice {
$URI = BuildNewURI -Segments $URIComponents.Segments
InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method POST
if ($PSCmdlet.ShouldProcess($Name, 'Create new Device')) {
InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method POST
}
}

View file

@ -13,12 +13,13 @@
function Set-NetboxDCIMInterface {
[CmdletBinding()]
[CmdletBinding(ConfirmImpact = 'Medium',
SupportsShouldProcess = $true)]
[OutputType([pscustomobject])]
param
(
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true)]
ValueFromPipelineByPropertyName = $true)]
[uint16[]]$Id,
[uint16]$Device,
@ -50,9 +51,9 @@ function Set-NetboxDCIMInterface {
)
begin {
# if ($null -ne $Form_Factor) {
# $PSBoundParameters.Form_Factor = ValidateDCIMChoice -ProvidedValue $Form_Factor -InterfaceFormFactor
# }
# if ($null -ne $Form_Factor) {
# $PSBoundParameters.Form_Factor = ValidateDCIMChoice -ProvidedValue $Form_Factor -InterfaceFormFactor
# }
if (-not [System.String]::IsNullOrWhiteSpace($Mode)) {
$PSBoundParameters.Mode = switch ($Mode) {
@ -88,7 +89,9 @@ function Set-NetboxDCIMInterface {
$URI = BuildNewURI -Segments $Segments
InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH
if ($Force -or $pscmdlet.ShouldProcess("Interface ID $($CurrentInterface.Id)", "Set")) {
InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH
}
}
}

View file

@ -13,6 +13,8 @@
function New-NetboxIPAMPrefix {
[CmdletBinding(ConfirmImpact = 'low',
SupportsShouldProcess = $true)]
[CmdletBinding()]
param
(
@ -40,7 +42,7 @@ function New-NetboxIPAMPrefix {
[switch]$Raw
)
# $PSBoundParameters.Status = ValidateIPAMChoice -ProvidedValue $Status -PrefixStatus
# $PSBoundParameters.Status = ValidateIPAMChoice -ProvidedValue $Status -PrefixStatus
<#
# As of 2018/10/18, this does not appear to be a validated IPAM choice
@ -55,5 +57,7 @@ function New-NetboxIPAMPrefix {
$URI = BuildNewURI -Segments $URIComponents.Segments
InvokeNetboxRequest -URI $URI -Method POST -Body $URIComponents.Parameters -Raw:$Raw
if ($PSCmdlet.ShouldProcess($Prefix, 'Create new Prefix')) {
InvokeNetboxRequest -URI $URI -Method POST -Body $URIComponents.Parameters -Raw:$Raw
}
}

View file

@ -1,5 +1,5 @@
function New-NetboxIPAMVLAN {
<#
<#
.SYNOPSIS
Create a new VLAN
@ -40,7 +40,8 @@
Additional information about the function.
#>
[CmdletBinding()]
[CmdletBinding(ConfirmImpact = 'low',
SupportsShouldProcess = $true)]
[OutputType([pscustomobject])]
param
(
@ -63,11 +64,11 @@
[switch]$Raw
)
# $PSBoundParameters.Status = ValidateIPAMChoice -ProvidedValue $Status -VLANStatus
# $PSBoundParameters.Status = ValidateIPAMChoice -ProvidedValue $Status -VLANStatus
# if ($null -ne $Role) {
# $PSBoundParameters.Role = ValidateIPAMChoice -ProvidedValue $Role -IPAddressRole
# }
# if ($null -ne $Role) {
# $PSBoundParameters.Role = ValidateIPAMChoice -ProvidedValue $Role -IPAddressRole
# }
$segments = [System.Collections.ArrayList]::new(@('ipam', 'vlans'))
@ -75,5 +76,7 @@
$URI = BuildNewURI -Segments $URIComponents.Segments
InvokeNetboxRequest -URI $URI -Method POST -Body $URIComponents.Parameters -Raw:$Raw
if ($PSCmdlet.ShouldProcess($nae, 'Create new Vlan $($vid)')) {
InvokeNetboxRequest -URI $URI -Method POST -Body $URIComponents.Parameters -Raw:$Raw
}
}

View file

@ -13,7 +13,8 @@
function New-NetboxVirtualMachine {
[CmdletBinding()]
[CmdletBinding(ConfirmImpact = 'low',
SupportsShouldProcess = $true)]
[OutputType([pscustomobject])]
param
(
@ -46,12 +47,12 @@ function New-NetboxVirtualMachine {
[string]$Comments
)
# $ModelDefinition = $script:NetboxConfig.APIDefinition.definitions.WritableVirtualMachineWithConfigContext
# $ModelDefinition = $script:NetboxConfig.APIDefinition.definitions.WritableVirtualMachineWithConfigContext
# # Validate the status against the APIDefinition
# if ($ModelDefinition.properties.status.enum -inotcontains $Status) {
# throw ("Invalid value [] for Status. Must be one of []" -f $Status, ($ModelDefinition.properties.status.enum -join ', '))
# }
# # Validate the status against the APIDefinition
# if ($ModelDefinition.properties.status.enum -inotcontains $Status) {
# throw ("Invalid value [] for Status. Must be one of []" -f $Status, ($ModelDefinition.properties.status.enum -join ', '))
# }
#$PSBoundParameters.Status = ValidateVirtualizationChoice -ProvidedValue $Status -VirtualMachineStatus
@ -61,7 +62,9 @@ function New-NetboxVirtualMachine {
$URI = BuildNewURI -Segments $URIComponents.Segments
InvokeNetboxRequest -URI $URI -Method POST -Body $URIComponents.Parameters
if ($PSCmdlet.ShouldProcess($name, 'Create new Virtual Machine')) {
InvokeNetboxRequest -URI $URI -Method POST -Body $URIComponents.Parameters
}
}