Skip to content

Commit c0306a9

Browse files
committed
finish Remove-VNVDTrafficRule, add Formats
1 parent 0419a0f commit c0306a9

9 files changed

+197
-108
lines changed

Update-ThisModuleManifest.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ begin {
1919
Copyright = "MIT License"
2020
Description = "Module with functions for managing VMware vSphere Virtual Distributed Networking components like traffic filtering and marking"
2121
# AliasesToExport = @()
22-
FileList = Write-Output "${strModuleName}.psd1" "${strModuleName}_ModRoot.psm1" "en-US\about_${strModuleName}.help.txt" GetItems.ps1 NewItems.ps1 "${strModuleName}_SupportingFunctions.ps1"
23-
# FormatsToProcess = "${strModuleName}.format.ps1xml"
24-
FunctionsToExport = Write-Output Get-VNVDTrafficFilterPolicyConfig Get-VNVDTrafficRuleSet Get-VNVDTrafficRule Get-VNVDTrafficRuleQualifier New-VNVDTrafficRuleQualifier New-VNVDTrafficRuleAction New-VNVDTrafficRule
22+
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"
23+
FormatsToProcess = "${strModuleName}.format.ps1xml"
24+
FunctionsToExport = Write-Output Get-VNVDTrafficFilterPolicyConfig Get-VNVDTrafficRuleSet Get-VNVDTrafficRule Get-VNVDTrafficRuleQualifier 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"
27-
NestedModules = Write-Output GetItems.ps1 NewItems.ps1 "${strModuleName}_SupportingFunctions.ps1"
27+
NestedModules = Write-Output GetItems.ps1 NewItems.ps1 RemoveItems.ps1 "${strModuleName}_SupportingFunctions.ps1"
2828
PowerShellVersion = [System.Version]"5.0"
2929
ProjectUri = "https://github.com/vNugglets/vNuggletsPSMod_vDNetworking"
3030
ReleaseNotes = "See release notes at https://github.com/vNugglets/vNuggletsPSMod_vDNetworking/blob/master/ChangeLog.md"

notes_SetCoSAndDSCPOnVDPG.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
## Notes on Traffic Filtering and Marking code
2+
3+
4+
### Need to make:
5+
- `Get-VNVDTrafficRuleAction`
6+
- `Set-VNVDTrafficRuleSet -Enabled -Precedence -TrafficRuleset`
7+
- define cmdlet `ConfirmImpact` to High
8+
- Examples/docs
9+
10+
### Maybe eventually add:
11+
- `Copy-VNVDTrafficRule -Rule -Ruleset <rulesettowhichtocopy>`
12+
- `Set-VNVDTrafficRule` -- to update a rule, maybe? (like change qualifier/action?)
13+
- `New-VNVDTrafficRule`
14+
- may need to add logic to ensure it meets requirement stated in API ref of, "There can be a maximum of 1 DvsIpNetworkRuleQualifier, 1 DvsMacNetworkRuleQualifier, and 1 DvsSystemTrafficNetworkRuleQualifier for a total of 3 qualifiers"
15+
- `Set-VNVDTrafficRuleSet -Enabled -Precedence -Rule -TrafficRuleset`
16+
- to enable/disable the Ruleset, and maybe to allow for the overwriting of the rules in the ruleset with just the new Rule(s) specified
17+
- add `-RunAsync` to `New-VNVDTrafficRule`, `Remove-VNVDTrafficRule`, and any other cmdlet where it makes sense
18+
19+
Done (to at least some extent -- some may have further features to implement):
20+
- `Get-VNVDTrafficFilterPolicyConfig`
21+
- `Get-VNVDTrafficRuleSet` (returns VNVDTrafficRuleSet object with VDPG property, too)
22+
- `Get-VNVDTrafficRule`
23+
- `Get-VNVDTrafficRuleQualifier`
24+
- `New-VNVDTrafficRuleQualifier`
25+
- `New-VNVDTrafficRuleAction`
26+
- remaining Action types to implement: DvsCopyNetworkRuleAction, DvsGreEncapNetworkRuleAction, DvsLogNetworkRuleAction, DvsMacRewriteNetworkRuleAction, DvsPuntNetworkRuleAction, DvsRateLimitNetworkRuleAction
27+
- Ruleset object returned from `Get-VNVDTrafficRuleSet` should have property of "parent vDPG", to be used for vDPG reconfig task (need to add vDPG property to return from `Get-VNVDTrafficFilterPolicyConfig`, `Get-VNVDTrafficRuleSet`, and `Get-VNVDTrafficRule`)
28+
- `New-VNVDTrafficRule`
29+
- adds rule to TrafficRuleset
30+
- `Remove-VNVDTrafficRule -Rule[]`
31+
- define cmdlet `ConfirmImpact` to `High`
32+
- removes a given rule from the associated ruleset on the given vDPortgroup
33+
- 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)
34+
- 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)
35+
- 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+
37+
## Get
38+
`Get-VDPortgroup | Get-VNVDTrafficRuleSet | Get-VNVDTrafficRule`
39+
40+
## New traffic rule, (adding traffic rule to traffic ruleset)
41+
`Get-VDPortgroup someVdpg | Get-VNVDTrafficRuleSet | New-VNVDTrafficRule -Rule (New-VNVDTrafficRule -Direction blahh -Qualifier (New-VNVDTrafficRuleQualifier -ParmsHere))`
42+
43+
## eventually? Set (overwrite) all rules in the ruleset (if any) with new rule(s) specified
44+
`Get-VDPortgroup someVdpg | Get-VNVDTrafficRuleSet | Set-VNVDTrafficRuleSet -Enabled -Rule (New-VNVDTrafficRule -Direction blahh -Qualifier (New-VNVDTrafficRuleQualifier -ParmsHere))`
45+
46+
## Remove some traffic rules
47+
`Get-VDPortgroup someVdpg | Get-VNVDTrafficRuleSet | Get-VNVDTrafficRule -Name testRule0, otherRule* | Remove-VNVDTrafficRule`
48+
49+
50+
## Other
51+
- example core code, from https://communities.vmware.com/thread/493610?q=distributed%20switch%20traffic%20filter
52+
``` PowerShell
53+
$dvSwName = 'dvSw1'
54+
$dvPgNames = 'dvPg1'
55+
56+
$dvSw = Get-VDSwitch -Name $dvSwName
57+
58+
foreach($pg in (Get-View -Id $dvSw.ExtensionData.Portgroup | Where {$dvPgNames -contains $_.Name})){
59+
$spec = New-Object VMware.Vim.DVPortgroupConfigSpec
60+
$spec.ConfigVersion = $pg.Config.ConfigVersion
61+
$spec.DefaultPortConfig = New-Object VMware.Vim.VMwareDVSPortSetting
62+
$spec.DefaultPortConfig.FilterPolicy = New-Object VMware.Vim.DvsFilterPolicy
63+
64+
$filter = New-Object VMware.Vim.DvsTrafficFilterConfig
65+
$filter.AgentName = 'dvfilter-generic-vmware'
66+
67+
$ruleSet = New-Object VMware.Vim.DvsTrafficRuleset
68+
$ruleSet.Enabled = $true
69+
70+
$rule =New-Object VMware.Vim.DvsTrafficRule
71+
$rule.Description = 'Traffic Rule Name'
72+
$rule.Direction = 'outgoingPackets'
73+
74+
$action = New-Object VMware.Vim.DvsUpdateTagNetworkRuleAction
75+
$action.QosTag = 4
76+
77+
$rule.Action += $action
78+
79+
$ruleSet.Rules += $rule
80+
81+
$filter.TrafficRuleSet += $ruleSet
82+
83+
$spec.DefaultPortConfig.FilterPolicy.FilterConfig += $filter
84+
85+
$pg.ReconfigureDVPortgroup($spec)
86+
}
87+
```
88+
89+
Other info:
90+
get VDTrafficFilterPolicyConfig:
91+
`$viewVDPG.Config.DefaultPortConfig.FilterPolicy.FilterConfig`

