Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(HC-30) Make path and setting namevars #30

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,25 @@ hocon_setting { 'sample map setting':

* `ensure`: Ensures that the resource is present. Valid values are 'present', 'absent'.

* `name`: An arbitrary name used as the identity of the resource.

* `path`: The HOCON file in which Puppet will ensure the specified setting.

This parameter, along with `setting`, is one of two namevars for the
`hocon_setting` type, meaning that Puppet will give an error if two `hocon_setting` resources have the same `setting`
and `path` parameters.

* `provider`: The specific backend to use for this `hocon_setting` resource. You will seldom need to specify this --- Puppet will usually discover the appropriate provider for your platform. The only available provider for `hocon_setting` is ruby.

* `setting`: The name of the HOCON file setting to be defined. This can be a top-level setting or a setting nested
within another setting. To define a nested setting, give the full path to that setting with each level separated
by a `.` So, to define a setting `foosetting` nested within a setting called `foo` contained on the top level,
the `setting` parameter would be set to `foo.foosetting`.
the `setting` parameter would be set to `foo.foosetting`.

This parameter, along with `path`, is one of two namevars for the
`hocon_setting` type, meaning that Puppet will give an error if two `hocon_setting` resources have the same `setting`
and `path` parameters.

If no `setting` value is explicitly set, the title of the resource will be used
as the value of `setting`.

* `value`: The value of the HOCON file setting to be defined.

Expand Down
16 changes: 10 additions & 6 deletions lib/puppet/type/hocon_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
defaultto :present
end

newparam(:name, :namevar => true) do
desc 'An arbitrary name used as the identity of the resource.'
end

newparam(:setting) do
newparam(:setting, :namevar => true) do
desc 'The name of the setting to be defined.'
end

newparam(:path) do
newparam(:path, :namevar => true) do
desc 'The file Puppet will ensure contains the specified setting.'
validate do |value|
unless (Puppet.features.posix? and value =~ /^\//) or (Puppet.features.microsoft_windows? and (value =~ /^.:\// or value =~ /^\/\/[^\/]+\/[^\/]+/))
Expand Down Expand Up @@ -106,6 +102,14 @@ def change_to_s(current, new)
end
end

def self.title_patterns
# This is the default title pattern for all types, except hard-wired to
# set the title to :setting instead of :name. This is also hard-wired to
# ONLY set :setting and nothing else, and this will be overridden if
# the :setting parameter is set manually.
[ [ /(.*)/m, [ [:setting] ] ] ]
end

validate do
message = ""
if self.original_parameters[:path].nil?
Expand Down
45 changes: 43 additions & 2 deletions spec/acceptance/conf_setting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,8 @@
end

{
"" => /setting is a required.+value is a required/,
"" => /value is a required/,
"setting => 'test.foo'," => /value is a required/,
"value => 'bar'," => /setting is a required/,
}.each do |parameter_list, error|
context parameter_list do
pp = <<-EOS
Expand Down Expand Up @@ -215,4 +214,46 @@
it_behaves_like 'has_error', 'foo', pp, /must be fully qualified/
end
end

describe 'path and setting parameters' do
context 'path and setting must be unique' do
pp = <<-EOS
hocon_setting {'one.two':
ensure => present,
value => 'one',
path => '#{tmpdir}/one.conf',
}

hocon_setting {'one.two2':
setting => 'one.two',
ensure => present,
value => 'two',
path => '#{tmpdir}/one.conf',
}
EOS

it_behaves_like 'has_error', 'foo', pp, /Cannot alias/
end

context 'setting can be the same if path is different' do
pp = <<-EOS
hocon_setting {'one.two3':
setting => 'one.two',
ensure => present,
value => 'one',
path => '#{tmpdir}/four.conf',
}

hocon_setting {'one.two4':
setting => 'one.two',
ensure => present,
value => 'two',
path => '#{tmpdir}/five.conf',
}
EOS

it_behaves_like 'has_content', "#{tmpdir}/four.conf", pp, "one {\n two=one\n}"
it_behaves_like 'has_content', "#{tmpdir}/five.conf", pp, "one {\n two=two\n}"
end
end
end
2 changes: 1 addition & 1 deletion spec/unit/puppet/type/hocon_setting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Puppet::Type.type(:hocon_setting) do
let(:resource) {
Puppet::Type.type(:hocon_setting).new(
:name => 'hocon setting',
:title => 'hocon setting',
:path => '/tmp/hocon.setting',
:setting => 'test_key.master',
:value => 'value',
Expand Down