Skip to content

Commit

Permalink
Merge pull request ajjahn#138 from jearls/add-all-option-to-ptr-param…
Browse files Browse the repository at this point in the history
…eter

Add `all` and `first` values to `ptr` parameter of `dns::record::a`
  • Loading branch information
solarkennedy committed May 24, 2016
2 parents bef2b91 + ff518fb commit 70a5129
Show file tree
Hide file tree
Showing 3 changed files with 300 additions and 9 deletions.
13 changes: 4 additions & 9 deletions manifests/record/a.pp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@
data => $data
}

if $ptr {
if $ptr == 'all' {
dns::record::ptr::by_ip { $data: host => $host, zone => $zone }
} elsif $ptr == 'first' or str2bool($ptr) {
$ip = inline_template('<%= @data.kind_of?(Array) ? @data.first : @data %>')
$reverse_zone = inline_template('<%= @ip.split(".")[0..-2].reverse.join(".") %>.IN-ADDR.ARPA')
$octet = inline_template('<%= @ip.split(".")[-1] %>')

dns::record::ptr { "${octet}.${reverse_zone}":
host => $octet,
zone => $reverse_zone,
data => "${host}.${zone}"
}
dns::record::ptr::by_ip { $ip: host => $host, zone => $zone }
}
}
111 changes: 111 additions & 0 deletions manifests/record/ptr/by_ip.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# == Definition: dns::record::ptr::by_ip
#
# This type is defined to allow the dns::record::a resource type to
# use pre-4.x 'iteration' to create reverse ptr records for hosts with
# multiple ip addresses.
#
# === Parameters
#
# * `$ip`
# The ip address for the ptr record. Defaults to the resource title.
#
# * `$host`
# The (unqualified) host pointed to by this ptr record.
#
# * `$ttl`
# The TTL of the records to be created. Defaults to undefined.
#
# * `$zone`
# The domain of the host pointed to by this ptr record.
#
# === Actions
#
# Reformats the $ip, $host, $zone parameters to create a `dns::record::ptr`
# resource
#
# === Examples
#
# @example
# dns::record::ptr::by_ip { '192.168.128.42':
# host => 'server' ,
# zone => 'example.com' ,
# }
#
# turns into:
#
# @example
# dns::record::ptr { '42.128.168.192.IN-ADDR.ARPA':
# host => '42' ,
# zone => '128.168.192.IN-ADDR.ARPA' ,
# data => 'server.example.com' ,
# }
#
# ---
# @example
# dns::record::ptr::by_ip { [ '192.168.128.68', '192.168.128.69', '192.168.128.70' ]:
# host => 'multihomed-server' ,
# zone => 'example.com' ,
# }
#
# turns into:
#
# @example
# dns::record::ptr { '68.128.168.192.IN-ADDR.ARPA':
# host => '68' ,
# zone => '128.168.192.IN-ADDR.ARPA' ,
# data => 'multihomed-server.example.com' ,
#
# @example
# dns::record::ptr { '69.128.168.192.IN-ADDR.ARPA':
# host => '69' ,
# zone => '128.168.192.IN-ADDR.ARPA' ,
# data => 'multihomed-server.example.com' ,
#
# @example
# dns::record::ptr { '70.128.168.192.IN-ADDR.ARPA':
# host => '70' ,
# zone => '128.168.192.IN-ADDR.ARPA' ,
# data => 'multihomed-server.example.com' ,
# }

