Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key watch #84

Merged
merged 4 commits into from
Nov 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"

This comment was marked as outdated.

end


##### Adding event watch
consul_event_watch_def 'event-name' do
handler "chef-client"
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 <jbellone@bloomberg.net>
# 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 <jbellone@bloomberg.net>
# 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_spec.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