Skip to content

Commit

Permalink
xDhcpServerOptionDefinition: Add DefaultValue (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
CraigTolley authored May 6, 2022
1 parent 501259e commit afb827b
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- xDhcpServerOptionDefinition
- Added DefaultValue parameter to xDhcpServerOptionDefinition ([issue #75](https://github.com/dsccommunity/xDhcpServer/issues/75)).

### Changed

- xDhcpServer
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ The resource examples are also available in the [xDhcpServer Wiki](https://githu
- **Description**: Option description.
- **AddressFamily**: Sets the address family for the option definition.
Currently only IPv4 is supported. { IPv4 }
- **DefaultValue**: The default value for the option.
- **Ensure**: Whether option should be set or removed. { *Present* | Absent }

### DhcpServerOptionValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function Get-TargetResource
Type = $dhcpServerOptionDefinition.Type
VendorClass = $dhcpServerOptionDefinition.VendorClass
MultiValued = $dhcpServerOptionDefinition.MultiValued
DefaultValue = $dhcpServerOptionDefinition.DefaultValue
Ensure = 'Present'
}
}
Expand All @@ -102,6 +103,7 @@ function Get-TargetResource
Type = $null
VendorClass = $null
MultiValued = $false
DefaultValue = $null
Ensure = 'Absent'
}
}
Expand Down Expand Up @@ -133,10 +135,13 @@ function Get-TargetResource
The data type of the option definition.
.PARAMETER MultiValued
Whether the option definition is multivalued or not.
Whether the option definition is multi-valued or not.
.PARAMETER AddressFamily
The option definition address family (IPv4 or IPv6). Currently only the IPv4 is supported.
.PARAMETER DefaultValue
Specifies the default value to set for the option definition.
#>
function Set-TargetResource
{
Expand Down Expand Up @@ -181,7 +186,11 @@ function Set-TargetResource
[Parameter(Mandatory = $true)]
[ValidateSet('IPv4')]
[System.String]
$AddressFamily
$AddressFamily,

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

# Reading the DHCP option
Expand All @@ -193,23 +202,49 @@ function Set-TargetResource
# Testing if option exists
if ($dhcpServerOptionDefinition.Ensure -eq 'Present')
{
# If it exists and any of multivalued, type or vendorclass is being changed remove then re-add the whole option definition
# If it exists and any of multi-valued, type or vendor class is being changed remove then re-add the whole option definition
if (($dhcpServerOptionDefinition.Type -ne $Type) -or ($dhcpServerOptionDefinition.MultiValued -ne $MultiValued) -or ($dhcpServerOptionDefinition.VendorClass -ne $VendorClass))
{
$scopeIDMessage = $script:localizedData.RecreatingOptionDefinitionIDMessage -f $OptionId, $VendorClass
Write-Verbose -Message $scopeIDMessage

Remove-DhcpServerv4OptionDefinition -OptionId $OptionId -VendorClass $VendorClass

Add-DhcpServerv4OptionDefinition -OptionId $OptionId -name $Name -Type $Type -Description $Description -MultiValued:$MultiValued -VendorClass $VendorClass
$addDhcpServerv4OptionDefinitionParameters = @{
OptionId = $OptionId
Name = $Name
Type = $Type
Description = $Description
MultiValued = $MultiValued
VendorClass = $VendorClass
}

if ($PSBoundParameters.ContainsKey('DefaultValue'))
{
$addDhcpServerv4OptionDefinitionParameters.DefaultValue = $DefaultValue
}

Add-DhcpServerv4OptionDefinition @addDhcpServerv4OptionDefinitionParameters
}
# If option exists we need only to adjust the parameters
else
{
$settingIDMessage = $script:localizedData.SettingOptionDefinitionIDMessage -f $OptionId, $VendorClass
Write-Verbose -Message $settingIDMessage

Set-DhcpServerv4OptionDefinition -OptionId $OptionId -VendorClass $VendorClass -name $Name -Description $Description
$setDhcpServerv4OptionDefinitionParameters = @{
OptionId = $OptionId
Name = $Name
Description = $Description
VendorClass = $VendorClass
}

if ($PSBoundParameters.ContainsKey('DefaultValue'))
{
$setDhcpServerv4OptionDefinitionParameters.DefaultValue = $DefaultValue
}

Set-DhcpServerv4OptionDefinition @setDhcpServerv4OptionDefinitionParameters
}
}
# If option does not exist we need to add it
Expand All @@ -218,7 +253,21 @@ function Set-TargetResource
$scopeIDMessage = $script:localizedData.AddingOptionDefinitionIDMessage -f $OptionId, $VendorClass
Write-Verbose -Message $scopeIDMessage

Add-DhcpServerv4OptionDefinition -OptionId $OptionId -name $Name -Type $Type -Description $Description -MultiValued:$MultiValued -VendorClass $VendorClass
$addDhcpServerv4OptionDefinitionParameters = @{
OptionId = $OptionId
Name = $Name
Type = $Type
Description = $Description
MultiValued = $MultiValued
VendorClass = $VendorClass
}

if ($PSBoundParameters.ContainsKey('DefaultValue'))
{
$addDhcpServerv4OptionDefinitionParameters.DefaultValue = $DefaultValue
}

Add-DhcpServerv4OptionDefinition @addDhcpServerv4OptionDefinitionParameters
}
}
# Testing for 'absent'
Expand Down Expand Up @@ -262,6 +311,9 @@ function Set-TargetResource
.PARAMETER AddressFamily
The option definition address family (IPv4 or IPv6). Currently only the IPv4 is supported.
.PARAMETER DefaultValue
Specifies the default value to set for the option definition.
#>
function Test-TargetResource
{
Expand Down Expand Up @@ -307,7 +359,11 @@ function Test-TargetResource
[Parameter(Mandatory = $true)]
[ValidateSet('IPv4')]
[System.String]
$AddressFamily
$AddressFamily,

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

# Region Input Validation
Expand Down Expand Up @@ -349,7 +405,7 @@ function Test-TargetResource
of the parameters need to be evaluated if they that they are in desired
state.
#>
$propertiesToEvaluate = @('Name','Description','Type','MultiValued')
$propertiesToEvaluate = @('Name', 'Description', 'Type', 'MultiValued', 'DefaultValue')

$result = $true

Expand All @@ -360,9 +416,20 @@ function Test-TargetResource
{
$desiredParameterValue = Get-Variable -Name $property -ValueOnly

if ($currentConfiguration.$property -ne $desiredParameterValue)
if ($property -eq 'DefaultValue')
{
# Force string comparison, else get mixed results with DefaultValue property
if ([System.String] $currentConfiguration.$property -ne [System.String] $desiredParameterValue)
{
$result = $false
}
}
else
{
$result = $false
if ($currentConfiguration.$property -ne $desiredParameterValue)
{
$result = $false
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ class MSFT_xDhcpServerOptionDefinition : OMI_BaseResource
[Key, Description("Vendor class. Use an empty string for standard option class.")] String VendorClass;
[Required, Description("Option name.")] String Name;
[Required, Description("Option data type."),ValueMap{"Byte","Word","Dword","DwordDword","IPv4Address","String","BinaryData","EncapsulatedData"},Values{"Byte","Word","Dword","DwordDword","IPv4Address","String","BinaryData","EncapsulatedData"}] string Type;
[Write, Description("Whether option is multivalued or not.")] Boolean Multivalued;
[Write, Description("Whether option is multi-valued or not.")] Boolean Multivalued;
[Write, Description("Option description.")] String Description;
[Write, Description("Default value for the option.")] String DefaultValue;
[Key, Description("Class address family. Currently needs to be IPv4."), ValueMap{"IPv4"}, Values{"IPv4"}] String AddressFamily;
[Write, Description("Whether the DHCP server class should exist."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
};
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,16 @@ configuration Example
VendorClass = '' #default option class
Description = 'Sample description'
}

xDhcpServerOptionDefinition 'DHCPServerOptionDefinition3'
{
Ensure = 'Present'
Name = 'PXEClient'
OptionID = '060'
Type = 'String'
AddressFamily = 'IPv4'
VendorClass = '' #default option class
Description = 'Sample description'
DefaultValue = 'PXEClient'
}
}
32 changes: 25 additions & 7 deletions tests/Unit/MSFT_xDhcpServerOptionDefinition.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ try
$type = 'IPv4Address'
$vendorClass = ''
$multiValued = $false
$defaultValue = '1.2.3.4'

$testParams = @{
OptionId = $optionId
Expand All @@ -59,6 +60,7 @@ try
Type = $type
VendorClass = $vendorClass
MultiValued = $multiValued
DefaultValue = $defaultValue
}

Describe 'MSFT_xDhcpServerOptionDefinition\Get-TargetResource' {
Expand Down Expand Up @@ -95,13 +97,15 @@ try
$mockDescription = 'Test Description'
$mockType = 'IPv4Address'
$mockVendorClass = 'MockVendorClass'
$mockDefaultValue = '1.2.3.4'

$mockDefaultParameters = @{
OptionId = $mockOptionId
Name = $mockName
VendorClass = $mockVendorClass
Type = $mockType
OptionId = $mockOptionId
Name = $mockName
VendorClass = $mockVendorClass
Type = $mockType
AddressFamily = $mockAddressFamily
DefaultValue = $mockDefaultValue
}
}

Expand All @@ -117,6 +121,7 @@ try
Type = $null
VendorClass = $null
MultiValued = $false
DefaultValue = $null
Ensure = 'Absent'
}
}
Expand All @@ -142,6 +147,7 @@ try
Type = $mockType
VendorClass = $mockVendorClass
MultiValued = $true
DefaultValue = $mockDefaultValue
Ensure = 'Present'
}
}
Expand All @@ -150,6 +156,7 @@ try
$testTargetResourceParameters['Ensure'] = 'Present'
$testTargetResourceParameters['Description'] = $mockDescription
$testTargetResourceParameters['MultiValued'] = $true
$testTargetResourceParameters['DefaultValue'] = $mockDefaultValue
}

