Skip to content

Commit

Permalink
feat: add list-environments and describe-environment
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 28, 2021
1 parent 416992a commit 4472d48
Show file tree
Hide file tree
Showing 18 changed files with 436 additions and 183 deletions.
79 changes: 0 additions & 79 deletions doc/pacts/markdown/Pact Broker Client - Pact Broker.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

* [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:environments_relation_exists_in_the_index_resource) given the pb:environments relation exists in the index resource

* [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 Down Expand Up @@ -68,8 +66,6 @@

* [A request to create a webhook with every possible event type](#a_request_to_create_a_webhook_with_every_possible_event_type_given_the_'Pricing_Service'_and_'Condor'_already_exist_in_the_pact-broker) given the 'Pricing Service' and 'Condor' already exist in the pact-broker

* [A request to create an environment](#a_request_to_create_an_environment)

* [A request to get the Pricing Service](#a_request_to_get_the_Pricing_Service_given_the_'Pricing_Service'_already_exists_in_the_pact-broker) given the 'Pricing Service' already exists in the pact-broker

* [A request to get the Pricing Service](#a_request_to_get_the_Pricing_Service_given_the_'Pricing_Service'_does_not_exist_in_the_pact-broker) given the 'Pricing Service' does not exist in the pact-broker
Expand Down Expand Up @@ -606,33 +602,6 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_for_the_index_resource_given_the_pb:environments_relation_exists_in_the_index_resource"></a>
Given **the pb:environments relation exists in the index resource**, 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:environments": {
"href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-ENVIRONMENTS"
}
}
}
}
```
<a name="a_request_for_the_index_resource_given_the_pb:latest-tagged-version_relation_exists_in_the_index_resource"></a>
Given **the pb:latest-tagged-version relation exists in the index resource**, upon receiving **a request for the index resource** from Pact Broker Client, with
```json
Expand Down Expand Up @@ -1485,54 +1454,6 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_to_create_an_environment"></a>
Upon receiving **a request to create an environment** from Pact Broker Client, with
```json
{
"method": "POST",
"path": "/HAL-REL-PLACEHOLDER-PB-ENVIRONMENTS",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"name": "test",
"displayName": "Test",
"production": false,
"contacts": [
{
"name": "Foo team",
"details": {
"emailAddress": "[email protected]"
}
}
]
}
}
```
Pact Broker will respond with:
```json
{
"status": 201,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"name": "test",
"displayName": "Test",
"production": false,
"contacts": [
{
"name": "Foo team",
"details": {
"emailAddress": "[email protected]"
}
}
],
"uuid": "ffe683ef-dcd7-4e4f-877d-f6eb3db8e86e"
}
}
```
<a name="a_request_to_get_the_Pricing_Service_given_the_&#39;Pricing_Service&#39;_already_exists_in_the_pact-broker"></a>
Given **the 'Pricing Service' already exists in the pact-broker**, upon receiving **a request to get the Pricing Service** from Pact Broker Client, with
```json
Expand Down
2 changes: 0 additions & 2 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class Broker < CustomThor
using PactBroker::Client::HashRefinements
include PactBroker::Client::CLI::EnvironmentCommands



desc 'can-i-deploy', ''
long_desc File.read(File.join(__dir__, 'can_i_deploy_long_desc.txt'))

Expand Down
57 changes: 37 additions & 20 deletions lib/pact_broker/client/cli/environment_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,8 @@ def self.included(thor)
shared_environment_options
shared_authentication_options
def create_environment
require 'pact_broker/client/environments/create_environment'
params = ENVIRONMENT_PARAM_NAMES.each_with_object({}) { | key, p | p[key] = options[key] }
result = PactBroker::Client::Environments::CreateEnvironment.call(params, options.broker_base_url, pact_broker_client_options)
$stdout.puts result.message
exit(1) unless result.success
end

desc "delete-environment", "Delete an environment"
method_option :uuid, required: true, desc: "The UUID of the environment to delete"
method_option :output, aliases: "-o", desc: "json or text", default: 'text'
shared_authentication_options
def delete_environment
require 'pact_broker/client/environments/delete_environment'
params = { uuid: options.uuid, output: options.output }
result = PactBroker::Client::Environments::DeleteEnvironment.call(params, options.broker_base_url, pact_broker_client_options)
$stdout.puts result.message
exit(1) unless result.success
execute_command(params, "CreateEnvironment")
end

ignored_and_hidden_potential_options_from_environment_variables
Expand All @@ -38,11 +23,43 @@ def delete_environment
shared_authentication_options

def update_environment
require 'pact_broker/client/environments/update_environment'
params = (ENVIRONMENT_PARAM_NAMES + [:uuid]).each_with_object({}) { | key, p | p[key] = options[key] }
result = PactBroker::Client::Environments::UpdateEnvironment.call(params, options.broker_base_url, pact_broker_client_options)
$stdout.puts result.message
exit(1) unless result.success
execute_command(params, "UpdateEnvironment")
end

desc "describe-environment", "Describe an environment"
method_option :uuid, required: true, desc: "The UUID of the environment to describe"
method_option :output, aliases: "-o", desc: "json or text", default: 'text'
shared_authentication_options
def describe_environment
params = { uuid: options.uuid, output: options.output }
execute_command(params, "DescribeEnvironment")
end

desc "list-environments", "List environment"
method_option :output, aliases: "-o", desc: "json or text", default: 'text'
shared_authentication_options
def list_environments
params = { output: options.output }
execute_command(params, "ListEnvironments")
end

desc "delete-environment", "Delete an environment"
method_option :uuid, required: true, desc: "The UUID of the environment to delete"
method_option :output, aliases: "-o", desc: "json or text", default: 'text'
shared_authentication_options
def delete_environment
params = { uuid: options.uuid, output: options.output }
execute_command(params, "DeleteEnvironment")
end

no_commands do
def execute_command(params, command_class_name)
require 'pact_broker/client/environments'
result = PactBroker::Client::Environments.const_get(command_class_name).call(params, options.broker_base_url, pact_broker_client_options)
$stdout.puts result.message
exit(1) unless result.success
end
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/pact_broker/client/environments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Dir.glob(File.join(__FILE__.gsub(".rb", "/**/*.rb"))).sort.each do | path |
require path
end
36 changes: 36 additions & 0 deletions lib/pact_broker/client/environments/describe_environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'pact_broker/client/environments/environment_command'
require 'pact_broker/client/generate_display_name'
require 'yaml'

module PactBroker
module Client
module Environments
class DescribeEnvironment < PactBroker::Client::Environments::EnvironmentCommand
include PactBroker::Client::GenerateDisplayName
private

def do_call
existing_environment_resource!
PactBroker::Client::CommandResult.new(true, result_message)
end

def result_message
if json_output?
existing_environment_resource.response.raw_body
else
YAML.dump(displayify_keys(existing_environment_resource.response.body.except("_links"))).gsub("---\n", "")
end
end

def displayify_keys(thing)
case thing
when Hash then thing.each_with_object({}) { | (key, value), new_hash | new_hash[generate_display_name(key)] = displayify_keys(value) }
when Array then thing.collect{ | value | displayify_keys(value) }
else
thing
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pact_broker/client/environments/environment_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def handle_http_error(e)
body = e.entity.response.raw_body
(body.nil? || body == "") ? "{}" : body
else
e.message
::Term::ANSIColor.red(e.message)
end
PactBroker::Client::CommandResult.new(false, message)
end
Expand Down
30 changes: 30 additions & 0 deletions lib/pact_broker/client/environments/list_environments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'pact_broker/client/environments/environment_command'
require 'pact_broker/client/environments/text_formatter'

module PactBroker
module Client
module Environments
class ListEnvironments < PactBroker::Client::Environments::EnvironmentCommand
private

attr_reader :environments_resource

def do_call
PactBroker::Client::CommandResult.new(true, result_message)
end

def environments_resource
@environments_resource = environments_link.get!
end

def result_message
if json_output?
environments_resource.response.raw_body
else
PactBroker::Client::Environments::TextFormatter.call(environments_resource._embedded["environments"])
end
end
end
end
end
end
30 changes: 30 additions & 0 deletions lib/pact_broker/client/environments/text_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'table_print'
require 'ostruct'

module PactBroker
module Client
module Environments
class TextFormatter

def self.call(environments)
return "" if environments.size == 0

data = environments.collect do | environment |
OpenStruct.new(environment)
end

uuid_width = data.collect(&:uuid).collect(&:size).max

tp_options = [
{ uuid: { width: uuid_width } },
{ name: {} },
{ displayName: { display_name: "Display name" } },
{ production: {} }
]

TablePrint::Printer.new(data, tp_options).table_print
end
end
end
end
end
27 changes: 27 additions & 0 deletions lib/pact_broker/client/generate_display_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'pact_broker/client/string_refinements'

module PactBroker
module Client
module GenerateDisplayName
using PactBroker::Client::StringRefinements

def self.call(name)
return nil if name.nil?
name
.to_s
.gsub(/([A-Z])([A-Z])([a-z])/,'\1 \2\3')
.gsub(/([a-z\d])([A-Z])(\S)/,'\1 \2\3')
.gsub(/(\S)([\-_\s\.])(\S)/, '\1 \3')
.gsub(/\s+/, " ")
.strip
.split(" ")
.collect{ |word| word.camelcase(true) }
.join(" ")
end

def generate_display_name(name)
GenerateDisplayName.call(name)
end
end
end
end
56 changes: 56 additions & 0 deletions lib/pact_broker/client/string_refinements.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module PactBroker
module Client
module StringRefinements
refine NilClass do
def blank?
true
end
end

refine String do
def not_blank?
!blank?
end

def blank?
self.strip.size == 0
end

# ripped from rubyworks/facets, thank you
def snakecase
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
.gsub(/([a-z\d])([A-Z])/,'\1_\2')
.tr('-', '_')
.gsub(/\s/, '_')
.gsub(/__+/, '_')
.downcase
end

# ripped from rubyworks/facets, thank you
def camelcase(*separators)
case separators.first
when Symbol, TrueClass, FalseClass, NilClass
first_letter = separators.shift
end

separators = ['_', '\s'] if separators.empty?

str = self.dup

separators.each do |s|
str = str.gsub(/(?:#{s}+)([a-z])/){ $1.upcase }
end

case first_letter
when :upper, true
str = str.gsub(/(\A|\s)([a-z])/){ $1 + $2.upcase }
when :lower, false
str = str.gsub(/(\A|\s)([A-Z])/){ $1 + $2.downcase }
end

str
end
end
end
end
end
7 changes: 7 additions & 0 deletions spec/fixtures/approvals/describe_environment.approved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Name: existing name
Display Name: existing display name
Production: true
Contacts:
- Name: Someone
Details:
Email Address: [email protected]
3 changes: 3 additions & 0 deletions spec/fixtures/approvals/list_environments.approved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
UUID | NAME | DISPLAY NAME | PRODUCTION
-------------------------------------|------|--------------|-----------
78e85fb2-9df1-48da-817e-c9bea6294e01 | test | Test | false
Loading

0 comments on commit 4472d48

Please sign in to comment.