99 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			99 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| require "json"
 | |
| require 'sinatra/base'
 | |
| require "sinatra/json"
 | |
| 
 | |
| require "providers/provider_factory"
 | |
| 
 | |
| module Devops
 | |
|   module API2_0
 | |
|     module Helpers
 | |
| 
 | |
|       def create_response msg, obj=nil, rstatus=200
 | |
|         logger.info(msg)
 | |
|         status rstatus
 | |
|         obj = {} if obj.nil?
 | |
|         obj[:message] = msg
 | |
|         json(obj)
 | |
|       end
 | |
| 
 | |
|       def halt_response msg, rstatus=400
 | |
|         obj = {:message => msg}
 | |
|         halt(rstatus, json(obj))
 | |
|       end
 | |
| 
 | |
|       def check_privileges cmd, p
 | |
|         # somewhy REMOTE_USER is missing
 | |
|         user = request.env['USER']
 | |
|         user.check_privileges(cmd, p)
 | |
|       end
 | |
| 
 | |
|       def check_provider provider
 | |
|         list = ::Provider::ProviderFactory.providers
 | |
|         halt_response("Invalid provider '#{provider}', available providers: '#{list.join("', '")}'", 404) unless list.include?(provider)
 | |
|       end
 | |
| 
 | |
|       def create_object_from_json_body type=Hash, empty_body=false
 | |
|         json = request.body.read.strip
 | |
|         return nil if json.empty? and empty_body
 | |
|         @body_json = begin
 | |
|           ::JSON.parse(json)
 | |
|         rescue ::JSON::ParserError => e
 | |
|           logger.error e.message
 | |
|           halt_response("Invalid JSON: #{e.message}")
 | |
|         end
 | |
|         halt_response("Invalid JSON, it should be an #{type == Array ? "array" : "object"}") unless @body_json.is_a?(type)
 | |
|         @body_json
 | |
|       end
 | |
| 
 | |
|       def check_string val, msg, _nil=false, empty=false
 | |
|         check_param val, String, msg, _nil, empty
 | |
|       end
 | |
| 
 | |
|       def check_array val, msg, vals_type=String, _nil=false, empty=false
 | |
|         check_param val, Array, msg, _nil, empty
 | |
|         val.each {|v| halt_response(msg) unless v.is_a?(vals_type)} unless val.nil?
 | |
|         val
 | |
|       end
 | |
| 
 | |
|       def check_filename file_name, not_string_msg, json_resp=true
 | |
|         check_string file_name, not_string_msg
 | |
|         r = Regexp.new("^[\\w _\\-.]{1,255}$", Regexp::IGNORECASE)
 | |
|         if r.match(file_name).nil?
 | |
|           msg = "Invalid file name '#{file_name}'. Expected name with 'a'-'z', '0'-'9', ' ', '_', '-', '.' symbols with length greate then 0 and less then 256 "
 | |
|           if json_resp
 | |
|             halt_response(msg)
 | |
|           else
 | |
|             halt(400, msg)
 | |
|           end
 | |
|         end
 | |
|         file_name
 | |
|       end
 | |
| 
 | |
|       def check_param val, type, msg, _nil=false, empty=false
 | |
|         if val.nil?
 | |
|           if _nil
 | |
|             return val
 | |
|           else
 | |
|             halt_response(msg)
 | |
|           end
 | |
|         end
 | |
|         if val.is_a?(type)
 | |
|           halt_response(msg) if val.empty? and !empty
 | |
|           val
 | |
|         else
 | |
|           halt_response(msg)
 | |
|         end
 | |
|       end
 | |
| 
 | |
|       # Save information about requests with methods POST, PUT, DELETE
 | |
|       def statistic msg=nil
 | |
|         unless request.get?
 | |
|           settings.mongo.statistic request.env['REMOTE_USER'], request.path, request.request_method, @body_json, response.status
 | |
|         end
 | |
|       end
 | |
| 
 | |
|     end
 | |
|   end
 | |
| end
 | |
| 
 | 
