Skip to content

Commit

Permalink
feat(BaseService): add new method configure_service & enhance vcap pa…
Browse files Browse the repository at this point in the history
…rsing
  • Loading branch information
jorge-ibm committed Nov 13, 2019
1 parent d6cf9b8 commit fcdab84
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 31 deletions.
20 changes: 11 additions & 9 deletions lib/ibm_cloud_sdk_core/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,11 @@ def initialize(vars)

raise ArgumentError.new("authenticator must be provided") if @authenticator.nil?

if @service_name && !@service_url
config = get_service_properties(@service_name)
@service_url = config[:url] unless config.nil?
end

configure_http_client(disable_ssl_verification: @disable_ssl_verification)
@temp_headers = {}
@conn = HTTP::Client.new(
headers: {}
).use normalize_uri: { normalizer: NORMALIZER }
configure_service(@service_name)
@temp_headers = {}
end

def disable_ssl_verification=(disable_ssl_verification)
Expand All @@ -61,6 +56,15 @@ def add_default_headers(headers: {})
headers.each_pair { |k, v| @conn.default_options.headers.add(k, v) }
end

def configure_service(service_name)
config = get_service_properties(service_name) if service_name

@service_url = config[:url] unless config.nil? || config[:url].nil?
disable_ssl_verification = explicitly_true(config[:disable_ssl]) unless config.nil? || config[:disable_ssl].nil?
# configure the http client if ssl is disabled
configure_http_client(disable_ssl_verification: disable_ssl_verification) if disable_ssl_verification
end

