| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | require "json" | 
					
						
							|  |  |  | require "db/exceptions/record_not_found" | 
					
						
							|  |  |  | require "db/exceptions/invalid_record" | 
					
						
							|  |  |  | require "exceptions/dependency_error" | 
					
						
							|  |  |  | require "exceptions/invalid_privileges" | 
					
						
							|  |  |  | require "fog" | 
					
						
							|  |  |  | require "logger" | 
					
						
							|  |  |  | require "providers/provider_factory" | 
					
						
							|  |  |  | require "sinatra/json" | 
					
						
							|  |  |  | require "sinatra/base" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Version2_0 | 
					
						
							|  |  |  |   # Basic class for devops routes classes | 
					
						
							|  |  |  |   class BaseRoutes < Sinatra::Base | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     helpers do | 
					
						
							|  |  |  |       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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-14 11:31:21 +04:00
										 |  |  |       def check_privileges cmd, p | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |         BaseRoutes.mongo.check_user_privileges(request.env['REMOTE_USER'], cmd, p) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def check_provider provider | 
					
						
							| 
									
										
										
										
											2014-06-18 15:11:47 +04:00
										 |  |  |         list = ::Provider::ProviderFactory.providers | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |         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 => e | 
					
						
							|  |  |  |           logger.error e.message | 
					
						
							|  |  |  |           logger.debug(json) | 
					
						
							|  |  |  |           halt_response("Invalid JSON") | 
					
						
							|  |  |  |         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? | 
					
						
							|  |  |  |           BaseRoutes.mongo.statistic request.env['REMOTE_USER'], request.path, request.request_method, @body_json, response.status | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |