-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #162 from cirrax/add_autoprimaries
add type/provider for autoprimary
- Loading branch information
Showing
8 changed files
with
200 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
Puppet::Type.type(:powerdns_autoprimary).provide(:pdnsutil) do | ||
desc "@summary provider which provides autprimary, | ||
using the pdnsutil command." | ||
|
||
commands pdnsutil: 'pdnsutil' | ||
|
||
def initialize(value = {}) | ||
super(value) | ||
@property_flush = {} | ||
end | ||
|
||
def self.instances | ||
pdnsutil('list-autoprimaries').split("\n").map do |line| | ||
raise Puppet::Error, "Cannot parse invalid autoprimary line: #{line}" unless line =~ %r{^IP=(\S+),\s+NS=(\S+),\s+account=(\S*)$} | ||
new( | ||
ensure: :present, | ||
name: Regexp.last_match(1) + '@' + Regexp.last_match(2), | ||
account: Regexp.last_match(3), | ||
) | ||
end | ||
end | ||
|
||
def self.prefetch(resources) | ||
autoprimaries = instances | ||
resources.each_key do |name| | ||
if (provider = autoprimaries.find { |aprim| aprim.name == name }) | ||
resources[name].provider = provider | ||
end | ||
end | ||
end | ||
|
||
def create | ||
pdnsutil('add-autoprimary', resource[:name].split('@'), resource[:account]) | ||
@property_hash[:ensure] = :present | ||
end | ||
|
||
def destroy | ||
pdnsutil('remove-autoprimary', resource[:name].split('@')) | ||
@property_hash[:ensure] = :absent | ||
end | ||
|
||
def account | ||
@property_hash[:account] | ||
end | ||
|
||
def account=(account) | ||
pdnsutil('remove-autoprimary', resource[:name].split('@')) | ||
pdnsutil('add-autoprimary', resource[:name].split('@'), account) | ||
@property_hash[:ensure] = account | ||
end | ||
|
||
def exists? | ||
@property_hash[:ensure] == :present | ||
end | ||
|
||
def flush | ||
return if @property_flush.empty? | ||
content = @property_flush[:content] || @resource[:content] | ||
virsh_define(content) | ||
@property_flush.clear | ||
end | ||
end |
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'puppet/parameter/boolean' | ||
|
||
Puppet::Type.newtype(:powerdns_autoprimary) do | ||
@doc = 'ensures autoprimary servers (for automatic provisioning of secondaries) | ||
' | ||
|
||
ensurable do | ||
desc 'Manage the state of this type.' | ||
defaultvalues | ||
defaultto :present | ||
end | ||
|
||
newparam(:name, namevar: true) do | ||
desc 'name of the autoprimary in the format IP@NAMESERVER' | ||
|
||
newvalues(%r{^\S+@\S+$}) | ||
end | ||
|
||
newproperty(:account) do | ||
desc 'account to ensure (default to no account)' | ||
defaultto '' | ||
|
||
validate do |value| | ||
raise ArgumentError, 'ip needs to be a string' unless value.is_a?(String) | ||
end | ||
end | ||
|
||
autorequire(:service) do | ||
['pdns'] | ||
end | ||
end |
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,34 @@ | ||
require 'spec_helper' | ||
|
||
describe 'Powerdns::Autoprimaries' do | ||
describe 'valid types' do | ||
context 'with valid types' do | ||
[ | ||
{}, | ||
{ '1.2.3.4@ns1.example.org' => {} }, | ||
{ '2001:db8::1@ns1.example.org' => { 'account' => 'test' } }, | ||
].each do |value| | ||
describe value.inspect do | ||
it { is_expected.to allow_value(value) } | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe 'invalid types' do | ||
context 'with garbage inputs' do | ||
[ | ||
true, | ||
nil, | ||
{ 'foo' => 'bar' }, | ||
'55555', | ||
{ '@ns1.example.org' => {} }, | ||
{ '1.2.3.4@' => {} }, | ||
].each do |value| | ||
describe value.inspect do | ||
it { is_expected.not_to allow_value(value) } | ||
end | ||
end | ||
end | ||
end | ||
end |
20 changes: 20 additions & 0 deletions
20
spec/unit/puppet/provider/powerdns_autoprimary/pdnsutil_spec.rb
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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
provider_class = Puppet::Type.type(:powerdns_autoprimary).provider(:pdnsutil) | ||
|
||
describe provider_class do | ||
let(:resource) do | ||
Puppet::Type::Powerdns_autoprimary.new( | ||
name: '1.2.3.4@ns1.example.com', | ||
provider: described_class.name, | ||
) | ||
end | ||
|
||
let(:provider) { provider_class.new(resource) } | ||
|
||
it 'has its name set' do | ||
expect(resource[:name]).to eq('1.2.3.4@ns1.example.com') | ||
end | ||
end |
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,13 @@ | ||
require 'puppet' | ||
require 'puppet/type/powerdns_autoprimary' | ||
|
||
describe Puppet::Type.type(:powerdns_autoprimary) do | ||
let!(:autoprimary) { Puppet::Type.type(:powerdns_autoprimary).new(name: '1.2.3.4@ns1.example.com') } | ||
|
||
it 'has its name set' do | ||
expect(autoprimary[:name]).to eq('1.2.3.4@ns1.example.com') | ||
end | ||
it 'has set account to empty string' do | ||
expect(autoprimary[:account]).to eq('') | ||
end | ||
end |
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,6 @@ | ||
type Powerdns::Autoprimaries=Hash[ | ||
Pattern[/.+@.+/], | ||
Struct[{ | ||
account => Optional[String], | ||
}] | ||
] |