| 
									
										
										
										
											2014-12-12 17:00:06 +03:00
										 |  |  | require "json" | 
					
						
							|  |  |  | require 'sinatra/base' | 
					
						
							|  |  |  | require "sinatra/json" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "providers/provider_factory" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 14:26:54 +03:00
										 |  |  | module Devops | 
					
						
							| 
									
										
										
										
											2014-12-12 17:00:06 +03:00
										 |  |  |   module Version2_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 | 
					
						
							| 
									
										
										
										
											2015-07-06 14:23:36 +03:00
										 |  |  |         # somewhy REMOTE_USER is missing | 
					
						
							|  |  |  |         user = request.env['HTTP_REMOTE_USER'] || request.env['REMOTE_USER'] | 
					
						
							|  |  |  |         settings.mongo.check_user_privileges(user, cmd, p) | 
					
						
							| 
									
										
										
										
											2014-12-12 17:00:06 +03:00
										 |  |  |       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 | 
					
						
							|  |  |  | 
 |