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

Commit 9949b9d

Browse files
author
Derik Evangelista
authored
v3(services): SI can be filtered by Org GUIDs (cloudfoundry#1766)
[finishes #171815586](https://www.pivotaltracker.com/story/show/171815586)
1 parent 16f59c7 commit 9949b9d

7 files changed

Lines changed: 50 additions & 17 deletions

File tree

app/controllers/v3/service_instances_controller.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,6 @@
3131
class ServiceInstancesV3Controller < ApplicationController
3232
include ServicePermissions
3333

34-
def show
35-
service_instance = ServiceInstance.first(guid: hashed_params[:guid])
36-
service_instance_not_found! unless service_instance && can_read_service_instance?(service_instance)
37-
38-
message = ServiceInstanceShowMessage.from_params(query_params)
39-
invalid_param!(message.errors.full_messages) unless message.valid?
40-
41-
presenter = Presenters::V3::ServiceInstancePresenter.new(
42-
service_instance,
43-
decorators: decorators_for_fields(message.fields)
44-
)
45-
46-
render status: :ok, json: presenter.to_json
47-
end
48-
4934
def index
5035
message = ServiceInstancesListMessage.from_params(query_params)
5136
invalid_param!(message.errors.full_messages) unless message.valid?
@@ -65,6 +50,21 @@ def index
6550
)
6651
end
6752

53+
def show
54+
service_instance = ServiceInstance.first(guid: hashed_params[:guid])
55+
service_instance_not_found! unless service_instance && can_read_service_instance?(service_instance)
56+
57+
message = ServiceInstanceShowMessage.from_params(query_params)
58+
invalid_param!(message.errors.full_messages) unless message.valid?
59+
60+
presenter = Presenters::V3::ServiceInstancePresenter.new(
61+
service_instance,
62+
decorators: decorators_for_fields(message.fields)
63+
)
64+
65+
render status: :ok, json: presenter.to_json
66+
end
67+
6868
def create
6969
FeatureFlag.raise_unless_enabled!(:service_instance_creation) unless admin?
7070

app/fetchers/service_instance_list_fetcher.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ def fetch(message, omniscient: false, readable_space_guids: [])
55
join(:spaces, id: Sequel[:service_instances][:space_id]).
66
left_join(:service_instance_shares, service_instance_guid: Sequel[:service_instances][:guid])
77

8-
if !omniscient
8+
unless omniscient
99
dataset = dataset.where do
1010
(Sequel[:spaces][:guid] =~ readable_space_guids) |
11-
(Sequel[:service_instance_shares][:target_space_guid] =~ readable_space_guids)
11+
(Sequel[:service_instance_shares][:target_space_guid] =~ readable_space_guids)
1212
end
1313
end
1414

@@ -37,6 +37,17 @@ def filter(dataset, message)
3737
end
3838
end
3939

40+
if message.requested?(:organization_guids)
41+
spaces_in_orgs = Space.dataset.select(:spaces__guid).
42+
join(:organizations, id: Sequel[:spaces][:organization_id]).
43+
where(Sequel[:organizations][:guid] =~ message.organization_guids)
44+
45+
dataset = dataset.where do
46+
(Sequel[:spaces][:guid] =~ spaces_in_orgs) |
47+
(Sequel[:service_instance_shares][:target_space_guid] =~ spaces_in_orgs)
48+
end
49+
end
50+
4051
if message.requested?(:space_guids)
4152
dataset = dataset.where do
4253
(Sequel[:spaces][:guid] =~ message.space_guids) |

app/messages/service_instances_list_message.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class ServiceInstancesListMessage < MetadataListMessage
55
@array_keys = [
66
:names,
77
:space_guids,
8+
:organization_guids,
89
:service_plan_guids,
910
:service_plan_names,
1011
]

docs/v3/source/includes/resources/service_instances/_list.md.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Name | Type | Description
3434
**names** | _list of strings_ | Comma-delimited list of service instance names to filter by
3535
**type** | _string_ | Filter by type; valid values are `managed` and `user-provided`
3636
**space_guids** | _list of strings_ | Comma-delimited list of space guids to filter by
37+
**organization_guids** | _list of strings_ | Comma-delimited list of organization guids to filter by
3738
**service_plan_guids** | _list of strings_ | Comma-delimited list of service plan guids to filter by
3839
**service_plan_names** | _list of strings_ | Comma-delimited list of service plan names to filter by
3940
**page** | _integer_ | Page to display; valid values are integers >= 1

spec/request/service_instances_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
{
183183
names: ['foo', 'bar'],
184184
space_guids: ['foo', 'bar'],
185+
organization_guids: ['org-1', 'org-2'],
185186
per_page: '10',
186187
page: 2,
187188
order_by: 'updated_at',
@@ -265,6 +266,15 @@
265266
)
266267
end
267268

269+
it 'filters by organization guids' do
270+
get "/v3/service_instances?organization_guids=#{another_space.organization.guid}", nil, admin_headers
271+
check_filtered_instances(
272+
create_managed_json(msi_2),
273+
create_user_provided_json(upsi_2),
274+
create_managed_json(ssi),
275+
)
276+
end
277+
268278
it 'filters by label' do
269279
VCAP::CloudController::ServiceInstanceLabelModel.make(key_name: 'fruit', value: 'strawberry', service_instance: msi_1)
270280
VCAP::CloudController::ServiceInstanceLabelModel.make(key_name: 'fruit', value: 'raspberry', service_instance: msi_2)

spec/unit/fetchers/service_instance_list_fetcher_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ module VCAP::CloudController
5959
end
6060
end
6161

62+
context 'by organization_guids' do
63+
let(:filters) { { organization_guids: [space_1.organization.guid, space_2.organization.guid, 'no-such-org-guid'] } }
64+
65+
it 'returns instances with matching org guids' do
66+
expect(fetcher.fetch(message, omniscient: true).map(&:guid)).to contain_exactly(*[msi_1, upsi, msi_2, ssi].map(&:guid))
67+
expect(fetcher.fetch(message, readable_space_guids: [space_1.guid, space_3.guid])).to contain_exactly(msi_1, ssi, upsi)
68+
end
69+
end
70+
6271
context 'by label selector' do
6372
let(:filters) { { 'label_selector' => 'key=value' } }
6473
before do

spec/unit/messages/service_instances_list_message_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module VCAP::CloudController
1111
'order_by' => 'name',
1212
'names' => 'rabbitmq, redis,mysql',
1313
'space_guids' => 'space-1, space-2, space-3',
14+
'organization_guids' => 'organization-1, organization-2',
1415
'label_selector' => 'key=value',
1516
'type' => 'managed',
1617
'service_plan_names' => 'plan1, plan2',

0 commit comments

Comments
 (0)