Skip to content

Commit

Permalink
Update Bluecat.Address.Manager.psm1
Browse files Browse the repository at this point in the history
  • Loading branch information
9whirls authored Oct 19, 2020
1 parent 7d962b6 commit 780f2e1
Showing 1 changed file with 227 additions and 16 deletions.
243 changes: 227 additions & 16 deletions Bluecat.Address.Manager.psm1
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
function Convert-BamProperty {
param (
[parameter(
mandatory = $true,
valueFromPipeline = $true
)]
[psobject] $bamObject
)
begin {}
process {
$prop = $bamObject.properties.trim("|") -split "\|"
foreach ($p in $prop) {
$member = $p -split "="
$bamObject | Add-Member -NotePropertyName $member[0] -NotePropertyValue $member[1]
if ($bamObject.properties) {
$newObject = $bamObject | select -property * -excludeproperty properties
$prop = $bamObject.properties.trim("|") -split "\|"
foreach ($p in $prop) {
$member = $p -split "="
$newObject | Add-Member -NotePropertyName "_$($member[0])" -NotePropertyValue $member[1]
}
$newObject
} else {
$bamObject
}
$bamObject | select -property * -excludeproperty properties
}
end {}
}
Expand Down Expand Up @@ -50,7 +54,7 @@ function Get-BamDnsZone {
$ZoneName,
$bam = $defaultBam
)
$cfg = get-BamConfiguration $ConfigurationName
$cfg = get-BamConfiguration $ConfigurationName $bam
$bam.getZonesByHint($cfg.id, 0, 10, "hint=$ZoneName")
}

Expand All @@ -69,13 +73,31 @@ function Get-BamServer {
$ConfigurationName,
$bam = $defaultBam
)
$cfg = get-BamConfiguration $ConfigurationName
$cfg = get-BamConfiguration $ConfigurationName $bam
$bam.getentities($cfg.id, "Server", 0, [int32]::MaxValue)
}

function Get-BamDevice {
param(
$ConfigurationName,
$bam = $defaultBam
)
$cfg = get-BamConfiguration $ConfigurationName $bam
$bam.getentities($cfg.id, "Device", 0, [int32]::MaxValue)
}

function Get-BamMac {
param(
$ConfigurationName,
$bam = $defaultBam
)
$cfg = get-BamConfiguration $ConfigurationName $bam
$bam.getentities($cfg.id, "MACAddress", 0, [int32]::MaxValue)
}

