Top Level Namespace
Defined Under Namespace
Modules: IBMCloudSdkCore
Constant Summary collapse
- DEFAULT_CREDENTIALS_FILE_NAME =
"ibm-credentials.env"
- NORMALIZER =
Custom URI normalizer when using HTTP Client
lambda do |uri| # Custom URI normalizer when using HTTP Client HTTP::URI.parse uri end
Instance Method Summary collapse
- #check_bad_first_or_last_char(str) ⇒ Object
-
#explicitly_true(value) ⇒ Object
checks if the provided value is truthy.
- #get_service_properties(service_name) ⇒ Object
-
#load_from_credential_file(service_name, separator = "=") ⇒ Object
Initiates the credentials based on the credential file.
- #load_from_environment_variables(service_name) ⇒ Object
- #load_from_vcap_services(service_name) ⇒ Object
- #parse_key(key, service_name) ⇒ Object
Instance Method Details
#check_bad_first_or_last_char(str) ⇒ Object
15 16 17 |
# File 'lib/ibm_cloud_sdk_core/utils.rb', line 15 def check_bad_first_or_last_char(str) return str.start_with?("{", "\"") || str.end_with?("}", "\"") unless str.nil? end |
#explicitly_true(value) ⇒ Object
checks if the provided value is truthy
20 21 22 23 24 |
# File 'lib/ibm_cloud_sdk_core/utils.rb', line 20 def explicitly_true(value) return value.to_s.casecmp("true").zero? unless value.nil? false end |
#get_service_properties(service_name) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/ibm_cloud_sdk_core/utils.rb', line 5 def get_service_properties(service_name) # - 1) Credential file config = load_from_credential_file(service_name) # - 2) Environment variables config = load_from_environment_variables(service_name) if config.nil? || config.empty? # - 3) VCAP_SERVICES env variable config = load_from_vcap_services(service_name) if config.nil? || config.empty? config end |
#load_from_credential_file(service_name, separator = "=") ⇒ Object
Initiates the credentials based on the credential file
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ibm_cloud_sdk_core/utils.rb', line 27 def load_from_credential_file(service_name, separator = "=") credential_file_path = ENV["IBM_CREDENTIALS_FILE"] # Top-level directory of the project if credential_file_path.nil? file_path = File.join(File.dirname(__FILE__), "/../../" + DEFAULT_CREDENTIALS_FILE_NAME) credential_file_path = file_path if File.exist?(file_path) end # Home directory if credential_file_path.nil? file_path = ENV["HOME"] + "/" + DEFAULT_CREDENTIALS_FILE_NAME credential_file_path = file_path if File.exist?(file_path) end return if credential_file_path.nil? file_contents = File.open(credential_file_path, "r") config = {} file_contents.each_line do |line| key_val = line.strip.split(separator, 2) if key_val.length == 2 && !line.start_with?("#") key = parse_key(key_val[0].downcase, service_name) unless key_val[0].nil? config.store(key.to_sym, key_val[1]) unless key.nil? end end config end |
#load_from_environment_variables(service_name) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/ibm_cloud_sdk_core/utils.rb', line 56 def load_from_environment_variables(service_name) config = {} ENV.each do |key, val| parsed_key = parse_key(key.downcase, service_name) unless key.nil? config.store(parsed_key.to_sym, val) unless parsed_key.nil? end config end |
#load_from_vcap_services(service_name) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ibm_cloud_sdk_core/utils.rb', line 69 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 = {} # 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 config[:auth_type] = "basic" if !config[:username].nil? && !config[:password].nil? config[:auth_type] = "iam" unless config[:apikey].nil? return config end nil end |
#parse_key(key, service_name) ⇒ Object
65 66 67 |
# File 'lib/ibm_cloud_sdk_core/utils.rb', line 65 def parse_key(key, service_name) key[service_name.length + 1, key.length] if key.include?(service_name) end |