Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit 2e8fab8

Browse files
Teal Stannardweymanf
authored andcommitted
Fetch users from UAA in batches of 100
- UAA doesn't allow filtering for users over 250 when calling their api [#174806614] Co-authored-by: Teal Stannard <tstannard@pivotal.io> Co-authored-by: Weyman Fung <weymanf@vmware.com>
1 parent ec61387 commit 2e8fab8

2 files changed

Lines changed: 62 additions & 6 deletions

File tree

lib/cloud_controller/uaa/uaa_client.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,17 @@ def scim
117117
def fetch_users(user_ids)
118118
return {} unless user_ids.present?
119119

120-
filter_string = user_ids.map { |user_id| %(id eq "#{user_id}") }.join(' or ')
121-
results = query(:user_id, filter: filter_string, count: user_ids.length)
122-
123-
results['resources'].each_with_object({}) do |resource, results_hash|
124-
results_hash[resource['id']] = resource
125-
results_hash
120+
results_hash = {}
121+
122+
user_ids.each_slice(200) do |batch|
123+
filter_string = batch.map { |user_id| %(id eq "#{user_id}") }.join(' or ')
124+
results = query(:user_id, filter: filter_string, count: batch.length)
125+
results['resources'].each do |user|
126+
results_hash[user['id']] = user
127+
end
126128
end
129+
130+
results_hash
127131
end
128132

129133
def token_issuer

spec/unit/lib/uaa/uaa_client_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,58 @@ module VCAP::CloudController
314314
end
315315
end
316316

317+
context 'when were asking for over 200 users' do
318+
let(:user_ids) { (0...300).to_a }
319+
let(:actual_users) do
320+
user_ids.map do |id|
321+
{ 'id' => "#{id}", 'origin' => 'uaa', 'username' => "user_#{id}" }
322+
end
323+
end
324+
let(:response_body1) do
325+
{
326+
'resources' => actual_users.slice(0, 200),
327+
'schemas' => ['urn:scim:schemas:core:1.0'],
328+
'startindex' => 1,
329+
'itemsperpage' => 200,
330+
'totalresults' => 2
331+
}
332+
end
333+
let(:response_body2) do
334+
{
335+
'resources' => actual_users.slice(200, 200),
336+
'schemas' => ['urn:scim:schemas:core:1.0'],
337+
'startindex' => 1,
338+
'itemsperpage' => 100,
339+
'totalresults' => 2
340+
}
341+
end
342+
343+
before do
344+
WebMock::API.stub_request(:get, "#{url}/ids/Users").
345+
with(query: {
346+
'filter' => user_ids.slice(0, 200).map { |user_id| %(id eq "#{user_id}") }.join(' or '),
347+
'count' => 200
348+
}).to_return(
349+
status: 200,
350+
headers: { 'content-type' => 'application/json' },
351+
body: response_body1.to_json)
352+
353+
WebMock::API.stub_request(:get, "#{url}/ids/Users").
354+
with(query: {
355+
'filter' => user_ids.slice(200, 100).map { |user_id| %(id eq "#{user_id}") }.join(' or '),
356+
'count' => 100
357+
}).to_return(
358+
status: 200,
359+
headers: { 'content-type' => 'application/json' },
360+
body: response_body2.to_json)
361+
end
362+
363+
it 'returns the list of users after making batch requests' do
364+
results = uaa_client.users_for_ids(user_ids)
365+
expect(results).to eq(actual_users.map { |user| [user['id'], user] }.to_h)
366+
end
367+
end
368+
317369
context 'when the endpoint returns an error' do
318370
let(:uaa_error) { CF::UAA::UAAError.new('some error') }
319371
let(:mock_logger) { double(:steno_logger, error: nil) }

0 commit comments

Comments
 (0)