diplomat 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6993773f2f540cc308337c3a47845e08201dcd93
4
+ data.tar.gz: 238df4c5465672f23f182c68d1e5a87ecc1b7928
5
+ SHA512:
6
+ metadata.gz: a1f2fd67dcdc5feb723c3ad660c2a971f5587726744007a10f7990d0682a4fbb33a6ce7f1ff1780798ad3b8254364e185ab500c8b9c603003468d9fa33fad4e9
7
+ data.tar.gz: 8619604f40bda32375f708a2b994c9a4f3590f480305f563ec376dc36b6ab84a90633511616234fb4789cfd6f0dccd386b46618e52e61a61542e0f0119db19f3
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ # Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in roundel.gemspec
4
+ gemspec
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ diplomat (0.0.3)
5
+ faraday (~> 0.9.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ coderay (1.1.0)
11
+ diff-lcs (1.2.5)
12
+ fakes (1.1.4)
13
+ fakes-rspec (1.0.5)
14
+ fakes
15
+ faraday (0.9.0)
16
+ multipart-post (>= 1.2, < 3)
17
+ json (1.8.1)
18
+ method_source (0.8.2)
19
+ multipart-post (2.0.0)
20
+ pry (0.9.12.6)
21
+ coderay (~> 1.0)
22
+ method_source (~> 0.8)
23
+ slop (~> 3.4)
24
+ rake (10.3.1)
25
+ rspec (2.14.1)
26
+ rspec-core (~> 2.14.0)
27
+ rspec-expectations (~> 2.14.0)
28
+ rspec-mocks (~> 2.14.0)
29
+ rspec-core (2.14.8)
30
+ rspec-expectations (2.14.5)
31
+ diff-lcs (>= 1.1.3, < 2.0)
32
+ rspec-mocks (2.14.6)
33
+ slop (3.4.7)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ bundler (~> 1.3)
40
+ diplomat!
41
+ fakes-rspec
42
+ json
43
+ pry
44
+ rake
45
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2014, Diplomat project contributors
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ * Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+ * Neither the name of the Ruby FFI project nor the
12
+ names of its contributors may be used to endorse or promote products
13
+ derived from this software without specific prior written permission.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
19
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,85 @@
1
+ # Diplomat
2
+
3
+ ### A HTTP Ruby API for [Consul](http://www.consul.io/)
4
+
5
+ ![Diplomacy Boad Game](http://i.imgur.com/Nkuy4b7.jpg)
6
+
7
+
8
+ ## FAQ
9
+
10
+ #### What's Diplomat for?
11
+
12
+ Diplomat allows any ruby application to interact with [Consul's](http://www.consul.io/) distributed key value store, and also receive information about services currently available in the Consol cluster.
13
+
14
+ #### Does it work in rails?
15
+
16
+ Yup! In fact, we're using it in all of our rails production apps instead of any previous case where it'd be right to use environment variables according to [12Factor configuration principals](http://12factor.net/config). This gives us the ability to scale up without making any changes to the actual project codebase, and to move applications around the cluster with ease.
17
+
18
+ Here's what a production database.yml file might look like:
19
+
20
+ ```erb
21
+ <% if Rails.env.production? %>
22
+ production:
23
+ adapter: postgresql
24
+ encoding: unicode
25
+ host: <%= Diplomat::Service.get('postgres').Address %>
26
+ database: <%= Diplomat.get('project/db/name') %>
27
+ pool: 5
28
+ username: <%= Diplomat.get('project/db/user') %>
29
+ password: <%= Diplomat.get('project/db/pass') %>
30
+ port: <%= Diplomat::Service.get('postgres').ServicePort %>
31
+ <% end %>
32
+ ```
33
+
34
+ #### Why would I use Consol over ZooKeeper, Doozerd, etcd, Nagios, Sensu, SmartStack, SkyDNS, Chef, Puppet, Ansible, etc?
35
+
36
+ [Read up what makes Consul different here](http://www.consul.io/intro/vs/index.html)
37
+
38
+ #### How do I install Consul?
39
+
40
+ [See here](http://www.consul.io/intro/). I managed to roll it out on my production machines with the help of [Ansible](http://www.ansible.com/) in one working day.
41
+
42
+ ## Usage
43
+
44
+ ### Key Values
45
+
46
+ #### Getting
47
+
48
+ Getting the value of a key in the key-value store is as simple as using one of the following:
49
+
50
+ ```ruby
51
+ foo = Diplomat.get('foo')
52
+ # => "bar"
53
+ ```
54
+
55
+ #### Setting
56
+
57
+ Setting the value of a key is just as easy:
58
+
59
+ ```ruby
60
+ foo = Diplomat.put('foo', 'bar')
61
+ # => "bar"
62
+ ```
63
+
64
+ ### Services
65
+
66
+ #### Getting
67
+
68
+ Looking up a service is easy as pie:
69
+
70
+ ```ruby
71
+ foo_service = Diplomat::Service.get('foo')
72
+ # => #<OpenStruct Node="hotel", Address="1.2.3.4", ServiceID="hotel_postgres", ServiceName="hotel_postgres", ServiceTags=["postgres"], ServicePort=5432>
73
+ ```
74
+
75
+ ### Todo
76
+
77
+ - Deleting Keys
78
+ - Listing available Services, PUTting and DELETEing services
79
+ - Health
80
+ - Members
81
+ - Status
82
+
83
+ ## Enjoy!
84
+
85
+ ![Photo Copyright "merlinmann". All rights reserved.](http://i.imgur.com/3mBwzR9.jpg Photo Copyright "merlinmann" https://www.flickr.com/photos/merlin/. All rights reserved.)
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'diplomat/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "diplomat"
8
+ spec.version = Diplomat::VERSION
9
+ spec.authors = ["John Hamelink"]
10
+ spec.email = ["john@johnhamelink.com"]
11
+ spec.description = %q{Diplomat is a simple wrapper for Consul}
12
+ spec.summary = %q{Diplomat is a simple wrapper for Consul}
13
+ spec.homepage = "https://github.com/johnhamelink/diplomat"
14
+ spec.license = "BSD"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake", "~> 10.3"
23
+ spec.add_development_dependency "pry", "~> 0.9"
24
+ spec.add_development_dependency "rspec", "~> 2.14"
25
+ spec.add_development_dependency "fakes-rspec", "~> 1.0"
26
+ spec.add_development_dependency "json", "~> 1.8"
27
+ spec.add_dependency "faraday", "~> 0.9"
28
+ end
@@ -0,0 +1,40 @@
1
+ module Diplomat
2
+
3
+ class << self
4
+
5
+ attr_accessor :root_path
6
+ attr_accessor :lib_path
7
+ attr_accessor :url
8
+
9
+ # Internal: Requires internal Faraday libraries.
10
+ #
11
+ # *libs - One or more relative String names to Faraday classes.
12
+ #
13
+ # Returns nothing.
14
+ def require_libs(*libs)
15
+ libs.each do |lib|
16
+ require "#{lib_path}/#{lib}"
17
+ end
18
+ end
19
+
20
+ alias require_lib require_libs
21
+
22
+ private
23
+
24
+ def method_missing(name, *args, &block)
25
+ Diplomat::Kv.new.send(name, *args, &block)
26
+ end
27
+
28
+ end
29
+
30
+ self.root_path = File.expand_path "..", __FILE__
31
+ self.lib_path = File.expand_path "../diplomat", __FILE__
32
+
33
+ # TODO: Make this configurable and overridable
34
+ self.url = "http://localhost:8500"
35
+
36
+
37
+
38
+ require_libs "rest_client", "kv", "service"
39
+
40
+ end
@@ -0,0 +1,45 @@
1
+ require 'base64'
2
+ require 'faraday'
3
+
4
+ module Diplomat
5
+ class Kv < Diplomat::RestClient
6
+
7
+ attr_reader :key, :value, :raw
8
+
9
+ def get key
10
+ @key = key
11
+ @raw = @conn.get "/v1/kv/#{@key}"
12
+ @raw = JSON.parse(@raw.body).first
13
+ @value = Base64.decode64(@raw["Value"])
14
+ return @value
15
+ end
16
+
17
+ def put key, value
18
+ @raw = @conn.put "/v1/kv/#{@key}", @value
19
+ @raw = JSON.parse(@raw.body).first
20
+ @key = @raw["Key"]
21
+ @value = Base64.decode64(@raw["Value"])
22
+ return @value
23
+ end
24
+
25
+ def delete key
26
+ @raw = @conn.delete "/v1/kv/#{@key}"
27
+ @key = nil
28
+ @value = nil
29
+ end
30
+
31
+ def self.get *args
32
+ Diplomat::Kv.new.get *args
33
+ end
34
+
35
+ def self.put *args
36
+ Diplomat::Kv.new.put *args
37
+ end
38
+
39
+ def self.delete *args
40
+ Diplomat::Kv.new.delete *args
41
+ end
42
+
43
+
44
+ end
45
+ end
@@ -0,0 +1,23 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module Diplomat
5
+ class RestClient
6
+
7
+ def initialize api_client=nil
8
+ start_connection api_client
9
+ end
10
+
11
+ private
12
+
13
+ def start_connection api_client=nil
14
+ @conn = api_client ||= Faraday.new(:url => Diplomat::url) do |faraday|
15
+ faraday.request :url_encoded
16
+ faraday.response :logger
17
+ faraday.adapter Faraday.default_adapter
18
+ faraday.use Faraday::Response::RaiseError
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ require 'base64'
2
+ require 'faraday'
3
+
4
+ module Diplomat
5
+ class Service < Diplomat::RestClient
6
+
7
+ def get key
8
+ ret = @conn.get "/v1/catalog/service/#{key}"
9
+ return OpenStruct.new JSON.parse(ret.body).first
10
+ end
11
+
12
+ def self.get *args
13
+ Diplomat::Service.new.get *args
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Diplomat
2
+ VERSION = "0.1.3"
3
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+ require 'base64'
4
+
5
+ describe Diplomat::Kv do
6
+
7
+ let(:faraday) { fake }
8
+
9
+ context "keys" do
10
+ let(:key) { "key" }
11
+ let(:key_url) { "/v1/kv/#{key}" }
12
+ let(:key_params) { "toast" }
13
+
14
+ it "GET" do
15
+ json = JSON.generate({
16
+ "Key" => key,
17
+ "Value" => Base64.encode64(key_params),
18
+ "Flags" => 0
19
+ })
20
+
21
+ faraday.stub(:get).and_return(json)
22
+ kv = Diplomat::Kv.new(faraday)
23
+
24
+ expect(kv.get("key")).to eq("toast")
25
+ end
26
+
27
+ it "PUT" do
28
+
29
+ json = JSON.generate({
30
+ "Key" => key,
31
+ "Value" => Base64.encode64(key_params),
32
+ "Flags" => 0
33
+ })
34
+
35
+ faraday.stub(:get).and_return(json)
36
+ faraday.stub(:put).and_return(json)
37
+ kv = Diplomat::Kv.new(faraday)
38
+
39
+ expect(kv.put(key, key_params)).to eq(key_params)
40
+ end
41
+
42
+ it "namespaces" do
43
+ json = JSON.generate({
44
+ "Key" => key,
45
+ "Value" => Base64.encode64(key_params),
46
+ "Flags" => 0
47
+ })
48
+
49
+ faraday.stub(:get).and_return(json)
50
+ faraday.stub(:put).and_return(json)
51
+ kv = Diplomat::Kv.new(faraday)
52
+
53
+ expect(kv.put("toast/#{key}", key_params)).to eq(key_params)
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,12 @@
1
+ require "bundler/setup"
2
+ Bundler.setup
3
+
4
+ require 'diplomat'
5
+ require 'fakes-rspec'
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ config.order = 'random'
12
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: diplomat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - John Hamelink
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: fakes-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: json
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.8'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.8'
97
+ - !ruby/object:Gem::Dependency
98
+ name: faraday
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.9'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.9'
111
+ description: Diplomat is a simple wrapper for Consul
112
+ email:
113
+ - john@johnhamelink.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - Gemfile
121
+ - Gemfile.lock
122
+ - LICENSE
123
+ - README.md
124
+ - diplomat.gemspec
125
+ - lib/diplomat.rb
126
+ - lib/diplomat/kv.rb
127
+ - lib/diplomat/rest_client.rb
128
+ - lib/diplomat/service.rb
129
+ - lib/diplomat/version.rb
130
+ - spec/kv_spec.rb
131
+ - spec/spec_helper.rb
132
+ homepage: https://github.com/johnhamelink/diplomat
133
+ licenses:
134
+ - BSD
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.2.1
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: Diplomat is a simple wrapper for Consul
156
+ test_files:
157
+ - spec/kv_spec.rb
158
+ - spec/spec_helper.rb
159
+ has_rdoc: