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

Commit 45d0d6a

Browse files
Get guid,name of service broker in service plan single request
[#172379944](https://www.pivotaltracker.com/story/show/172379944)
1 parent 9d8269f commit 45d0d6a

7 files changed

Lines changed: 129 additions & 2 deletions

File tree

app/controllers/v3/service_plans_controller.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'fetchers/service_plan_fetcher'
44
require 'controllers/v3/mixins/service_permissions'
55
require 'messages/service_plans_list_message'
6+
require 'messages/service_plans_show_message'
67
require 'actions/service_plan_delete'
78
require 'messages/metadata_update_message'
89
require 'actions/transactional_metadata_update'
@@ -16,11 +17,17 @@ class ServicePlansController < ApplicationController
1617
def show
1718
not_authenticated! if user_cannot_see_marketplace?
1819

20+
message = ServicePlansShowMessage.from_params(query_params)
21+
invalid_param!(message.errors.full_messages) unless message.valid?
22+
1923
service_plan = ServicePlanFetcher.fetch(hashed_params[:guid])
2024
service_plan_not_found! if service_plan.nil?
2125
service_plan_not_found! unless visible_to_current_user?(plan: service_plan)
2226

23-
presenter = Presenters::V3::ServicePlanPresenter.new(service_plan)
27+
decorators = []
28+
decorators << FieldServicePlanServiceBrokerDecorator.new(message.fields) if FieldServicePlanServiceBrokerDecorator.match?(message.fields)
29+
30+
presenter = Presenters::V3::ServicePlanPresenter.new(service_plan, decorators: decorators)
2431
render status: :ok, json: presenter.to_json
2532
end
2633

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module VCAP::CloudController
2+
class ServicePlansShowMessage < BaseMessage
3+
register_allowed_keys [:fields]
4+
5+
validates_with NoAdditionalParamsValidator
6+
validates :fields, allow_nil: true, fields: {
7+
allowed: {
8+
'service_offering.service_broker' => ['guid', 'name']
9+
}
10+
}
11+
12+
def self.from_params(params)
13+
super(params, [], fields: %w(fields))
14+
end
15+
end
16+
end

app/presenters/v3/service_plan_presenter.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ServicePlanPresenter < BasePresenter
88
include VCAP::CloudController::Presenters::Mixins::MetadataPresentationHelpers
99

1010
def to_hash
11-
{
11+
hash = {
1212
guid: service_plan.guid,
1313
created_at: service_plan.created_at,
1414
updated_at: service_plan.updated_at,
@@ -43,6 +43,8 @@ def to_hash
4343
},
4444
links: links
4545
}
46+
47+
@decorators.reduce(hash) { |memo, d| d.decorate(memo, [service_plan]) }
4648
end
4749

4850
private

docs/v3/source/includes/experimental_resources/service_plans/_get.md.erb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ This endpoint retrieves the service plan by GUID.
2626
#### Definition
2727
`GET /v3/service_plans/:guid`
2828

29+
#### Query parameters
30+
31+
Name | Type | Description
32+
---- | ---- | ------------
33+
**fields** | [_fields parameter_](#fields-parameter) | [_Allowed values_](#service-plan-fields)
34+
35+
##### Service Plan Fields
36+
37+
Resource | Allowed Keys
38+
------------------- | ----
39+
service_offering.service_broker| `guid`, `name`
40+
41+
2942
#### Permitted roles
3043
|
3144
--- | ---

spec/request/service_plans_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@
127127
expect(parsed_response).to eq(link_response)
128128
end
129129
end
130+
131+
describe 'fields' do
132+
let!(:service_plan) { VCAP::CloudController::ServicePlan.make }
133+
let(:guid) { service_plan.guid }
134+
135+
it 'can include service broker name and guid' do
136+
get "/v3/service_plans/#{guid}?fields[service_offering.service_broker]=name,guid", nil, admin_headers
137+
expect(last_response).to have_status_code(200)
138+
139+
expect(parsed_response['included']['service_brokers']).to have(1).elements
140+
expect(parsed_response['included']['service_brokers'][0]['guid']).to eq(service_plan.service.service_broker.guid)
141+
expect(parsed_response['included']['service_brokers'][0]['name']).to eq(service_plan.service.service_broker.name)
142+
end
143+
end
130144
end
131145

132146
describe 'GET /v3/service_plans' do
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require 'spec_helper'
2+
require 'messages/service_plans_show_message'
3+
require 'field_message_spec_shared_examples'
4+
5+
module VCAP::CloudController
6+
RSpec.describe ServicePlansShowMessage do
7+
describe '.from_params' do
8+
let(:params) do
9+
{
10+
'fields' => { 'service_offering.service_broker' => 'guid,name' },
11+
}.with_indifferent_access
12+
end
13+
14+
it 'returns the correct ServicePlansShowMessage' do
15+
message = described_class.from_params(params)
16+
17+
expect(message).to be_valid
18+
expect(message).to be_a(described_class)
19+
expect(message.fields).to match({ 'service_offering.service_broker': ['guid', 'name'] })
20+
end
21+
22+
it 'converts requested keys to symbols' do
23+
message = described_class.from_params(params)
24+
expect(message.requested?(:fields)).to be_truthy
25+
end
26+
27+
it 'accepts an empty set' do
28+
message = described_class.from_params({})
29+
expect(message).to be_valid
30+
end
31+
32+
it 'does not accept arbitrary parameters' do
33+
message = described_class.from_params({ foobar: 'pants' }.with_indifferent_access)
34+
35+
expect(message).not_to be_valid
36+
expect(message.errors[:base][0]).to include("Unknown query parameter(s): 'foobar'")
37+
end
38+
39+
context 'fields' do
40+
it 'validates `fields` is a hash' do
41+
message = described_class.from_params({ 'fields' => 'foo' }.with_indifferent_access)
42+
expect(message).not_to be_valid
43+
expect(message.errors[:fields][0]).to include('must be an object')
44+
end
45+
46+
it_behaves_like 'field query parameter', 'service_offering.service_broker', 'guid,name'
47+
48+
it 'does not accept fields resources that are not allowed' do
49+
message = described_class.from_params({ 'fields' => { 'space.foo': 'name' } })
50+
expect(message).not_to be_valid
51+
expect(message.errors[:fields]).to include(
52+
"[space.foo] valid resources are: 'service_offering.service_broker'"
53+
)
54+
end
55+
end
56+
end
57+
end
58+
end

spec/unit/presenters/v3/service_plan_presenter_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,22 @@
234234
})
235235
end
236236
end
237+
238+
context 'when a decorator is provided' do
239+
class FakeDecorator
240+
def self.decorate(hash, resources)
241+
hash[:included] = { resource: { guid: resources[0].guid } }
242+
hash
243+
end
244+
end
245+
246+
let(:result) { described_class.new(service_plan, decorators: [FakeDecorator]).to_hash.deep_symbolize_keys }
247+
248+
let(:service_plan) { VCAP::CloudController::ServicePlan.make }
249+
250+
it 'uses the decorator' do
251+
expect(result[:included]).to match({ resource: { guid: service_plan.guid } })
252+
end
253+
end
237254
end
238255
end

0 commit comments

Comments
 (0)