diff --git a/README.md b/README.md index 25abd7d8..88dcc4da 100644 --- a/README.md +++ b/README.md @@ -249,10 +249,18 @@ Include `consul::ui` in your node's `run_list`: ### LWRP ##### Adding event watch + consul_event_watch_def 'event-name' do handler "chef-client" end +##### Adding key watch + + consul_event_watch_def 'key-watch-name' do + key "/key/path" + handler "chef-client" + end + ##### Adding service without check consul_service_def 'voice1' do port 5060 diff --git a/providers/key_watch_def.rb b/providers/key_watch_def.rb new file mode 100644 index 00000000..68debf19 --- /dev/null +++ b/providers/key_watch_def.rb @@ -0,0 +1,35 @@ +# +# Copyright 2014 John Bellone +# Copyright 2014 Bloomberg Finance L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +use_inline_resources + +action :create do + file new_resource.path do + user node['consul']['service_user'] + group node['consul']['service_group'] + mode 0600 + content new_resource.to_json + + action :create + end +end + +action :delete do + file new_resource.path do + action :delete + end +end diff --git a/resources/key_watch_def.rb b/resources/key_watch_def.rb new file mode 100644 index 00000000..aae9efc3 --- /dev/null +++ b/resources/key_watch_def.rb @@ -0,0 +1,42 @@ +# +# Copyright 2014 John Bellone +# Copyright 2014 Bloomberg Finance L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'json' + +actions :create, :delete +default_action :create + +attribute :name, name_attribute: true, required: true, kind_of: String +attribute :key, required: true, kind_of: String +attribute :handler, required: true, kind_of: String + +def path + ::File.join(node['consul']['config_dir'], "key-watch-#{name}.json") +end + +def to_json + JSON.pretty_generate(to_hash) +end + +def to_hash + hash = { + type: "key" + } + hash[:key] = key + hash[:handler] = handler + hash +end diff --git a/spec/fixtures/cookbooks/consul_spec/recipes/key_watch_create.rb b/spec/fixtures/cookbooks/consul_spec/recipes/key_watch_create.rb new file mode 100644 index 00000000..26c6a9b8 --- /dev/null +++ b/spec/fixtures/cookbooks/consul_spec/recipes/key_watch_create.rb @@ -0,0 +1,5 @@ +include_recipe "consul" +consul_key_watch_def "dummy" do + key "/key/path" + handler "chef-client" +end diff --git a/spec/fixtures/cookbooks/consul_spec/recipes/key_watch_delete.rb b/spec/fixtures/cookbooks/consul_spec/recipes/key_watch_delete.rb new file mode 100644 index 00000000..679f115a --- /dev/null +++ b/spec/fixtures/cookbooks/consul_spec/recipes/key_watch_delete.rb @@ -0,0 +1,4 @@ +include_recipe "consul" +consul_key_watch_def "dummy" do + action :delete +end diff --git a/spec/unit/resources/key_watch_def.rb b/spec/unit/resources/key_watch_def.rb new file mode 100644 index 00000000..c807fa0c --- /dev/null +++ b/spec/unit/resources/key_watch_def.rb @@ -0,0 +1,27 @@ +require 'spec_helper' +require 'chefspec/berkshelf' + +describe_resource "consul_key_watch_def" do + let(:service_def_path) { "/etc/consul.d/key-watch-dummy.json" } + + describe "create" do + let(:example_recipe) { "consul_spec::key_watch_create" } + + it "register the service" do + ['"key": "/key/path"', '"type": "key"', + '"handler": "chef-client"'].each do |content| + expect(chef_run).to render_file(service_def_path) + .with_content(content) + end + end + end + + describe "delete" do + let(:example_recipe) { "consul_spec::key_watch_delete" } + + it "de-register the service" do + expect(chef_run).to delete_file(service_def_path) + expect(chef_run.file(service_def_path)) + end + end +end