Skip to content

Commit 2b0a6a0

Browse files
committed
Add field to include broker catalog unique id to service instances
1 parent 388c2f8 commit 2b0a6a0

12 files changed

+108
-35
lines changed

Diff for: app/decorators/field_service_instance_offering_decorator.rb

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module VCAP::CloudController
22
class FieldServiceInstanceOfferingDecorator
33
def self.allowed
4-
Set.new(%w[name guid description documentation_url tags relationships.service_broker])
4+
Set.new(%w[name guid description documentation_url tags broker_catalog.id relationships.service_broker])
55
end
66

77
def self.match?(fields)
@@ -16,14 +16,7 @@ def decorate(hash, service_instances)
1616
managed_service_instances = service_instances.select(&:managed_instance?)
1717
return hash if managed_service_instances.empty?
1818

19-
offerings = Service.
20-
join(:service_plans, service_id: :services__id).
21-
join(:service_instances, service_plan_id: :service_plans__id).
22-
where(service_instances__id: managed_service_instances.map(&:id)).
23-
distinct(:services__created_at, :services__guid).
24-
order_by(:services__created_at, :services__guid).
25-
select(:services__label, :services__guid, :services__description, :services__tags, :services__extra, :services__service_broker_id, :services__created_at).
26-
all
19+
offerings = service_offerings(managed_service_instances)
2720

