From e0e456259e56be355bb6df4328dbd93fa5c6c5f5 Mon Sep 17 00:00:00 2001 From: Eric Fode Date: Thu, 6 Nov 2014 13:36:14 -0800 Subject: [PATCH 1/3] Update README.md --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 04914ef2..94fca54f 100644 --- a/README.md +++ b/README.md @@ -248,6 +248,13 @@ Include `consul::ui` in your node's `run_list`: ### LWRP +##### Adding key watch + consul_key_watch_def 'key-watch-name' do + key "/key/path" + handler "chef-client" + end + + ##### Adding event watch consul_event_watch_def 'event-name' do handler "chef-client" From 64a0f9142a0a83f61ce540fdc6b915323968468d Mon Sep 17 00:00:00 2001 From: Eric Fode Date: Thu, 6 Nov 2014 13:37:43 -0800 Subject: [PATCH 2/3] added key-watch --- providers/key_watch_def.rb | 35 ++++++++++++++++ resources/key_watch_def.rb | 42 +++++++++++++++++++ .../consul_spec/recipes/key_watch_create.rb | 5 +++ .../consul_spec/recipes/key_watch_delete.rb | 4 ++ spec/unit/resources/key_watch_def.rb | 27 ++++++++++++ 5 files changed, 113 insertions(+) create mode 100644 providers/key_watch_def.rb create mode 100644 resources/key_watch_def.rb create mode 100644 spec/fixtures/cookbooks/consul_spec/recipes/key_watch_create.rb create mode 100644 spec/fixtures/cookbooks/consul_spec/recipes/key_watch_delete.rb create mode 100644 spec/unit/resources/key_watch_def.rb 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 From 8a7eeb6d0857ae3368e14d87fd1cdad2d5f8304b Mon Sep 17 00:00:00 2001 From: Eric Fode Date: Thu, 6 Nov 2014 13:52:59 -0800 Subject: [PATCH 3/3] renamed spec files so they actually run --- .../resources/{event_watch_def.rb => event_watch_def_spec.rb} | 0 spec/unit/resources/{key_watch_def.rb => key_watch_def_spec.rb} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename spec/unit/resources/{event_watch_def.rb => event_watch_def_spec.rb} (100%) rename spec/unit/resources/{key_watch_def.rb => key_watch_def_spec.rb} (100%) diff --git a/spec/unit/resources/event_watch_def.rb b/spec/unit/resources/event_watch_def_spec.rb similarity index 100% rename from spec/unit/resources/event_watch_def.rb rename to spec/unit/resources/event_watch_def_spec.rb diff --git a/spec/unit/resources/key_watch_def.rb b/spec/unit/resources/key_watch_def_spec.rb similarity index 100% rename from spec/unit/resources/key_watch_def.rb rename to spec/unit/resources/key_watch_def_spec.rb