Skip to content

Commit

Permalink
Fix style violations in MSFT_DefaultGatewayAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
PlagueHO committed Nov 12, 2019
1 parent 3850a93 commit dd07c7c
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 163 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

- DefaultGatewayAddress:
- Refactored to reduce code duplication.
- Fixed hash table style violations - fixes [Issue #429](https://github.com/PowerShell/NetworkingDsc/issues/429).
- Fixed general style violations.

## 7.4.0.0

- Added Comment Based Help for `New-NotImplementedException` common
Expand Down
182 changes: 119 additions & 63 deletions DSCResources/MSFT_DefaultGatewayAddress/MSFT_DefaultGatewayAddress.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -29,50 +29,53 @@ function Get-TargetResource
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
[System.String]
$InterfaceAlias,

[Parameter(Mandatory = $true)]
[ValidateSet('IPv4', 'IPv6')]
[String]
[System.String]
$AddressFamily,

[Parameter()]
[String]
[System.String]
$Address
)

Write-Verbose -Message ( @("$($MyInvocation.MyCommand): "
$($script:localizedData.GettingDefaultGatewayAddressMessage)
) -join '' )

# Use $AddressFamily to select the IPv4 or IPv6 destination prefix
$destinationPrefix = '0.0.0.0/0'
if ($AddressFamily -eq 'IPv6')
{
$destinationPrefix = '::/0'
}
# Get all the default routes
$defaultRoutes = Get-NetRoute -InterfaceAlias $InterfaceAlias -AddressFamily `
$AddressFamily -ErrorAction Stop | `
Where-Object { $_.DestinationPrefix -eq $destinationPrefix }
$destinationPrefix = Get-NetDefaultGatewayDestinationPrefix `
-AddressFamily $AddressFamily

$defaultRoutes = Get-NetDefaultRoute `
-InterfaceAlias $InterfaceAlias `
-AddressFamily $AddressFamily

$returnValue = @{
AddressFamily = $AddressFamily
InterfaceAlias = $InterfaceAlias
}
# If there is a Default Gateway defined for this interface/address family add it
# to the return value.

<#
If there is a Default Gateway defined for this interface/address family add it
to the return value.
#>
if ($defaultRoutes)
{
$returnValue += @{ Address = $defaultRoutes.NextHop }
$returnValue += @{
Address = $defaultRoutes.NextHop
}
}
else
{
$returnValue += @{ Address = $null }
$returnValue += @{
Address = $null
}
}

$returnValue
return $returnValue
}

<#
Expand All @@ -95,38 +98,28 @@ function Set-TargetResource
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
[System.String]
$InterfaceAlias,

[Parameter(Mandatory = $true)]
[ValidateSet('IPv4', 'IPv6')]
[String]
[System.String]
$AddressFamily,

[Parameter()]
[String]
[System.String]
$Address
)

Write-Verbose -Message ( @("$($MyInvocation.MyCommand): "
$($script:localizedData.ApplyingDefaultGatewayAddressMessage)
) -join '' )

# Use $AddressFamily to select the IPv4 or IPv6 destination prefix
$destinationPrefix = '0.0.0.0/0'
$defaultRoutes = @(Get-NetDefaultRoute `
-InterfaceAlias $InterfaceAlias `
-AddressFamily $AddressFamily)

if ($AddressFamily -eq 'IPv6')
{
$destinationPrefix = '::/0'
}

# Get all the default routes
$defaultRoutes = @(Get-NetRoute `
-InterfaceAlias $InterfaceAlias `
-AddressFamily $AddressFamily `
-ErrorAction Stop).Where( { $_.DestinationPrefix -eq $destinationPrefix } )