define dns::record::ptr::by_ip (
$host,
$zone,
$ttl = undef ,
$ip = $name ) {

# split the IP address up into three host/zone pairs based on class A, B, or C splits:
# For IP address A.B.C.D,
# class C => host D / zone C.B.A.IN-ADDR.ARPA
# class B => host D.C / zone B.A.IN-ADDR.ARPA
# class A => host D.C.B / zone A.IN-ADDR.ARPA
$class_C_zone = inline_template('<%= @ip.split(".").reverse()[1..3].join(".") %>.IN-ADDR.ARPA')
$class_C_host = inline_template('<%= @ip.split(".").reverse()[0..0].join(".") %>')
$class_B_zone = inline_template('<%= @ip.split(".").reverse()[2..3].join(".") %>.IN-ADDR.ARPA')
$class_B_host = inline_template('<%= @ip.split(".").reverse()[0..1].join(".") %>')
$class_A_zone = inline_template('<%= @ip.split(".").reverse()[3..3].join(".") %>.IN-ADDR.ARPA')
$class_A_host = inline_template('<%= @ip.split(".").reverse()[0..2].join(".") %>')

# choose the most specific defined reverse zone file (class C, then B, then A).
# Default to class C if none are defined.
if defined(Dns::Zone[$class_C_zone]) {
$reverse_zone = $class_C_zone
$octet = $class_C_host
} elsif defined(Dns::Zone[$class_B_zone]) {
$reverse_zone = $class_B_zone
$octet = $class_B_host
} elsif defined(Dns::Zone[$class_A_zone]) {
$reverse_zone = $class_A_zone
$octet = $class_A_host
} else {
$reverse_zone = $class_C_zone
$octet = $class_C_host
}

dns::record::ptr { "${octet}.${reverse_zone}":
host => $octet,
zone => $reverse_zone,
ttl => $ttl ,
data => "${host}.${zone}"
}
}
185 changes: 185 additions & 0 deletions spec/defines/dns__record__a_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
require 'spec_helper'

describe 'dns::record::a', :type => :define do
let(:title) { 'atest' }
let(:facts) { { :concat_basedir => '/tmp' } }

context 'passing a single ip address with ptr=>false' do
let :params do {
:host => 'atest',
:zone => 'example.com',
:data => '192.168.128.42',
:ptr => false,
} end
it { should_not raise_error }
it { should contain_concat__fragment('db.example.com.atest,A,example.com.record').with_content(/^atest\s+IN\s+A\s+192\.168\.128\.42$/) }
it { should_not contain_concat__fragment('db.128.168.192.IN-ADDR.ARPA.42.128.168.192.IN-ADDR.ARPA,PTR,128.168.192.IN-ADDR.ARPA.record') }
end

context 'passing a single ip address with ptr=>true' do
let :params do {
:host => 'atest',
:zone => 'example.com',
:data => '192.168.128.42',
:ptr => true,
} end
it { should_not raise_error }
it { should contain_concat__fragment('db.example.com.atest,A,example.com.record').with_content(/^atest\s+IN\s+A\s+192\.168\.128\.42$/) }
it { should contain_concat__fragment('db.128.168.192.IN-ADDR.ARPA.42.128.168.192.IN-ADDR.ARPA,PTR,128.168.192.IN-ADDR.ARPA.record').with_content(/^42\s+IN\s+PTR\s+atest\.example\.com\.$/) }
end

context 'passing a single ip address with ptr=>all' do
let :params do {
:host => 'atest',
:zone => 'example.com',
:data => '192.168.128.42',
:ptr => 'all',
} end
it { should_not raise_error }
it { should contain_concat__fragment('db.example.com.atest,A,example.com.record').with_content(/^atest\s+IN\s+A\s+192\.168\.128\.42$/) }
it { should contain_concat__fragment('db.128.168.192.IN-ADDR.ARPA.42.128.168.192.IN-ADDR.ARPA,PTR,128.168.192.IN-ADDR.ARPA.record').with_content(/^42\s+IN\s+PTR\s+atest\.example\.com\.$/) }
end

