fluke/devops-service/app/api3/helpers/query.rb

67 lines
1.7 KiB
Ruby
Raw Normal View History

2014-12-12 17:00:06 +03:00
require "json"
require 'sinatra/base'
require "sinatra/json"
2018-04-04 22:44:39 +03:00
require "db/mongo/models/statistic"
2014-12-12 17:00:06 +03:00
2014-12-15 14:26:54 +03:00
module Devops
2018-04-04 22:44:39 +03:00
module API3
2014-12-12 17:00:06 +03:00
module Helpers
2015-08-03 15:09:04 +03:00
include Sinatra::JSON
2014-12-12 17:00:06 +03:00
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_provider provider
2018-04-04 22:44:39 +03:00
list = ::Provider.providers
2015-11-02 17:33:25 +03:00
halt_response("Invalid provider '#{provider}', available providers: '#{list.join("', '")}'", 400) unless list.include?(provider)
2014-12-12 17:00:06 +03:00
end
2018-04-04 22:44:39 +03:00
def check_provider_account provider, account
halt_response("Provider acount '#{account}' not found", 400) if ::Provider.get_connector(provider, account).nil?
end
def check_policy policy
return unless policy
user = request.env['USER']
user.check_policy(policy)
end
2014-12-12 17:00:06 +03:00
# Save information about requests with methods POST, PUT, DELETE
2015-09-17 13:15:19 +03:00
def insert_statistic msg=nil
2014-12-12 17:00:06 +03:00
unless request.get?
2018-04-04 22:44:39 +03:00
return if response.status == 401 and request.env['REMOTE_USER'].nil?
2015-09-29 16:37:59 +03:00
# parse body
2015-09-29 16:16:59 +03:00
request.body.rewind
2015-09-29 16:37:59 +03:00
raw_body = request.body.read
body = begin
::JSON.parse(raw_body)
rescue ::JSON::ParserError
raw_body
end
2018-04-04 22:44:39 +03:00
Devops::Model::Statistic.create({
user: request.env['REMOTE_USER'],
path: request.path,
method: request.request_method,
body: body,
response_code: response.status
})
2014-12-12 17:00:06 +03:00
end
end
end
end
end