function Get-BamDnsRecord {
param(
$Zone,
$ZoneId,
[ValidateSet(
"HostRecord",
"AliasRecord",
Expand All @@ -89,27 +111,216 @@ function Get-BamDnsRecord {
$type,
$bam = $defaultBam
)
$bam.getEntities($zone.id, $type, 0, [int32]::MaxValue)
$bam.getEntities($zoneId, $type, 0, [int32]::MaxValue)
}

function Add-BamDnsHostRecord {
param(
$view,
$fqdn,
$viewId,
$name,
$zoneName,
$ip,
$ttl,
$prop = "reverseRecord=true|parentZoneName=$zoneName",
$bam = $defaultBam
)
$bam.addHostRecord($view.id, $fqdn, $ip, $ttl, "reverseRecord=true")
try {
$bam.addHostRecord($viewId, "$name.$zoneName", $ip, $ttl, $prop)
} catch {
$err = $_.exception.message
switch -regex ($err) {
"IP Address doesn't belong to a Network" {
Add-BamDnsGenericRecord -viewId $viewId `
-name $name `
-zonename $zoneName `
-type "A" `
-rdata $ip `
-ttl $ttl `
-bam $bam
break
}
"Duplicate of another item" {
$zone = $bam.getZonesByHint($viewid, 0, 1, "hint=$zonename")
$record = $bam.getEntityByName($zone.id,$name,'HostRecord')
$r = $record | Convert-BamProperty
if ($ip -notin $r._addresses) {
$record.properties = $record.properties -replace "addresses=", "addresses=$ip,"
try {
$bam.update($record)
} catch {
$err = $_.exception.message
switch -regex ($err) {
"Some of the specified addresses are reserved" {
write-host "cannot update $name because $ip is reserved"
break
}
default {
write-host "$name.$zoneName"
write-error $err
}
}
}
}
$record.id
break
}
"Some of the specified addresses are reserved" {
write-host "cannot add $name because $ip is reserved"
break
}
default {
write-host "$name.$zoneName"
write-error $err
}
}
}
}

function Add-BamDnsAliasRecord {
param(
$view,
$fqdn,
$viewId,
$name,
$zoneName,
$targetFqdn,
$ttl,
$prop = "parentZoneName=$zoneName",
$bam = $defaultBam
)
try {
$bam.addAliasRecord($viewId, "$name.$zoneName", $targetFqdn, $ttl, $prop)
} catch {
$err = $_.exception.message
switch -regex ($err) {
"Object was not found" {
if ($targetFqdn -notmatch $zoneName) {
Add-BamDnsExternalRecord $viewId $targetFqdn $bam
Add-BamDnsAliasRecord $viewId $name $zonename $targetFqdn $ttl $bam
}
break
}
default {
"$name.$zoneName"
write-error $err
}
}
}
}

function Add-BamDnsExternalRecord {
param(
$viewId,
$fqdn,
$prop = '',
$bam = $defaultBam
)
$bam.addAliasRecord($view.id, $fqdn, $targetFqdn, $ttl, "")
$bam.addExternalHostRecord($viewId, $fqdn, $prop)
}

function Add-BamDnsSrvRecord {
param(
$viewId,
$name,
$zoneName,
$priority,
$port,
$weight,
$linkedRecordName,
$ttl,
$prop = "parentZoneName=$zoneName",
$bam = $defaultBam
)
try {
$bam.addSrvRecord($viewId, "$name.$zoneName", $priority, $port, $weight, $linkedRecordName, $ttl, $prop)
} catch {
$err = $_.exception.message
switch -regex ($err) {
"Duplicate of another item" {
$zone = $bam.getZonesByHint($viewid, 0, 1, "hint=$zonename")
$record = $bam.getEntityByName($zone.id,$name,'SRVRecord')
$record.id
break
}
default {
"$name.$zoneName"
write-error $err
}
}
}
}

function Add-BamDnsGenericRecord {
param(
$viewId,
$name,
$zoneName,
[ValidateSet(
"A", "A6", "AAAA", "AFSDB", "APL", "CAA", "CERT", "DHCID", "DNAME",
"DNSKEY", "DS", "ISDN", "KEY", "KX", "LOC", "MB", "MG", "MINFO", "MR",
"NS", "NSAP", "PX", "RP", "RT", "SINK", "SSHFP", "TLSA", "WKS", "X25"
)]
$type,
$rdata,
$ttl,
$prop = "parentZoneName=$zoneName",
$bam = $defaultBam
)
try {
$bam.addGenericRecord($viewId, "$name.$zoneName", $type, $rdata, $ttl, $prop)
} catch {
$err = $_.exception.message
switch -regex ($err) {
"Duplicate of another item" {
$zone = $bam.getZonesByHint($viewid, 0, 1, "hint=$zonename")
$record = $bam.getEntityByName($zone.id,$name,'GenericRecord')
$record.id
break
}
default {
"$name.$zoneName"
write-error $err
}
}
}
}

Function Get-BamIPv4Block {
param(
[parameter(
mandatory = $true,
ValueFromPipelineByPropertyName,
helpmessage = 'Container ID or container object'
)]
$id,
[switch]
$recurse = $false,
$bam = $defaultBam
)
begin {}
process {
$subBlock = $bam.getEntities($id, 'IP4Block', 0, [int32]::MaxValue)
$subBlock
if ($recurse) { $subBlock | Get-BamIPv4Block -recurse -bam $bam }
}
end {}
}

Function Get-BamIPv4Network {
param(
[parameter(
mandatory = $true,
ValueFromPipelineByPropertyName,
helpmessage = 'Container ID or container object'
)]
$id,
$bam = $defaultBam
)
begin {}
process {
try {
$bam.getEntities($id, 'IP4Network', 0, [int32]::MaxValue)
} catch {
write-verbose "Failed to get network in block $id"
}
}
end {}
}

0 comments on commit 780f2e1

Please sign in to comment.