# @return [DetailedResponse]
def request(args)
defaults = { method: nil, url: nil, accept_json: false, headers: nil, params: nil, json: {}, data: nil }
Expand Down Expand Up @@ -143,8 +147,6 @@ def configure_http_client(proxy: {}, timeout: {}, disable_ssl_verification: fals
ssl_context = OpenSSL::SSL::SSLContext.new
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
@conn.default_options = { ssl_context: ssl_context }

@token_manager&.ssl_verification(true)
end
add_proxy(proxy) unless proxy.empty? || !proxy.dig(:address).is_a?(String) || !proxy.dig(:port).is_a?(Integer)
add_timeout(timeout) unless timeout.empty? || (!timeout.key?(:per_operation) && !timeout.key?(:global))
Expand Down
23 changes: 20 additions & 3 deletions lib/ibm_cloud_sdk_core/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ def check_bad_first_or_last_char(str)
return str.start_with?("{", "\"") || str.end_with?("}", "\"") unless str.nil?
end

# checks if the provided value is truthy
def explicitly_true(value)
return value.to_s.casecmp("true").zero? unless value.nil?

false
end

# Initiates the credentials based on the credential file
def load_from_credential_file(service_name, separator = "=")
credential_file_path = ENV["IBM_CREDENTIALS_FILE"]
Expand Down Expand Up @@ -63,10 +70,20 @@ def load_from_vcap_services(service_name)
vcap_services = ENV["VCAP_SERVICES"]
unless vcap_services.nil?
services = JSON.parse(vcap_services)
credentials = ""
# search for matching inner name value
services.each do |_key, val|
service = val.detect { |item| item["name"] == service_name }
credentials = service["credentials"] unless service.nil?
break unless credentials.nil? || credentials.empty?
end
config = {}
credentials = services[service_name][0]["credentials"] if services.key?(service_name)
return config if credentials.nil?

# if no matching inner key is found, then search outer keys
if credentials.nil? || credentials.empty?
credentials = services[service_name][0]["credentials"] if services.key?(service_name) && !services[service_name].empty?
return config if credentials.nil? || credentials.empty?
end
# store credentials
credentials.each do |key, val|
config.store(key.to_sym, val)
end
Expand Down
3 changes: 2 additions & 1 deletion resources/ibm-credentials.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
VISUAL_RECOGNITION_APIKEY=mesSi
VISUAL_RECOGNITION_URL=https://gateway.ronaldo.com
VISUAL_RECOGNITION_DISABLE_SSL=true
WATSON_APIKEY=salaH
WATSON_URL=https://gateway-mo.stevieG.net/watson/api
RONALDO_USERNAME=apikey
Expand All @@ -12,4 +13,4 @@ LEO_MESSI_AUTH_TYPE=bearerToken
WRONG_APIKEY={key
RED_SOX_AUTH_TYPE=noAuth
MY_SERVICE_APIKEY=mesSi
MY_SERVICE_AUTH_URL=https://my.link/identity/token
MY_SERVICE_AUTH_URL=https://my.link/identity/token
171 changes: 155 additions & 16 deletions resources/vcap-testing.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,159 @@
{
"salah": [
{
"credentials": {
"password": "password",
"url": "https://gateway.net/salah/api",
"username": "mo"
},
"label": "goat",
"name": "salah",
"plan": "standard",
"tags": [
"watson",
"ibm_created",
"ibm_dedicated_public",
"lite"
]
}
{
"credentials": {
"password": "password",
"url": "https://gateway.net/salah/api",
"username": "mo"
},
"label": "goat",
"name": "salah",
"plan": "standard",
"tags": [
"watson",
"ibm_created",
"ibm_dedicated_public",
"lite"
]
}
],
"visual_recognition": [
{
"credentials": {
"password": "password",
"url": "https://gateway.net/visual_recognition/api",
"username": "mo"
},
"label": "goat",
"name": "visual_recognition",
"plan": "standard",
"tags": [
"watson",
"ibm_created",
"ibm_dedicated_public",
"lite"
]
}
],
"devops_insights": [
{
"credentials": {
"password": "password",
"url": "https://gateway.net/visual_recognition/api",
"username": "mo"
},
"label": "goat",
"name": "devops_insights_1",
"plan": "standard",
"tags": [
"watson",
"ibm_created",
"ibm_dedicated_public",
"lite"
]
},
{
"credentials": {
"password": "password",
"url": "https://ibmtesturl.net/devops_insights/api",
"username": "mo"
},
"label": "goat",
"name": "devops_insights",
"plan": "standard",
"tags": [
"watson",
"ibm_created",
"ibm_dedicated_public",
"lite"
]
}
],
"no_matching_name": [
{
"name": "different_name_two",
"label": "different_name_two",
"plan": "different_name__two_plan",
"credentials": {
"url": "https://gateway.watsonplatform.net/different-name-two/api",
"username": "not-a-username",
"password": "not-a-password"
}
},
{
"name": "different_name",
"label": "different_name",
"plan": "different_name_plan",
"credentials": {
"url": "https://gateway.watsonplatform.net/different-name/api",
"username": "not-a-username",
"password": "not-a-password"
}
}
],
"key_to_service_entry_1": [
{
"name": "service_entry_key_and_key_to_service_entries",
"label": "devops-insights",
"plan": "standard",
"credentials": {
"url": "https://on.the.toolchainplatform.net/devops-insights/api",
"username": "not-a-username",
"password": "not-a-password"
}
},
{
"name": "service_entry_key_and_key_to_service_entries_two",
"label": "devops-insights",
"plan": "standard",
"credentials": {
"url": "https://on.the.toolchainplatform.net/devops-insights-2/api",
"username": "not-a-username",
"password": "not-a-password"
}
}
],
"key_to_service_entry_2": [
{
"label": "devops-insights",
"plan": "standard",
"credentials": {
"url": "https://on.the.toolchainplatform.net/devops-insights-3/api",
"username": "not-a-username-3",
"password": "not-a-password-3"
}
}
],
"service_entry_key_and_key_to_service_entries": [
{
"name": "service_entry_key-2",
"label": "devops-insights",
"plan": "elite",
"credentials": {
"url": "https://on.the.toolchainplatform.net/devops-insights-2/api",
"username": "not-a-username-2",
"password": "not-a-password-2"
}
}
],
"empty_service": [

],
"no-creds-service": [
{
"name": "no-creds-service-one",
"label": "devops-insights",
"plan": "elite",
"credentials": {
"url": "https://on.the.toolchainplatform.net/no-creds-service-one/api",
"username": "not-a-username-2",
"password": "not-a-password-2"
}
},
{
"name": "no-creds-service-two",
"label": "devops-insights",
"plan": "elite"
}
]
}
Loading

0 comments on commit fcdab84

Please sign in to comment.