context 'passing multiple ip addresses with ptr=>false' do
let :params do {
:host => 'atest',
:zone => 'example.com',
:data => [ '192.168.128.68', '192.168.128.69', '192.168.128.70' ],
:ptr => false,
} end
it { should_not raise_error }
it { should contain_concat__fragment('db.example.com.atest,A,example.com.record').with_content(/^atest\s+IN\s+A\s+192\.168\.128\.68\natest\s+IN\s+A\s+192\.168\.128\.69\natest\s+IN\s+A\s+192\.168\.128\.70$/) }
it { should_not contain_concat__fragment('db.128.168.192.IN-ADDR.ARPA.68.128.168.192.IN-ADDR.ARPA,PTR,128.168.192.IN-ADDR.ARPA.record') }
end

context 'passing multiple ip addresses with ptr=>true' do
let :params do {
:host => 'atest',
:zone => 'example.com',
:data => [ '192.168.128.68', '192.168.128.69', '192.168.128.70' ],
:ptr => true,
} end
it { should_not raise_error }
it { should contain_concat__fragment('db.example.com.atest,A,example.com.record').with_content(/^atest\s+IN\s+A\s+192\.168\.128\.68\natest\s+IN\s+A\s+192\.168\.128\.69\natest\s+IN\s+A\s+192\.168\.128\.70$/) }
it { should contain_concat__fragment('db.128.168.192.IN-ADDR.ARPA.68.128.168.192.IN-ADDR.ARPA,PTR,128.168.192.IN-ADDR.ARPA.record').with_content(/^68\s+IN\s+PTR\s+atest\.example\.com\.$/) }
end

context 'passing multiple ip addresses with ptr=>all' do
let :params do {
:host => 'atest',
:zone => 'example.com',
:data => [ '192.168.128.68', '192.168.128.69', '192.168.128.70' ],
:ptr => 'all',
} end
it { should_not raise_error }
it { should contain_concat__fragment('db.example.com.atest,A,example.com.record').with_content(/^atest\s+IN\s+A\s+192\.168\.128\.68\natest\s+IN\s+A\s+192\.168\.128\.69\natest\s+IN\s+A\s+192\.168\.128\.70$/) }
it { should contain_concat__fragment('db.128.168.192.IN-ADDR.ARPA.68.128.168.192.IN-ADDR.ARPA,PTR,128.168.192.IN-ADDR.ARPA.record').with_content(/^68\s+IN\s+PTR\s+atest\.example\.com\.$/) }
it { should contain_concat__fragment('db.128.168.192.IN-ADDR.ARPA.69.128.168.192.IN-ADDR.ARPA,PTR,128.168.192.IN-ADDR.ARPA.record').with_content(/^69\s+IN\s+PTR\s+atest\.example\.com\.$/) }
it { should contain_concat__fragment('db.128.168.192.IN-ADDR.ARPA.70.128.168.192.IN-ADDR.ARPA,PTR,128.168.192.IN-ADDR.ARPA.record').with_content(/^70\s+IN\s+PTR\s+atest\.example\.com\.$/) }
end

context 'passing ptr=>true with class A network defined' do
let :params do {
:host => 'atest',
:zone => 'example.com',
:data => [ '192.168.128.68', '192.168.128.69', '192.168.128.70' ],
:ptr => 'all',
} end
let :pre_condition do [
'dns::zone { "192.IN-ADDR.ARPA": }',
] end
it { should_not raise_error }
it { should contain_concat__fragment('db.192.IN-ADDR.ARPA.68.128.168.192.IN-ADDR.ARPA,PTR,192.IN-ADDR.ARPA.record').with_content(/^68\.128\.168\s+IN\s+PTR\s+atest\.example\.com\.$/) }
end

context 'passing ptr=>true with class B network defined' do
let :params do {
:host => 'atest',
:zone => 'example.com',
:data => [ '192.168.128.68', '192.168.128.69', '192.168.128.70' ],
:ptr => 'all',
} end
let :pre_condition do [
'dns::zone { "168.192.IN-ADDR.ARPA": }',
] end
it { should_not raise_error }
it { should contain_concat__fragment('db.168.192.IN-ADDR.ARPA.68.128.168.192.IN-ADDR.ARPA,PTR,168.192.IN-ADDR.ARPA.record').with_content(/^68\.128\s+IN\s+PTR\s+atest\.example\.com\.$/) }
end

