Skip to content

Commit

Permalink
Adds config resource attribute for custom options.
Browse files Browse the repository at this point in the history
This fixes #209 by adding ability to put custom options into a
configuration file. These are merged in prior to writing out the JSON
document.
  • Loading branch information
John Bellone committed Aug 24, 2015
1 parent a7a7a3d commit 2d415d8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
18 changes: 11 additions & 7 deletions libraries/consul_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ConsulConfig < Chef::Resource

# @!attribute owner
# @return [String]
attribute(:user, kind_of: String, default: 'consul')
attribute(:owner, kind_of: String, default: 'consul')

# @!attribute group
# @return [String]
Expand All @@ -33,6 +33,10 @@ class ConsulConfig < Chef::Resource
# @return [String]
attribute(:bag_item, kind_of: String, default: 'secrets')

# @!attribute options
# @return [Hash]
attribute(:options, option_collector: true)

# @see: http://www.consul.io/docs/agent/options.html
attribute(:acl_datacenter, kind_of: String)
attribute(:acl_default_policy, kind_of: String)
Expand Down Expand Up @@ -65,7 +69,7 @@ class ConsulConfig < Chef::Resource
attribute(:node_name, kind_of: String)
attribute(:ports, kind_of: [Hash, Mash])
attribute(:protocol, kind_of: String)
attribute(:recurser, kind_of: String)
attribute(:recursor, kind_of: String)
attribute(:retry_interval, kind_of: Integer)
attribute(:server, equal_to: [true, false], default: true)
attribute(:server_name, kind_of: String)
Expand All @@ -87,7 +91,7 @@ def to_json
for_keeps << %i{ca_file cert_file key_file} if tls?
config = to_hash.keep_if do |k, _|
for_keeps.include?(k.to_sym)
end
end.merge(options)
JSON.pretty_generate(config, quirks_mode: true)
end

Expand All @@ -108,7 +112,7 @@ def tls?
file new_resource.ca_file do
content item['ca_certificate']
mode '0644'
owner new_resource.user
owner new_resource.owner
group new_resource.group
end

Expand All @@ -119,7 +123,7 @@ def tls?
file new_resource.cert_file do
content item['certificate']
mode '0644'
owner new_resource.user
owner new_resource.owner
group new_resource.group
end

Expand All @@ -131,7 +135,7 @@ def tls?
sensitive true
content item['private_key']
mode '0640'
owner new_resource.user
owner new_resource.owner
group new_resource.group
end
end
Expand All @@ -141,7 +145,7 @@ def tls?
end

file new_resource.path do
owner new_resource.user
owner new_resource.owner
group new_resource.group
content new_resource.to_json
mode '0640'
Expand Down
3 changes: 1 addition & 2 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
end

config = consul_config node['consul']['service_name'] do |r|
user node['consul']['service_user']
owner node['consul']['service_user']
group node['consul']['service_group']

node['consul']['config'].each_pair { |k, v| r.send(k, v) }
Expand All @@ -36,5 +36,4 @@

node['consul']['service'].each_pair { |k, v| r.send(k, v) }
subscribes :restart, "consul_config[#{config.name}]", :delayed
action [:enable, :start]
end
32 changes: 23 additions & 9 deletions test/spec/libraries/consul_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,31 @@

describe ConsulCookbook::Resource::ConsulConfig do
step_into(:consul_config)
before do
recipe = double('Chef::Recipe')
allow_any_instance_of(Chef::RunContext).to receive(:include_recipe).and_return([recipe])
allow_any_instance_of(Chef::Provider).to receive(:chef_vault_item) { { 'ca_certificate' => 'foo', 'certificate' => 'bar', 'private_key' => 'baz' } }
end

context '#action_create' do
before do
recipe = double('Chef::Recipe')
allow_any_instance_of(Chef::RunContext).to receive(:include_recipe).and_return([recipe])
allow_any_instance_of(Chef::Provider).to receive(:chef_vault_item) { { 'ca_certificate' => 'foo', 'certificate' => 'bar', 'private_key' => 'baz' } }
context 'sets options directly' do
recipe do
consul_config '/etc/consul/default.json' do
options do
recurser 'foo'
end
end
end

it { is_expected.to render_file('/etc/consul/default.json').with_content(<<-EOH.chomp) }
{
"verify_incoming": false,
"verify_outgoing": false,
"recurser": "foo"
}
EOH
end

context 'manages certificates' do
recipe do
consul_config '/etc/consul/default.json' do
key_file '/etc/consul/ssl/private/consul.key'
Expand Down Expand Up @@ -52,18 +69,15 @@

it { is_expected.to create_directory('/etc/consul') }
it { is_expected.to create_file('/etc/consul/default.json') }

it { run_chef }
end

context '#action_delete' do
context 'deletes configuration' do
recipe do
consul_config '/etc/consul/default.json' do
action :delete
end

it { is_expected.to delete_file('/etc/consul/default.json') }
it { run_chef }
end
end
end

0 comments on commit 2d415d8

Please sign in to comment.