Module: App42::Base::Util
- Included in:
- Client::App42RestClient, Command::Auth, Command::Base, Command::User
- Defined in:
- lib/app42/base/util.rb
Instance Method Summary collapse
-
#app42_client_info ⇒ Object
return client info
ruby-version
,os
andclient-version
details. -
#ask_app_name ⇒ Object
ask application name to user.
-
#camel_case_to_whitespace(str) ⇒ Object
camel case to whitespace.
-
#check_transaction_status(transaction_id, previous_completed, what) ⇒ Object
checks transaction status.
-
#check_transaction_status_of_setup(transaction_id, previous_completed, what) ⇒ Object
checks transaction status of setup cloud API.
-
#escape_path(path) ⇒ Object
Escape white space and special character from path as per OS.
-
#get_flavour_for_upgrade_or_downgrade(setup_name, type) ⇒ Object
Get flavour for upgrade.
-
#get_instance_config_for_upgrade_or_downgrade(iaas, vm_type, setup_name, type) ⇒ Object
Get instance configuration.
-
#input(message, choices, indexed = true) ⇒ Object
Reading input from user.
-
#ip_address_valid?(source_ip) ⇒ Boolean
validate whether the value in the variable is really an IP.
-
#is_app_exist?(app_name) ⇒ Boolean
rest call to server to check whether
application
exist OR not. -
#is_service_exist?(service_name) ⇒ Boolean
rest call to server to check whether
service
exist OR not. -
#is_setup_name_exist?(setup_name) ⇒ Boolean
rest call to server to check whether setup name exist OR not.
-
#is_static_ip_assigned?(gpaas_name) ⇒ Boolean
rest call to server to check whether Static IP assigned OR not.
-
#json_parse(str) ⇒ Object
json parser.
-
#message(msg, new_line = true, color) ⇒ Object
console message.
-
#number_valid?(num) ⇒ Boolean
Check whether number is valid number.
-
#numeric?(obj) ⇒ Boolean
regex for number excluding zero.
-
#numeric_including_zero?(obj) ⇒ Boolean
regex for number including zero.
-
#params ⇒ Object
hash of params that will return API key, version and timestamp.
-
#parse_error_message(status, body) ⇒ Object
error message parser.
-
#print_new_line ⇒ Object
:nodoc:.
-
#request_failed?(status) ⇒ Boolean
:nodoc:.
-
#resource_url(resource, what) ⇒ Object
build resource url as per requested params.
-
#show_wait_spinner(fps = 10) ⇒ Object
Spinning code for latest status.
-
#signature(params) ⇒ Object
Generate signature using requested params and Secret key.
-
#status_call(transaction_id) ⇒ Object
rest call for transaction status check.
-
#time_valid?(num) ⇒ Boolean
Check whether
access_time
is valid access time should be a valid number and not be more than720
hours. -
#util_base ⇒ Object
return util instance.
-
#validate_app_and_service_name(name, str) ⇒ Object
Check whether app OR service name is valid OR not app OR service name length should not be more than 30 character And should not contain any special character.
-
#validate_database_name(name, str) ⇒ Object
Check whether database name is valid OR not database name length should not be more than 64 character And should not contain any special character.
-
#validate_git_url(git_url) ⇒ Object
Check whether git URL is valid OR not given git URL must end with
.git
extension. -
#validate_setup_name(name, str) ⇒ Object
Check whether
setup
name is valid OR notsetup
name length should not be more than 30 character And should not contain any special character. -
#validate_upload_backup_path(path) ⇒ Object
Check whether
uploadbackup
path is valid OR not predefined extension are allowed only like .sql, .zip, .dump etc. -
#validate_url(url) ⇒ Object
Check whether
URL
is valid OR notURL
should start withhttp
andhttps
And should not contain any special character. -
#validate_vm_config(kontena) ⇒ Object
Check whether vm config is valid OR not vm config should be a valid number And should not contain any special character.
Instance Method Details
#app42_client_info ⇒ Object
return client info ruby-version
, os
and client-version
details
76 77 78 |
# File 'lib/app42/base/util.rb', line 76 def app42_client_info puts Paint["INFO: #{App42.user_agent}", :red] end |
#ask_app_name ⇒ Object
ask application name to user
10 11 12 13 |
# File 'lib/app42/base/util.rb', line 10 def ask_app_name input "Enter App Name", [], true print_new_line end |
#camel_case_to_whitespace(str) ⇒ Object
camel case to whitespace
258 259 260 261 262 263 264 265 266 |
# File 'lib/app42/base/util.rb', line 258 def camel_case_to_whitespace str str_cap = str.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_").downcase str_capitalize = str_cap.tr("_", " ").capitalize str_capitalize.split(' ').map(&:capitalize).join(' ') end |
#check_transaction_status(transaction_id, previous_completed, what) ⇒ Object
checks transaction status
REVIEW, need to verify FAILED use case
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/app42/base/util.rb', line 129 def check_transaction_status transaction_id, previous_completed, what begin flag = false "#{Message::LATEST_S_WAIT}", false, 'green' while flag == false do response = status_call transaction_id re_try ||= 1 if response["success"] == true && response["transactionStatus"] == "COMPLETED" print_new_line "#{response["message"]}", true, 'green' return true elsif response["success"] == true && response["transactionStatus"] == "INPROGRESS" if previous_completed != response["completed"] print_new_line puts Paint["#{response["completed"]} out of #{response["requested"]} #{what}", :green] previous_completed = response["completed"] end show_wait_spinner{ sleep rand(4)+2 } unless what.to_s == 'Uploaded' if(re_try += 1 ) >= 25 "#{Message::REQUEST_PROGRESS}", true, 'red' exit! end end else response["success"] == true && response["transactionStatus"] == "FAILED" print_new_line "#{response["message"]}", true, 'red' exit! end sleep 5 end rescue Interrupt "#{Message::LATEST_S_INTERRUPT}", true, 'red' exit! end end |
#check_transaction_status_of_setup(transaction_id, previous_completed, what) ⇒ Object
checks transaction status of setup cloud API
REVIEW, need to verify FAILED use case
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/app42/base/util.rb', line 172 def check_transaction_status_of_setup transaction_id, previous_completed, what begin flag = false "#{Message::LATEST_S_WAIT}", false, 'green' while flag == false do response = status_call transaction_id re_try ||= 1 if response["success"] == true && response["transactionStatus"] == "COMPLETED" print_new_line "#{response["message"]}", true, 'green' return true elsif response["success"] == true && response["transactionStatus"] == "INPROGRESS" if previous_completed != response["completed"] print_new_line puts Paint["#{response["completed"]} out of #{response["requested"]} #{what}", :green] previous_completed = response["completed"] end show_wait_spinner{ sleep rand(4)+2 } unless what.to_s == 'Uploaded' if(re_try += 1 ) >= 250 "#{Message::REQUEST_PROGRESS}", true, 'red' exit! end end else response["success"] == true && response["transactionStatus"] == "FAILED" print_new_line "#{response["message"]}", true, 'red' exit! end sleep 5 end rescue Interrupt "#{Message::LATEST_S_INTERRUPT}", true, 'red' exit! end end |
#escape_path(path) ⇒ Object
Escape white space and special character from path as per OS
228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/app42/base/util.rb', line 228 def escape_path(path) path = File.(path) if RUBY_PLATFORM =~ /mingw|mswin32|cygwin/ if path.include?(' ') #FIXME, should be universal for window os # return '"' + path + '"' return path.gsub(' ', '\ ') else return path end else return path.gsub(' ', '\ ') end end |
#get_flavour_for_upgrade_or_downgrade(setup_name, type) ⇒ Object
Get flavour for upgrade
433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/app42/base/util.rb', line 433 def get_flavour_for_upgrade_or_downgrade setup_name, type flavour_hash = {} flavour_type = App42::Command::Config.new.get_flavour_type_fm_server_for_upgrade_or_downgrade setup_name, type flavour_type['flavours'].select {|each_flavour| flavour_hash["#{each_flavour['id']}"] = each_flavour['name']} flavour = input "Select Flavour", flavour_hash.values, true flavour_id = nil flavour_hash.each_pair{|fl| flavour_id = fl[0] if fl[1] == flavour} return flavour_id end |
#get_instance_config_for_upgrade_or_downgrade(iaas, vm_type, setup_name, type) ⇒ Object
Get instance configuration
446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/app42/base/util.rb', line 446 def get_instance_config_for_upgrade_or_downgrade iaas, vm_type, setup_name, type instance_config_hash = {} instance_config_type = App42::Command::Config.new.get_instance_config_for_upgrade_or_downgrade iaas, vm_type, setup_name, type instance_config_type['IMList'].select {|each_instance_config| instance_config_hash["#{each_instance_config['id']}"] = each_instance_config['name']} instance_config = input "Select Instance Configuration", instance_config_hash.values, true instance_config_id = nil instance_config_hash.each_pair{|ic| instance_config_id = ic[0] if ic[1] == instance_config} return instance_config_id end |
#input(message, choices, indexed = true) ⇒ Object
Reading input from user
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/app42/base/util.rb', line 23 def input , choices, indexed = true if choices.empty? ans = ask Paint["#{}", :cyan] else list = choices.compact unless choices.empty? ans = ask Paint["#{}", :cyan], :choices => list, :default => list[0], :indexed => indexed end print_new_line return ans end |
#ip_address_valid?(source_ip) ⇒ Boolean
validate whether the value in the variable is really an IP
269 270 271 272 273 274 275 |
# File 'lib/app42/base/util.rb', line 269 def ip_address_valid? source_ip if source_ip == "0.0.0.0" || source_ip.match(App42::IP_REGEX) true else false end end |
#is_app_exist?(app_name) ⇒ Boolean
rest call to server to check whether application
exist OR not. return true if application
exist else display ERROR message and exit
366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/app42/base/util.rb', line 366 def is_app_exist? app_name query_params = params query_params.store('appName', app_name) response = build_get_request query_params, 'app', 'availability' unless response["success"] return true else "App with name '#{app_name}' does not exist.", true, 'red' exit! end end |
#is_service_exist?(service_name) ⇒ Boolean
rest call to server to check whether service
exist OR not. return true if service
exist else display ERROR message and exit
382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/app42/base/util.rb', line 382 def is_service_exist? service_name query_params = params query_params.store('serviceName', service_name) response = build_get_request query_params, 'service', 'availability' unless response["success"] return true else "Service with name '#{service_name}' does not exist.", true, 'red' exit! end end |
#is_setup_name_exist?(setup_name) ⇒ Boolean
rest call to server to check whether setup name exist OR not. return true if setup name exist else display ERROR message and exit
398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/app42/base/util.rb', line 398 def is_setup_name_exist? setup_name query_params = params query_params.store('setupName', setup_name) response = build_get_request query_params, 'setup', 'availability' unless response["success"] return true else "Setup with name '#{setup_name}' does not exist.", true, 'red' exit! end end |
#is_static_ip_assigned?(gpaas_name) ⇒ Boolean
rest call to server to check whether Static IP assigned OR not. return true if Static IP assigned else false
413 414 415 416 417 418 419 |
# File 'lib/app42/base/util.rb', line 413 def is_static_ip_assigned? gpaas_name query_params = params query_params.store('setupName', gpaas_name) setup_info = build_get_request query_params, 'gpaas', "#{@options[:setup]}" setup_info["setupInfo"]["staticIP"] == "NULL" ? (return false) : (return true) end |
#json_parse(str) ⇒ Object
json parser
48 49 50 |
# File 'lib/app42/base/util.rb', line 48 def json_parse(str) JSON.parse(str) if str end |
#message(msg, new_line = true, color) ⇒ Object
console message
43 44 45 |
# File 'lib/app42/base/util.rb', line 43 def msg, new_line = true, color new_line ? (puts Paint["#{msg}\n", color.to_sym]) : (print Paint["#{msg}", color.to_sym]) end |
#number_valid?(num) ⇒ Boolean
Check whether number is valid number
278 279 280 |
# File 'lib/app42/base/util.rb', line 278 def number_valid? num ((!numeric_including_zero? num) || (num.to_s =~ App42::REGEX) || (num.to_i == 0)) ? (return false) : (return num) end |
#numeric?(obj) ⇒ Boolean
regex for number excluding zero
248 249 250 |
# File 'lib/app42/base/util.rb', line 248 def numeric?(obj) obj.to_s.match(/^[-+]?[1-9]*\.?[1-9]+$/) == nil ? false : true end |
#numeric_including_zero?(obj) ⇒ Boolean
regex for number including zero
253 254 255 |
# File 'lib/app42/base/util.rb', line 253 def numeric_including_zero?(obj) obj.to_s.match(/^[-+]?[0-9]*\.?[0-9]+$/) == nil ? false : true end |
#params ⇒ Object
hash of params that will return API key, version and timestamp
86 87 88 89 90 91 92 |
# File 'lib/app42/base/util.rb', line 86 def params params = { 'apiKey'=> @api_key, 'version' => VERSION, 'timeStamp' => util_base., } end |
#parse_error_message(status, body) ⇒ Object
error message parser
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/app42/base/util.rb', line 60 def (status, body) if status && body && body["message"] && body["details"] desc = body["details"].gsub("\"","'") # TODO, may need later # app42_client_info "#{body["details"]}", true, 'red' exit! else # TODO, may need later # app42_client_info "Error: Something wrong", true, 'red' exit! end end |
#print_new_line ⇒ Object
:nodoc:
243 244 245 |
# File 'lib/app42/base/util.rb', line 243 def print_new_line #:nodoc: puts "\n" end |
#request_failed?(status) ⇒ Boolean
:nodoc:
53 54 55 56 57 |
# File 'lib/app42/base/util.rb', line 53 def request_failed?(status) #:nodoc: # TODO, if more than one error code # APP42_HTTP_ERROR_CODES.detect{|error_code| status >= error_code} APP42_HTTP_ERROR_CODES.include? status end |
#resource_url(resource, what) ⇒ Object
build resource url as per requested params
95 96 97 |
# File 'lib/app42/base/util.rb', line 95 def resource_url resource, what what.nil? ? (resource_url = "/#{resource}") : (resource_url = "/#{resource}/#{what}") end |
#show_wait_spinner(fps = 10) ⇒ Object
Spinning code for latest status
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/app42/base/util.rb', line 109 def show_wait_spinner(fps=10) chars = %w[| / - \\] delay = 1.0/fps iter = 0 spinner = Thread.new do while iter do print chars[(iter+=1) % chars.length] sleep delay print "\b" end end yield.tap{ iter = false spinner.join } end |
#signature(params) ⇒ Object
Generate signature using requested params and Secret key
@ return signature
104 105 106 |
# File 'lib/app42/base/util.rb', line 104 def signature params signature = util_base.sign(@secret_key, params ) end |
#status_call(transaction_id) ⇒ Object
rest call for transaction status check
213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/app42/base/util.rb', line 213 def status_call transaction_id begin query_params = params query_params.store('transactionId', transaction_id) @same_status_retry ||= 1 response = get_request signature(query_params), resource_url('info', 'transaction'), query_params return response rescue Interrupt "#{Message::LATEST_S_INTERRUPT}", true, 'red' exit! end end |
#time_valid?(num) ⇒ Boolean
Check whether access_time
is valid access time should be a valid number and not be more than 720
hours
285 286 287 |
# File 'lib/app42/base/util.rb', line 285 def time_valid? num ((!numeric_including_zero? num) || (num.to_i > 720)) ? (return false) : (return num) end |
#util_base ⇒ Object
return util instance
81 82 83 |
# File 'lib/app42/base/util.rb', line 81 def util_base util = App42::Client::RestUtil.new end |
#validate_app_and_service_name(name, str) ⇒ Object
Check whether app OR service name is valid OR not app OR service name length should not be more than 30 character And should not contain any special character
292 293 294 295 296 297 298 299 |
# File 'lib/app42/base/util.rb', line 292 def validate_app_and_service_name name, str if str.match(App42::REGEX) || str.length > 30 "#{name} should not contain any special character or white space and length should be less than 30.", true, 'red' return false else return str end end |
#validate_database_name(name, str) ⇒ Object
Check whether database name is valid OR not database name length should not be more than 64 character And should not contain any special character
340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/app42/base/util.rb', line 340 def validate_database_name name, str if str.match(App42::DBNAME_REGEX) || str.length > 64 || (numeric_including_zero? str) "Invalid database name. Should be less than 64 characters (Alphabets, alphanumeric and underscore(_) is allowed).", true, 'red' return false elsif App42::DATABASE_NAME_NOT_ALLOWED.include? str "Database Name should not be '#{str}'.", true, 'red' return false else return str end end |
#validate_git_url(git_url) ⇒ Object
Check whether git URL is valid OR not given git URL must end with .git
extension
423 424 425 426 427 428 429 430 |
# File 'lib/app42/base/util.rb', line 423 def validate_git_url git_url unless git_url.include?('.git') "#{Message::GIT_URL_NOT_VALID}", true, 'red' return false else return git_url end end |
#validate_setup_name(name, str) ⇒ Object
Check whether setup
name is valid OR not setup
name length should not be more than 30 character And should not contain any special character
328 329 330 331 332 333 334 335 |
# File 'lib/app42/base/util.rb', line 328 def validate_setup_name name, str if str.match(App42::REGEX) || str.length > 30 "#{name} should not contain any special character or white space and length should be less than 30.", true, 'red' return false else return str end end |
#validate_upload_backup_path(path) ⇒ Object
Check whether uploadbackup
path is valid OR not predefined extension are allowed only like .sql, .zip, .dump etc
316 317 318 319 320 321 322 323 |
# File 'lib/app42/base/util.rb', line 316 def validate_upload_backup_path path if path.include?('.zip') || path.include?('.sql') || path.include?('.tar.gz') || path.include?('.gzip') || path.include?('.dump') || path.include?('.rdb') return path else "Please provide valid file.", true, 'red' return false end end |
#validate_url(url) ⇒ Object
Check whether URL
is valid OR not URL
should start with http
and https
And should not contain any special character
304 305 306 307 308 309 310 311 312 |
# File 'lib/app42/base/util.rb', line 304 def validate_url url # Using the URI module distributed with Ruby, We can write own URL_REGEX(defined in constant.rb) too. unless url =~ /^#{URI::regexp}$/ "The provided URL (#{url}) is either not valid or contains white space and special character.", true, 'red' return false else return url end end |
#validate_vm_config(kontena) ⇒ Object
Check whether vm config is valid OR not vm config should be a valid number And should not contain any special character
355 356 357 358 359 360 361 362 |
# File 'lib/app42/base/util.rb', line 355 def validate_vm_config kontena unless numeric?(kontena) "#{Message::NOT_A_VALID_KONTENA}", true, 'red' false else return kontena end end |