Skip to content

Commit 41a0337

Browse files
committed
add Get-VNVDTrafficRuleAction, start of Pester tests, fix two minor bugs
1 parent 75a9609 commit 41a0337

File tree

7 files changed

+105
-6
lines changed

7 files changed

+105
-6
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
### Need to make:
5-
- `Get-VNVDTrafficRuleAction`
65
- `Set-VNVDTrafficRuleSet -Enabled -Precedence -TrafficRuleset`
76
- define cmdlet `ConfirmImpact` to High
87
- Examples/docs
@@ -34,6 +33,7 @@ Done (to at least some extent -- some may have further features to implement):
3433
- implemented, but initially with a bug (now worked around): cannot rely on TrafficRule object's `Key` property, as that changes with every vDPortgroup reconfig, apparently (so, if iterating through several Rules, after the removal of the 1st one, the keys for the rest in the pipeline are invalid)
3534
- so, must do the `Process` differently so that all TrafficRule items per vDPortgroup are removed in one reconfig (or, other, less reliable ways, for which I did not opt)
3635
- Operating with the understanding/observation that there is only ever one (1) `Config.DefaultPortConfig.FilterPolicy.FilterConfig` per vDPortgroup (and, so, one subsequent TrafficRuleset, since a FilterConfig has one TrafficRuleset), even though the `.FilterConfig` property is of type `VMware.Vim.DvsFilterConfig[]`; so, using single TrafficRuleset per group of TrafficRules to remove; may need revisited in the future
36+
- `Get-VNVDTrafficRuleAction`
3737

3838
## Get
3939
`Get-VDPortgroup | Get-VNVDTrafficRuleSet | Get-VNVDTrafficRule`

