class ChefZero::RestBase

Attributes

server[R]

Public Class Methods

build_uri(base_uri, rest_path) click to toggle source
# File lib/chef_zero/rest_base.rb, line 135
def self.build_uri(base_uri, rest_path)
  "#{base_uri}/#{rest_path.join('/')}"
end
new(server) click to toggle source
# File lib/chef_zero/rest_base.rb, line 7
def initialize(server)
  @server = server
end

Public Instance Methods

already_json_response(response_code, json_text) click to toggle source
# File lib/chef_zero/rest_base.rb, line 118
def already_json_response(response_code, json_text)
  [response_code, {"Content-Type" => "application/json"}, json_text]
end
build_uri(base_uri, rest_path) click to toggle source

To be called from inside rest endpoints

# File lib/chef_zero/rest_base.rb, line 123
def build_uri(base_uri, rest_path)
  if server.options[:single_org]
    # Strip off /organizations/chef if we are in single org mode
    if rest_path[0..1] != [ 'organizations', server.options[:single_org] ]
      raise "Unexpected URL #{rest_path[0..1]} passed to build_uri in single org mode"
    end
    "#{base_uri}/#{rest_path[2..-1].join('/')}"
  else
    "#{base_uri}/#{rest_path.join('/')}"
  end
end
call(request) click to toggle source
# File lib/chef_zero/rest_base.rb, line 17
def call(request)
  method = request.method.downcase.to_sym
  if !self.respond_to?(method)
    accept_methods = [:get, :put, :post, :delete].select { |m| self.respond_to?(m) }
    accept_methods_str = accept_methods.map { |m| m.to_s.upcase }.join(', ')
    return [405, {"Content-Type" => "text/plain", "Allow" => accept_methods_str}, "Bad request method for '#{request.env['REQUEST_PATH']}': #{request.env['REQUEST_METHOD']}"]
  end
  if json_only && request.env['HTTP_ACCEPT'] && !request.env['HTTP_ACCEPT'].split(';').include?('application/json')
    return [406, {"Content-Type" => "text/plain"}, "Must accept application/json"]
  end
  # Dispatch to get()/post()/put()/delete()
  begin
    self.send(method, request)
  rescue RestErrorResponse => e
    ChefZero::Log.debug("#{e.inspect}\n#{e.backtrace.join("\n")}")
    error(e.response_code, e.error)
  end
end
create_data(request, rest_path, name, data, *options) click to toggle source
# File lib/chef_zero/rest_base.rb, line 89
def create_data(request, rest_path, name, data, *options)
  rest_path ||= request.rest_path
  begin
    data_store.create(rest_path, name, data, *options)
  rescue DataStore::DataNotFoundError
    raise RestErrorResponse.new(404, "Parent not found: #{build_uri(request.base_uri, request.rest_path)}")
  rescue DataStore::DataAlreadyExistsError
    raise RestErrorResponse.new(409, "Object already exists: #{build_uri(request.base_uri, request.rest_path + [name])}")
  end
end
data_store() click to toggle source
# File lib/chef_zero/rest_base.rb, line 13
def data_store
  server.data_store
end
delete_data(request, rest_path=nil) click to toggle source
# File lib/chef_zero/rest_base.rb, line 62
def delete_data(request, rest_path=nil)
  rest_path ||= request.rest_path
  begin
    data_store.delete(rest_path)
  rescue DataStore::DataNotFoundError
    raise RestErrorResponse.new(404, "Object not found: #{build_uri(request.base_uri, request.rest_path)}")
  end
end
delete_data_dir(request, rest_path, *options) click to toggle source
# File lib/chef_zero/rest_base.rb, line 71
def delete_data_dir(request, rest_path, *options)
  rest_path ||= request.rest_path
  begin
    data_store.delete_dir(rest_path, *options)
  rescue DataStore::DataNotFoundError
    raise RestErrorResponse.new(404, "Object not found: #{build_uri(request.base_uri, request.rest_path)}")
  end
end
error(response_code, error) click to toggle source
# File lib/chef_zero/rest_base.rb, line 110
def error(response_code, error)
  json_response(response_code, {"error" => [error]})
end
exists_data?(request, rest_path=nil) click to toggle source
# File lib/chef_zero/rest_base.rb, line 100
def exists_data?(request, rest_path=nil)
  rest_path ||= request.rest_path
  data_store.exists?(rest_path)
end
exists_data_dir?(request, rest_path=nil) click to toggle source
# File lib/chef_zero/rest_base.rb, line 105
def exists_data_dir?(request, rest_path=nil)
  rest_path ||= request.rest_path
  data_store.exists_dir?(rest_path)
end
get_data(request, rest_path=nil, *options) click to toggle source
# File lib/chef_zero/rest_base.rb, line 40
def get_data(request, rest_path=nil, *options)
  rest_path ||= request.rest_path
  begin
    data_store.get(rest_path, request)
  rescue DataStore::DataNotFoundError
    if options.include?(:nil)
      nil
    else
      raise RestErrorResponse.new(404, "Object not found: #{build_uri(request.base_uri, rest_path)}")
    end
  end
end
json_only() click to toggle source
# File lib/chef_zero/rest_base.rb, line 36
def json_only
  true
end
json_response(response_code, json) click to toggle source
# File lib/chef_zero/rest_base.rb, line 114
def json_response(response_code, json)
  already_json_response(response_code, JSON.pretty_generate(json))
end
list_data(request, rest_path=nil) click to toggle source
# File lib/chef_zero/rest_base.rb, line 53
def list_data(request, rest_path=nil)
  rest_path ||= request.rest_path
  begin
    data_store.list(rest_path)
  rescue DataStore::DataNotFoundError
    raise RestErrorResponse.new(404, "Object not found: #{build_uri(request.base_uri, rest_path)}")
  end
end
populate_defaults(request, response) click to toggle source
# File lib/chef_zero/rest_base.rb, line 139
def populate_defaults(request, response)
  response
end
set_data(request, rest_path, data, *options) click to toggle source
# File lib/chef_zero/rest_base.rb, line 80
def set_data(request, rest_path, data, *options)
  rest_path ||= request.rest_path
  begin
    data_store.set(rest_path, request.body, *options)
  rescue DataStore::DataNotFoundError
    raise RestErrorResponse.new(404, "Object not found: #{build_uri(request.base_uri, request.rest_path)}")
  end
end