Skip to content

Commit

Permalink
Merge pull request #24 from iainbrighton/dev
Browse files Browse the repository at this point in the history
xDnsRecord: Fixed Test-TargetResource function to support array of DNS records. Fixes #23
  • Loading branch information
TravisEz13 committed Mar 24, 2016
2 parents dcae67f + b1fa734 commit 7a1a6c0
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DSCResource.Tests
79 changes: 55 additions & 24 deletions DSCResources/MSFT_xDnsRecord/MSFT_xDnsRecord.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function Get-TargetResource
# Localized messages
data LocalizedData
{
# culture="en-US"
ConvertFrom-StringData @'
GettingDnsRecordMessage = Getting DNS record '{0}' ({1}) in zone '{2}'.
CreatingDnsRecordMessage = Creating DNS record '{0}' for target '{1}' in zone '{2}'.
RemovingDnsRecordMessage = Removing DNS record '{0}' for target '{1}' in zone '{2}'.
NotDesiredPropertyMessage = DNS record property '{0}' is not correct. Expected '{1}', actual '{2}'
InDesiredStateMessage = DNS record '{0}' is in the desired state.
NotInDesiredStateMessage = DNS record '{0}' is NOT in the desired state.
'@
}


function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
Expand Down Expand Up @@ -26,7 +41,7 @@
$Ensure = 'Present'
)

Write-Verbose "Looking up DNS record for $Name in $Zone"
Write-Verbose -Message ($LocalizedData.GettingDnsRecordMessage -f $Name, $Type, $Zone)
$record = Get-DnsServerResourceRecord -ZoneName $Zone -Name $Name -ErrorAction SilentlyContinue

if ($record -eq $null)
Expand All @@ -40,21 +55,20 @@
}
if ($Type -eq "CName")
{
$Recorddata = ($record.RecordData.hostnamealias).TrimEnd('.')
$recordData = ($record.RecordData.hostnamealias).TrimEnd('.')
}
if ($Type -eq "ARecord")
{
$Recorddata = $record.RecordData.IPv4address.IPAddressToString
$recordData = $record.RecordData.IPv4address.IPAddressToString
}

return @{
Name = $record.HostName;
Zone = $Zone;
Target = $Recorddata;
Ensure = 'Present'
Target = $recordData;
Ensure = 'Present';
}
}

} #end function Get-TargetResource

function Set-TargetResource
{
Expand Down Expand Up @@ -83,9 +97,10 @@ function Set-TargetResource
$Ensure = 'Present'
)

$DNSParameters = @{Name=$Name; ZoneName=$Zone}
$DNSParameters = @{ Name = $Name; ZoneName = $Zone; }

if ($Ensure -eq 'Present') {
if ($Ensure -eq 'Present')
{
if ($Type -eq "ARecord")
{
$DNSParameters.Add('A',$true)
Expand All @@ -96,11 +111,11 @@ function Set-TargetResource
$DNSParameters.Add('CName',$true)
$DNSParameters.Add('HostNameAlias',$Target)
}
Write-Verbose "Creating $Type for DNS $Target in $Zone"
Add-DnsServerResourceRecord @DNSParameters
Write-Verbose -Message ($LocalizedData.CreatingDnsRecordMessage -f $Type, $Target, $Zone)
Add-DnsServerResourceRecord @DNSParameters
}

elseif ($Ensure -eq 'Absent') {
elseif ($Ensure -eq 'Absent')
{

$DNSParameters.Add('Computername','localhost')
$DNSParameters.Add('Force',$true)
Expand All @@ -113,11 +128,10 @@ function Set-TargetResource
{
$DNSParameters.Add('RRType','CName')
}
Write-Verbose "Removing $Type for DNS $Target in $Zone"
Remove-DnsServerResourceRecord @DNSParameters
Write-Verbose -Message ($LocalizedData.RemovingDnsRecordMessage -f $Type, $Target, $Zone)
Remove-DnsServerResourceRecord @DNSParameters
}
}

} #end function Set-TargetResource

function Test-TargetResource
{
Expand Down Expand Up @@ -147,13 +161,30 @@ function Test-TargetResource
$Ensure = 'Present'
)