2821
hash[:included] ||= {}
2922
hash[:included][:service_offerings] = offerings.map do |offering|
@@ -33,6 +26,13 @@ def decorate(hash, service_instances)
3326
offering_view[:description] = offering.description if @fields.include?('description')
3427
offering_view[:tags] = offering.tags if @fields.include?('tags')
3528
offering_view[:documentation_url] = extract_documentation_url(offering.extra) if @fields.include?('documentation_url')
29+
30+
if @fields.include?('broker_catalog.id')
31+
offering_view[:broker_catalog] = {
32+
id: offering.unique_id
33+
}
34+
end
35+
3636
if @fields.include?('relationships.service_broker')
3737
offering_view[:relationships] = {
3838
service_broker: {
@@ -51,6 +51,17 @@ def decorate(hash, service_instances)
5151

5252
private
5353

54+
def service_offerings(managed_service_instances)
55+
Service.join(:service_plans, service_id: :services__id).
56+
join(:service_instances, service_plan_id: :service_plans__id).
57+
where(service_instances__id: managed_service_instances.map(&:id)).
58+
distinct(:services__created_at, :services__guid).
59+
order_by(:services__created_at, :services__guid).
60+
select(:services__label, :services__guid, :services__description, :services__tags, :services__extra,
61+
:services__service_broker_id, :services__created_at, :services__unique_id).
62+
all
63+
end
64+
5465
def extract_documentation_url(extra)
5566
metadata = Oj.load(extra)
5667
metadata['documentationUrl']

Diff for: app/decorators/field_service_instance_plan_decorator.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module VCAP::CloudController
22
class FieldServiceInstancePlanDecorator
33
def self.allowed
4-
Set['guid', 'name', 'relationships.service_offering']
4+
Set['guid', 'name', 'broker_catalog.id', 'relationships.service_offering']
55
end
66

77
def self.match?(fields)
@@ -23,6 +23,11 @@ def decorate(hash, service_instances)
2323
plan_view = {}
2424
plan_view[:guid] = plan.guid if @fields.include?('guid')
2525
plan_view[:name] = plan.name if @fields.include?('name')
26+
if @fields.include?('broker_catalog.id')
27+
plan_view[:broker_catalog] = {
28+
id: plan.unique_id
29+
}
30+
end
2631
if @fields.include?('relationships.service_offering')
2732
plan_view[:relationships] = {
2833
service_offering: {

Diff for: app/messages/service_instance_show_message.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class ServiceInstanceShowMessage < BaseMessage
77
allowed: {
88
'space' => %w[name guid],
99
'space.organization' => %w[name guid],
10-
'service_plan' => %w[name guid],
11-
'service_plan.service_offering' => %w[name guid description tags documentation_url],
10+
'service_plan' => %w[name guid broker_catalog.id],
11+
'service_plan.service_offering' => %w[name guid description tags documentation_url broker_catalog.id],
1212
'service_plan.service_offering.service_broker' => %w[name guid]
1313
}
1414
}

Diff for: app/messages/service_instances_list_message.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class ServiceInstancesListMessage < MetadataListMessage
2828
allowed: {
2929
'space' => %w[guid name relationships.organization],
3030
'space.organization' => %w[name guid],
31-
'service_plan' => %w[guid name relationships.service_offering],
32-
'service_plan.service_offering' => %w[name guid description documentation_url tags relationships.service_broker],
31+
'service_plan' => %w[guid name broker_catalog.id relationships.service_offering],
32+
'service_plan.service_offering' => %w[name guid description documentation_url tags broker_catalog.id relationships.service_broker],
3333
'service_plan.service_offering.service_broker' => %w[name guid]
3434
}
3535
}

Diff for: docs/v3/source/includes/resources/service_instances/_get.md.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ Resource | Allowed Keys
5151
------------------- | ----
5252
space | `guid`, `name`
5353
space.organization | `name`, `guid`
54-
service_plan| `name`, `guid`
55-
service_plan.service_offering| `name`, `guid`, `description`, `documentation_url`, `tags`
54+
service_plan| `name`, `guid`, `broker_catalog.id`
55+
service_plan.service_offering| `name`, `guid`, `description`, `documentation_url`, `tags`, `broker_catalog.id`
5656
service_plan.service_offering.service_broker| `name`, `guid`
5757

5858
#### Permitted roles

Diff for: docs/v3/source/includes/resources/service_instances/_list.md.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ Resource | Allowed Keys
5151
------------------- | ----
5252
space | `guid`, `name`, `relationships.organization`
5353
space.organization| `guid`, `name`
54-
service_plan| `guid`, `name`, `relationships.service_offering`
55-
service_plan.service_offering| `guid`, `name`, `description`, `documentation_url`, `tags`, `relationships.service_broker`
54+
service_plan| `guid`, `name`, `broker_catalog.id`, `relationships.service_offering`
55+
service_plan.service_offering| `guid`, `name`, `description`, `documentation_url`, `tags`, `broker_catalog.id`, `relationships.service_broker`
5656
service_plan.service_offering.service_broker| `guid`, `name`
5757

5858
#### Permitted roles

Diff for: spec/request/service_instances_spec.rb

+31-7
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,19 @@
9696
expect({ included: parsed_response['included'] }).to match_json_response({ included: })
9797
end
9898

99-
it 'can include service plan guid and name fields' do
100-
get "/v3/service_instances/#{guid}?fields[service_plan]=guid,name", nil, admin_headers
99+
it 'can include service plan guid, name and broker catalog fields' do
100+
get "/v3/service_instances/#{guid}?fields[service_plan]=guid,name,broker_catalog.id", nil, admin_headers
101101

102102
expect(last_response).to have_status_code(200)
103103

104104
included = {
105105
service_plans: [
106106
{
107107
guid: instance.service_plan.guid,
108-
name: instance.service_plan.name
108+
name: instance.service_plan.name,
109+
broker_catalog: {
110+
id: instance.service_plan.unique_id
111+
}
109112
}
110113
]
111114
}
@@ -114,7 +117,7 @@
114117
end
115118

116119
it 'can include service offering and broker fields' do
117-
get "/v3/service_instances/#{guid}?fields[service_plan.service_offering]=name,guid,description,documentation_url&" \
120+
get "/v3/service_instances/#{guid}?fields[service_plan.service_offering]=name,guid,description,documentation_url,broker_catalog.id&" \
118121
'fields[service_plan.service_offering.service_broker]=name,guid', nil, admin_headers
119122
expect(last_response).to have_status_code(200)
120123

@@ -124,7 +127,10 @@
124127
name: instance.service_plan.service.name,
125128
guid: instance.service_plan.service.guid,
126129
description: instance.service_plan.service.description,
127-
documentation_url: 'https://some.url.for.docs/'
130+
documentation_url: 'https://some.url.for.docs/',
131+
broker_catalog: {
132+
id: instance.service_plan.service.unique_id
133+
}
128134
}
129135
],
130136
service_brokers: [
@@ -386,8 +392,8 @@ def check_filtered_instances(*instances)
386392
end
387393

388394
it 'can include the service plan, offering and broker fields' do
389-
get '/v3/service_instances?fields[service_plan]=guid,name,relationships.service_offering&' \
390-
'fields[service_plan.service_offering]=name,guid,description,documentation_url,relationships.service_broker&' \
395+
get '/v3/service_instances?fields[service_plan]=guid,name,broker_catalog.id,relationships.service_offering&' \
396+
'fields[service_plan.service_offering]=name,guid,description,documentation_url,broker_catalog.id,relationships.service_broker&' \
391397
'fields[service_plan.service_offering.service_broker]=name,guid', nil, admin_headers
392398

393399
expect(last_response).to have_status_code(200)
@@ -397,6 +403,9 @@ def check_filtered_instances(*instances)
397403
{
398404
guid: msi_1.service_plan.guid,
399405
name: msi_1.service_plan.name,
406+
broker_catalog: {
407+
id: msi_1.service_plan.unique_id
408+
},
400409
relationships: {
401410
service_offering: {
402411
data: {
@@ -408,6 +417,9 @@ def check_filtered_instances(*instances)
408417
{
409418
guid: msi_2.service_plan.guid,
410419
name: msi_2.service_plan.name,
420+
broker_catalog: {
421+
id: msi_2.service_plan.unique_id
422+
},
411423
relationships: {
412424
service_offering: {
413425
data: {
@@ -419,6 +431,9 @@ def check_filtered_instances(*instances)
419431
{
420432
guid: ssi.service_plan.guid,
421433
name: ssi.service_plan.name,
434+
broker_catalog: {
435+
id: ssi.service_plan.unique_id
436+
},
422437
relationships: {
423438
service_offering: {
424439
data: {
@@ -434,6 +449,9 @@ def check_filtered_instances(*instances)
434449
guid: msi_1.service_plan.service.guid,
435450
description: msi_1.service_plan.service.description,
436451
documentation_url: 'https://some.url.for.docs/',
452+
broker_catalog: {
453+
id: msi_1.service_plan.service.unique_id
454+
},
437455
relationships: {
438456
service_broker: {
439457
data: {
@@ -447,6 +465,9 @@ def check_filtered_instances(*instances)
447465
guid: msi_2.service_plan.service.guid,
448466
description: msi_2.service_plan.service.description,
449467
documentation_url: 'https://some.url.for.docs/',
468+
broker_catalog: {
469+
id: msi_2.service_plan.service.unique_id
470+
},
450471
relationships: {
451472
service_broker: {
452473
data: {
@@ -460,6 +481,9 @@ def check_filtered_instances(*instances)
460481
guid: ssi.service_plan.service.guid,
461482
description: ssi.service_plan.service.description,
462483
documentation_url: 'https://some.url.for.docs/',
484+
broker_catalog: {
485+
id: ssi.service_plan.service.unique_id
486+
},
463487
relationships: {
464488
service_broker: {
465489
data: {

Diff for: spec/support/fakes/blueprints.rb

+1
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ module VCAP::CloudController
604604
unique_id { SecureRandom.uuid }
605605
active { true }
606606
maintenance_info {}
607+
unique_id { SecureRandom.uuid }
607608
end
608609

609610
ServicePlan.blueprint(:routing) do

Diff for: spec/unit/decorators/field_service_instance_offering_decorator_spec.rb

+27-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,32 @@ module VCAP::CloudController
123123
})
124124
end
125125

126+
it 'can decorate with the service offering broker_catalog_id' do
127+
undecorated_hash = { foo: 'bar', included: { monkeys: %w[zach greg] } }
128+
decorator = described_class.new({ 'service_plan.service_offering': %w[broker_catalog_id foo] })
129+
130+
hash = decorator.decorate(undecorated_hash, [service_instance_1, service_instance_2])
131+
132+
expect(hash).to match({
133+
foo: 'bar',
134+
included: {
135+
monkeys: %w[zach greg],
136+
service_offerings: [
137+
{
138+
broker_catalog: {
139+
id: offering1.unique_id
140+
}
141+
},
142+
{
143+
broker_catalog: {
144+
id: offering2.unique_id
145+
}
146+
}
147+
]
148+
}
149+
})
150+
end
151+
126152
it 'decorated the given hash with offering relationship to broker from service instances' do
127153
undecorated_hash = { foo: 'bar', included: { monkeys: %w[zach greg] } }
128154
decorator = described_class.new({ 'service_plan.service_offering': ['relationships.service_broker', 'foo'] })
@@ -182,7 +208,7 @@ module VCAP::CloudController
182208
end
183209

184210
describe '.match?' do
185-
fields = %w[name guid description documentation_url tags relationships.service_broker]
211+
fields = %w[name guid description documentation_url tags broker_catalog.id relationships.service_broker]
186212

187213
fields.each do |field|
188214
it "matches value `#{field}` for key symbol `service_plan.service_offering`" do

Diff for: spec/unit/decorators/field_service_instance_plan_decorator_spec.rb

+11-5
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ module VCAP::CloudController
1111
let(:service_instance_1) { ManagedServiceInstance.make(service_plan: plan1) }
1212
let(:service_instance_2) { ManagedServiceInstance.make(service_plan: plan2) }
1313

14-
it 'decorated the given hash with plan guid and name from service instances' do
14+
it 'decorated the given hash with plan guid, name and broker_catalog.id from service instances' do
1515
undecorated_hash = { foo: 'bar', included: { monkeys: %w[zach greg] } }
16-
decorator = described_class.new({ service_plan: %w[guid name foo] })
16+
decorator = described_class.new({ service_plan: %w[guid name broker_catalog.id foo] })
1717

1818
hash = decorator.decorate(undecorated_hash, [service_instance_1, service_instance_2])
1919

@@ -24,11 +24,17 @@ module VCAP::CloudController
2424
service_plans: [
2525
{
2626
guid: plan1.guid,
27-
name: plan1.name
27+
name: plan1.name,
28+
broker_catalog: {
29+
id: plan1.unique_id
30+
}
2831
},
2932
{
3033
guid: plan2.guid,
31-
name: plan2.name
34+
name: plan2.name,
35+
broker_catalog: {
36+
id: plan2.unique_id
37+
}
3238
}
3339
]
3440
}
@@ -93,7 +99,7 @@ module VCAP::CloudController
9399
end
94100

95101
describe '.match?' do
96-
it_behaves_like 'field decorator match?', 'service_plan', ['name', 'guid', 'relationships.service_offering']
102+
it_behaves_like 'field decorator match?', 'service_plan', ['name', 'guid', 'broker_catalog.id', 'relationships.service_offering']
97103
end
98104
end
99105
end

Diff for: spec/unit/messages/service_instance_show_message_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ module VCAP::CloudController
88

99
it_behaves_like 'field query parameter', 'space.organization', 'name,guid'
1010

11-
it_behaves_like 'field query parameter', 'service_plan', 'name,guid'
11+
it_behaves_like 'field query parameter', 'service_plan', 'name,guid,broker_catalog.id'
1212

13-
it_behaves_like 'field query parameter', 'service_plan.service_offering', 'name,guid,description,tags,documentation_url'
13+
it_behaves_like 'field query parameter', 'service_plan.service_offering', 'name,guid,description,tags,documentation_url,broker_catalog.id'
1414

1515
it_behaves_like 'field query parameter', 'service_plan.service_offering.service_broker', 'name,guid'
1616

Diff for: spec/unit/messages/service_instances_list_message_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ module VCAP::CloudController
8282

8383
it_behaves_like 'field query parameter', 'space.organization', 'name,guid'
8484

85-
it_behaves_like 'field query parameter', 'service_plan', 'guid,name,relationships.service_offering'
85+
it_behaves_like 'field query parameter', 'service_plan', 'guid,name,broker_catalog.id,relationships.service_offering'
8686

87-
it_behaves_like 'field query parameter', 'service_plan.service_offering', 'name,guid,description,documentation_url,tags,relationships.service_broker'
87+
it_behaves_like 'field query parameter', 'service_plan.service_offering', 'name,guid,description,documentation_url,tags,broker_catalog.id,relationships.service_broker'
8888

8989
it_behaves_like 'field query parameter', 'service_plan.service_offering.service_broker', 'name,guid'
9090
end

0 commit comments

Comments
 (0)