Skip to content

Commit

Permalink
Merge pull request #54 from michaeltchapman/config_default
Browse files Browse the repository at this point in the history
Add config_defaults hash parameter
  • Loading branch information
solarkennedy committed Dec 8, 2014
2 parents f927ddf + 20fcfce commit f245198
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
13 changes: 9 additions & 4 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
$config_dir = '/etc/consul',
$extra_options = '',
$config_hash = {},
$config_defaults = {},
$service_enable = true,
$service_ensure = 'running',
$init_style = $consul::params::init_style,
Expand All @@ -65,13 +66,17 @@
validate_bool($purge_config_dir)
validate_bool($manage_user)
validate_hash($config_hash)
validate_hash($config_defaults)

if $config_hash['data_dir'] {
$data_dir = $config_hash['data_dir']
$_config_hash = merge($config_defaults, $config_hash)
validate_hash($_config_hash)

if $_config_hash['data_dir'] {
$data_dir = $_config_hash['data_dir']
}

if $config_hash['ui_dir'] {
$ui_dir = $config_hash['ui_dir']
if $_config_hash['ui_dir'] {
$ui_dir = $_config_hash['ui_dir']
}

if ($ui_dir and ! $data_dir) {
Expand Down
2 changes: 1 addition & 1 deletion manifests/run_service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
cwd => $consul::config_dir,
path => [$consul::bin_dir,'/bin','/usr/bin'],
command => "consul join -wan ${consul::join_wan}",
onlyif => "consul members -wan -detailed | grep -vP \"dc=${consul::config_hash['datacenter']}\" | grep -P 'alive'",
onlyif => "consul members -wan -detailed | grep -vP \"dc=${consul::_config_hash['datacenter']}\" | grep -P 'alive'",
subscribe => Service['consul'],
}
}
Expand Down
29 changes: 29 additions & 0 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,35 @@
it { should_not contain_file('config.json').with_content(/"bootstrap_expect": "5"/) }
end

context 'Config_defaults is used to provide additional config' do
let(:params) {{
:config_defaults => {
'data_dir' => '/dir1',
},
:config_hash => {
'bootstrap_expect' => '5',
}
}}
it { should contain_file('config.json').with_content(/"bootstrap_expect": 5/) }
it { should contain_file('config.json').with_content(/"data_dir": "\/dir1"/) }
end

context 'Config_defaults is used to provide additional config and is overridden' do
let(:params) {{
:config_defaults => {
'data_dir' => '/dir1',
'server' => false,
},
:config_hash => {
'bootstrap_expect' => '5',
'server' => true,
}
}}
it { should contain_file('config.json').with_content(/"bootstrap_expect": 5/) }
it { should contain_file('config.json').with_content(/"data_dir": "\/dir1"/) }
it { should contain_file('config.json').with_content(/"server": true/) }
end

context "When asked not to manage the user" do
let(:params) {{ :manage_user => false }}
it { should_not contain_user('consul') }
Expand Down
6 changes: 3 additions & 3 deletions templates/config.json.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%- require 'json' -%>
<%- @config_hash.has_key?("protocol") and @config_hash["protocol"] = @config_hash["protocol"].to_i -%>
<%- @config_hash.has_key?("bootstrap_expect") and @config_hash["bootstrap_expect"] = @config_hash["bootstrap_expect"].to_i -%>
<%= JSON.pretty_generate(Hash[@config_hash.sort]) %>
<%- @_config_hash.has_key?("protocol") and @_config_hash["protocol"] = @_config_hash["protocol"].to_i -%>
<%- @_config_hash.has_key?("bootstrap_expect") and @_config_hash["bootstrap_expect"] = @_config_hash["bootstrap_expect"].to_i -%>
<%= JSON.pretty_generate(Hash[@_config_hash.sort]) %>

0 comments on commit f245198

Please sign in to comment.