Skip to content

Commit

Permalink
feat: add create-or-update-pacticipant
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Apr 9, 2020
1 parent cc1ba5f commit f1c33ae
Show file tree
Hide file tree
Showing 12 changed files with 611 additions and 1 deletion.
158 changes: 158 additions & 0 deletions doc/pacts/markdown/Pact Broker Client - Pact Broker.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

* [A request for the index resource](#a_request_for_the_index_resource)

* [A request for the index resource](#a_request_for_the_index_resource_given_the_pacticipant_relations_are_present) given the pacticipant relations are present

* [A request for the index resource](#a_request_for_the_index_resource_given_the_pb:latest-tagged-version_relation_exists_in_the_index_resource) given the pb:latest-tagged-version relation exists in the index resource

* [A request for the index resource](#a_request_for_the_index_resource_given_the_pb:latest-version_relation_exists_in_the_index_resource) given the pb:latest-version relation exists in the index resource
Expand All @@ -38,6 +40,8 @@

* [A request to create a global webhook with a JSON body](#a_request_to_create_a_global_webhook_with_a_JSON_body)

* [A request to create a pacticipant](#a_request_to_create_a_pacticipant)

* [A request to create a webhook for a consumer and provider](#a_request_to_create_a_webhook_for_a_consumer_and_provider_given_'Condor'_does_not_exist_in_the_pact-broker) given 'Condor' does not exist in the pact-broker

* [A request to create a webhook with a JSON body and a uuid](#a_request_to_create_a_webhook_with_a_JSON_body_and_a_uuid_given_the_'Pricing_Service'_and_'Condor'_already_exist_in_the_pact-broker) given the 'Pricing Service' and 'Condor' already exist in the pact-broker
Expand Down Expand Up @@ -76,6 +80,10 @@

* [A request to register the repository URL of a pacticipant](#a_request_to_register_the_repository_URL_of_a_pacticipant_given_the_'Pricing_Service'_does_not_exist_in_the_pact-broker) given the 'Pricing Service' does not exist in the pact-broker

* [A request to retrieve a pacticipant](#a_request_to_retrieve_a_pacticipant_given_a_pacticipant_with_name_Foo_exists) given a pacticipant with name Foo exists

* [A request to retrieve a pacticipant](#a_request_to_retrieve_a_pacticipant)

* [A request to retrieve the latest 'production' version of Condor](#a_request_to_retrieve_the_latest_'production'_version_of_Condor_given_'Condor'_exists_in_the_pact-broker_with_the_latest_tagged_'production'_version_1.2.3) given 'Condor' exists in the pact-broker with the latest tagged 'production' version 1.2.3

* [A request to retrieve the latest pact between Condor and the Pricing Service](#a_request_to_retrieve_the_latest_pact_between_Condor_and_the_Pricing_Service_given_a_pact_between_Condor_and_the_Pricing_Service_exists) given a pact between Condor and the Pricing Service exists
Expand All @@ -92,6 +100,8 @@

* [A request to tag the production version of Condor](#a_request_to_tag_the_production_version_of_Condor_given_'Condor'_exists_in_the_pact-broker) given 'Condor' exists in the pact-broker

* [A request to update a pacticipant](#a_request_to_update_a_pacticipant_given_a_pacticipant_with_name_Foo_exists) given a pacticipant with name Foo exists

* [A request to update a webhook](#a_request_to_update_a_webhook_given_a_webhook_with_the_uuid_696c5f93-1b7f-44bc-8d03-59440fcaa9a0_exists) given a webhook with the uuid 696c5f93-1b7f-44bc-8d03-59440fcaa9a0 exists

* [An invalid request to create a webhook for a consumer and provider](#an_invalid_request_to_create_a_webhook_for_a_consumer_and_provider_given_the_'Pricing_Service'_and_'Condor'_already_exist_in_the_pact-broker) given the 'Pricing Service' and 'Condor' already exist in the pact-broker
Expand Down Expand Up @@ -506,6 +516,42 @@ Pact Broker will respond with:
"_links": {
"pb:webhooks": {
"href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-WEBHOOKS"
},
"pb:pacticipants": {
"href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-PACTICIPANTS"
},
"pb:pacticipant": {
"href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-PACTICIPANT-{pacticipant}"
}
}
}
}
```
<a name="a_request_for_the_index_resource_given_the_pacticipant_relations_are_present"></a>
Given **the pacticipant relations are present**, upon receiving **a request for the index resource** from Pact Broker Client, with
```json
{
"method": "get",
"path": "/",
"headers": {
"Accept": "application/hal+json"
}
}
```
Pact Broker will respond with:
```json
{
"status": 200,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_links": {
"pb:pacticipants": {
"href": "http://localhost:1234/pacticipants"
},
"pb:pacticipant": {
"href": "http://localhost:1234/pacticipants/{pacticipant}"
}
}
}
Expand Down Expand Up @@ -778,6 +824,40 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_to_create_a_pacticipant"></a>
Upon receiving **a request to create a pacticipant** from Pact Broker Client, with
```json
{
"method": "post",
"path": "/pacticipants",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"name": "Foo",
"repositoryUrl": "http://foo"
}
}
```
Pact Broker will respond with:
```json
{
"status": 201,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"name": "Foo",
"repositoryUrl": "http://foo",
"_links": {
"self": {
"href": "http://localhost:1234/pacticipants/Foo"
}
}
}
}
```
<a name="a_request_to_create_a_webhook_for_a_consumer_and_provider_given_&#39;Condor&#39;_does_not_exist_in_the_pact-broker"></a>
Given **'Condor' does not exist in the pact-broker**, upon receiving **a request to create a webhook for a consumer and provider** from Pact Broker Client, with
```json
Expand Down Expand Up @@ -1598,6 +1678,50 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_to_retrieve_a_pacticipant_given_a_pacticipant_with_name_Foo_exists"></a>
Given **a pacticipant with name Foo exists**, upon receiving **a request to retrieve a pacticipant** from Pact Broker Client, with
```json
{
"method": "get",
"path": "/pacticipants/Foo",
"headers": {
"Accept": "application/hal+json"
}
}
```
Pact Broker will respond with:
```json
{
"status": 200,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_links": {
"self": {
"href": "http://localhost:1234/pacticipants/Foo"
}
}
}
}
```
<a name="a_request_to_retrieve_a_pacticipant"></a>
Upon receiving **a request to retrieve a pacticipant** from Pact Broker Client, with
```json
{
"method": "get",
"path": "/pacticipants/Foo",
"headers": {
"Accept": "application/hal+json"
}
}
```
Pact Broker will respond with:
```json
{
"status": 404
}
```
<a name="a_request_to_retrieve_the_latest_&#39;production&#39;_version_of_Condor_given_&#39;Condor&#39;_exists_in_the_pact-broker_with_the_latest_tagged_&#39;production&#39;_version_1.2.3"></a>
Given **'Condor' exists in the pact-broker with the latest tagged 'production' version 1.2.3**, upon receiving **a request to retrieve the latest 'production' version of Condor** from Pact Broker Client, with
```json
Expand Down Expand Up @@ -1809,6 +1933,40 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_to_update_a_pacticipant_given_a_pacticipant_with_name_Foo_exists"></a>
Given **a pacticipant with name Foo exists**, upon receiving **a request to update a pacticipant** from Pact Broker Client, with
```json
{
"method": "patch",
"path": "/pacticipants/Foo",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"name": "Foo",
"repositoryUrl": "http://foo"
}
}
```
Pact Broker will respond with:
```json
{
"status": 200,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"name": "Foo",
"repositoryUrl": "http://foo",
"_links": {
"self": {
"href": "http://localhost:1234/pacticipants/Foo"
}
}
}
}
```
<a name="a_request_to_update_a_webhook_given_a_webhook_with_the_uuid_696c5f93-1b7f-44bc-8d03-59440fcaa9a0_exists"></a>
Given **a webhook with the uuid 696c5f93-1b7f-44bc-8d03-59440fcaa9a0 exists**, upon receiving **a request to update a webhook** from Pact Broker Client, with
```json
Expand Down
12 changes: 12 additions & 0 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ def generate_uuid
puts SecureRandom.uuid
end

desc 'create-or-update-pacticipant', 'Create or update pacticipant by name'
method_option :name, type: :string, required: true, desc: "Pacticipant name"
method_option :repository_url, type: :string, required: false, desc: "The repository URL of the pacticipant"
shared_authentication_options_for_pact_broker
verbose_option
def create_or_update_pacticipant(*required_but_ignored)
raise ::Thor::RequiredArgumentMissingError, "Pacticipant name cannot be blank" if options.name.strip.size == 0
require 'pact_broker/client/pacticipants/create'
result = PactBroker::Client::Pacticipants2::Create.call({ name: options.name, repository_url: options.repository_url }, options.broker_base_url, pact_broker_client_options)
$stdout.puts result.message
end

ignored_and_hidden_potential_options_from_environment_variables
desc 'version', "Show the pact_broker-client gem version"
def version
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/client/cli/custom_thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def self.shared_authentication_options_for_pact_broker
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
end

def self.verbose_option
method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
end
end
end
end
Expand Down
13 changes: 12 additions & 1 deletion lib/pact_broker/client/hal/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def put(key, *args)
_link(key).put(*args)
end

def patch(key, *args)
_link(key).patch(*args)
end

def can?(key)
@links.key? key.to_s
end
Expand All @@ -55,6 +59,10 @@ def success?
true
end

def does_not_exist?
false
end

def response
@response
end
Expand Down Expand Up @@ -83,7 +91,6 @@ def assert_success!
end

class ErrorEntity < Entity

def initialize(href, data, http_client, response = nil)
@href = href
@data = data
Expand All @@ -92,6 +99,10 @@ def initialize(href, data, http_client, response = nil)
@response = response
end

def does_not_exist?
response&.status == 404
end

def success?
false
end
Expand Down
5 changes: 5 additions & 0 deletions lib/pact_broker/client/hal/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def post href, body = nil, headers = {}
perform_request(create_request(uri, 'Post', body, headers), uri)
end

def patch href, body = nil, headers = {}
uri = URI(href)
perform_request(create_request(uri, 'Patch', body, headers), uri)
end

def create_request uri, http_method, body = nil, headers = {}
request = Net::HTTP.const_get(http_method).new(uri.request_uri)
request['Content-Type'] = "application/json" if ['Post', 'Put', 'Patch'].include?(http_method)
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/client/hal/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def post(payload = nil, headers = {})
wrap_response(href, @http_client.post(href, payload ? JSON.dump(payload) : nil, headers))
end

def patch(payload = nil, headers = {})
wrap_response(href, @http_client.patch(href, payload ? JSON.dump(payload) : nil, headers))
end

def expand(params)
expanded_url = expand_url(params, href)
new_attrs = @attrs.merge('href' => expanded_url)
Expand Down
15 changes: 15 additions & 0 deletions lib/pact_broker/client/hal_client_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'pact_broker/client/hal'

module PactBroker
module Client
module HalClientMethods
def create_index_entry_point(pact_broker_base_url, pact_broker_client_options)
PactBroker::Client::Hal::EntryPoint.new(pact_broker_base_url, create_http_client(pact_broker_client_options))
end

def create_http_client(pact_broker_client_options)
PactBroker::Client::Hal::HttpClient.new(pact_broker_client_options.merge(pact_broker_client_options[:basic_auth] || {}))
end
end
end
end
57 changes: 57 additions & 0 deletions lib/pact_broker/client/pacticipants/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'pact_broker/client/hal'
require 'json'
require 'pact_broker/client/command_result'
require 'pact_broker/client/hal_client_methods'

module PactBroker
module Client
module Pacticipants2
class Create

include HalClientMethods

def self.call(params, pact_broker_base_url, pact_broker_client_options)
new(params, pact_broker_base_url, pact_broker_client_options).call
end

def initialize(params, pact_broker_base_url, pact_broker_client_options)
@params = params
@index_entry_point = create_index_entry_point(pact_broker_base_url, pact_broker_client_options)
@verbose = pact_broker_client_options[:verbose]
end

def call
pacticipant_entity = index_entity._link('pb:pacticipant').expand('pacticipant' => params[:name]).get
message = nil
response_entity = if pacticipant_entity.does_not_exist?
message = "Pacticipant \"#{params[:name]}\" created"
index_entity._link!('pb:pacticipants').post(pacticipant_resource_params)
else
message = "Pacticipant \"#{params[:name]}\" updated"
pacticipant_entity._link!('self').patch(pacticipant_resource_params)
end

response_entity.assert_success!
PactBroker::Client::CommandResult.new(true, message)
rescue StandardError => e
$stderr.puts("#{e.class} - #{e}\n#{e.backtrace.join("\n")}") if verbose
PactBroker::Client::CommandResult.new(false, "#{e.class} - #{e}")
end

private

attr_reader :index_entry_point, :params, :verbose

def index_entity
@index_entity ||= index_entry_point.get!
end

def pacticipant_resource_params
p = { name: params[:name] }
p[:repositoryUrl] = params[:repository_url] if params[:repository_url]
p
end
end
end
end
end
Loading

0 comments on commit f1c33ae

Please sign in to comment.