notes_SetCoSAndDSCPOnVDPG.ps1

Lines changed: 0 additions & 89 deletions
This file was deleted.

vNugglets.VDNetworking/GetItems.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ function Get-VNVDTrafficRule {
130130
$arrRulesOfInterest | Foreach-Object {
131131
$oThisTrafficRule = $_
132132
New-Object -Type VNVDTrafficRule -Property @{
133-
TrafficRule = $oThisTrafficRule
134133
Name = $oThisTrafficRule.Description
134+
TrafficRule = $oThisTrafficRule
135135
VDPortgroupView = $oThisTrafficRuleset.VDPortgroupView
136+
VNVDTrafficRuleSet = $oThisTrafficRuleset
136137
} ## end new-object
137138
} ## end foreach-object
138139
} ## end foreach-object

vNugglets.VDNetworking/NewItems.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ function New-VNVDTrafficRuleAction {
296296

297297
function New-VNVDTrafficRule {
298298
<# .Description
299-
Make new Traffic Rule and adds its to the given Traffic Ruleset of a vDPortgroup traffic filter policy
299+
Make new Traffic Rule and add it to the given Traffic Ruleset of a vDPortgroup traffic filter policy
300300
301301
.Example
302302
Get-VDPortGroup myVDPG0 | Get-VNVDTrafficRuleSet | New-VNVDTrafficRule -Name "Allow vMotion from source network" -Action (New-VNVDTrafficRuleAction -Allow) -Qualifier (New-VNVDTrafficRuleQualifier -SystemTrafficType vMotion), (New-VNVDTrafficRuleQualifier -SourceIpAddress 10.0.0.0/8)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
function Remove-VNVDTrafficRule {
2+
<# .Description
3+
Remove a Traffic Rule from the given Traffic Ruleset of a vDPortgroup traffic filter policy
4+
5+
.Notes
6+
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[] (an array)
7+
This is based on testing to try to make a vDPortgroup with more than one FilterConfig, and only ever having a maximum of one
8+
9+
.Example
10+
Get-VDSwitch -Name myVDSw0 | Get-VDPortGroup -Name myVDPG0 | Get-VNVDTrafficRuleSet | Get-VNVDTrafficRule -Name test* | Remove-VNVDTrafficRule
11+
Get the TrafficRules named like "test*" from the TrafficRuleSet for the given vDPortGroup and delete them.
12+
13+
.Outputs
14+
Null. Removes rule(s) as directed, returning nothing upon success.
15+
#>
16+
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="High")]
17+
param (
18+
## The traffic ruleset rule(s) to remove from the traffic filter policy of associated virtual distributed portgroup
19+
[parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="ByTrafficRule")][VNVDTrafficRule[]]$TrafficRule
20+
) ## end param
21+
22+
begin {
23+
## arraylist to hold all of the TrafficRules to be removed (in one swell foop, since each upate/reconfig of the vDPortgroup results in new keys for the rule objects)
24+
$arrlVNVDTrafficRulesToRemove = New-Object -TypeName System.Collections.ArrayList
25+
} ## end begin
26+
27+
process {
28+
## put each TrafficRule into the arraylist for later action (one reconfig per vDPortgroup)
29+
$TrafficRule | Foreach-Object {$arrlVNVDTrafficRulesToRemove.Add($_) | Out-Null}
30+
} ## end process
31+
32+
end {
33+
## 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
34+
$arrlVNVDTrafficRulesToRemove | Group-Object -Property @{e={$_.VDPortgroupView.MoRef}}, @{e={$_.VDPortgroupView.Client.ServiceUrl}} | Foreach-Object {
35+
$oThisPSGroupInfoOfTrafficRules = $_
36+
# The vDPortgroup with these TrafficRules (used in logging/reporting)
37+
$oVDPG_TheseRules = $oThisPSGroupInfoOfTrafficRules.Group[0].VDPortgroupView
38+
# The VNVDTrafficRuleSet for these TrafficRules, to be used to remove the given TrafficRule(s)
39+
$oVNVDTrafficRuleset_TheseRules = $oThisPSGroupInfoOfTrafficRules.Group[0].VNVDTrafficRuleSet
40+
## the VMware.Vim.DvsTrafficRule objects to remove from the given TrafficRuleset
41+
$arrDvsTrafficRulesToRemove = $oThisPSGroupInfoOfTrafficRules.Group.TrafficRule
42+
43+
$strMsgForShouldProcess_Target = "Traffic ruleset '{0}' on vDPortgroup '{1}'" -f $oVNVDTrafficRuleset_TheseRules.TrafficRuleset.Key, $oVDPG_TheseRules.Name
44+
$intNumDvsTrafficRulesToRemove = ($arrDvsTrafficRulesToRemove | Measure-Object).Count
45+
$strMsgForShouldProcess_Action = "Remove {0} traffic rule{1} (of name{1} '{2}')" -f $intNumDvsTrafficRulesToRemove, $(if ($intNumDvsTrafficRulesToRemove -ne 1) {"s"}), ($arrDvsTrafficRulesToRemove.Description -join ", ")
46+
if ($PSCmdlet.ShouldProcess($strMsgForShouldProcess_Target, $strMsgForShouldProcess_Action)) {
47+
try {
48+
## use the helper function to remove this TrafficRule from the TrafficRuleSet Rules array
49+
$oUpdatedTrafficRuleset = _Set-VNVDTrafficRuleset_helper -TrafficRuleSet $oVNVDTrafficRuleset_TheseRules -TrafficRule $arrDvsTrafficRulesToRemove -Operation Remove
50+
} ## end try
51+
catch {Throw $_}
52+
} ## end if
53+
} ## end foreach-object
54+
} ## end end
55+
} ## end fn
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Configuration>
3+
<ViewDefinitions>
4+
<View>
5+
<Name>VNVDTrafficRule_items</Name>
6+
<ViewSelectedBy><TypeName>VNVDTrafficRule</TypeName></ViewSelectedBy>
7+
<TableControl>
8+
<TableHeaders>
9+
<TableColumnHeader></TableColumnHeader>
10+
<TableColumnHeader></TableColumnHeader>
11+
<TableColumnHeader></TableColumnHeader>
12+
</TableHeaders>
13+
<TableRowEntries>
14+
<TableRowEntry>
15+
<TableColumnItems>
16+
<TableColumnItem><PropertyName>Name</PropertyName></TableColumnItem>
17+
<TableColumnItem><PropertyName>TrafficRule</PropertyName></TableColumnItem>
18+
<TableColumnItem><PropertyName>VDPortgroupView</PropertyName></TableColumnItem>
19+
</TableColumnItems>
20+
</TableRowEntry>
21+
</TableRowEntries>
22+
</TableControl>
23+
</View>
24+
</ViewDefinitions>
25+
</Configuration>