Update-ThisModuleManifest.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ begin {
2121
# AliasesToExport = @()
2222
FileList = Write-Output "${strModuleName}.psd1" "${strModuleName}_ModRoot.psm1" "en-US\about_${strModuleName}.help.txt" GetItems.ps1 NewItems.ps1 RemoveItems.ps1 "${strModuleName}_SupportingFunctions.ps1" "${strModuleName}.format.ps1xml"
2323
FormatsToProcess = "${strModuleName}.format.ps1xml"
24-
FunctionsToExport = Write-Output Get-VNVDTrafficFilterPolicyConfig Get-VNVDTrafficRuleSet Get-VNVDTrafficRule Get-VNVDTrafficRuleQualifier New-VNVDTrafficRuleQualifier New-VNVDTrafficRuleAction New-VNVDTrafficRule Remove-VNVDTrafficRule
24+
FunctionsToExport = Write-Output Get-VNVDTrafficFilterPolicyConfig Get-VNVDTrafficRuleSet Get-VNVDTrafficRule Get-VNVDTrafficRuleQualifier Get-VNVDTrafficRuleAction New-VNVDTrafficRuleQualifier New-VNVDTrafficRuleAction New-VNVDTrafficRule Remove-VNVDTrafficRule
2525
IconUri = "https://avatars0.githubusercontent.com/u/10615837"
2626
LicenseUri = "https://github.com/vNugglets/vNuggletsPSMod_vDNetworking/blob/master/License"
2727
NestedModules = Write-Output GetItems.ps1 NewItems.ps1 RemoveItems.ps1 "${strModuleName}_SupportingFunctions.ps1"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<# .Description
2+
Initialization code for use by multiple *.Tests.ps1 files for testing vNugglets.vDNetworking PowerShell module
3+
#>
4+
param (
5+
## Name of the vCenter virtual Datacenter to use in the vNugglets.vDNetworking testing
6+
[parameter(Mandatory=$true)][string]$Datacenter
7+
)
8+
9+
$strThisModuleName = "vNugglets.vDNetworking"
10+
## if module not already loaded, try to load it (assumes that module is in PSModulePath)
11+
if (-not ($oModuleInfo = Get-Module $strThisModuleName)) {
12+
$oModuleInfo = Import-Module $strThisModuleName -PassThru
13+
if (-not ($oModuleInfo -is [System.Management.Automation.PSModuleInfo])) {Throw "Could not load module '$strThisModuleName' -- is it available in the PSModulePath? You can manually load the module and start tests again"}
14+
} ## end if
15+
Write-Verbose -Verbose ("Starting testing of module '{0}' (version '{1}' from '{2}')" -f $oModuleInfo.Name, $oModuleInfo.Version, $oModuleInfo.Path)
16+
17+
## get the VIServer connection to use
18+
$oVIServerConnectionToUse = if (-not (($global:DefaultVIServers | Measure-Object).Count -gt 0)) {
19+
$hshParamForConnectVIServer = @{Server = $(Read-Host -Prompt "vCenter server to which to connect for testing")}
20+
Connect-VIServer @hshParamForConnectVIServer
21+
} ## end if
22+
else {$global:DefaultVIServers[0]}
23+
Write-Verbose -Verbose "Testing using VIServer of name '$($oVIServerConnectionToUse.Name)'"
24+
25+
26+
## get the datacenter use for testing
27+
$oVDatacenterToUse = Get-Datacenter -Name $Datacenter | Select-Object -First 1
28+
Write-Verbose -Verbose "Testing using virtual datacenter '$oVDatacenterToUse'"
29+
30+
return $oVDatacenterToUse
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<# .Description
2+
Pester tests for vNugglets.VDNetworking PowerShell module. Expects that:
3+
0) vNugglets.VDNetworking module is already loaded (but, will try to load it if not)
4+
1) a connection to at least one vCenter is in place (but, will prompt for vCenter to which to connect if not)
5+
6+
.Example
7+
Invoke-Pester -Script @{Path = '\\some\path\vNuggletsPSMod_VDNetworking\testing\vNugglets.VDNetworking.Tests_Get.ps1'; Parameters = @{Datacenter = "myFavoriteDatacenter"}}
8+
Invokes the tests in said Tests script, passing the given Datacenter parameter value, to be used for the cluster-specific tests
9+
#>
10+
param (
11+
## Name of the vCenter cluster to use in the vNugglets.VDNetworking testing
12+
[parameter(Mandatory=$true)][string]$Datacenter
13+
)
14+
15+
## initialize things, preparing for tests
16+
$oDatacenterToUse = & $PSScriptRoot\vNugglets.VDNetworking.TestingInit.ps1 -Datacenter $Datacenter
17+
$strGuidForThisTest = (New-Guid).Guid
18+
19+
## create a new VDSwitch on which to test
20+
$oTestVDSwitch = New-VDSwitch -Name "vNuggsTestVDS_toDelete_${strGuidForThisTest}" -Location $oDatacenterToUse -Verbose
21+
22+
## create a new vDPortgroup
23+
$oTestVDPG = $oTestVDSwitch | New-VDPortgroup -Name "vNuggsTestVDPG_toDelete_${strGuidForThisTest}" -Notes "testing vDPG" -Verbose
24+
25+
<# tests
26+
- get TrafficRuleSet (should be disabled)
27+
- get TrafficRule (should be 0)
28+
- create three TrafficRules
29+
$oTestVDPG | Get-VNVDTrafficRuleSet | New-VNVDTrafficRule -Name "testRule0_toDelete_${strGuidForThisTest}" -Action (New-VNVDTrafficRuleAction -Allow) -Direction both -Qualifier (New-VNVDTrafficRuleQualifier -SystemTrafficType faultTolerance -NegateSystemTrafficType), (New-VNVDTrafficRuleQualifier -SourceIpAddress 172.16.10.0/24 -DestinationIpAddress 10.0.0.0/8 -SourceIpPort 443-444)
30+
$oTestVDPG | Get-VNVDTrafficRuleSet | New-VNVDTrafficRule -Name "testRule1_toDelete_${strGuidForThisTest}" -Action (New-VNVDTrafficRuleAction -QosTag 5 -DscpTag 23) -Direction both -Qualifier (New-VNVDTrafficRuleQualifier -SystemTrafficType vsan)
31+
$oTestVDPG | Get-VNVDTrafficRuleSet | New-VNVDTrafficRule -Name "testRule2_toDelete_${strGuidForThisTest}" -Action (New-VNVDTrafficRuleAction -QosTag 7 -DscpTag 30) -Direction both -Qualifier (New-VNVDTrafficRuleQualifier -SystemTrafficType vdp), (New-VNVDTrafficRuleQualifier -DestinationIpAddress 172.16.100.0/24)
32+
- get TrafficRuleSet (should have three TrafficRules)
33+
- get TrafficRules (should be three)
34+
- remove two TrafficRules
35+
- get TrafficRuleSet (should have one TrafficRule)
36+
- get TrafficRules (should be one)
37+
#>
38+
39+
## remove the VDSwitch when done
40+
$oTestVDSwitch | Remove-VDSwitch -Verbose

