sinatra methods_with_headers
This commit is contained in:
parent
bc06d015ed
commit
ad4c823251
@ -87,7 +87,7 @@ class User < MongoModel
|
||||
end
|
||||
|
||||
private
|
||||
def privileges_with_value v, options={}
|
||||
def privileges_with_value value, options={}
|
||||
privileges = {}
|
||||
[
|
||||
'flavor',
|
||||
|
||||
@ -22,6 +22,8 @@ require_relative "client"
|
||||
require_relative "report"
|
||||
require_relative "version"
|
||||
|
||||
require_relative "sinatra/methods_with_headers"
|
||||
|
||||
class DevopsService
|
||||
|
||||
include Wisper::Publisher
|
||||
@ -81,7 +83,6 @@ class DevopsService
|
||||
send(:generate_method, name, &block)
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
require "wisper_fix"
|
||||
|
||||
@ -2,6 +2,8 @@ require "socket"
|
||||
|
||||
class DevopsConfig
|
||||
|
||||
OBJECT_NAME = /[\w\-]+/
|
||||
|
||||
@@config = nil
|
||||
|
||||
class << self
|
||||
|
||||
@ -26,38 +26,6 @@ module Devops
|
||||
true
|
||||
end
|
||||
|
||||
# Check request headers
|
||||
def check_headers *headers
|
||||
ha = (headers.empty? ? [:accept, :content_type] : headers)
|
||||
ha.each do |h|
|
||||
case h
|
||||
when :accept, "accept"
|
||||
accept_json
|
||||
when :content_type, "content_type"
|
||||
request_json
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Check Accept header
|
||||
#
|
||||
# Can client works with JSON?
|
||||
def accept_json
|
||||
logger.debug(request.accept)
|
||||
unless request.accept? 'application/json'
|
||||
response.headers['Accept'] = 'application/json'
|
||||
halt_response("Accept header should contains 'application/json' type", 406)
|
||||
end
|
||||
rescue NoMethodError => e
|
||||
#error in sinatra 1.4.4 (https://github.com/sinatra/sinatra/issues/844, https://github.com/sinatra/sinatra/pull/805)
|
||||
response.headers['Accept'] = 'application/json'
|
||||
halt_response("Accept header should contains 'application/json' type", 406)
|
||||
end
|
||||
|
||||
# Check Content-Type header
|
||||
def request_json
|
||||
halt_response("Content-Type should be 'application/json'", 415) if request.media_type.nil? or request.media_type != 'application/json'
|
||||
end
|
||||
|
||||
def check_provider provider
|
||||
list = ::Provider::ProviderFactory.providers
|
||||
|
||||
@ -4,12 +4,6 @@ module Devops
|
||||
module FlavorRoutes
|
||||
|
||||
def self.registered(app)
|
||||
|
||||
app.before "/flavors/:provider" do
|
||||
check_headers :accept
|
||||
check_privileges("flavor", "r")
|
||||
check_provider(params[:provider])
|
||||
end
|
||||
# Get list of flavors for :provider
|
||||
#
|
||||
# * *Request*
|
||||
@ -37,7 +31,7 @@ module Devops
|
||||
# "disk": 20
|
||||
# }
|
||||
# ]
|
||||
app.get "/flavors/:provider", &Devops::Version2_0::Handler::Flavor.get_flavors
|
||||
app.get_with_headers "/flavors/:provider", :headers => [:accept], &Devops::Version2_0::Handler::Flavor.get_flavors
|
||||
|
||||
puts "Flavor routes initialized"
|
||||
end
|
||||
|
||||
@ -6,6 +6,8 @@ module Devops
|
||||
class Flavor
|
||||
def self.get_flavors
|
||||
lambda {
|
||||
check_privileges("flavor", "r")
|
||||
check_provider(params[:provider])
|
||||
p = ::Provider::ProviderFactory.get params[:provider]
|
||||
json p.flavors
|
||||
}
|
||||
|
||||
@ -10,6 +10,8 @@ module Devops
|
||||
|
||||
def self.get_images
|
||||
lambda {
|
||||
check_privileges("image", "r")
|
||||
check_provider(params[:provider]) if params[:provider]
|
||||
images = settings.mongo.images(params[:provider])
|
||||
json(images.map {|i| i.to_hash})
|
||||
}
|
||||
@ -17,18 +19,22 @@ module Devops
|
||||
|
||||
def self.get_provider_images
|
||||
lambda {
|
||||
check_privileges("image", "r")
|
||||
check_provider(params[:provider])
|
||||
json get_images(settings.mongo, params[:provider])
|
||||
}
|
||||
end
|
||||
|
||||
def self.get_image
|
||||
lambda {
|
||||
check_privileges("image", "r")
|
||||
json settings.mongo.image(params[:image_id])
|
||||
}
|
||||
end
|
||||
|
||||
def self.create_image
|
||||
lambda {
|
||||
check_privileges("image", "w")
|
||||
image = create_object_from_json_body
|
||||
settings.mongo.image_insert Image.new(image)
|
||||
create_response "Created", nil, 201
|
||||
@ -37,6 +43,7 @@ module Devops
|
||||
|
||||
def self.update_image
|
||||
lambda {
|
||||
check_privileges("image", "w")
|
||||
settings.mongo.image params[:image_id]
|
||||
image = Image.new(create_object_from_json_body)
|
||||
image.id = params[:image_id]
|
||||
@ -47,6 +54,7 @@ module Devops
|
||||
|
||||
def self.delete_image
|
||||
lambda {
|
||||
check_privileges("image", "w")
|
||||
projects = settings.mongo.projects_by_image params[:image_id]
|
||||
unless projects.empty?
|
||||
ar = []
|
||||
|
||||
@ -8,11 +8,6 @@ module Devops
|
||||
statistic
|
||||
end
|
||||
|
||||
app.before "/images" do
|
||||
check_headers :accept
|
||||
check_privileges("image", "r")
|
||||
check_provider(params[:provider]) if params[:provider]
|
||||
end
|
||||
# Get devops images list
|
||||
#
|
||||
# * *Request*
|
||||
@ -32,13 +27,8 @@ module Devops
|
||||
# "id": "36dc7618-4178-4e29-be43-286fbfe90f50"
|
||||
# }
|
||||
# ]
|
||||
app.get "/images", &Devops::Version2_0::Handler::Image.get_images
|
||||
app.get "/images", :headers => [:accept], &Devops::Version2_0::Handler::Image.get_images
|
||||
|
||||
app.before "/images/provider/:provider" do
|
||||
check_headers :accept
|
||||
check_privileges("image", "r")
|
||||
check_provider(params[:provider])
|
||||
end
|
||||
# Get raw images for :provider
|
||||
#
|
||||
# * *Request*
|
||||
@ -63,18 +53,8 @@ module Devops
|
||||
# "status": "ACTIVE"
|
||||
# }
|
||||
# ]
|
||||
app.get "/images/provider/:provider", &Devops::Version2_0::Handler::Image.get_provider_images
|
||||
app.get "/images/provider/:provider", :headers => [:accept], &Devops::Version2_0::Handler::Image.get_provider_images
|
||||
|
||||
app.before "/image/:image_id" do
|
||||
case request.method
|
||||
when "get"
|
||||
check_headers :accept
|
||||
check_privileges("image", "r")
|
||||
when "delete", "put"
|
||||
check_headers
|
||||
check_privileges("image", "w")
|
||||
end
|
||||
end
|
||||
# Get devops image by id
|
||||
#
|
||||
# * *Request*
|
||||
@ -90,12 +70,8 @@ module Devops
|
||||
# "bootstrap_template": null,
|
||||
# "id": "36dc7618-4178-4e29-be43-286fbfe90f50"
|
||||
# }
|
||||
app.get "/image/:image_id", &Devops::Version2_0::Handler::Image.get_image
|
||||
app.get "/image/:image_id", :headers => [:accept], &Devops::Version2_0::Handler::Image.get_image
|
||||
|
||||
app.before "/image" do
|
||||
check_headers
|
||||
check_privileges("image", "w")
|
||||
end
|
||||
# Create devops image
|
||||
#
|
||||
# * *Request*
|
||||
|
||||
99
devops-service/sinatra/methods_with_headers.rb
Normal file
99
devops-service/sinatra/methods_with_headers.rb
Normal file
@ -0,0 +1,99 @@
|
||||
require "sinatra/base"
|
||||
|
||||
module Sinatra
|
||||
|
||||
module HeadersHelpers
|
||||
|
||||
end
|
||||
|
||||
helpers HeadersHelpers
|
||||
|
||||
class Base
|
||||
class << self
|
||||
|
||||
# TODO: add protect! method
|
||||
def get_with_headers path, opt={}, &block
|
||||
headers = opt.delete(:headers) || []
|
||||
before path do
|
||||
check_headers *headers
|
||||
end
|
||||
|
||||
get path, opt, &block
|
||||
end
|
||||
|
||||
def post_with_headers path, opt={}, &block
|
||||
headers = opt.delete(:headers) || []
|
||||
before path do
|
||||
check_headers *headers
|
||||
end
|
||||
|
||||
post path, opt, &block
|
||||
|
||||
after path do
|
||||
statistic
|
||||
end
|
||||
end
|
||||
|
||||
def put_with_headers path, opt={}, &block
|
||||
headers = opt.delete(:headers) || []
|
||||
before path do
|
||||
check_headers *headers
|
||||
end
|
||||
|
||||
put path, opt, &block
|
||||
|
||||
after path do
|
||||
statistic
|
||||
end
|
||||
end
|
||||
|
||||
def delete_with_headers path, opt={}, &block
|
||||
headers = opt.delete(:headers) || []
|
||||
before path do
|
||||
check_headers *headers
|
||||
end
|
||||
|
||||
delete path, opt, &block
|
||||
|
||||
after path do
|
||||
statistic
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Check request headers
|
||||
def check_headers *headers
|
||||
ha = (headers.empty? ? [:accept, :content_type] : headers)
|
||||
ha.each do |h|
|
||||
case h
|
||||
when :accept, "accept"
|
||||
accept_json
|
||||
when :content_type, "content_type"
|
||||
request_json
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Check Accept header
|
||||
#
|
||||
# Can client works with JSON?
|
||||
def accept_json
|
||||
logger.debug(request.accept)
|
||||
unless request.accept? 'application/json'
|
||||
response.headers['Accept'] = 'application/json'
|
||||
halt_response("Accept header should contains 'application/json' type", 406)
|
||||
end
|
||||
rescue NoMethodError => e
|
||||
#error in sinatra 1.4.4 (https://github.com/sinatra/sinatra/issues/844, https://github.com/sinatra/sinatra/pull/805)
|
||||
response.headers['Accept'] = 'application/json'
|
||||
halt_response("Accept header should contains 'application/json' type", 406)
|
||||
end
|
||||
|
||||
# Check Content-Type header
|
||||
def request_json
|
||||
halt_response("Content-Type should be 'application/json'", 415) if request.media_type.nil? or request.media_type != 'application/json'
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user