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

[NetAdapterRename] Allow rename with HyperV Device Naming #523

Open
wants to merge 3 commits into
base: main
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Updated CHANGELOG.md
- Renamed NetworkingDSc to NetworkingDsc in CHANGELOG.md - fixes [Issue #513](https://github.com/dsccommunity/NetworkingDsc/issues/513).
- NetAdapterName
- Added new parameter HyperVNetworkAdapterName to filter if device naming is configured on a Hyper-V network adapter.
- CI Pipeline
- Updated pipeline files to match current DSC Community patterns - fixes [Issue #528](https://github.com/dsccommunity/NetworkingDsc/issues/528).
- Updated HQRM and build steps to use windows-latest image.
Expand Down
22 changes: 22 additions & 0 deletions source/DSCResources/DSC_NetAdapterName/DSC_NetAdapterName.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'
.PARAMETER DriverDescription
This is the driver description of the network adapter.

.PARAMETER HyperVNetworkAdapterName
This is the name of the network adapter to find if device naming is enabled.

.PARAMETER InterfaceNumber
This is the interface number of the network adapter if more than one
are returned by the parameters.
Expand Down Expand Up @@ -96,6 +99,10 @@ function Get-TargetResource
[System.String]
$DriverDescription,

[Parameter()]
[System.String]
$HyperVNetworkAdapterName,

[Parameter()]
[System.UInt32]
$InterfaceNumber = 1,
Expand Down Expand Up @@ -139,6 +146,7 @@ function Get-TargetResource
InterfaceIndex = $adapter.InterfaceIndex
InterfaceGuid = $adapter.InterfaceGuid
DriverDescription = $adapter.DriverDescription
HyperVNetworkAdapterName = $adapter.HyperVNetworkAdapterName
InterfaceNumber = $InterfaceNumber
IgnoreMultipleMatchingAdapters = $IgnoreMultipleMatchingAdapters
}
Expand Down Expand Up @@ -177,6 +185,9 @@ function Get-TargetResource
.PARAMETER DriverDescription
This is the driver description of the network adapter.

.PARAMETER HyperVNetworkAdapterName
This is the name of the network adapter to find if device naming is enabled.

.PARAMETER InterfaceNumber
This is the interface number of the network adapter if more than one
are returned by the parameters.
Expand Down Expand Up @@ -230,6 +241,10 @@ function Set-TargetResource
[System.String]
$DriverDescription,

[Parameter()]
[System.String]
$HyperVNetworkAdapterName,

[Parameter()]
[System.UInt32]
$InterfaceNumber = 1,
Expand Down Expand Up @@ -293,6 +308,9 @@ function Set-TargetResource
.PARAMETER DriverDescription
This is the driver description of the network adapter.

.PARAMETER HyperVNetworkAdapterName
This is the name of the network adapter to find if device naming is enabled.

.PARAMETER InterfaceNumber
This is the interface number of the network adapter if more than one
are returned by the parameters.
Expand Down Expand Up @@ -347,6 +365,10 @@ function Test-TargetResource
[System.String]
$DriverDescription,

[Parameter()]
[System.String]
$HyperVNetworkAdapterName,

[Parameter()]
[System.UInt32]
$InterfaceNumber = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class DSC_NetAdapterName : OMI_BaseResource
[Write, Description("This is the interface index of the network adapter to find.")] UInt32 InterfaceIndex;
[Write, Description("This is the interface GUID of the network adapter to find.")] String InterfaceGuid;
[Write, Description("This is the driver description of the network adapter.")] String DriverDescription;
[Write, Description("This is the name of the network adapter to find if device naming is enabled.")] String HyperVNetworkAdapterName;
[Write, Description("This is the interface number of the network adapter if more than one are returned by the parameters.")] UInt32 InterfaceNumber;
[Write, Description("This switch will suppress an error occurring if more than one matching adapter matches the parameters passed.")] Boolean IgnoreMultipleMatchingAdapters;
};
9 changes: 9 additions & 0 deletions source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ function Find-NetworkAdapter
[System.UInt32]
$InterfaceNumber = 1,

[Parameter()]
[System.String]
$HyperVNetworkAdapterName,

[Parameter()]
[System.Boolean]
$IgnoreMultipleMatchingAdapters = $false
Expand Down Expand Up @@ -209,6 +213,11 @@ function Find-NetworkAdapter
$adapterFilters += @('($_.DriverDescription -eq $DriverDescription)')
} # if

if ($PSBoundParameters.ContainsKey('HyperVNetworkAdapterName'))
{
$adapterFilters += @('((Get-NetAdapterAdvancedProperty -Name $_.Name -RegistryKeyword HyperVNetworkAdapterName -ErrorAction SilentlyContinue).DisplayValue -eq $HyperVNetworkAdapterName)')
} # if

