lib/app42/base/util.rb in app42-0.5.5 vs lib/app42/base/util.rb in app42-0.5.6

- old
+ new

@@ -46,11 +46,11 @@ def json_parse(str) JSON.parse(str) if str end - def request_failed?(status) + 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 @@ -58,12 +58,10 @@ def parse_error_message(status, body) if status && body && body["message"] && body["details"] desc = body["details"].gsub("\"","'") # TODO, may need later # app42_client_info - # FIXME, should be proper message - # message "#{body["message"]}: #{body["details"]}", true, 'red' message "#{body["details"]}", true, 'red' exit! else # TODO, may need later # app42_client_info @@ -94,11 +92,11 @@ # build resource url as per requested params def resource_url resource, what what.nil? ? (resource_url = "/#{resource}") : (resource_url = "/#{resource}/#{what}") end - # will build signature using requested params and Secret key + # Generate signature using requested params and Secret key # # @params +requested params+ # # @ return +signature+ def signature params @@ -164,10 +162,53 @@ message "#{Message::LATEST_S_INTERRUPT}", true, 'red' exit! end end + # checks transaction status of setup cloud API + # + #REVIEW, need to verify FAILED use case + def check_transaction_status_of_setup transaction_id, previous_completed, what + begin + flag = false + message "#{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 + message "#{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 "#{Message::REQUEST_PROGRESS}", true, 'red' + exit! + end + end + else response["success"] == true && response["transactionStatus"] == "FAILED" + print_new_line + message "#{response["message"]}", true, 'red' + exit! + end + sleep 5 + end + rescue Interrupt + message "#{Message::LATEST_S_INTERRUPT}", true, 'red' + exit! + end + end + # rest call for transaction status check def status_call transaction_id begin query_params = params query_params.store('transactionId', transaction_id) @@ -242,23 +283,35 @@ def time_valid? num ((!numeric_including_zero? num) || (num.to_i > 720)) ? (return false) : (return num) end # Check whether +app OR service+ name is valid OR not - # +app OR service+ name lenght should not be more than 30 character + # +app OR service+ name length should not be more than 30 character # And should not contain any special character def validate_app_and_service_name name, str if str.match(App42::REGEX) || str.length > 30 message "#{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 + # 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 + def validate_setup_name name, str + if str.match(App42::REGEX) || str.length > 30 + message "#{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 + # Check whether +database name+ is valid OR not - # +database name+ lenght should not be more than 64 character + # +database name+ length should not be more than 64 character # And should not contain any special character def validate_database_name name, str if str.match(App42::DBNAME_REGEX) || str.length > 64 || (numeric_including_zero? str) message "Invalid database name. Should be less than 64 characters (Alphabets, alphanumeric and underscore(_) is allowed).", true, 'red' return false @@ -268,10 +321,22 @@ else return str end end + # Check whether +vm config+ is valid OR not + # +vm config+ should be a valid number + # And should not contain any special character + def validate_vm_config kontena + unless numeric?(kontena) + message "#{Message::NOT_A_VALID_KONTENA}", true, 'red' + false + else + return kontena + end + end + # rest call to server to check whether +application+ exist OR not. # return true if +application+ exist else display ERROR message and exit def is_app_exist? app_name query_params = params query_params.store('appName', app_name) @@ -297,9 +362,35 @@ unless response["success"] return true else message "Service with the name #{service_name} does not exist.", true, 'red' exit! + end + end + + # rest call to server to check whether +setup name+ exist OR not. + # return true if +setup name+ exist else display ERROR message and exit + 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 + message "Setup name with the name #{setup_name} does not exist.", true, 'red' + exit! + end + end + + # Check whether +git URL+ is valid OR not + # given +git URL+ must end with +.git+ extension + def validate_git_url git_url + unless git_url.include?('.git') + message "#{Message::GIT_URL_NOT_VALID}", true, 'red' + return false + else + return git_url end end end end \ No newline at end of file