Skip to content

Commit

Permalink
Merge pull request #227 from ai-traders/fix-parameters-to-json
Browse files Browse the repository at this point in the history
support to specify explicit service name other that filename
  • Loading branch information
johnbellone committed Oct 6, 2015
2 parents 40f2b98 + f814cd8 commit ca47a08
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion libraries/consul_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class ConsulDefinition < Chef::Resource
attribute(:parameters, option_collector: true, default: {})

def to_json
JSON.pretty_generate(type => parameters.merge(name: name))
final_parameters = parameters
final_parameters = final_parameters.merge(name: name) if final_parameters[:name].nil?
JSON.pretty_generate(type => final_parameters)
end

action(:create) do
Expand Down
24 changes: 24 additions & 0 deletions test/spec/libraries/consul_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@
end
end

context 'service definition with explicit name' do
recipe do
consul_definition 'redis' do
type 'service'
parameters(name: 'myredis', tags: %w{master}, address: '127.0.0.1', port: 6379, interval: '10s')
end
end

it { is_expected.to create_directory('/etc/consul') }
it do
is_expected.to create_file('/etc/consul/redis.json')
.with(user: 'consul', group: 'consul', mode: '0640')
.with(content: JSON.pretty_generate(
service: {
name: 'myredis',
tags: ['master'],
address: '127.0.0.1',
port: 6379,
interval: '10s'
}
))
end
end

context 'check definition' do
recipe do
consul_definition 'web-api' do
Expand Down

0 comments on commit ca47a08

Please sign in to comment.