-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DnsRecordNs: Implemented as a class-based resource (#254)
- DnsRecordNs - Added new resource to manage NS records - DnsRecordNsScoped - Added new resource to manage scoped NS records
- Loading branch information
Showing
20 changed files
with
2,095 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<# | ||
.SYNOPSIS | ||
The DnsRecordNs DSC resource manages NS DNS records against a specific zone on a Domain Name System (DNS) server. | ||
.DESCRIPTION | ||
The DnsRecordNs DSC resource manages NS DNS records against a specific zone on a Domain Name System (DNS) server. | ||
.PARAMETER DomainName | ||
Specifies the fully qualified DNS domain name for which the NameServer is authoritative. It must be a subdomain the zone or the zone itself. To specify all subdomains, use the '*' character (i.e.: *.contoso.com). (Key Parameter) | ||
.PARAMETER NameServer | ||
Specifies the name server of a domain. This should be a fully qualified domain name, not an IP address (Key Parameter) | ||
#> | ||
|
||
[DscResource()] | ||
class DnsRecordNs : DnsRecordBase | ||
{ | ||
[DscProperty(Key)] | ||
[System.String] | ||
$DomainName | ||
|
||
[DscProperty(Key)] | ||
[System.String] | ||
$NameServer | ||
|
||
[DnsRecordNs] Get() | ||
{ | ||
return ([DnsRecordBase] $this).Get() | ||
} | ||
|
||
[void] Set() | ||
{ | ||
([DnsRecordBase] $this).Set() | ||
} | ||
|
||
[System.Boolean] Test() | ||
{ | ||
return ([DnsRecordBase] $this).Test() | ||
} | ||
|
||
[System.String] getRecordName() | ||
{ | ||
$aRecordName = $null | ||
|
||
# Use regex matching to determine if the domain name provided is a subdomain of the ZoneName (ends in ZoneName). | ||
$regexMatch = $this.DomainName | Select-String -Pattern "^((.*?)\.){0,1}$($this.ZoneName)`$" | ||
|
||
if ($null -eq $regexMatch) | ||
{ | ||
throw ($this.localizedData.DomainZoneMismatch -f $this.DomainName, $this.ZoneName) | ||
} | ||
else | ||
{ | ||
# Match group 2 contains the value in which we are interested. | ||
$aRecordName = $regexMatch.Matches.Groups[2].Value | ||
if ($aRecordName -eq '') | ||
{ | ||
$aRecordName = '.' | ||
} | ||
} | ||
return $aRecordName | ||
} | ||
|
||
hidden [Microsoft.Management.Infrastructure.CimInstance] GetResourceRecord() | ||
{ | ||
Write-Verbose -Message ($this.localizedData.GettingDnsRecordMessage -f 'Ns', $this.ZoneName, $this.ZoneScope, $this.DnsServer) | ||
|
||
$dnsParameters = @{ | ||
ZoneName = $this.ZoneName | ||
ComputerName = $this.DnsServer | ||
RRType = 'NS' | ||
} | ||
|
||
if ($this.isScoped) | ||
{ | ||
$dnsParameters['ZoneScope'] = $this.ZoneScope | ||
} | ||
|
||
$record = Get-DnsServerResourceRecord @dnsParameters -ErrorAction SilentlyContinue | Where-Object -FilterScript { | ||
$translatedRecordName = $this.getRecordName() | ||
if ($translatedRecordName -eq '.') | ||
{ | ||
$translatedRecordName = '@' | ||
} | ||
$_.HostName -eq $translatedRecordName -and | ||
$_.RecordData.NameServer -eq "$($this.NameServer)." | ||
} | ||
|
||
return $record | ||
} | ||
|
||
hidden [DnsRecordNs] NewDscResourceObjectFromRecord([Microsoft.Management.Infrastructure.CimInstance] $record) | ||
{ | ||
$dscResourceObject = [DnsRecordNs] @{ | ||
ZoneName = $this.ZoneName | ||
DomainName = $this.DomainName | ||
NameServer = $this.NameServer | ||
TimeToLive = $record.TimeToLive.ToString() | ||
DnsServer = $this.DnsServer | ||
Ensure = 'Present' | ||
} | ||
|
||
return $dscResourceObject | ||
} | ||
|
||
hidden [void] AddResourceRecord() | ||
{ | ||
$dnsParameters = @{ | ||
ZoneName = $this.ZoneName | ||
ComputerName = $this.DnsServer | ||
NS = $true | ||
Name = $this.getRecordName() | ||
NameServer = $this.NameServer | ||
} | ||
|
||
if ($this.isScoped) | ||
{ | ||
$dnsParameters['ZoneScope'] = $this.ZoneScope | ||
} | ||
|
||
if ($null -ne $this.TimeToLive) | ||
{ | ||
$dnsParameters.Add('TimeToLive', $this.TimeToLive) | ||
} | ||
|
||
Write-Verbose -Message ($this.localizedData.CreatingDnsRecordMessage -f 'NS', $this.ZoneName, $this.ZoneScope, $this.DnsServer) | ||
|
||
Add-DnsServerResourceRecord @dnsParameters | ||
} | ||
|
||
hidden [void] ModifyResourceRecord([Microsoft.Management.Infrastructure.CimInstance] $existingRecord, [System.Collections.Hashtable[]] $propertiesNotInDesiredState) | ||
{ | ||
$dnsParameters = @{ | ||
ZoneName = $this.ZoneName | ||
ComputerName = $this.DnsServer | ||
} | ||
|
||
if ($this.isScoped) | ||
{ | ||
$dnsParameters['ZoneScope'] = $this.ZoneScope | ||
} | ||
|
||
# Copy the existing record and modify values as appropriate | ||
$newRecord = [Microsoft.Management.Infrastructure.CimInstance]::new($existingRecord) | ||
|
||
foreach ($propertyToChange in $propertiesNotInDesiredState) | ||
{ | ||
switch ($propertyToChange.Property) | ||
{ | ||
# Key parameters will never be affected, so only include Mandatory and Optional values in the switch statement | ||
|
||
'TimeToLive' | ||
{ | ||
$newRecord.TimeToLive = [System.TimeSpan] $propertyToChange.ExpectedValue | ||
} | ||
|
||
} | ||
} | ||
|
||
Set-DnsServerResourceRecord @dnsParameters -OldInputObject $existingRecord -NewInputObject $newRecord -Verbose | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<# | ||
.SYNOPSIS | ||
The DnsRecordNsScoped DSC resource manages NS DNS records against a specific zone and zone scope on a Domain Name System (DNS) server. | ||
.DESCRIPTION | ||
The DnsRecordNsScoped DSC resource manages NS DNS records against a specific zone and zone scope on a Domain Name System (DNS) server. | ||
.PARAMETER ZoneScope | ||
Specifies the name of a zone scope. (Key Parameter) | ||
#> | ||
|
||
[DscResource()] | ||
class DnsRecordNsScoped : DnsRecordNs | ||
{ | ||
[DscProperty(Key)] | ||
[System.String] | ||
$ZoneScope | ||
|
||
[DnsRecordNsScoped] Get() | ||
{ | ||
return ([DnsRecordBase] $this).Get() | ||
} | ||
|
||
[void] Set() | ||
{ | ||
([DnsRecordBase] $this).Set() | ||
} | ||
|
||
[System.Boolean] Test() | ||
{ | ||
return ([DnsRecordBase] $this).Test() | ||
} | ||
|
||
hidden [Microsoft.Management.Infrastructure.CimInstance] GetResourceRecord() | ||
{ | ||
return ([DnsRecordNs] $this).GetResourceRecord() | ||
} | ||
|
||
hidden [DnsRecordNsScoped] NewDscResourceObjectFromRecord([Microsoft.Management.Infrastructure.CimInstance] $record) | ||
{ | ||
$dscResourceObject = [DnsRecordNsScoped] @{ | ||
ZoneName = $this.ZoneName | ||
ZoneScope = $this.ZoneScope | ||
DomainName = $this.DomainName | ||
NameServer = $this.NameServer | ||
TimeToLive = $record.TimeToLive.ToString() | ||
DnsServer = $this.DnsServer | ||
Ensure = 'Present' | ||
} | ||
|
||
return $dscResourceObject | ||
} | ||
|
||
hidden [void] AddResourceRecord() | ||
{ | ||
([DnsRecordNs] $this).AddResourceRecord() | ||
} | ||
|
||
hidden [void] ModifyResourceRecord([Microsoft.Management.Infrastructure.CimInstance] $existingRecord, [System.Collections.Hashtable[]] $propertiesNotInDesiredState) | ||
{ | ||
([DnsRecordNs] $this).ModifyResourceRecord($existingRecord, $propertiesNotInDesiredState) | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
source/Examples/Resources/DnsRecordNs/1-DnsRecordNs_Mandatory_config.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<#PSScriptInfo | ||
.VERSION 1.0.1 | ||
.GUID 308f2896-a19f-42bf-a371-140418850175 | ||
.AUTHOR DSC Community | ||
.COMPANYNAME DSC Community | ||
.COPYRIGHT DSC Community contributors. All rights reserved. | ||
.TAGS DSCConfiguration | ||
.LICENSEURI https://github.com/dsccommunity/DnsServerDsc/blob/main/LICENSE | ||
.PROJECTURI https://github.com/dsccommunity/DnsServerDsc | ||
.ICONURI https://dsccommunity.org/images/DSC_Logo_300p.png | ||
.EXTERNALMODULEDEPENDENCIES | ||
.REQUIREDSCRIPTS | ||
.EXTERNALSCRIPTDEPENDENCIES | ||
.RELEASENOTES | ||
Updated author, copyright notice, and URLs. | ||
.PRIVATEDATA 2016-Datacenter,2016-Datacenter-Server-Core | ||
#> | ||
|
||
#Requires -Module DnsServerDsc | ||
|
||
|
||
<# | ||
.DESCRIPTION | ||
This configuration will ensure a DNS NS record exists when only the mandatory properties are specified. | ||
#> | ||
|
||
Configuration DnsRecordNs_Mandatory_config | ||
{ | ||
Import-DscResource -ModuleName 'DnsServerDsc' | ||
|
||
Node localhost | ||
{ | ||
DnsRecordNs 'TestRecord' | ||
{ | ||
ZoneName = 'contoso.com' | ||
DomainName = 'contoso.com' | ||
NameServer = 'ns.contoso.com' | ||
Ensure = 'Present' | ||
} | ||
} | ||
} |
Oops, something went wrong.