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

add Configuration for vDPorts and pester tests #2

Open
wants to merge 4 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
6 changes: 3 additions & 3 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ Contents:
This PowerShell module provides functionality to automate the management of VMware vSphere virtual distributed networking items for which VMware PowerCLI does not already provide support. For example, for the reporting on-, creation of-, and removal of traffic filtering and marking rules at the vDPortgroup level. Another capability: managing the VDUplink of which a VMHost VMNIC is a part.

Some of the functionality provided by the cmdlets in this module:
- Get VDPortgroup traffic policy
- Get VDPortgroup and VDPort traffic policy
- Get traffic policy rules
- Get traffic policy rule qualifiers
- Create traffic policy rule qualifiers, for use in creation of new policy rules
- Create new traffic rules for the ruleset for the given vDPortgroup
- Remove given traffic rule(s) from a vDPortgroup
- Create new traffic rules for the ruleset for the given vDPortgroup or vDPort
- Remove given traffic rule(s) from a vDPortgroup or vDPort
- Set the VDSwitch Uplink for a VMHost physical NIC ("VMNIC") on the VDSwitch of which the VMNIC is already a part

<a id="quickStart"></a>
Expand Down
2 changes: 1 addition & 1 deletion Update-ThisModuleManifest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ begin {
RequiredModules = "VMware.VimAutomation.Vds"
RootModule = "${strModuleName}_ModRoot.psm1"
ScriptsToProcess = "${strModuleName}_init.ps1", "${strModuleName}_ClassDefinition.ps1"
Tags = Write-Output vNugglets VMware vSphere PowerCLI VDPortGroup TrafficFiltering Filter Filtering TrafficMarking Mark Marking VDSwitch Uplink VDUplink VMHostNetworkAdapater VMNIC
Tags = Write-Output vNugglets VMware vSphere PowerCLI VDPortGroup VDPort TrafficFiltering Filter Filtering TrafficMarking Mark Marking VDSwitch Uplink VDUplink VMHostNetworkAdapater VMNIC
# Verbose = $true
} ## end hashtable

Expand Down
13 changes: 13 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Get-VDSwitch -Name myVDSw0 | Get-VDPortGroup -Name myVDPG0 | Get-VNVDTrafficFilt

## Get traffic rules whose name is like "myTestRule*"
Get-VDSwitch -Name myVDSw0 | Get-VDPortGroup -Name myVDPG0 | Get-VNVDTrafficFilterPolicyConfig | Get-VNVDTrafficRuleSet | Get-VNVDTrafficRule myTestRule*

## Get the traffic rules from the TrafficeRuleset, which was gotten from the vDP's TrafficFilterPolicyConfig
Get-VDSwitch -Name myVDSw0 | Get-VDPortGroup -Name myVDPG0 | Get-Port 123 | Get-VNVDTrafficFilterPolicyConfig | Get-VNVDTrafficRuleSet | Get-VNVDTrafficRule
```

#### `Get-VNVDTrafficRuleAction`: Get the VDTrafficRule Action for the TrafficRule from the given VDTrafficFilterPolicy configuration from VDPortgroup(s)
Expand Down Expand Up @@ -107,6 +110,16 @@ Get-VDSwitch -Name myVDSw0 | Get-VDPortGroup -Name myVDPG0 | Get-VNVDTrafficFilt
Get-VDSwitch -Name myVDSw0 | Get-VDPortGroup -Name myVDPG0 | Get-VNVDTrafficRuleSet | Set-VNVDTrafficRuleSet -Enabled:$false
```

#### `New-VNVDTrafficRule`: Make new Traffic Rule and add it to the given Traffic Ruleset of a vDPort traffic filter policy

