Comments (10)
Ahh, so, I just reviewed the test code and have other concerns, too... What if the IP is correct, but the subnet mask is not? Or if the default gateway is incorrect? This code will skip those issues, won't it? Apologies if I'm not reading the code correctly. This is my first time diving into DSC code.
from networkingdsc.
I think I remember having concerns about that when I was last looking at this module. You are correct that if the default gateway, subnet mask, etc will not be validated correctly if the IP address is correct. If you wanted to submit a fix for this I'd be happy to review it and offer suggestions.
from networkingdsc.
Not sure I'll have time to test this beyond my local workstation, but to address the second issue, maybe something like the below for that try/catch block.
This code doesn't address the DHCP question, but I was thinking that if this module gets the ability to disable DHCP, then it should probably be able to enable it as well, but enabling DHCP doesn't seem relevant for an 'IPAddress' module. Perhaps it would make more sense if this was an 'IPInterface' module, bent on managing all the configuration settings of the interface? Is there such a module yet?
try
{
Write-Verbose -Message "Checking the IPAddress ..."
#Get the current IP Address based on the parameters given.
$currentIP = Get-NetIPAddress -InterfaceAlias $InterfaceAlias -AddressFamily $AddressFamily -ErrorAction Stop
$requiresChanges=$false
#Test if the IP Address passed is equal to the current ip address
if(!$currentIP.IPAddress.Equals($IPAddress))
{
Write-Verbose -Message "IPAddress not correct. Expected $IPAddress, actual $($currentIP.IPAddress)"
$requiresChanges = $true
}
else
{
Write-Verbose -Message "IPAddress is correct."
}
#Test if the Subnet Mask passed is equal to the current subnet mask
if(!$currentIP.PrefixLength.Equals([byte]$SubnetMask))
{
Write-Verbose -Message "Subnet mask not correct. Expected $SubnetMask, actual $($currentIP.PrefixLength)"
$requiresChanges = $true
}
else
{
Write-Verbose -Message "Subnet mask is correct."
}
#Test if the Default Gateway passed is equal to the current default gateway
if($DefaultGateway)
{
$netIpConf = Get-NetIPConfiguration -InterfaceAlias $InterfaceAlias -ErrorAction Stop
$currentGateway = ($netIpConf."$($AddressFamily)DefaultGateway").NextHop
if(!$currentGateway.Equals($DefaultGateway))
{
Write-Verbose -Message "Default gateway not correct. Expected $DefaultGateway, actual $($currentIP.PrefixLength)"
$requiresChanges = $true
}
else
{
Write-Verbose -Message "Default gateway is correct."
}
}
if($requiresChanges)
{
#Apply is true in the case of set - target resource - in which case, it will apply the required IP configuration
$Parameters = @{}
if($Apply)
{
Write-Verbose -Message "At least one setting differs from the passed parameters. Applying configuration ..."
$Parameters["IPAddress"] = $IPAddress
$Parameters["PrefixLength"] = $SubnetMask
$Parameters["InterfaceAlias"] = $currentIP[0].InterfaceAlias
if($DefaultGateway){ $Parameters["DefaultGateWay"] = $DefaultGateway }
$null = New-NetIPAddress @Parameters -ErrorAction Stop
# Make the connection profile private
Get-NetConnectionProfile -InterfaceAlias $InterfaceAlias | Set-NetConnectionProfile -NetworkCategory Private -ErrorAction SilentlyContinue
Write-Verbose -Message "IPAddress is set to $IPAddress."
}
else {return $false}
}
else
{
Write-Verbose -Message "No changes were required."
return $true
}
}
catch
{
Write-Verbose -Message $_
throw "Can not set or find valid IPAddress using InterfaceAlias $InterfaceAlias and AddressFamily $AddressFamily"
}
from networkingdsc.
I suppose in the $DefaultGateway test I would probably also check the parameter's format using [System.Net.Ipaddress]::TryParse()
...
from networkingdsc.
So looking over this code again I really disagree with the way its written. Most DSC code is written where Test just validates the config where Set actually does the work. With the combined methods it's logically hard to parse, but that's probably a different issue for a different pull request.
Speaking of which could you please submit this as a pull request? We'll want to run it through the existing tests.
To your point, I do not believe there is an IPInterface module. I'm not sure a seperate one is needed, but we would want to design something that would check if DHCP is being used or not.
from networkingdsc.
Agree on all points. I'll work up a pull request sometime this week (got a few short term deadlines with work that I need to focus on first). Is the test suite public? I like to execute tests locally before pushing, if I can. Also, is there a DSC module you'd recommend as an example for The Right Way to structure the functions? I'll first get it working just with the modified if
structure, but then I can look at refactoring the functions.
While I slept on it, I realized with the proposed if
structure, I can easily test whether DHCP is enabled to trigger the Set
function. May have to modify the $apply
code somewhat to make sure it disables DHCP when nothing else changes, but I should be able to work that in.
Really not sure how we might enable DHCP with the current parameter-set, though, so I'll leave that for a future request.
from networkingdsc.
Found the Contributing document and the Tests directory. I'll refrain from further stupid questions until I read it and study the Tests.
from networkingdsc.
@lorengordon no stupid questions only stupid, possibly sarcastic answers. 😆
As for a "good" module, I did just finish fixing up MSFT_xWebVirtualDirectory so it looks more like I expect a good resource to look like.
Which is to say.
- Get/Set/Test-TargetResources each do their own chunk of work.
- Set shouldn't call test again in order to see if work needs to be done, if Test fails Set should just configure the resource.
- Verb-Noun functions
- Good test coverage and tests call xDscResourceDesigner to validate the schema and resource.
I'm of course a bit biased because I wrote the thing, so keep that in mind. 😀
Also don't worry about how long it takes you, most open source is written in people's free time so there isn't much expectation of things being done in a specific amount of time around here.
from networkingdsc.
@tysonjhayes thanks for the pointers. I just submitted PR #11. I didn't refactor Set and Test yet. Not really used to the syntax of the pester tests yet, so I did just enough to fix this issue and get the existing tests passing.
I'm rather confused by the function of Get-TargetResource. It doesn't seem to be doing much all that useful -- other than the IP Address, it just returns the parameters passed to it? I would think that it should also be returning the configured SubnetMask and DefaultGateway values. Also, would it be appropriate to call Get-TargetResource from the Test/Set functions, rather than duplicate the functionality?
from networkingdsc.
Get-TargetResource is garbage in my mind. I totally vote we change it to something better.
Also yes I think it makes sense to reference Get-TargetResource as appropriate.
I'll have some comments on the PR. 👍 Thanks for doing this.
from networkingdsc.
Related Issues (20)
- NetAdapterBinding: Use of State & CurrentState Properties is non-idiomatic HOT 6
- IpConfiguration: Can't configure IPv4 address on adapter that only has IPv6 on it
- Update Sampler Build Tasks
- Enable Code Coverage Reporting
- PowerShell-DLM
- Resource parameter naming inconsistent HOT 1
- Include hidden adapters by default? HOT 3
- Remove group of FW rules HOT 1
- NetAdapterAdvancedProperty Constantly Re-Running
- Issue with Networkteaminterface when vlanid is used
- Problem with DSC Automate Account
- Update Azure DevOps Pipeline Images
- Update CI Pipeline Files from Latest Pattern
- Intermittent Integration Test Failures on Windows Server 2022
- Netbios: Fails to disable when NIC has link down HOT 6
- Correct Changelog.md Heading HOT 3
- DnsServerAddress: Fails when trying to rename network adapter when running Test
- NetBios resource failing for Azure VMs with Accelerated Networking enabled
- Firewall resource isn't properly identifying some built-in rules on Windows Server (2019 and 2022 tested) HOT 5
- The first byte of the proxy settings binary was '60' but should have been 0x46. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from networkingdsc.