Skip to content

Commit

Permalink
feat: add delete-environment
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 28, 2021
1 parent 3cf62f9 commit 361eed1
Show file tree
Hide file tree
Showing 16 changed files with 469 additions and 110 deletions.
3 changes: 2 additions & 1 deletion doc/pacts/markdown/Pact Broker Client - Pact Broker.md
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,8 @@ Pact Broker will respond with:
"emailAddress": "[email protected]"
}
}
]
],
"uuid": "ffe683ef-dcd7-4e4f-877d-f6eb3db8e86e"
}
}
```
Expand Down
16 changes: 13 additions & 3 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class VersionCreationError < ::Thor::Error; end
class Broker < CustomThor
using PactBroker::Client::HashRefinements

ENVIRONMENT_PARAM_NAMES = [:name, :display_name, :production, :contact_name, :contact_email_address]
ENVIRONMENT_PARAM_NAMES = [:name, :display_name, :production, :contact_name, :contact_email_address, :output]

desc 'can-i-deploy', ''
long_desc File.read(File.join(__dir__, 'can_i_deploy_long_desc.txt'))
Expand Down Expand Up @@ -186,7 +186,6 @@ def list_latest_pact_versions(*required_but_ignored)
desc "create-environment", "Create an environment resource in the Pact Broker to represent a real world deployment or release environment."
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] }
Expand All @@ -195,6 +194,18 @@ def create_environment
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
end

ignored_and_hidden_potential_options_from_environment_variables
desc "update-environment", "Update an environment resource in the Pact Broker."
method_option :uuid, required: true, desc: "The UUID of the environment to update"
Expand All @@ -209,7 +220,6 @@ def update_environment
exit(1) unless result.success
end


if ENV.fetch("PACT_BROKER_FEATURES", "").include?("deployments")
ignored_and_hidden_potential_options_from_environment_variables
desc "record-deployment", "Record deployment of a pacticipant version to an environment. See https://docs.pact.io/go/record_deployment for more information."
Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/client/cli/custom_thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def self.shared_environment_options
method_option :production, type: :boolean, default: false, desc: "Whether or not this environment is a production environment. Default: false"
method_option :contact_name, required: false, desc: "The name of the team/person responsible for this environment"
method_option :contact_email_address, required: false, desc: "The email address of the team/person responsible for this environment"
method_option :output, aliases: "-o", desc: "json or text", default: 'text'
end

def self.verbose_option
Expand Down
71 changes: 12 additions & 59 deletions lib/pact_broker/client/environments/create_environment.rb
Original file line number Diff line number Diff line change
@@ -1,76 +1,29 @@
require 'pact_broker/client/hal_client_methods'
require 'pact_broker/client/error'
require 'pact_broker/client/command_result'
require 'term/ansicolor'
require 'pact_broker/client/environments/environment_command'

module PactBroker
module Client
module Environments
class CreateEnvironment
include PactBroker::Client::HalClientMethods

NOT_SUPPORTED_MESSAGE = "This version of the Pact Broker does not support environments. Please upgrade to version 2.80.0 or later."

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
@pact_broker_base_url = pact_broker_base_url
@pact_broker_client_options = pact_broker_client_options
end

def call
check_if_command_supported
create_environment
rescue PactBroker::Client::Error => e
PactBroker::Client::CommandResult.new(false, ::Term::ANSIColor.red(e.message))
end
class CreateEnvironment < PactBroker::Client::Environments::EnvironmentCommand

private

attr_reader :params
attr_reader :pact_broker_base_url, :pact_broker_client_options
attr_reader :created_environment_resource

def create_environment
index_resource
._link!("pb:environments")
.post!(request_body)
PactBroker::Client::CommandResult.new(true, result_message)
def do_call
@created_environment_resource = environments_link.post!(new_environment_body)
PactBroker::Client::CommandResult.new(created_environment_resource.success?, result_message)
end

def request_body
{
"name" => params[:name],
"displayName" => params[:display_name],
"production" => params[:production],
"contacts" => contacts
}.compact
end

def contacts
if params[:contact_name] || params[:contact_email_address]
contact = {}
contact[:name] = params[:contact_name] || "unknown"
if params[:contact_email_address]
contact[:details] = { emailAddress: params[:contact_email_address] }
end
[contact]
def result_message
if json_output?
created_environment_resource.response.raw_body
else
nil
::Term::ANSIColor.green("Created #{params[:name]} environment in #{pact_broker_name} with UUID #{uuid}")
end
end

def result_message
prod_label = params[:production] ? "production" : "non-production"
::Term::ANSIColor.green("Created #{prod_label} environment #{params[:name]} in #{pact_broker_name}")
end

def check_if_command_supported
unless index_resource.can?("pb:environments")
raise PactBroker::Client::Error.new(NOT_SUPPORTED_MESSAGE)
end
def uuid
created_environment_resource.uuid
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions lib/pact_broker/client/environments/delete_environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'pact_broker/client/environments/environment_command'

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

attr_reader :deletion_request_resource

def do_call
existing_environment_resource!
@deletion_request_resource = existing_environment_link.delete!
PactBroker::Client::CommandResult.new(deletion_request_resource.success?, result_message)
end

def result_message
if json_output?
deletion_request_resource.response.raw_body
else
::Term::ANSIColor.green("Deleted environment #{existing_environment_resource.name} from #{pact_broker_name}")
end
end
end
end
end
end
117 changes: 117 additions & 0 deletions lib/pact_broker/client/environments/environment_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
require 'pact_broker/client/hal_client_methods'
require 'pact_broker/client/error'
require 'pact_broker/client/command_result'
require 'term/ansicolor'
require 'pact_broker/client/backports'

module PactBroker
module Client
module Environments
class EnvironmentCommand
include PactBroker::Client::HalClientMethods

NOT_SUPPORTED_MESSAGE = "This version of the Pact Broker does not support environments. Please upgrade to version 2.80.0 or later."

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
@pact_broker_base_url = pact_broker_base_url
@pact_broker_client_options = pact_broker_client_options
end

def call
check_if_command_supported
do_call
rescue PactBroker::Client::Hal::ErrorResponseReturned => e
handle_http_error(e)
rescue PactBroker::Client::Error => e
handle_ruby_error(e)
end

private

attr_reader :params
attr_reader :pact_broker_base_url, :pact_broker_client_options

def handle_http_error(e)
message = if json_output?
body = e.entity.response.raw_body
(body.nil? || body == "") ? "{}" : body
else
e.message
end
PactBroker::Client::CommandResult.new(false, message)
end

def handle_ruby_error(e)
message = if json_output?
{ error: { message: e.message, class: e.class.name } }.to_json
else
::Term::ANSIColor.red(e.message)
end
PactBroker::Client::CommandResult.new(false, message)
end

def new_environment_body
{
"name" => params[:name],
"displayName" => params[:display_name],
"production" => params[:production],
"contacts" => contacts
}.compact
end

def environments_link
index_resource._link!("pb:environments")
end

def existing_environment_link
index_resource
._link!("pb:environment")
.expand(uuid: params[:uuid])
end

def existing_environment_resource
@existing_environment_resource ||= existing_environment_link.get
end

def existing_environment_resource!
existing_environment_resource.assert_success!
end

def existing_environment_body
@existing_environment_params ||= existing_environment_resource!
.response
.body
.except("uuid", "_links", "createdAt", "updatedAt")
end

def contacts
if params[:contact_name] || params[:contact_email_address]
contact = {}
contact["name"] = params[:contact_name] || "unknown"
if params[:contact_email_address]
contact["details"] = { "emailAddress" => params[:contact_email_address] }
end
[contact]
else
nil
end
end

def check_if_command_supported
unless index_resource.can?("pb:environments")
raise PactBroker::Client::Error.new(NOT_SUPPORTED_MESSAGE)
end
end

def json_output?
params[:output] == "json"
end
end
end
end
end
43 changes: 13 additions & 30 deletions lib/pact_broker/client/environments/update_environment.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,29 @@
require 'pact_broker/client/environments/create_environment'
require 'pact_broker/client/backports'
require 'pact_broker/client/environments/environment_command'

module PactBroker
module Client
module Environments
class UpdateEnvironment < PactBroker::Client::Environments::CreateEnvironment
def call
check_if_command_supported
update_environment
rescue PactBroker::Client::Error => e
PactBroker::Client::CommandResult.new(false, ::Term::ANSIColor.red(e.message))
end
class UpdateEnvironment < PactBroker::Client::Environments::EnvironmentCommand

private

def update_environment
index_resource
._link!("pb:environment")
.expand(uuid: params[:uuid])
.put!(request_body)
PactBroker::Client::CommandResult.new(true, result_message)
end
attr_reader :updated_environment_resource

def request_body
@request_body ||= begin
incoming_params = super
existing_environment_params.merge(incoming_params)
end
def do_call
@updated_environment_resource = existing_environment_link.put!(request_body)
PactBroker::Client::CommandResult.new(updated_environment_resource.success?, result_message)
end

def existing_environment_params
@existing_environment_params ||= index_resource
._link!("pb:environment")
.expand(uuid: params[:uuid])
.get!
.response
.body
.except("_links", "createdAt", "updatedAt")
def request_body
@request_body ||= existing_environment_body.merge(new_environment_body)
end

def result_message
::Term::ANSIColor.green("Updated environment #{request_body["name"]} in #{pact_broker_name}")
if json_output?
updated_environment_resource.response.raw_body
else
::Term::ANSIColor.green("Updated #{request_body["name"]} environment in #{pact_broker_name}")
end
end
end
end
Expand Down
11 changes: 9 additions & 2 deletions lib/pact_broker/client/hal/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ module PactBroker
module Client
module Hal
class RelationNotFoundError < ::PactBroker::Client::Error; end
class ErrorResponseReturned < ::PactBroker::Client::Error; end
class ErrorResponseReturned < ::PactBroker::Client::Error
attr_reader :entity

def initialize(message, entity)
super(message)
@entity = entity
end
end

class Entity
def initialize(href, data, http_client, response = nil)
Expand Down Expand Up @@ -165,7 +172,7 @@ def assert_success!(messages = {})
else
default_message
end
raise ErrorResponseReturned.new(message)
raise ErrorResponseReturned.new(message, self)
end
end
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 @@ -39,6 +39,11 @@ def patch href, body = nil, headers = {}
perform_request(create_request(uri, 'Patch', body, headers), uri)
end

def delete href, body = nil, headers = {}
uri = URI(href)
perform_request(create_request(uri, 'Delete', 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
Loading

0 comments on commit 361eed1

Please sign in to comment.