```PowerShell
## Override to the given TrafficRuleset from the given vDPort. Then you can configure it.
Get-VDPortGroup myVDPG0 | Get-Port 123 | Get-VNVDTrafficRuleSet | Set-VNVDTrafficRuleSet -Override:$true

## Create a new Traffic Rule that has two Qualifiers and add it to the given TrafficRuleset from the given vDPortgroup. The new Traffic Rule adds a DSCP tag with value 8 to VM traffic from given source IP
Get-VDPortGroup myVDPG0 | Get-Port 123 | Get-VNVDTrafficRuleSet | New-VNVDTrafficRule -Name "Apply DSCP tag to VM traffic from given address" -Action (New-VNVDTrafficRuleAction -DscpTag 8) -Qualifier (New-VNVDTrafficRuleQualifier -SystemTrafficType virtualMachine), (New-VNVDTrafficRuleQualifier -SourceIpAddress 172.16.1.2) -Direction outgoingPackets
```

#### `Set-VNVMHostNetworkAdapterVDUplink`: Set the VDSwitch Uplink for a VMHost physical NIC ("VMNIC") on the VDSwitch of which the VMNIC is already a part

```PowerShell
Expand Down
307 changes: 307 additions & 0 deletions testing/vNugglets.VDNetworking.Tests.ps1

Large diffs are not rendered by default.

51 changes: 0 additions & 51 deletions testing/vNugglets.VDNetworking.Tests_Get.ps1

This file was deleted.

128 changes: 92 additions & 36 deletions vNugglets.VDNetworking/GetItems.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,67 @@ function Get-VNVDTrafficFilterPolicyConfig {
[OutputType([VNVDTrafficFilterPolicyConfig])]
param (
## The virtual distributed portgroup for which to get the traffic filtering and marking policy configuration
[parameter(Mandatory=$true, ValueFromPipeline=$true)][VMware.VimAutomation.Vds.Types.V1.VmwareVDPortgroup[]]$VDPortgroup
[parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName = 'ByVDPortgroup')][VMware.VimAutomation.Vds.Types.V1.VmwareVDPortgroup[]]$VDPortgroup,

## The View object for the virtual distributed portgroup for which to get the traffic filtering and marking policy configuration
[parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "ByVDPortGroupView")][VMware.Vim.DistributedVirtualPortgroup[]]$VDPortgroupView,

## The virtual distributed port for which to get the traffic filtering and marking policy configuration
[parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ByVDPort')][VMware.VimAutomation.Vds.Types.V1.VDPort[]]$VDPort,

## The View object for the virtual distributed port for which to get the traffic filtering and marking policy configuration
[parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="ByVDPortView")][VMware.Vim.DistributedVirtualPort[]]$VDPortView,

## The VM nic for which to get the traffic filtering and marking policy configuration
[parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ByNetworkAdapter')][VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.NetworkAdapter[]]$NetworkAdapter,

## The VM for which to get the traffic filtering and marking policy configuration
[parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ByVM')][VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]]$VM
) ## end param

process {
$VDPortgroup | Foreach-Object {
$oThisVDPG = $_
New-Object -Type VNVDTrafficFilterPolicyConfig -Property @{
TrafficFilterPolicyConfig = $oThisVDPG.ExtensionData.Config.DefaultPortConfig.FilterPolicy.FilterConfig
VDPortgroupView = $oThisVDPG.ExtensionData
} ## end new-object
} ## end foreach-object
Switch ($PSCmdlet.ParameterSetName) {
{"ByVDPortGroup", "ByVDPortGroupView" -contains $_} {
## get the View objects over which to iterate (either the .ExtensionData)
$(if ($PSCmdlet.ParameterSetName -eq "ByVDPortGroup") {$VDPortgroup | Foreach-Object {$_.ExtensionData}} else {$VDPortgroupView}) | Foreach-Object {
## update the ViewData for this vDPG, just to be sure that all is current
$oThisVDPGView = $_; $oThisVDPGView.UpdateViewData("Config")
New-Object -Type VNVDTrafficFilterPolicyConfig -Property @{
TrafficFilterPolicyConfig = $oThisVDPGView.Config.DefaultPortConfig.FilterPolicy.FilterConfig
VDPortgroupView = $oThisVDPGView
} ## end new-object
} ## end foreach-object
} ## end case

{"ByVDPort", "ByVDPortView" -contains $_} {
## get the View objects over which to iterate (either the .ExtensionData)
$(if ($PSCmdlet.ParameterSetName -eq "ByVDPort") {$VDPort | Foreach-Object {$_.ExtensionData}} else {$VDPortView}) | Foreach-Object {
## update the ViewData for this vDP, just to be sure that all is current
## UpdateViewData not exist on port, so we have to take the long way
$oThisVDPView = (Get-VDPort -VDPortgroup (Get-VDPortgroup -Id "DistributedVirtualPortgroup-$($_.PortgroupKey)") -Key $_.Key).ExtensionData
$oThisVDPView.Config.Setting.FilterPolicy.FilterConfig | ForEach-Object {
New-Object -Type VNVDTrafficFilterPolicyConfig -Property @{
TrafficFilterPolicyConfig = $_
VDPortView = $oThisVDPView
} ## end new-object
} ## end foreach-object
} ## end foreach-object
} ## end case

{"ByNetworkAdapter", "ByVM" -contains $_} {
## get the NetworkAdapter objects over which to iterate (either the the nics of the VM)
$(if ($PSCmdlet.ParameterSetName -eq "ByVM") {$VM | Foreach-Object {$_ | Get-NetworkAdapter}} else {$NetworkAdapter}) | Foreach-Object {
## get the vDPort View of the VM nic
$oThisVDPView = (Get-VDPort -VDPortgroup (Get-VDPortgroup -Id "DistributedVirtualPortgroup-$($_.ExtensionData.Backing.Port.PortgroupKey)") -Key $_.ExtensionData.Backing.Port.PortKey).ExtensionData
$oThisVDPView.Config.Setting.FilterPolicy.FilterConfig | ForEach-Object {
New-Object -Type VNVDTrafficFilterPolicyConfig -Property @{
TrafficFilterPolicyConfig = $_
VDPortView = $oThisVDPView
} ## end new-object
} ## end foreach-object
} ## end foreach-object
} ## end case
} ## end switch
} ## end process
} ## end fn

Expand Down Expand Up @@ -54,39 +104,44 @@ function Get-VNVDTrafficRuleSet {
[parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="ByVDPortGroup")][VMware.VimAutomation.Vds.Types.V1.VmwareVDPortgroup[]]$VDPortgroup,

## The View object for the virtual distributed portgroup for which to get the traffic ruleset
[parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="ByVDPortGroupView")][VMware.Vim.DistributedVirtualPortgroup[]]$VDPortgroupView
[parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="ByVDPortGroupView")][VMware.Vim.DistributedVirtualPortgroup[]]$VDPortgroupView,

## The virtual distributed port for which to get the traffic ruleset
[parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="ByVDPort")][VMware.VimAutomation.Vds.Types.V1.VDPort[]]$VDPort,

## The View object for the virtual distributed port for which to get the traffic ruleset
[parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "ByVDPortView")][VMware.Vim.DistributedVirtualPort[]]$VDPortView,

## The VM nic for which to get the traffic filtering and marking policy configuration
[parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ByNetworkAdapter')][VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.NetworkAdapter[]]$NetworkAdapter,

## The VM for which to get the traffic filtering and marking policy configuration
[parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ByVM')][VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]]$VM
) ## end param

process {
# get the traffic filtering and marking policy configuration from portgroup or port
Switch ($PSCmdlet.ParameterSetName) {
"ByTrafficFilterPolicyConfig" {
$TrafficFilterPolicyConfig | Foreach-Object {
New-Object -Type VNVDTrafficRuleSet -Property @{
TrafficRuleset = $_.TrafficFilterPolicyConfig.TrafficRuleset
TrafficRulesetEnabled = $_.TrafficFilterPolicyConfig.TrafficRuleset.Enabled
NumTrafficRule = ($_.TrafficFilterPolicyConfig.TrafficRuleset.Rules | Measure-Object).Count
VDPortgroupView = $_.VDPortgroupView
} ## end new-object
} ## end foreach-object
break
} ## end case

{"ByVDPortGroup", "ByVDPortGroupView" -contains $_} {
## get the View objects over which to iterate (either the .ExtensionData)
$(if ($PSCmdlet.ParameterSetName -eq "ByVDPortGroup") {$VDPortgroup | Foreach-Object {$_.ExtensionData}} else {$VDPortgroupView}) | Foreach-Object {
## update the ViewData for this vDPG, just to be sure that all is current
$oThisVDPGView = $_; $oThisVDPGView.UpdateViewData("Config")
$oThisVDPGView.Config.DefaultPortConfig.FilterPolicy.FilterConfig | Foreach-Object {
New-Object -Type VNVDTrafficRuleSet -Property @{
TrafficRuleset = $_.TrafficRuleset
TrafficRulesetEnabled = $_.TrafficRuleset.Enabled
NumTrafficRule = ($_.TrafficRuleset.Rules | Measure-Object).Count
VDPortgroupView = $oThisVDPGView
} ## end new-object
} ## end foreach-object
} ## end foreach-object
} ## end case
"ByVDPortGroup" { $TrafficFilterPolicyConfig = Get-VNVDTrafficFilterPolicyConfig -VDPortgroup $VDPortgroup }
"ByVDPortGroupView" { $TrafficFilterPolicyConfig = Get-VNVDTrafficFilterPolicyConfig -VDPortgroupView $VDPortgroupView }
"ByVDPort" { $TrafficFilterPolicyConfig = Get-VNVDTrafficFilterPolicyConfig -VDPort $VDPort }
"ByVDPortView" { $TrafficFilterPolicyConfig = Get-VNVDTrafficFilterPolicyConfig -VDPortView $VDPortView }
"ByNetworkAdapter" { $TrafficFilterPolicyConfig = Get-VNVDTrafficFilterPolicyConfig -NetworkAdapter $NetworkAdapter }
"ByVM" { $TrafficFilterPolicyConfig = Get-VNVDTrafficFilterPolicyConfig -VM $VM }
} ## end switch

$TrafficFilterPolicyConfig | Foreach-Object {
$oThisTrafficPolicyConfig = $_
$_.TrafficFilterPolicyConfig | Foreach-Object {
New-Object -Type VNVDTrafficRuleSet -Property @{
TrafficRuleset = $_.TrafficRuleset
TrafficRulesetEnabled = $_.TrafficRuleset.Enabled
NumTrafficRule = ($_.TrafficRuleset.Rules | Measure-Object).Count
VDPortgroupView = $oThisTrafficPolicyConfig.VDPortgroupView
VDPortView = $oThisTrafficPolicyConfig.VDPortView
} ## end new-object
} ## end foreach-object
} ## end foreach-object
} ## end process
} ## end function

Expand Down Expand Up @@ -134,6 +189,7 @@ function Get-VNVDTrafficRule {
Name = $oThisTrafficRule.Description
TrafficRule = $oThisTrafficRule
VDPortgroupView = $oThisTrafficRuleset.VDPortgroupView
VDPortView = $oThisTrafficRuleset.VDPortView
VNVDTrafficRuleSet = $oThisTrafficRuleset
} ## end new-object
} ## end foreach-object
Expand Down
3 changes: 3 additions & 0 deletions vNugglets.VDNetworking/NewItems.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ function New-VNVDTrafficRule {
## always add Direction key/value is in the hsh (so that, if not specified by user, it takes default value)
$hshParamForNewRuleObject = @{direction = $Direction}

## Add Sequence in Rules to make it visable in WebUI, when add more then 1 rule
$hshParamForNewRuleObject += @{sequence = ($TrafficRuleSet.TrafficRuleset.Rules.Sequence | Sort-Object | Select-Object -Last 1) + 10}

## for any of the other bound parameters that are for specific properties of a new Traffic Rule (i.e., that are not "common" PowerShell parameters like -Verbose or -PipelineVariable)
$PSBoundParameters.Keys | Where-Object {$hshParameterNameToNewObjectPropertyNameMapping.ContainsKey($_)} | Foreach-Object {
## get the new API object property name to use from the NameMapping hashtable, and set the value to that of the given bound parameter
Expand Down
11 changes: 9 additions & 2 deletions vNugglets.VDNetworking/RemoveItems.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,23 @@ function Remove-VNVDTrafficRule {

end {
## Group the TrafficRules by vDPortgroup (by grouping by MoRef per vCenter), then reconfig each vDPortgroup to remove the given Rule(s) for that vDPortgroup's sole TrafficRuleset all at once
$arrlVNVDTrafficRulesToRemove | Group-Object -Property @{e={$_.VDPortgroupView.MoRef}}, @{e={$_.VDPortgroupView.Client.ServiceUrl}} | Foreach-Object {
$arrlVNVDTrafficRulesToRemove | Group-Object -Property @{e = {$_.VDPortgroupView.MoRef}}, @{e = {$_.VDPortView.MoRef}}, @{e = {$_.VDPortgroupView.Client.ServiceUrl}} | Foreach-Object {
$oThisPSGroupInfoOfTrafficRules = $_
# The vDPortgroup with these TrafficRules (used in logging/reporting)
$oVDPG_TheseRules = $oThisPSGroupInfoOfTrafficRules.Group[0].VDPortgroupView
# The vDPort with these TrafficRules (used in logging/reporting)
$oVDP_TheseRules = $oThisPSGroupInfoOfTrafficRules.Group[0].VDPortView
# The VNVDTrafficRuleSet for these TrafficRules, to be used to remove the given TrafficRule(s)
$oVNVDTrafficRuleset_TheseRules = $oThisPSGroupInfoOfTrafficRules.Group[0].VNVDTrafficRuleSet
## the VMware.Vim.DvsTrafficRule objects to remove from the given TrafficRuleset
$arrDvsTrafficRulesToRemove = $oThisPSGroupInfoOfTrafficRules.Group.TrafficRule

$strMsgForShouldProcess_Target = "Traffic ruleset '{0}' on vDPortgroup '{1}'" -f $oVNVDTrafficRuleset_TheseRules.TrafficRuleset.Key, $oVDPG_TheseRules.Name
if ($null -ne $oVDPG_TheseRules) {
$strMsgForShouldProcess_Target = "Traffic ruleset '{0}' on vDPortgroup '{1}'" -f $oVNVDTrafficRuleset_TheseRules.TrafficRuleset.Key, $oVDPG_TheseRules.Name
} ## end if
else {
$strMsgForShouldProcess_Target = "Traffic ruleset '{0}' on vDPort '{1}'" -f $oVNVDTrafficRuleset_TheseRules.TrafficRuleset.Key, $oVDP_TheseRules.Key
} ## end else
$intNumDvsTrafficRulesToRemove = ($arrDvsTrafficRulesToRemove | Measure-Object).Count
$strMsgForShouldProcess_Action = "Remove {0} traffic rule{1} (of name{1} '{2}')" -f $intNumDvsTrafficRulesToRemove, $(if ($intNumDvsTrafficRulesToRemove -ne 1) {"s"}), ($arrDvsTrafficRulesToRemove.Description -join ", ")
if ($PSCmdlet.ShouldProcess($strMsgForShouldProcess_Target, $strMsgForShouldProcess_Action)) {
Expand Down
Loading