pact_broker-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+ reports
15
+ /.idea
16
+
17
+ # YARD artifacts
18
+ .yardoc
19
+ _yardoc
20
+ doc/
21
+ .bin
22
+
23
+ # Editor files
24
+ *.sublime*
25
+ log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ #source 'http://rea-rubygems'
3
+
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,76 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pact_broker-client (0.0.1)
5
+ httparty
6
+ json
7
+ pact
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ awesome_print (1.1.0)
13
+ coderay (1.0.9)
14
+ columnize (0.3.6)
15
+ daemons (1.1.9)
16
+ debugger (1.6.2)
17
+ columnize (>= 0.3.1)
18
+ debugger-linecache (~> 1.2.0)
19
+ debugger-ruby_core_source (~> 1.2.3)
20
+ debugger-linecache (1.2.0)
21
+ debugger-ruby_core_source (1.2.3)
22
+ diff-lcs (1.2.4)
23
+ eventmachine (1.0.3)
24
+ fakefs (0.4.2)
25
+ find_a_port (1.0.1)
26
+ httparty (0.12.0)
27
+ json (~> 1.8)
28
+ multi_xml (>= 0.5.2)
29
+ json (1.8.1)
30
+ method_source (0.8.2)
31
+ multi_xml (0.5.5)
32
+ pact (1.0.15)
33
+ awesome_print (~> 1.1.0)
34
+ find_a_port (~> 1.0.1)
35
+ json
36
+ rack-test (~> 0.6.2)
37
+ randexp (~> 0.1.7)
38
+ rspec (~> 2.12)
39
+ thin
40
+ thor
41
+ pry (0.9.12.2)
42
+ coderay (~> 1.0.5)
43
+ method_source (~> 0.8)
44
+ slop (~> 3.4)
45
+ rack (1.5.2)
46
+ rack-test (0.6.2)
47
+ rack (>= 1.0)
48
+ rake (10.0.4)
49
+ randexp (0.1.7)
50
+ rspec (2.14.1)
51
+ rspec-core (~> 2.14.0)
52
+ rspec-expectations (~> 2.14.0)
53
+ rspec-mocks (~> 2.14.0)
54
+ rspec-core (2.14.6)
55
+ rspec-expectations (2.14.3)
56
+ diff-lcs (>= 1.1.3, < 2.0)
57
+ rspec-fire (1.2.0)
58
+ rspec (~> 2.11)
59
+ rspec-mocks (2.14.4)
60
+ slop (3.4.6)
61
+ thin (1.6.0)
62
+ daemons (>= 1.0.9)
63
+ eventmachine (>= 1.0.0)
64
+ rack (>= 1.5.0)
65
+ thor (0.18.1)
66
+
67
+ PLATFORMS
68
+ ruby
69
+
70
+ DEPENDENCIES
71
+ debugger
72
+ fakefs (~> 0.4)
73
+ pact_broker-client!
74
+ pry
75
+ rake (~> 10.0.3)
76
+ rspec-fire
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # PactBrokerClient
2
+
3
+ Add a detailed description of the new service here before your first commit.
4
+
5
+ Information here will be consumed by the Treasure Map (via the Rumour Mill). Therefore, please retain the existing formatting on this page, importantly:
6
+
7
+ * The service name at the top of the page
8
+ * The location of this description text (between service name and Custodian(s)
9
+ * Component Custodian(s)
10
+
11
+ Other headings can be deleted if they are not relevant.
12
+
13
+ **NOTE: The following is provided for convenience. Please check it carefully before your first commit.**
14
+
15
+ ## Component Custodian(s)
16
+
17
+ Enter Custodian(s) here
18
+
19
+ ## Development
20
+
21
+ Enter any developer instructions here.
22
+
23
+ ### Database setup
24
+
25
+ Instructions to set up the database for local testing.
26
+
27
+ Then run migrations:
28
+
29
+ $ bundle exec rake db:migrate
30
+
31
+ ### Testing
32
+
33
+ $ bundle exec rake
34
+
35
+ ### Running
36
+
37
+ Instructions to run the service locally.
38
+
39
+ ### Debugging
40
+
41
+ Any specific debugging tools built in to the service (health check is assumed so don't include that).
42
+
43
+ ### Build
44
+
45
+ **edit the below and add the correct endpoint**
46
+
47
+ [Standalone (Bamboo)](http://master.cd.vpc.realestate.com.au/browse/YOUR_NEW_SERVICE_BUILD)
48
+
49
+ ## Deployment
50
+
51
+ Deployment scripts have been generated using [biq-deploy](https://git.realestate.com.au/business-systems/biq-deploy).
52
+
53
+ #### EC2 environments
54
+
55
+ Before deploying to EC2 environments, you may wish to check that the build you wish to deploy has been indexed.
56
+
57
+ 1. Log in into the agent unix box:
58
+
59
+ $ rea-ec2-ssh agent-01.biq
60
+
61
+ 2. See the list of versions indexed and verify the expected version in the list:
62
+
63
+ $ yum list --showduplicates --enablerepo=rea-el6-dev pact_broker_client
64
+
65
+ You can get up an running with these scripts like so (from within the pact_broker_client project on your local machine):
66
+
67
+ $ cd deploy
68
+ $ bundle
69
+ $ bundle exec bin/pact-broker-client-deploy-ec2 --help
70
+
71
+ When deploying to an EC2 environment for the first time, you must create a node for the app:
72
+
73
+ $ bundle exec bin/pact-broker-client-deploy-ec2 -t create -s $YOUR_SUBDOMAIN -p dev
74
+
75
+ The first time the service is deployed, a database must be created. After adding the [required config values](https://git.realestate.com.au/business-systems/pact_broker_client/blob/master/lib/environment_variables.rb) to the
76
+ environment's config-svc, you can run the db provision task:
77
+
78
+ $ bundle exec bin/pact-broker-client-deploy-ec2 -t db_provision -s $YOUR_SUBDOMAIN
79
+
80
+ Then you are free to migrate the db from then on:
81
+
82
+ $ bundle exec bin/pact-broker-client-deploy-ec2 -t db_migrate -s $YOUR_SUBDOMAIN
83
+
84
+ To deploy to a test environment (eg biq-qa1):
85
+
86
+ $ bundle exec bin/pact-broker-client-deploy-ec2 -t deploy -s biq-qa1 -p dev -y
87
+
88
+ #### Production
89
+
90
+ Refer to the [production deployment readme](https://git.realestate.com.au/business-systems/pact_broker_client/blob/master/README-ProdDeploy.md).
91
+
92
+ ### Architecture
93
+
94
+ Any important architectural information.
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ require 'bundler/gem_helper'
2
+ module Bundler
3
+ class GemHelper
4
+ def install
5
+ desc "Build #{name}-#{version}.gem into the pkg directory"
6
+ task 'build' do
7
+ build_gem
8
+ end
9
+
10
+ desc "Build and install #{name}-#{version}.gem into system gems"
11
+ task 'install' do
12
+ install_gem
13
+ end
14
+
15
+ GemHelper.instance = self
16
+ end
17
+ end
18
+ end
19
+ Bundler::GemHelper.install_tasks
20
+ require 'rspec/core/rake_task'
21
+ #require 'geminabox-client'
22
+
23
+ Dir.glob('lib/tasks/**/*.rake').each { |task| load task }
24
+ Dir.glob('tasks/**/*.rake').each { |task| load task }
25
+ RSpec::Core::RakeTask.new(:spec)
26
+
27
+ task :default => [:spec]
28
+
29
+ # desc "Release to REA gems host"
30
+ # task :publish => :build do
31
+ # gem_file = "pkg/pact-broker-client#{PactBroker::Client::VERSION}.gem"
32
+ # Geminabox::Client.new('http://rea-rubygems').upload(gem_file)
33
+ # end
data/ci.sh ADDED
@@ -0,0 +1,10 @@
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ RUBY_VERSION=1.9.3-p194
5
+
6
+ eval $( curl http://dist/rea/toolchain/ruby-env-rbenv-polite | bash -s $RUBY_VERSION )
7
+ rbenv local $RUBY_VERSION
8
+ bundle config --delete path
9
+ bundle config --delete without
10
+ bundle install && bundle exec rake "$@"
@@ -0,0 +1,76 @@
1
+ require 'erb'
2
+ require 'httparty'
3
+
4
+ module PactBroker
5
+ module Client
6
+
7
+ module UrlHelpers
8
+ def encode_param param
9
+ ERB::Util.url_encode param
10
+ end
11
+ end
12
+
13
+ module StringToSymbol
14
+
15
+ #Only works for one level, not recursive!
16
+ def string_keys_to_symbols hash
17
+ hash.keys.each_with_object({}) do | key, new_hash |
18
+ new_hash[key.to_sym] = hash[key]
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ class BaseClient
25
+ include UrlHelpers
26
+ include HTTParty
27
+ include StringToSymbol
28
+
29
+ attr_reader :base_url
30
+
31
+ def initialize options
32
+ @base_url = options[:base_url]
33
+ self.class.base_uri base_url
34
+ end
35
+
36
+ def default_request_headers
37
+ {'Accept' => 'application/json'}
38
+ end
39
+
40
+ def default_get_headers
41
+ default_request_headers
42
+ end
43
+
44
+ def default_patch_headers
45
+ default_request_headers.merge('Content-Type' => 'application/json+patch')
46
+ end
47
+
48
+ def default_put_headers
49
+ default_request_headers.merge('Content-Type' => 'application/json')
50
+ end
51
+
52
+ def handle_response response
53
+ if response.success?
54
+ yield
55
+ elsif response.code == 404
56
+ nil
57
+ else
58
+ raise response.body
59
+ end
60
+ end
61
+
62
+ def patch url, options
63
+ self.class.patch(url, options.merge(body: options[:body].to_json))
64
+ end
65
+
66
+ def put url, *args
67
+ self.class.put(url, *args)
68
+ end
69
+
70
+ def get url, *args
71
+ self.class.get(url, *args)
72
+ end
73
+
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,31 @@
1
+ require 'pact_broker/client/pacticipants'
2
+ require 'pact_broker/client/versions'
3
+ require 'pact_broker/client/pacts'
4
+
5
+
6
+ module PactBroker
7
+
8
+
9
+ module Client
10
+
11
+ DEFAULT_PACT_BROKER_BASE_URL = 'http://pact-broker'
12
+
13
+ class PactBrokerClient
14
+
15
+ DEFAULT_OPTIONS = {base_url: DEFAULT_PACT_BROKER_BASE_URL}
16
+
17
+ attr_reader :base_url
18
+
19
+ def initialize options = {}
20
+ merged_options = DEFAULT_OPTIONS.merge(options)
21
+ @base_url = merged_options[:base_url]
22
+ end
23
+
24
+ def pacticipants
25
+ PactBroker::Client::Pacticipants.new base_url: base_url
26
+ end
27
+
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,34 @@
1
+ require 'pact_broker/client/base_client'
2
+
3
+ module PactBroker
4
+ module Client
5
+ class Pacticipants < BaseClient
6
+
7
+ def versions
8
+ Versions.new base_url: base_url
9
+ end
10
+
11
+ def update options
12
+ body = options.select{ | key, v | [:repository_url].include?(key)}
13
+ response = patch(pacticipant_base_url(options), body: body, headers: default_patch_headers)
14
+ handle_response(response) do
15
+ true
16
+ end
17
+ end
18
+
19
+ def repository_url options
20
+ response = get("#{pacticipant_base_url(options)}/repository_url", headers: default_get_headers.merge('Accept' => 'text/plain'))
21
+ handle_response(response) do
22
+ response.body
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def pacticipant_base_url options
29
+ "/pacticipant/#{encode_param(options[:pacticipant])}"
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,45 @@
1
+ require 'pact/consumer_contract'
2
+ require_relative 'base_client'
3
+
4
+
5
+ module PactBroker
6
+ module Client
7
+ class Pacts < BaseClient
8
+
9
+ def publish options
10
+ consumer_version = options[:consumer_version]
11
+ pact_string = options[:pact]
12
+ consumer_contract = ::Pact::ConsumerContract.from_json pact_string
13
+ url = save_consumer_contract_url consumer_contract, consumer_version
14
+ response = self.class.put(url, body: pact_string, headers: default_put_headers)
15
+ handle_response(response) do
16
+ true
17
+ end
18
+ end
19
+
20
+ def last options
21
+ url = find_last_consumer_contract_url options
22
+ query = options[:tag] ? {tag: options[:tag]} : {}
23
+ response = self.class.get(url, headers: default_get_headers, query: query)
24
+ handle_response(response) do
25
+ response.body
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def find_last_consumer_contract_url options
32
+ consumer_name = encode_param(options[:consumer])
33
+ provider_name = encode_param(options[:provider])
34
+ "/pacticipant/#{consumer_name}/versions/last/pacts/#{provider_name}"
35
+ end
36
+
37
+ def save_consumer_contract_url consumer_contract, consumer_version
38
+ consumer_name = encode_param(consumer_contract.consumer.name)
39
+ provider_name = encode_param(consumer_contract.provider.name)
40
+ version = encode_param(consumer_version)
41
+ "/pacticipant/#{consumer_name}/versions/#{version}/pacts/#{provider_name}"
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,44 @@
1
+ require 'pact_broker/client'
2
+
3
+ module PactBroker
4
+ module Client
5
+ class PublishPacts
6
+
7
+ def initialize pact_broker_base_url, pact_files, consumer_version
8
+ @pact_broker_base_url = pact_broker_base_url
9
+ @pact_files = pact_files
10
+ @consumer_version = consumer_version
11
+ end
12
+
13
+ def call
14
+ validate
15
+ pact_files.collect{ | pact_file | publish_pact pact_file }.all?
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :pact_broker_base_url, :pact_files, :consumer_version
21
+
22
+ def pact_broker_client
23
+ @pact_broker_client ||= PactBroker::Client::PactBrokerClient.new(base_url: pact_broker_base_url)
24
+ end
25
+
26
+ def publish_pact pact_file
27
+ begin
28
+ pact_broker_client.pacticipants.versions.pacts.publish(pact_json: File.read(pact_file), consumer_version: consumer_version)
29
+ true
30
+ rescue => e
31
+ $stderr.puts "Failed to publish pact: #{pact_file} due to error: #{e.to_s}\n#{e.backtrace.join("\n")}"
32
+ false
33
+ end
34
+ end
35
+
36
+ def validate
37
+ raise "Please specify the consumer_version" unless (consumer_version && consumer_version.to_s.strip.size > 0)
38
+ raise "Please specify the pact_broker_base_url" unless (pact_broker_base_url && pact_broker_base_url.to_s.strip.size > 0)
39
+ raise "No pact files found" unless (pact_files && pact_files.any?)
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,46 @@
1
+ require 'rake/tasklib'
2
+
3
+ =begin
4
+ require pact_broker/client/tasks
5
+
6
+ PactBroker::Client::PublicationTask.new do | task |
7
+ require 'consumer/version'
8
+ task.pact_broker_base_url = 'http://pact-broker'
9
+ task.consumer_version = Consumer::VERSION
10
+ end
11
+
12
+ =end
13
+
14
+ module PactBroker
15
+ module Client
16
+ class PublicationTask < ::Rake::TaskLib
17
+
18
+ attr_accessor :pattern, :pact_broker_base_url, :consumer_version
19
+
20
+ def initialize name = nil
21
+ @name = name
22
+ @pattern = 'spec/pacts/*.json'
23
+ @pact_broker_base_url = 'http://pact-broker'
24
+ yield self
25
+ rake_task
26
+ end
27
+
28
+ private
29
+
30
+ def rake_task
31
+ namespace :pact do
32
+ task task_name do
33
+ require 'pact_broker/client/publish_pacts'
34
+ success = PactBroker::Client::PublishPacts.new(pact_broker_base_url, FileList[@pattern], consumer_version).call
35
+ raise "One or more pacts failed to be published" unless success
36
+ end
37
+ end
38
+ end
39
+
40
+ def task_name
41
+ @name ? "publish:#{@name}" : "publish"
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1 @@
1
+ require 'pact_broker/client/tasks/publication_task'
@@ -0,0 +1,5 @@
1
+ module PactBroker
2
+ module Client
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,38 @@
1
+ require_relative 'base_client'
2
+
3
+ module PactBroker
4
+ module Client
5
+ class Versions < BaseClient
6
+
7
+
8
+ def last options
9
+ query = options[:tag] ? {tag: options[:tag]} : {}
10
+ response = self.class.get("#{version_base_url(options)}/last", query: query, headers: default_get_headers)
11
+
12
+ handle_response(response) do
13
+ string_keys_to_symbols(response.to_hash)
14
+ end
15
+ end
16
+
17
+ def update options
18
+ body = options.select{ | key, v | [:repository_ref, :repository_url, :tags].include?(key)}
19
+ body[:tags] ||= []
20
+ (body[:tags] << options[:tag]) if options[:tag]
21
+ response = patch("#{version_base_url(options)}/#{options[:version]}", body: body, headers: default_patch_headers)
22
+ handle_response(response) do
23
+ true
24
+ end
25
+ end
26
+
27
+ def pacts
28
+ Pacts.new base_url: base_url
29
+ end
30
+
31
+ private
32
+ def version_base_url options
33
+ pacticipant = encode_param(options[:pacticipant])
34
+ "/pacticipant/#{pacticipant}/versions"
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,2 @@
1
+ require 'pact_broker/client/version'
2
+ require 'pact_broker/client/pact_broker_client'
@@ -0,0 +1,2 @@
1
+ require 'pact_broker_client/version'
2
+ require 'pact_broker_client/client'
@@ -0,0 +1,32 @@
1
+
2
+ # -*- encoding: utf-8 -*-
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'pact_broker/client/version'
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "pact_broker-client"
9
+ gem.version = PactBroker::Client::VERSION
10
+ gem.authors = ["Bethany Skurrie"]
11
+ gem.email = ["bskurrie@dius.com.au"]
12
+ gem.description = %q{Publishes pacts to, and retrieves pacts from, the pact broker server.}
13
+ gem.summary = %q{See description}
14
+ gem.homepage = "https://github.com/bethesque/pact_broker-client.git"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+ gem.license = 'MIT'
21
+
22
+ gem.add_runtime_dependency 'pact'
23
+ gem.add_runtime_dependency 'httparty'
24
+ gem.add_runtime_dependency 'json' #Not locking down a version because buncher gem requires 1.6, while other projects use 1.7.
25
+
26
+ #gem.add_development_dependency 'geminabox-client'
27
+ gem.add_development_dependency 'rake', '~> 10.0.3'
28
+ gem.add_development_dependency 'pry'
29
+ gem.add_development_dependency 'fakefs', '~> 0.4'
30
+ gem.add_development_dependency 'rspec-fire'
31
+ gem.add_development_dependency 'debugger'
32
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ require 'pact_broker/client'
4
+
5
+ module PactBroker::Client
6
+ describe PactBrokerClient do
7
+
8
+ describe 'initialize' do
9
+ subject { PactBrokerClient.new }
10
+ it 'sets the base_uri to http://pact-broker by default' do
11
+ expect(subject.base_url).to eq('http://pact-broker')
12
+ end
13
+
14
+ it 'allows configuration of the base_uri' do
15
+ expect(PactBrokerClient.new(base_url: 'https://blah').base_url).to eq('https://blah')
16
+ end
17
+ end
18
+ end
19
+
20
+ end