# Remove any existing default route
# Remove any existing default routes
foreach ($defaultRoute in $defaultRoutes)
{
Remove-NetRoute `
Expand All @@ -139,16 +132,18 @@ function Set-TargetResource

if ($Address)
{
$destinationPrefix = Get-NetDefaultGatewayDestinationPrefix `
-AddressFamily $AddressFamily

# Set the correct Default Route
# Build parameter hash table
$parameters = @{
$newNetRouteParameters = @{
DestinationPrefix = $destinationPrefix
InterfaceAlias = $InterfaceAlias
AddressFamily = $AddressFamily
NextHop = $Address
}

New-NetRoute @Parameters -ErrorAction Stop
New-NetRoute @newNetRouteParameters -ErrorAction Stop

Write-Verbose -Message ( @("$($MyInvocation.MyCommand): "
$($script:localizedData.DefaultGatewayAddressSetToDesiredStateMessage)
Expand Down Expand Up @@ -183,58 +178,51 @@ function Test-TargetResource
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
[System.String]
$InterfaceAlias,

[Parameter(Mandatory = $true)]
[ValidateSet('IPv4', 'IPv6')]
[String]
[System.String]
$AddressFamily,

[Parameter()]
[String]
[System.String]
$Address
)

# Flag to signal whether settings are correct
[Boolean] $desiredConfigurationMatch = $true
$desiredConfigurationMatch = $true

Write-Verbose -Message ( @("$($MyInvocation.MyCommand): "
$($script:localizedData.CheckingDefaultGatewayAddressMessage)
) -join '' )

Assert-ResourceProperty @PSBoundParameters

# Use $AddressFamily to select the IPv4 or IPv6 destination prefix
$destinationPrefix = '0.0.0.0/0'
if ($AddressFamily -eq 'IPv6')
{
$destinationPrefix = '::/0'
}

# Get all the default routes
$defaultRoutes = @(Get-NetRoute `
-InterfaceAlias $InterfaceAlias `
-AddressFamily $AddressFamily `
-ErrorAction Stop).Where( { $_.DestinationPrefix -eq $destinationPrefix } )
$defaultRoutes = @(Get-NetDefaultRoute `
-InterfaceAlias $InterfaceAlias `
-AddressFamily $AddressFamily)

# Test if the Default Gateway passed is equal to the current default gateway
if ($Address)
{
if ($defaultRoutes)
{
if (-not $defaultRoutes.Where( { $_.NextHop -eq $Address } ))
if ($defaultRoutes.Where( {
$_.NextHop -eq $Address
} ))
{
Write-Verbose -Message ( @("$($MyInvocation.MyCommand): "
$($script:localizedData.DefaultGatewayNotMatchMessage) -f $Address, $defaultRoutes.NextHop
$($script:localizedData.DefaultGatewayCorrectMessage)
) -join '' )
$desiredConfigurationMatch = $false
}
else
{
Write-Verbose -Message ( @("$($MyInvocation.MyCommand): "
$($script:localizedData.DefaultGatewayCorrectMessage)
$($script:localizedData.DefaultGatewayNotMatchMessage) -f $Address, $defaultRoutes.NextHop
) -join '' )
$desiredConfigurationMatch = $false
}
}
else
Expand All @@ -259,7 +247,6 @@ function Test-TargetResource
{
Write-Verbose -Message ( @("$($MyInvocation.MyCommand): "
$($script:localizedData.DefaultGatewayExistsAndShouldMessage)
'Default Gateway does not exist which is correct.'
) -join '' )
}
}
Expand Down Expand Up @@ -288,16 +275,16 @@ function Assert-ResourceProperty
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
[System.String]
$InterfaceAlias,

[Parameter()]
[ValidateSet('IPv4', 'IPv6')]
[String]
[System.String]
$AddressFamily = 'IPv4',

[Parameter()]
[String]
[System.String]
$Address
)

Expand All @@ -316,7 +303,7 @@ function Assert-ResourceProperty
-ArgumentName 'Address'
}

$detectedAddressFamily = ([System.Net.IPAddress]$Address).AddressFamily.ToString()
$detectedAddressFamily = ([System.Net.IPAddress] $Address).AddressFamily.ToString()

if (($detectedAddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork.ToString()) `
-and ($AddressFamily -ne 'IPv4'))
Expand All @@ -336,4 +323,73 @@ function Assert-ResourceProperty
}
} # Assert-ResourceProperty

<#
.SYNOPSIS
Get the default gateway destination prefix for the IP address family.
.PARAMETER AddressFamily
IP address family.
#>
function Get-NetDefaultGatewayDestinationPrefix
{
[CmdletBinding()]
[OutputType([System.String])]
param
(
[Parameter()]
[ValidateSet('IPv4', 'IPv6')]
[System.String]
$AddressFamily = 'IPv4'
)

if ($AddressFamily -eq 'IPv4')
{
$destinationPrefix = '0.0.0.0/0'
}
else
{
$destinationPrefix = '::/0'
}

return $destinationPrefix
} # Get-NetDefaultGatewayDestinationPrefix

<#
.SYNOPSIS
Get the default network routes assigned to the interface.
.PARAMETER InterfaceAlias
Alias of the network interface for which the default gateway address is set.
.PARAMETER AddressFamily
IP address family.
#>
function Get-NetDefaultRoute
{
[CmdletBinding()]
[OutputType([System.String[]])]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$InterfaceAlias,

[Parameter()]
[ValidateSet('IPv4', 'IPv6')]
[System.String]
$AddressFamily = 'IPv4'
)

$destinationPrefix = Get-NetDefaultGatewayDestinationPrefix `
-AddressFamily $AddressFamily

return @(Get-NetRoute `
-InterfaceAlias $InterfaceAlias `
-AddressFamily $AddressFamily `
-ErrorAction Stop).Where({
$_.DestinationPrefix -eq $destinationPrefix
})
} # Get-NetDefaultRoute

Export-ModuleMember -function *-TargetResource
Loading

0 comments on commit dd07c7c

Please sign in to comment.