vNugglets.VDNetworking/vNugglets.VDNetworking.psd1

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Generated by: V2X5333
55
#
6-
# Generated on: 12/22/2017
6+
# Generated on: 12/27/2017
77
#
88

99
@{
@@ -63,18 +63,19 @@ ScriptsToProcess = 'vNugglets.VDNetworking_ClassDefinition.ps1'
6363
# TypesToProcess = @()
6464

6565
# Format files (.ps1xml) to be loaded when importing this module
66-
# FormatsToProcess = @()
66+
FormatsToProcess = 'vNugglets.VDNetworking.format.ps1xml'
6767

6868
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
69-
NestedModules = @('GetItems.ps1',
70-
'NewItems.ps1',
69+
NestedModules = @('GetItems.ps1',
70+
'NewItems.ps1',
71+
'RemoveItems.ps1',
7172
'vNugglets.VDNetworking_SupportingFunctions.ps1')
7273

7374
# 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.
74-
FunctionsToExport = 'Get-VNVDTrafficFilterPolicyConfig', 'Get-VNVDTrafficRuleSet',
75-
'Get-VNVDTrafficRule', 'Get-VNVDTrafficRuleQualifier',
76-
'New-VNVDTrafficRuleQualifier', 'New-VNVDTrafficRuleAction',
77-
'New-VNVDTrafficRule'
75+
FunctionsToExport = 'Get-VNVDTrafficFilterPolicyConfig', 'Get-VNVDTrafficRuleSet',
76+
'Get-VNVDTrafficRule', 'Get-VNVDTrafficRuleQualifier',
77+
'New-VNVDTrafficRuleQualifier', 'New-VNVDTrafficRuleAction',
78+
'New-VNVDTrafficRule', 'Remove-VNVDTrafficRule'
7879

7980
# 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.
8081
CmdletsToExport = @()
@@ -92,10 +93,12 @@ AliasesToExport = @()
9293
# ModuleList = @()
9394

9495
# List of all files packaged with this module
95-
FileList = 'vNugglets.VDNetworking.psd1',
96-
'vNugglets.VDNetworking_ModRoot.psm1',
97-
'en-US\about_vNugglets.VDNetworking.help.txt', 'GetItems.ps1',
98-
'NewItems.ps1', 'vNugglets.VDNetworking_SupportingFunctions.ps1'
96+
FileList = 'vNugglets.VDNetworking.psd1',
97+
'vNugglets.VDNetworking_ModRoot.psm1',
98+
'en-US\about_vNugglets.VDNetworking.help.txt', 'GetItems.ps1',
99+
'NewItems.ps1', 'RemoveItems.ps1',
100+
'vNugglets.VDNetworking_SupportingFunctions.ps1',
101+
'vNugglets.VDNetworking.format.ps1xml'
99102

100103
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
101104
PrivateData = @{
@@ -121,7 +124,7 @@ PrivateData = @{
121124
# ExternalModuleDependencies = ''
122125

123126
} # End of PSData hashtable
124-
127+
125128
} # End of PrivateData hashtable
126129

127130
# HelpInfo URI of this module

vNugglets.VDNetworking/vNugglets.VDNetworking_ClassDefinition.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ class VNVDTrafficRule : VNVDBase {
4141

4242
## the full View object for the vDPG that is the "parent" of this object
4343
[VMware.Vim.DistributedVirtualPortgroup]$VDPortgroupView
44+
45+
## the "parent" VNVDTrafficRuleSet to which this TrafficRule belongs
46+
[VNVDTrafficRuleSet]$VNVDTrafficRuleSet
4447
} ## end class

0 commit comments

Comments
 (0)