Write-Verbose "Testing for DNS $Name in $Zone"
$result = @(Get-TargetResource @PSBoundParameters)
if ($Ensure -ne $result.Ensure) { return $false }
elseif ($Ensure -eq 'Present' -and ($result.Target -ne $Target)) { return $false }
if ($Ensure -ne $result.Ensure)
{
Write-Verbose -Message ($LocalizedData.NotDesiredPropertyMessage -f 'Ensure', $Ensure, $result.Ensure)
Write-Verbose -Message ($LocalizedData.NotInDesiredStateMessage -f $Name)
return $false
}
elseif ($Ensure -eq 'Present')
{
if ($result.Target -notcontains $Target)
{
$resultTargetString = $result.Target
if ($resultTargetString -is [System.Array])
{
## We have an array, create a single string for verbose output
$resultTargetString = $result.Target -join ','
}
Write-Verbose -Message ($LocalizedData.NotDesiredPropertyMessage -f 'Target', $Target, $resultTargetString)
Write-Verbose -Message ($LocalizedData.NotInDesiredStateMessage -f $Name)
return $false
}
}
Write-Verbose -Message ($LocalizedData.InDesiredStateMessage -f $Name)
return $true
}

} #end function Test-TargetResource

Export-ModuleMember -Function *-TargetResource

40 changes: 33 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Please check out common DSC Resources [contributing guidelines](https://github.c
* **xDnsServerForwarder** sets a DNS forwarder on a given DNS server.
* **xDnsServerADZone** sets an AD integrated zone on a given DNS server.
* **xDnsServerPrimaryZone** sets a standalone Primary zone on a given DNS server.
__NOTE: AD integrated zones are not (yet) supported.__
* **xDnsServerSecondaryZone** sets a Secondary zone on a given DNS server.
Secondary zones allow client machine in primary DNS zones to do DNS resolution of machines in the secondary DNS zone.
* **xDnsServerZoneTransfer** This resource allows a DNS Server zone data to be replicated to another DNS server.
Expand Down Expand Up @@ -68,16 +67,14 @@ Values include: { None | Any | Named | Specific }
* **Name**: Name of the host
* **Zone**: The name of the zone to create the host record in
* **Target**: Target Hostname or IP Address {*Only Supports IPv4 in the current release*}
* **Type**: DNS Record Type.
Values include: { A-Record | C-Name }
* **Ensure**: Whether the host record should be present or removed

### xDnsRecord
* **Name**: Name of the host
* **Zone**: The name of the zone to create the host record in
* **Target**: Target Hostname or IP Address {*Only Supports IPv4 in the current release*}
* **Type**: DNS Record Type.
Values include: { A-Record | C-Name }
Values include: { ARecord | CName }
* **Ensure**: Whether the host record should be present or removed


Expand All @@ -89,6 +86,8 @@ Values include: { A-Record | C-Name }
* Updated README.md with documentation and examples for xDnsServerForwarder resource.
* Added Resource xDnsServerADZone that sets an AD integrated DNS zone.
* Updated README.md with documentation and examples for xDnsServerADZone resource.
* Fixed bug in xDnsRecord causing Test-TargetResource to fail with multiple (round-robin) entries.
* Updated README.md with example DNS round-robin configuration.

### 1.5.0.0

Expand Down Expand Up @@ -274,13 +273,40 @@ configuration Sample_Arecord
Name = "testArecord"
Target = "192.168.0.123"
Zone = "contoso.com"
Type = "ARecord"
Type = "ARecord"
Ensure = "Present"
}
}
Sample_Arecord
```

### Adding round-robin DNS ARecords

```powershell
configuration Sample_RoundRobin_Arecord
{
Import-DscResource -module xDnsServer
xDnsRecord TestRecord1
{
Name = "testArecord"
Target = "192.168.0.123"
Zone = "contoso.com"
Type = "ARecord"
Ensure = "Present"
}
xDnsRecord TestRecord2
{
Name = "testArecord"
Target = "192.168.0.124"
Zone = "contoso.com"
Type = "ARecord"
Ensure = "Present"
}
}
Sample_RoundRobin_Arecord
```

### Adding a DNS CName

```powershell
Expand All @@ -292,7 +318,7 @@ configuration Sample_CName
Name = "testCName"
Target = "test.contoso.com"
Zone = "contoso.com"
Type = "CName"
Type = "CName"
Ensure = "Present"
}
}
Expand All @@ -310,7 +336,7 @@ configuration Sample_Remove_Record
Name = "testArecord"
Target = "192.168.0.123"
Zone = "contoso.com"
Type = "ARecord"
Type = "ARecord"
Ensure = "Absent"
}
}
Expand Down
Loading

0 comments on commit 7a1a6c0

Please sign in to comment.