зеркало из https://github.com/mozilla/labs-vcap.git
[cc] [bulk] [fix] ensure valid json is returned by bulk api
Change-Id: I1f233298cc2a83b45a266abd270a2167f66809a1
This commit is contained in:
Родитель
611c78077a
Коммит
d966809f61
|
@ -1,14 +1,12 @@
|
|||
class BulkController < ApplicationController
|
||||
|
||||
skip_before_filter :fetch_user_from_token
|
||||
before_filter :authenticate_bulk_api
|
||||
|
||||
#the password is randomly generated at startup and is
|
||||
#discoverable through NATS.request('cloudcontroller.bulk.credentials')
|
||||
# the password is randomly generated at startup and is
|
||||
# discoverable through NATS.request('cloudcontroller.bulk.credentials')
|
||||
|
||||
DEFAULT_BATCH_SIZE = 200
|
||||
|
||||
|
||||
def users
|
||||
render_results_and_token_for_model(User)
|
||||
end
|
||||
|
@ -18,7 +16,6 @@ class BulkController < ApplicationController
|
|||
end
|
||||
|
||||
private
|
||||
|
||||
def authenticate_bulk_api
|
||||
authenticate_or_request_with_http_basic do |user, pass|
|
||||
if user==AppConfig[:bulk_api][:auth][:user] &&
|
||||
|
@ -29,21 +26,31 @@ class BulkController < ApplicationController
|
|||
false
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def render_results_and_token_for_model(model)
|
||||
results = retrieve_results(model)
|
||||
update_token(results)
|
||||
render :json => { :results => hash_by_id(results), :bulk_token => bulk_token }
|
||||
render :json => { :results => hash_by_id(model, results), :bulk_token => bulk_token }
|
||||
end
|
||||
|
||||
def retrieve_results(model)
|
||||
model.where(where_clause).limit(batch_size).to_a
|
||||
CloudController.logger.debug("Params: #{params}")
|
||||
CloudController.logger.debug("Retrieving bulk results for bulk_token: #{bulk_token}")
|
||||
CloudController.logger.debug("WHERE-clause: #{where_clause}")
|
||||
|
||||
model.where(where_clause).order('id').limit(batch_size).to_a
|
||||
end
|
||||
|
||||
def hash_by_id arr
|
||||
arr.inject({}) { |hash, elem| hash[elem.id] = elem; hash }
|
||||
def hash_by_id(model, arr)
|
||||
arr.inject({}) { |hash, elem| hash[elem.id] = hashify(model,elem); hash }
|
||||
end
|
||||
|
||||
def hashify(model, record)
|
||||
model.column_names.inject(Hash.new) { |hash, col|
|
||||
hash[col] = record.send(col)
|
||||
hash
|
||||
}
|
||||
end
|
||||
|
||||
def update_token(results)
|
||||
|
@ -55,18 +62,21 @@ class BulkController < ApplicationController
|
|||
end
|
||||
|
||||
def sanitize_atom(atom)
|
||||
unless atom =~ /^\w+$/
|
||||
atom = atom.to_s
|
||||
unless atom =~ /^[a-zA-Z0-9_]+$/
|
||||
CloudController.logger.error("invalid atom #{atom} in bulk_token #{bulk_token}")
|
||||
raise CloudError.new(CloudError::BAD_REQUEST, "bad atom #{atom} in bulk_api token")
|
||||
end
|
||||
atom
|
||||
end
|
||||
|
||||
|
||||
def batch_size
|
||||
@batch_size||=params['batch_size'] ? json_param('batch_size').to_i : DEFAULT_BATCH_SIZE
|
||||
end
|
||||
|
||||
def bulk_token
|
||||
@bulk_token||=params['bulk_token'] ? params['bulk_token'] : {}
|
||||
@bulk_token = Yajl::Parser.parse(@bulk_token) if @bulk_token.kind_of? String
|
||||
@bulk_token
|
||||
end
|
||||
end
|
||||
|
|
|
@ -114,6 +114,19 @@ describe "bulk_api" do
|
|||
results.should_not be_nil
|
||||
end
|
||||
|
||||
it "returns results that are valid json" do
|
||||
get_apps
|
||||
results.each { |res|
|
||||
res.should be_a_kind_of Array
|
||||
res.size.should == 2
|
||||
res.last.should be_a_kind_of Hash
|
||||
res.last["id"].should_not be_nil
|
||||
res.last["framework"].should == 'sinatra'
|
||||
}
|
||||
|
||||
end
|
||||
|
||||
|
||||
it "respects the batch_size parameter" do
|
||||
[3,5].each { |size|
|
||||
get_apps :batch_size=>size
|
||||
|
|
Загрузка…
Ссылка в новой задаче