Skip to content

Commit

Permalink
Share codebase for Enable, Disable, Switch and Remove
Browse files Browse the repository at this point in the history
  • Loading branch information
fvanroie committed Nov 18, 2018
1 parent e4f8798 commit f16b5e9
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 28 deletions.
65 changes: 65 additions & 0 deletions Private/Items/Initialize-CommonItemSplat.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<# MIT License
Copyright (c) 2018 fvanroie, NetwiZe.be
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#>

<#
Shared with Enable, Disable, Switch or Remove
#>
Function Initialize-CommonItemSplat {
# .EXTERNALHELP ../PS_OPNsense.psd1-Help.xml
[OutputType([Object[]])]
[CmdletBinding(
)]
Param(
[String]$CmdletName,
[switch]$PassThru
)

# Get the Verb of this cmdlet
$Verb, $null = $CmdletName -split "-", 2

$Splat = @{
Verb = $Verb
PassThru = $PassThru
Enabled = ($verb -eq "Enable")
}

switch ($Verb) {
'Enable' {
$Action = 'toggle'
}
'Disable' {
$Action = 'toggle'
}
'Switch' {
$Action = 'toggle'
$Splat.Remove('Enabled')
}
'Remove' {
$Action = 'remove'
$Splat.Remove('Enabled')
}
default {}
}

return $Action, $Splat
}
12 changes: 5 additions & 7 deletions Public/Items/Disable-OPNsenseItem.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ Function Disable-OPNsenseItem {
[Switch]$PassThru
)
BEGIN {
# Get the Verb of this cmdlet
$Verb, $null = $MyInvocation.MyCommand -split "-", 2
$Action = 'toggle'
$Enabled = ($verb -eq "Enable")

# Common Splat parameters shared with Enable, Disable, Switch and Remove
$Action, $CommonSplat = Initialize-CommonItemSplat -CmdletName $MyInvocation.MyCommand -PassThru:$PassThru

# Pre-filter all matching api calls for this action
$Commands = $OPNsenseOpenApi.values.values.values | Where-Object { $_.action -eq $action }
}
Expand All @@ -56,10 +54,10 @@ Function Disable-OPNsenseItem {
# Ask confirmation before performing action, if needed
if ($Force -Or $PSCmdlet.ShouldProcess(
("{{{0}}}" -f $Obj.Uuid) ,
("{0} {1}" -f $Verb, $Call.Object)
("{0} {1}" -f $CommonSplat.Verb, $Call.Object)
) ) {

Invoke-OPNsenseItem -Call $Call -InputObject $Obj -Enabled:$Enabled -Verb $Verb -PassThru:$PassThru
Invoke-OPNsenseItem -Call $Call -InputObject $Obj @CommonSplat #-Enabled:$Enabled -Verb $Verb -PassThru:$PassThru

} # ShouldProcess

Expand Down
12 changes: 5 additions & 7 deletions Public/Items/Enable-OPNsenseItem.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ Function Enable-OPNsenseItem {
[Switch]$PassThru
)
BEGIN {
# Get the Verb of this cmdlet
$Verb, $null = $MyInvocation.MyCommand -split "-", 2
$Action = 'toggle'
$Enabled = ($verb -eq "Enable")

# Common Splat parameters shared with Enable, Disable, Switch and Remove
$Action, $CommonSplat = Initialize-CommonItemSplat -CmdletName $MyInvocation.MyCommand -PassThru:$PassThru

# Pre-filter all matching api calls for this action
$Commands = $OPNsenseOpenApi.values.values.values | Where-Object { $_.action -eq $action }
}
Expand All @@ -56,10 +54,10 @@ Function Enable-OPNsenseItem {
# Ask confirmation before performing action, if needed
if ($Force -Or $PSCmdlet.ShouldProcess(
("{{{0}}}" -f $Obj.Uuid) ,
("{0} {1}" -f $Verb, $Call.Object)
("{0} {1}" -f $CommonSplat.Verb, $Call.Object)
) ) {

Invoke-OPNsenseItem -Call $Call -InputObject $Obj -Enabled:$Enabled -Verb $Verb -PassThru:$PassThru
Invoke-OPNsenseItem -Call $Call -InputObject $Obj @CommonSplat #-Enabled:$Enabled -Verb $Verb -PassThru:$PassThru

} # ShouldProcess

Expand Down
12 changes: 5 additions & 7 deletions Public/Items/Remove-OPNsenseItem.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ Function Remove-OPNsenseItem {
[Switch]$PassThru
)
BEGIN {
# Get the Verb of this cmdlet
$Verb, $null = $MyInvocation.MyCommand -split "-", 2
$Action = 'delete'
#$Enabled = ($verb -eq "Enable")

# Common Splat parameters shared with Enable, Disable, Switch and Remove
$Action, $CommonSplat = Initialize-CommonItemSplat -CmdletName $MyInvocation.MyCommand -PassThru:$PassThru

# Pre-filter all matching api calls for this action
$Commands = $OPNsenseOpenApi.values.values.values | Where-Object { $_.action -eq $action }
}
Expand All @@ -56,10 +54,10 @@ Function Remove-OPNsenseItem {
# Ask confirmation before performing action, if needed
if ($Force -Or $PSCmdlet.ShouldProcess(
("{{{0}}}" -f $Obj.Uuid) ,
("{0} {1}" -f $Verb, $Call.Object)
("{0} {1}" -f $CommonSplat.Verb, $Call.Object)
) ) {

Invoke-OPNsenseItem -Call $Call -InputObject $Obj -Verb $Verb -PassThru:$PassThru
Invoke-OPNsenseItem -Call $Call -InputObject $Obj @CommonSplat #-Enabled:$Enabled -Verb $Verb -PassThru:$PassThru

} # ShouldProcess

Expand Down
12 changes: 5 additions & 7 deletions Public/Items/Switch-OPNsenseItem.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ Function Switch-OPNsenseItem {
[Switch]$PassThru
)
BEGIN {
# Get the Verb of this cmdlet
$Verb, $null = $MyInvocation.MyCommand -split "-", 2
$Action = 'toggle'
$Enabled = ($verb -eq "Enable")

# Common Splat parameters shared with Enable, Disable, Switch and Remove
$Action, $CommonSplat = Initialize-CommonItemSplat -CmdletName $MyInvocation.MyCommand -PassThru:$PassThru

# Pre-filter all matching api calls for this action
$Commands = $OPNsenseOpenApi.values.values.values | Where-Object { $_.action -eq $action }
}
Expand All @@ -56,10 +54,10 @@ Function Switch-OPNsenseItem {
# Ask confirmation before performing action, if needed
if ($Force -Or $PSCmdlet.ShouldProcess(
("{{{0}}}" -f $Obj.Uuid) ,
("{0} {1}" -f $Verb, $Call.Object)
("{0} {1}" -f $CommonSplat.Verb, $Call.Object)
) ) {

Invoke-OPNsenseItem -Call $Call -InputObject $Obj -Verb $Verb -PassThru:$PassThru
Invoke-OPNsenseItem -Call $Call -InputObject $Obj @CommonSplat #-Enabled:$Enabled -Verb $Verb -PassThru:$PassThru

} # ShouldProcess

Expand Down

0 comments on commit f16b5e9

Please sign in to comment.