if ($adapterFilters.Count -eq 0)
{
Write-Verbose -Message ( @("$($MyInvocation.MyCommand): "
Expand Down
38 changes: 21 additions & 17 deletions tests/Unit/DSC_NetAdapterName.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,31 @@ try
$script:adapterInterfaceIndex = 2
$script:adapterInterfaceGuid = '75670D9B-5879-4DBA-BC99-86CDD33EB66A'
$script:adapterDriverDescription = 'Hyper-V Virtual Ethernet Adapter'
$script:adapterHyperVNetworkAdapterName = 'hv-res-001'

$script:adapterParameters = [PSObject]@{
Name = $script:adapterName
NewName = $script:newAdapterName
PhysicalMediaType = $script:adapterPhysicalMediaType
Status = $script:adapterStatus
MacAddress = $script:adapterMacAddress
InterfaceDescription = $script:adapterInterfaceDescription
InterfaceIndex = $script:adapterInterfaceIndex
InterfaceGuid = $script:adapterInterfaceGuid
DriverDescription = $script:adapterDriverDescription
Name = $script:adapterName
NewName = $script:newAdapterName
PhysicalMediaType = $script:adapterPhysicalMediaType
Status = $script:adapterStatus
MacAddress = $script:adapterMacAddress
InterfaceDescription = $script:adapterInterfaceDescription
InterfaceIndex = $script:adapterInterfaceIndex
InterfaceGuid = $script:adapterInterfaceGuid
DriverDescription = $script:adapterDriverDescription
HyperVNetworkAdapterName = $script:adapterHyperVNetworkAdapterName
}

$script:mockAdapter = [PSObject]@{
Name = $script:adapterName
PhysicalMediaType = $script:adapterPhysicalMediaType
Status = $script:adapterStatus
MacAddress = $script:adapterMacAddress
InterfaceDescription = $script:adapterInterfaceDescription
InterfaceIndex = $script:adapterInterfaceIndex
InterfaceGuid = $script:adapterInterfaceGuid
DriverDescription = $script:adapterDriverDescription
Name = $script:adapterName
PhysicalMediaType = $script:adapterPhysicalMediaType
Status = $script:adapterStatus
MacAddress = $script:adapterMacAddress
InterfaceDescription = $script:adapterInterfaceDescription
InterfaceIndex = $script:adapterInterfaceIndex
InterfaceGuid = $script:adapterInterfaceGuid
DriverDescription = $script:adapterDriverDescription
HyperVNetworkAdapterName = $script:adapterHyperVNetworkAdapterName
}

$script:mockRenamedAdapter = [PSObject]@{
Expand Down Expand Up @@ -141,6 +144,7 @@ try
$script:result.InterfaceIndex | Should -Be $script:mockAdapter.InterfaceIndex
$script:result.InterfaceGuid | Should -Be $script:mockAdapter.InterfaceGuid
$script:result.DriverDescription | Should -Be $script:mockAdapter.DriverDescription
$script:result.HyperVNetworkAdapterName | Should -Be $script:mockAdapter.HyperVNetworkAdapterName
}

It 'Should call all the mocks' {
Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/NetworkingDsc.Common.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ InModuleScope $script:subModuleName {
$adapterInterfaceIndex = 2
$adapterInterfaceGuid = '75670D9B-5879-4DBA-BC99-86CDD33EB66A'
$adapterDriverDescription = 'Hyper-V Virtual Ethernet Adapter'
$adapterHyperVNetworkAdapterName = 'hv-res-001'

$nomatchAdapter = [PSObject]@{
Name = 'No Match Adapter'
PhysicalMediaType = '802.11'
Expand Down Expand Up @@ -380,6 +382,54 @@ InModuleScope $script:subModuleName {
}
}

Context 'HyperVNetworkAdapterName is passed and one adapter matches' {
Mock `
-CommandName Get-NetAdapter `
-MockWith { $adapterArray }

Mock -CommandName Get-NetAdapterAdvancedProperty -MockWith {
@{
DisplayValue = $adapterHyperVNetworkAdapterName
}
} -ParameterFilter { $Name -eq $adapterName }

Mock -CommandName Get-NetAdapterAdvancedProperty -ParameterFilter { $Name -eq 'No Match Adapter' }

It 'Should not throw exception' {
{
$script:result = Find-NetworkAdapter -HyperVNetworkAdapterName $adapterHyperVNetworkAdapterName -Verbose
} | Should -Not -Throw
}

It 'Should return expected adapter' {
$script:result.Name | Should -Be $adapterName
}

It 'Should call expected mocks' {
Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1
Assert-MockCalled -CommandName Get-NetAdapterAdvancedProperty -Exactly -Times 2
}
}

Context 'HyperVNetworkAdapterName is passed and no adapters match' {
Mock `
-CommandName Get-NetAdapter `
-MockWith { $adapterArray }
Mock -CommandName Get-NetAdapterAdvancedProperty

$errorRecord = Get-InvalidOperationRecord `
-Message ($script:localizedData.NetAdapterNotFoundError)

It 'Should throw the correct exception' {
{ $script:result = Find-NetworkAdapter -HyperVNetworkAdapterName 'NOMATCH' -Verbose } | Should -Throw $errorRecord
}

It 'Should call expected mocks' {
Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1
Assert-MockCalled -CommandName Get-NetAdapterAdvancedProperty -Exactly -Times 2
}
}

Context 'No parameters are passed and multiple Adapters adapters match but IgnoreMultipleMatchingAdapters is not set' {
Mock `
-CommandName Get-NetAdapter `
Expand Down
Loading