-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
469 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1528,7 +1528,8 @@ Pact Broker will respond with: | |
"emailAddress": "[email protected]" | ||
} | ||
} | ||
] | ||
], | ||
"uuid": "ffe683ef-dcd7-4e4f-877d-f6eb3db8e86e" | ||
} | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 12 additions & 59 deletions
71
lib/pact_broker/client/environments/create_environment.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
117
lib/pact_broker/client/environments/environment_command.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
lib/pact_broker/client/environments/update_environment.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.