vNugglets.VDNetworking/GetItems.ps1

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function Get-VNVDTrafficRule {
126126
## if -Name was passed, only return rules whose descriptions are like the given name value(s)
127127
if ($PSBoundParameters.ContainsKey("Name")) {$_.TrafficRuleset.Rules | Where-Object {$oThisDescription = $_.Description; ($Name | Foreach-Object {$oThisDescription -like $_}) -contains $true}}
128128
elseif ($PSBoundParameters.ContainsKey("LiteralName")) {$_.TrafficRuleset.Rules | Where-Object {$LiteralName -contains $_.Description}}
129-
else {$_.TrafficRuleset.Rules}
129+
else {$_.TrafficRuleset.Rules | Where-Object {$null -ne $_}}
130130
$arrRulesOfInterest | Foreach-Object {
131131
$oThisTrafficRule = $_
132132
New-Object -Type VNVDTrafficRule -Property @{
@@ -174,3 +174,30 @@ function Get-VNVDTrafficRuleQualifier {
174174
} ## end foreach-object
175175
} ## end process
176176
} ## end function
177+
178+
179+
180+
function Get-VNVDTrafficRuleAction {
181+
<# .Description
182+
Function to get the VDTrafficRule Action for the TrafficRule from the given VDTrafficFilterPolicy configuration from VDPortgroup(s).
183+
184+
.Example
185+
Get-VDSwitch -Name myVDSw0 | Get-VDPortGroup -Name myVDPG0 | Get-VNVDTrafficFilterPolicyConfig | Get-VNVDTrafficRule | Get-VNVDTrafficRuleAction
186+
Get the traffic rules action from the traffic rules from the TrafficeRuleset property of the TrafficFilterPolicyConfig
187+
188+
.Outputs
189+
VMware.Vim.DvsNetworkRuleAction
190+
#>
191+
[CmdletBinding()]
192+
[OutputType([VMware.Vim.DvsNetworkRuleAction])]
193+
param (
194+
## The traffic ruleset rule from the traffic filter policy of the virtual distributed portgroup for which to get the traffic rule action
195+
[parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="ByTrafficRule")][VNVDTrafficRule[]]$TrafficRule
196+
) ## end param
197+
198+
process {
199+
$TrafficRule | Foreach-Object {
200+
$_.TrafficRule.Action
201+
} ## end foreach-object
202+
} ## end process
203+
} ## end function

vNugglets.VDNetworking/vNugglets.VDNetworking.psd1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ NestedModules = @('GetItems.ps1',
7474
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
7575
FunctionsToExport = 'Get-VNVDTrafficFilterPolicyConfig', 'Get-VNVDTrafficRuleSet',
7676
'Get-VNVDTrafficRule', 'Get-VNVDTrafficRuleQualifier',
77-
'New-VNVDTrafficRuleQualifier', 'New-VNVDTrafficRuleAction',
78-
'New-VNVDTrafficRule', 'Remove-VNVDTrafficRule'
77+
'Get-VNVDTrafficRuleAction', 'New-VNVDTrafficRuleQualifier',
78+
'New-VNVDTrafficRuleAction', 'New-VNVDTrafficRule',
79+
'Remove-VNVDTrafficRule'
7980

8081
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
8182
CmdletsToExport = @()

vNugglets.VDNetworking/vNugglets.VDNetworking_SupportingFunctions.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ function _Set-VNVDTrafficRuleset_helper {
4444
DefaultPortConfig = New-Object -Type VMware.Vim.VMwareDVSPortSetting -Property @{
4545
FilterPolicy = New-Object -Type VMware.Vim.DvsFilterPolicy -Property @{
4646
FilterConfig = New-Object -Type VMware.Vim.DvsTrafficFilterConfig -Property @{
47-
TrafficRuleset = $oThisVNVDTrafficRuleset.TrafficRuleset
47+
## if the current TrafficRuleset property is $null, create a new TrafficRuleset; else, use the existing TrafficRuleset
48+
TrafficRuleset = if ($null -eq $oThisVNVDTrafficRuleset.TrafficRuleset) {New-Object -TypeName VMware.Vim.DvsTrafficRuleset} else {$oThisVNVDTrafficRuleset.TrafficRuleset}
4849
## use the current FilterConfig value for this property, and not setting the other properties
4950
AgentName = $oVDPortgroupView_ThisTrafficRuleset.Config.DefaultPortConfig.FilterPolicy.FilterConfig.AgentName
5051
} ## end new-object

0 commit comments

Comments
 (0)