Skip to content

Commit

Permalink
added key watch
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Fode committed Nov 6, 2014
1 parent a856108 commit a192707
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions providers/key_watch_def.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# Copyright 2014 John Bellone <[email protected]>
# 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
42 changes: 42 additions & 0 deletions resources/key_watch_def.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# Copyright 2014 John Bellone <[email protected]>
# 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include_recipe "consul"
consul_key_watch_def "dummy" do
key "/key/path"
handler "chef-client"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include_recipe "consul"
consul_key_watch_def "dummy" do
action :delete
end
27 changes: 27 additions & 0 deletions spec/unit/resources/key_watch_def.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a192707

Please sign in to comment.