Skip to content

Commit

Permalink
Interface(System): Add -data parameter(with Tests)
Browse files Browse the repository at this point in the history
for Add or Set Interface
  • Loading branch information
alagoutte committed Feb 1, 2024
1 parent f50de5f commit 1961d8a
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
28 changes: 28 additions & 0 deletions PowerFGT/Public/cmdb/system/interface.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ function Add-FGTSystemInterface {
Add-FGTSystemInterface -name PowerFGT_lo -loopback -mode static -ip 192.0.2.1 -netmask 255.255.255.255 -allowaccess ping
This creates a new interface loopback with IP 192.0.2.1(/32) and allow access to ping
.EXAMPLE
$data = @{ 'sflow-sampler' = "enable" }
Add-FGTSystemInterface -name PowerFGT -interface port10 -vlan_id 10 -data $data
This creates a new interface with sflow-sampler enable using -data parameter
#>

Param(
Expand Down Expand Up @@ -77,6 +83,8 @@ function Add-FGTSystemInterface {
[string]$netmask,
[Parameter (Mandatory = $false)]
[string]$vdom_interface = "root",
[Parameter (Mandatory = $false)]
[hashtable]$data,
[Parameter(Mandatory = $false)]
[String[]]$vdom,
[Parameter(Mandatory = $false)]
Expand Down Expand Up @@ -166,6 +174,12 @@ function Add-FGTSystemInterface {
$_interface | add-member -name "device-identification" -membertype NoteProperty -Value $device_identification
}

if ( $PsBoundParameters.ContainsKey('data') ) {
$data.GetEnumerator() | ForEach-Object {
$_interface | Add-member -name $_.key -membertype NoteProperty -Value $_.value
}
}

$null = Invoke-FGTRestMethod -uri $uri -method 'POST' -body $_interface -connection $connection @invokeParams

Get-FGTSystemInterface -name $name -connection $connection @invokeParams
Expand Down Expand Up @@ -377,6 +391,12 @@ function Set-FGTSystemInterface {
Get-FGTSystemInterface -name PowerFGT | Set-FGTSystemInterface -dhcprelayip $null
This disables DCHP relay and clears the relay ip addresses
.EXAMPLE
$data = @{ "sflow-sampler" = "enable" }
PS C:\>Get-FGTSystemInterface -name PowerFGT | Set-FGTSystemInterface -data $data
Configure sflow-sampler setting using -data parameter on interface PowerFGT
#>

[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'medium')]
Expand Down Expand Up @@ -405,6 +425,8 @@ function Set-FGTSystemInterface {
[string]$status,
[Parameter (Mandatory = $false)]
[string]$device_identification,
[Parameter (Mandatory = $false)]
[hashtable]$data,
[Parameter(Mandatory = $false)]
[String[]]$vdom,
[Parameter(Mandatory = $false)]
Expand Down Expand Up @@ -477,6 +499,12 @@ function Set-FGTSystemInterface {
}
}

if ( $PsBoundParameters.ContainsKey('data') ) {
$data.GetEnumerator() | ForEach-Object {
$_interface | Add-member -name $_.key -membertype NoteProperty -Value $_.value
}
}

if ($PSCmdlet.ShouldProcess($interface.name, 'Set interface')) {
$null = Invoke-FGTRestMethod -uri $uri -method 'PUT' -body $_interface -connection $connection @invokeParams
Get-FGTSystemInterface -name $interface.name -connection $connection @invokeParams
Expand Down
84 changes: 84 additions & 0 deletions Tests/integration/SystemInterface.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,75 @@ Describe "Add System Interface" -ForEach $type {
$interface.ip | Should -Be "192.0.2.1 255.255.255.0"
}

It "Add System Interface (with -data (1 field))" {
$data = @{ "alias" = "int_PowerFGT" }
$p = $_.param
Add-FGTSystemInterface -name $pester_int1 @p -data $data
$interface = Get-FGTSystemInterface -name $pester_int1
$interface.name | Should -Be $pester_int1
switch ($_.type) {
"vlan" {
$interface.type | Should -Be "vlan"
$interface.vlanid | Should -Be $pester_vlanid1
$interface.interface | Should -Be $pester_port1
}
"aggregate_lacp" {
$interface.type | Should -Be "aggregate"
$interface.member.'interface-name' | Should -BeIn $pester_port1, $pester_port2
}
"aggregate_static" {
if (($fgt_version -ge "6.2.0")) {
$interface.type | Should -Be "redundant"
}
else {
$interface.type | Should -Be "aggregate"
}
$interface.member.'interface-name' | Should -BeIn $pester_port1, $pester_port2
}
"loopback" {
$interface.type | Should -Be "loopback"
}
}
$interface.role | Should -Be "lan"
$interface.mode | Should -Be "static"
$interface.alias | Should -Be "int_PowerFGT"
}

It "Add System Interface (with -data (2 fields))" {
$data = @{ "alias" = "int_PowerFGT"; description = "Add via PowerFGT using -data" }
$p = $_.param
Add-FGTSystemInterface -name $pester_int1 @p -data $data
$interface = Get-FGTSystemInterface -name $pester_int1
$interface.name | Should -Be $pester_int1
switch ($_.type) {
"vlan" {
$interface.type | Should -Be "vlan"
$interface.vlanid | Should -Be $pester_vlanid1
$interface.interface | Should -Be $pester_port1
}
"aggregate_lacp" {
$interface.type | Should -Be "aggregate"
$interface.member.'interface-name' | Should -BeIn $pester_port1, $pester_port2
}
"aggregate_static" {
if (($fgt_version -ge "6.2.0")) {
$interface.type | Should -Be "redundant"
}
else {
$interface.type | Should -Be "aggregate"
}
$interface.member.'interface-name' | Should -BeIn $pester_port1, $pester_port2
}
"loopback" {
$interface.type | Should -Be "loopback"
}
}
$interface.role | Should -Be "lan"
$interface.mode | Should -Be "static"
$interface.alias | Should -Be "int_PowerFGT"
$interface.description | Should -Be "Add via PowerFGT using -data"
}

It "Add Vlan System Interface (on aggregate $($_.type) interface)" -Skip:($_.type -eq "loopback" -or $_.type -eq "vlan") {
$p = $_.param
Add-FGTSystemInterface -name $pester_int1 @p
Expand Down Expand Up @@ -802,6 +871,21 @@ Describe "Set System Interface" -ForEach $type {
$interface.'dhcp-relay-service' | Should -Be "disable"
}

It "Set System Interface using -data (1 field)" {
$data = @{ "alias" = "int_PowerFGT" }
Get-FGTSystemInterface -name $pester_int1 | Set-FGTSystemInterface -data $data
$interface = Get-FGTSystemInterface -name $pester_int1
$interface.alias | Should -Be "int_PowerFGT"
}

It "Set System Interface using -data (2 fields)" {
$data = @{ "alias" = "int_PowerFGT" ; description = "Modified via PowerFGT using -data" }
Get-FGTSystemInterface -name $pester_int1 | Set-FGTSystemInterface -data $data
$interface = Get-FGTSystemInterface -name $pester_int1
$interface.alias | Should -Be "int_PowerFGT"
$interface.description | Should -Be "Modified via PowerFGT using -data"
}

AfterAll {
Get-FGTSystemInterface -name $pester_int1 | Remove-FGTSystemInterface -Confirm:$false
}
Expand Down

0 comments on commit 1961d8a

Please sign in to comment.