context 'passing ptr=>true with class C network defined' do
let :params do {
:host => 'atest',
:zone => 'example.com',
:data => [ '192.168.128.68', '192.168.128.69', '192.168.128.70' ],
:ptr => 'all',
} end
let :pre_condition do [
'dns::zone { "128.168.192.IN-ADDR.ARPA": }',
] end
it { should_not raise_error }
it { should contain_concat__fragment('db.128.168.192.IN-ADDR.ARPA.68.128.168.192.IN-ADDR.ARPA,PTR,128.168.192.IN-ADDR.ARPA.record').with_content(/^68\s+IN\s+PTR\s+atest\.example\.com\.$/) }
end

context 'passing ptr=>true with class A and class B network defined' do
let :params do {
:host => 'atest',
:zone => 'example.com',
:data => [ '192.168.128.68', '192.168.128.69', '192.168.128.70' ],
:ptr => 'all',
} end
let :pre_condition do [
'dns::zone { "192.IN-ADDR.ARPA": }',
'dns::zone { "168.192.IN-ADDR.ARPA": }',
] end
it { should_not raise_error }
it { should contain_concat__fragment('db.168.192.IN-ADDR.ARPA.68.128.168.192.IN-ADDR.ARPA,PTR,168.192.IN-ADDR.ARPA.record').with_content(/^68\.128\s+IN\s+PTR\s+atest\.example\.com\.$/) }
end

context 'passing ptr=>true with class A and class C network defined' do
let :params do {
:host => 'atest',
:zone => 'example.com',
:data => [ '192.168.128.68', '192.168.128.69', '192.168.128.70' ],
:ptr => 'all',
} end
let :pre_condition do [
'dns::zone { "192.IN-ADDR.ARPA": }',
'dns::zone { "128.168.192.IN-ADDR.ARPA": }',
] end
it { should_not raise_error }
it { should contain_concat__fragment('db.128.168.192.IN-ADDR.ARPA.68.128.168.192.IN-ADDR.ARPA,PTR,128.168.192.IN-ADDR.ARPA.record').with_content(/^68\s+IN\s+PTR\s+atest\.example\.com\.$/) }
end

context 'passing ptr=>true with class B and class C network defined' do
let :params do {
:host => 'atest',
:zone => 'example.com',
:data => [ '192.168.128.68', '192.168.128.69', '192.168.128.70' ],
:ptr => 'all',
} end
let :pre_condition do [
'dns::zone { "168.192.IN-ADDR.ARPA": }',
'dns::zone { "128.168.192.IN-ADDR.ARPA": }',
] end
it { should_not raise_error }
it { should contain_concat__fragment('db.128.168.192.IN-ADDR.ARPA.68.128.168.192.IN-ADDR.ARPA,PTR,128.168.192.IN-ADDR.ARPA.record').with_content(/^68\s+IN\s+PTR\s+atest\.example\.com\.$/) }
end

context 'passing ptr=>true with class A, class B and class C network defined' do
let :params do {
:host => 'atest',
:zone => 'example.com',
:data => [ '192.168.128.68', '192.168.128.69', '192.168.128.70' ],
:ptr => 'all',
} end
let :pre_condition do [
'dns::zone { "192.IN-ADDR.ARPA": }',
'dns::zone { "168.192.IN-ADDR.ARPA": }',
'dns::zone { "128.168.192.IN-ADDR.ARPA": }',
] end
it { should_not raise_error }
it { should contain_concat__fragment('db.128.168.192.IN-ADDR.ARPA.68.128.168.192.IN-ADDR.ARPA,PTR,128.168.192.IN-ADDR.ARPA.record').with_content(/^68\s+IN\s+PTR\s+atest\.example\.com\.$/) }
end

end

0 comments on commit 70a5129

Please sign in to comment.