Skip to content

Commit

Permalink
Use space subqueries for fetchting service credential bindings
Browse files Browse the repository at this point in the history
As a regular user the select query for service credential bindings is
filtered by a list of space guids (spaces the user has access to).
This can produce very long queries. Instead we can use subqueries to filter the spaces directly in the db.
Same approach is used in service lists, see #2580.
  • Loading branch information
johha committed Dec 14, 2022
1 parent ec85582 commit b3ee59a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions app/controllers/v3/service_credential_bindings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def index
invalid_param!(message.errors.full_messages) unless message.valid?

results = list_fetcher.fetch(
space_guids: space_guids,
readable_spaces_query: spaces_query,
message: message,
eager_loaded_associations: Presenters::V3::ServiceCredentialBindingPresenter.associated_resources,
)
Expand Down Expand Up @@ -328,14 +328,14 @@ def fetch_credentials_value(name)
end

def service_credential_binding
@service_credential_binding ||= fetcher.fetch(hashed_params[:guid], space_guids: space_guids)
@service_credential_binding ||= fetcher.fetch(hashed_params[:guid], readable_spaces_query: spaces_query)
end

def space_guids
def spaces_query
if permission_queryer.can_read_globally?
:all
nil
else
permission_queryer.readable_space_guids
permission_queryer.readable_space_guids_query
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/fetchers/service_credential_binding_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module VCAP
module CloudController
class ServiceCredentialBindingFetcher
def fetch(guid, space_guids:)
list_fetcher.fetch(space_guids: space_guids).first(guid: guid)
def fetch(guid, readable_spaces_query: nil)
list_fetcher.fetch(readable_spaces_query: readable_spaces_query).first(guid: guid)
end

private
Expand Down
8 changes: 4 additions & 4 deletions app/fetchers/service_credential_binding_list_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ class << self
service_offering_guid
}.freeze

def fetch(space_guids:, message: nil, eager_loaded_associations: [])
dataset = case space_guids
when :all
def fetch(readable_spaces_query: nil, message: nil, eager_loaded_associations: [])
dataset = case readable_spaces_query
when nil
ServiceCredentialBinding::View.dataset
else
ServiceCredentialBinding::View.where { Sequel[:space_guid] =~ space_guids }
ServiceCredentialBinding::View.where { Sequel[:space_id] =~ readable_spaces_query.select(:id) }
end

dataset = dataset.eager(eager_loaded_associations)
Expand Down
4 changes: 2 additions & 2 deletions app/models/services/service_credential_binding_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Types
Sequel.as(:service_keys__id, :id),
Sequel.as(:service_keys__guid, :guid),
Sequel.as(Types::SERVICE_KEY, :type),
Sequel.as(:spaces__guid, :space_guid),
Sequel.as(:spaces__id, :space_id),
Sequel.as(:service_keys__created_at, :created_at),
Sequel.as(:service_keys__updated_at, :updated_at),
Sequel.as(:service_keys__name, :name),
Expand Down Expand Up @@ -44,7 +44,7 @@ module Types
Sequel.as(:service_bindings__id, :id),
Sequel.as(:service_bindings__guid, :guid),
Sequel.as(Types::SERVICE_BINDING, :type),
Sequel.as(:spaces__guid, :space_guid),
Sequel.as(:spaces__id, :space_id),
Sequel.as(:service_bindings__created_at, :created_at),
Sequel.as(:service_bindings__updated_at, :updated_at),
Sequel.as(:service_bindings__name, :name),
Expand Down

0 comments on commit b3ee59a

Please sign in to comment.