It 'Should return the state as $true' {
Expand All @@ -171,6 +178,7 @@ try
Type = $mockType
VendorClass = $mockVendorClass
MultiValued = $true
DefaultValue = $mockDefaultValue
Ensure = 'Present'
}
}
Expand Down Expand Up @@ -330,7 +338,6 @@ try
Assert-MockCalled -CommandName Add-DhcpServerv4OptionDefinition -Exactly -Times 1 -Scope It
}


It 'Should call "Remove-DhcpServerv4OptionDefinition" and then "Add-DhcpServerv4OptionDefinition" when "Ensure" = "Present" and Type has changed' {
Mock -CommandName Get-DhcpServerv4OptionDefinition -MockWith {
return $fakeDhcpServerv4OptionDefinition
Expand All @@ -345,7 +352,6 @@ try
Assert-MockCalled -CommandName Add-DhcpServerv4OptionDefinition -Exactly -Times 1 -Scope It
}


It 'Should call "Remove-DhcpServerv4OptionDefinition" and then "Add-DhcpServerv4OptionDefinition" when "Ensure" = "Present" and MultiValued has changed' {
Mock -CommandName Get-DhcpServerv4OptionDefinition -MockWith {
return $fakeDhcpServerv4OptionDefinition
Expand Down Expand Up @@ -374,7 +380,6 @@ try
Assert-MockCalled -CommandName Add-DhcpServerv4OptionDefinition -Exactly -Times 1 -Scope It
}


It 'Should call "Remove-DhcpServerv4OptionDefinition" and then "Add-DhcpServerv4OptionDefinition" when "Ensure" = "Present" and VendorClass and Description has changed' {
Mock -CommandName Get-DhcpServerv4OptionDefinition -MockWith {
return $fakeDhcpServerv4OptionDefinition
Expand Down Expand Up @@ -402,6 +407,19 @@ try

Assert-MockCalled -CommandName Set-DhcpServerv4OptionDefinition -Exactly -Times 1 -Scope It
}

It 'Should call "Set-DhcpServerv4OptionDefinition" when "Ensure" = "Present" and DefaultValue has changed' {
Mock -CommandName Get-DhcpServerv4OptionDefinition -MockWith {
return $fakeDhcpServerv4OptionDefinition
}

$TempParams = $testParams.Clone()
$TempParams.DefaultValue = '1.2.3.5'

Set-TargetResource @testParams -Ensure 'Present'

Assert-MockCalled -CommandName Set-DhcpServerv4OptionDefinition -Exactly -Times 1 -Scope It
}
}
}
}
Expand Down

0 comments on commit afb827b

Please sign in to comment.