Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions app/controllers/v3/service_instances_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@
class ServiceInstancesV3Controller < ApplicationController
include ServicePermissions

def show
service_instance = ServiceInstance.first(guid: hashed_params[:guid])
service_instance_not_found! unless service_instance && can_read_service_instance?(service_instance)

message = ServiceInstanceShowMessage.from_params(query_params)
invalid_param!(message.errors.full_messages) unless message.valid?

presenter = Presenters::V3::ServiceInstancePresenter.new(
service_instance,
decorators: decorators_for_fields(message.fields)
)

render status: :ok, json: presenter.to_json
end

def index
message = ServiceInstancesListMessage.from_params(query_params)
invalid_param!(message.errors.full_messages) unless message.valid?
Expand All @@ -64,6 +49,21 @@ def index
)
end

def show
service_instance = ServiceInstance.first(guid: hashed_params[:guid])
service_instance_not_found! unless service_instance && can_read_service_instance?(service_instance)

message = ServiceInstanceShowMessage.from_params(query_params)
invalid_param!(message.errors.full_messages) unless message.valid?

presenter = Presenters::V3::ServiceInstancePresenter.new(
service_instance,
decorators: decorators_for_fields(message.fields)
)

render status: :ok, json: presenter.to_json
end

def create
FeatureFlag.raise_unless_enabled!(:service_instance_creation) unless admin?

Expand Down
15 changes: 13 additions & 2 deletions app/fetchers/service_instance_list_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ def fetch(message, omniscient: false, readable_space_guids: [])
join(:spaces, id: Sequel[:service_instances][:space_id]).
left_join(:service_instance_shares, service_instance_guid: Sequel[:service_instances][:guid])

if !omniscient
unless omniscient
dataset = dataset.where do
(Sequel[:spaces][:guid] =~ readable_space_guids) |
(Sequel[:service_instance_shares][:target_space_guid] =~ readable_space_guids)
(Sequel[:service_instance_shares][:target_space_guid] =~ readable_space_guids)
end
end

Expand Down Expand Up @@ -37,6 +37,17 @@ def filter(dataset, message)
end
end

if message.requested?(:organization_guids)
spaces_in_orgs = Space.dataset.select(:spaces__guid).
join(:organizations, id: Sequel[:spaces][:organization_id]).
where(Sequel[:organizations][:guid] =~ message.organization_guids)

dataset = dataset.where do
(Sequel[:spaces][:guid] =~ spaces_in_orgs) |
(Sequel[:service_instance_shares][:target_space_guid] =~ spaces_in_orgs)
end
end

if message.requested?(:space_guids)
dataset = dataset.where do
(Sequel[:spaces][:guid] =~ message.space_guids) |
Expand Down
1 change: 1 addition & 0 deletions app/messages/service_instances_list_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ServiceInstancesListMessage < MetadataListMessage
@array_keys = [
:names,
:space_guids,
:organization_guids,
:service_plan_guids,
:service_plan_names,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Name | Type | Description
**names** | _list of strings_ | Comma-delimited list of service instance names to filter by
**type** | _string_ | Filter by type; valid values are `managed` and `user-provided`
**space_guids** | _list of strings_ | Comma-delimited list of space guids to filter by
**organization_guids** | _list of strings_ | Comma-delimited list of organization guids to filter by
**service_plan_guids** | _list of strings_ | Comma-delimited list of service plan guids to filter by
**service_plan_names** | _list of strings_ | Comma-delimited list of service plan names to filter by
**page** | _integer_ | Page to display; valid values are integers >= 1
Expand Down
10 changes: 10 additions & 0 deletions spec/request/service_instances_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
{
names: ['foo', 'bar'],
space_guids: ['foo', 'bar'],
organization_guids: ['org-1', 'org-2'],
per_page: '10',
page: 2,
order_by: 'updated_at',
Expand Down Expand Up @@ -265,6 +266,15 @@
)
end

it 'filters by organization guids' do
get "/v3/service_instances?organization_guids=#{another_space.organization.guid}", nil, admin_headers
check_filtered_instances(
create_managed_json(msi_2),
create_user_provided_json(upsi_2),
create_managed_json(ssi),
)
end

it 'filters by label' do
VCAP::CloudController::ServiceInstanceLabelModel.make(key_name: 'fruit', value: 'strawberry', service_instance: msi_1)
VCAP::CloudController::ServiceInstanceLabelModel.make(key_name: 'fruit', value: 'raspberry', service_instance: msi_2)
Expand Down
9 changes: 9 additions & 0 deletions spec/unit/fetchers/service_instance_list_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ module VCAP::CloudController
end
end

context 'by organization_guids' do
let(:filters) { { organization_guids: [space_1.organization.guid, space_2.organization.guid, 'no-such-org-guid'] } }

it 'returns instances with matching org guids' do
expect(fetcher.fetch(message, omniscient: true).map(&:guid)).to contain_exactly(*[msi_1, upsi, msi_2, ssi].map(&:guid))
expect(fetcher.fetch(message, readable_space_guids: [space_1.guid, space_3.guid])).to contain_exactly(msi_1, ssi, upsi)
end
end

context 'by label selector' do
let(:filters) { { 'label_selector' => 'key=value' } }
before do
Expand Down
1 change: 1 addition & 0 deletions spec/unit/messages/service_instances_list_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module VCAP::CloudController
'order_by' => 'name',
'names' => 'rabbitmq, redis,mysql',
'space_guids' => 'space-1, space-2, space-3',
'organization_guids' => 'organization-1, organization-2',
'label_selector' => 'key=value',
'type' => 'managed',
'service_plan_names' => 'plan1, plan2',
Expand Down