Skip to content

Commit 2d415d8

Browse files
author
John Bellone
committed
Adds config resource attribute for custom options.
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.
1 parent a7a7a3d commit 2d415d8

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

libraries/consul_config.rb

+11-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ConsulConfig < Chef::Resource
1919

2020
# @!attribute owner
2121
# @return [String]
22-
attribute(:user, kind_of: String, default: 'consul')
22+
attribute(:owner, kind_of: String, default: 'consul')
2323

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

36+
# @!attribute options
37+
# @return [Hash]
38+
attribute(:options, option_collector: true)
39+
3640
# @see: http://www.consul.io/docs/agent/options.html
3741
attribute(:acl_datacenter, kind_of: String)
3842
attribute(:acl_default_policy, kind_of: String)
@@ -65,7 +69,7 @@ class ConsulConfig < Chef::Resource
6569
attribute(:node_name, kind_of: String)
6670
attribute(:ports, kind_of: [Hash, Mash])
6771
attribute(:protocol, kind_of: String)
68-
attribute(:recurser, kind_of: String)
72+
attribute(:recursor, kind_of: String)
6973
attribute(:retry_interval, kind_of: Integer)
7074
attribute(:server, equal_to: [true, false], default: true)
7175
attribute(:server_name, kind_of: String)
@@ -87,7 +91,7 @@ def to_json
8791
for_keeps << %i{ca_file cert_file key_file} if tls?
8892
config = to_hash.keep_if do |k, _|
8993
for_keeps.include?(k.to_sym)
90-
end
94+
end.merge(options)
9195
JSON.pretty_generate(config, quirks_mode: true)
9296
end
9397

@@ -108,7 +112,7 @@ def tls?
108112
file new_resource.ca_file do
109113
content item['ca_certificate']
110114
mode '0644'
111-
owner new_resource.user
115+
owner new_resource.owner
112116
group new_resource.group
113117
end
114118

@@ -119,7 +123,7 @@ def tls?
119123
file new_resource.cert_file do
120124
content item['certificate']
121125
mode '0644'
122-
owner new_resource.user
126+
owner new_resource.owner
123127
group new_resource.group
124128
end
125129

@@ -131,7 +135,7 @@ def tls?
131135
sensitive true
132136
content item['private_key']
133137
mode '0640'
134-
owner new_resource.user
138+
owner new_resource.owner
135139
group new_resource.group
136140
end
137141
end
@@ -141,7 +145,7 @@ def tls?
141145
end
142146

143147
file new_resource.path do
144-
owner new_resource.user
148+
owner new_resource.owner
145149
group new_resource.group
146150
content new_resource.to_json
147151
mode '0640'

recipes/default.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
end
2323

2424
config = consul_config node['consul']['service_name'] do |r|
25-
user node['consul']['service_user']
25+
owner node['consul']['service_user']
2626
group node['consul']['service_group']
2727

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

3737
node['consul']['service'].each_pair { |k, v| r.send(k, v) }
3838
subscribes :restart, "consul_config[#{config.name}]", :delayed
39-
action [:enable, :start]
4039
end

test/spec/libraries/consul_config_spec.rb

+23-9
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,31 @@
33

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

7-
context '#action_create' do
8-
before do
9-
recipe = double('Chef::Recipe')
10-
allow_any_instance_of(Chef::RunContext).to receive(:include_recipe).and_return([recipe])
11-
allow_any_instance_of(Chef::Provider).to receive(:chef_vault_item) { { 'ca_certificate' => 'foo', 'certificate' => 'bar', 'private_key' => 'baz' } }
12+
context 'sets options directly' do
13+
recipe do
14+
consul_config '/etc/consul/default.json' do
15+
options do
16+
recurser 'foo'
17+
end
18+
end
1219
end
1320

21+
it { is_expected.to render_file('/etc/consul/default.json').with_content(<<-EOH.chomp) }
22+
{
23+
"verify_incoming": false,
24+
"verify_outgoing": false,
25+
"recurser": "foo"
26+
}
27+
EOH
28+
end
29+
30+
context 'manages certificates' do
1431
recipe do
1532
consul_config '/etc/consul/default.json' do
1633
key_file '/etc/consul/ssl/private/consul.key'
@@ -52,18 +69,15 @@
5269

5370
it { is_expected.to create_directory('/etc/consul') }
5471
it { is_expected.to create_file('/etc/consul/default.json') }
55-
56-
it { run_chef }
5772
end
5873

59-
context '#action_delete' do
74+
context 'deletes configuration' do
6075
recipe do
6176
consul_config '/etc/consul/default.json' do
6277
action :delete
6378
end
6479

6580
it { is_expected.to delete_file('/etc/consul/default.json') }
66-
it { run_chef }
6781
end
6882
end
6983
end

0 commit comments

Comments
 (0)