Skip to content

Commit 5c867e5

Browse files
authored
Revert "Tighten the semantics of POST /v3/service_plans/:guid/visibility (#4609)" (#4975)
This reverts commit 1306e1c.
1 parent ab9c4c5 commit 5c867e5

File tree

7 files changed

+44
-74
lines changed

7 files changed

+44
-74
lines changed

app/actions/v3/service_plan_visibility_update.rb

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,16 @@ class UnprocessableRequest < ::StandardError
88
end
99

1010
def update(service_plan, message, append_organizations: false)
11-
requested_type = message.type
11+
type = message.type
1212
requested_org_guids = message.organizations&.pluck(:guid) || []
1313

14-
unprocessable!("cannot update plans with visibility type 'space'") if space?(service_plan.visibility_type)
15-
16-
if append_organizations # i.e. POST request
17-
unprocessable!("type must be 'organization'") unless org?(requested_type)
18-
unprocessable!("can only append organizations to plans with visibility type 'organization'") unless org?(service_plan.visibility_type)
19-
end
14+
unprocessable!("cannot update plans with visibility type 'space'") if space?(service_plan)
2015

2116
service_plan.db.transaction do
2217
service_plan.lock!
23-
service_plan.public = public?(requested_type)
18+
service_plan.public = public?(type)
2419
service_plan.save
25-
if org?(requested_type)
20+
if org?(type)
2621
update_service_plan_visibilities(service_plan, requested_org_guids, append_organizations)
2722
else
2823
service_plan.remove_all_service_plan_visibilities
@@ -74,8 +69,8 @@ def unprocessable!(message)
7469
raise UnprocessableRequest.new(message)
7570
end
7671

77-
def space?(type)
78-
type == VCAP::CloudController::ServicePlanVisibilityTypes::SPACE
72+
def space?(service_plan)
73+
service_plan.visibility_type == VCAP::CloudController::ServicePlanVisibilityTypes::SPACE
7974
end
8075

8176
def org?(type)

app/controllers/v3/service_plan_visibility_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ def event_repository
5959
VCAP::CloudController::Repositories::ServiceEventRepository::WithUserActor.new(user_audit_info)
6060
end
6161

62-
def update_visibility(append_organizations: false)
62+
def update_visibility(opts={})
6363
service_plan = ServicePlanFetcher.fetch(hashed_params[:guid])
6464
service_plan_not_found! unless service_plan.present? && visible_to_current_user?(plan: service_plan)
6565
unauthorized! unless current_user_can_write?(service_plan)
6666

6767
message = ServicePlanVisibilityUpdateMessage.new(hashed_params[:body])
6868
bad_request!(message.errors.full_messages) unless message.valid?
6969

70-
updated_service_plan = V3::ServicePlanVisibilityUpdate.new.update(service_plan, message, append_organizations:)
70+
updated_service_plan = V3::ServicePlanVisibilityUpdate.new.update(service_plan, message, **opts)
7171
event_repository.record_service_plan_update_visibility_event(service_plan, message.audit_hash)
7272
updated_service_plan
7373
rescue V3::ServicePlanVisibilityUpdate::UnprocessableRequest => e

docs/v3/source/includes/resources/service_plan_visibility/_apply.md.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Content-Type: application/json
2828
<%= yield_content :service_plan_visibility_response %>
2929
```
3030

31-
This endpoint appends to the existing list of organizations. See [PATCH service plan visibility endpoint](#update-a-service-plan-visibility) to replace the existing list or change the visibility `type`.
31+
This endpoint applies a service plan visibility. It behaves similar to the [PATCH service plan visibility endpoint](#update-a-service-plan-visibility) but this endpoint will append to the existing list of organizations when the service plan is `organization` visible.
3232

3333
#### Definition
3434
`POST /v3/service_plans/:guid/visibility`
@@ -37,8 +37,8 @@ This endpoint appends to the existing list of organizations. See [PATCH service
3737

3838
Name | Type | Description
3939
---- | ---- | -----------
40-
**type** | _string_ | Denotes the visibility of the plan; must be `organization`, see [_list of visibility types_](#list-of-visibility-types)
41-
**organizations** | _array of objects_ | Desired list of organizations GUIDs where the plan will be accessible; required.
40+
**type** | _string_ | Denotes the visibility of the plan; can be `public`, `admin`, `organization`, see [_list of visibility types_](#list-of-visibility-types)
41+
**organizations** | _array of objects_ | Desired list of organizations GUIDs where the plan will be accessible; required if `type` is `organization`
4242

4343
#### Permitted roles
4444
|

docs/v3/source/includes/resources/service_plan_visibility/_update.md.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Content-Type: application/json
2828
<%= yield_content :new_org_service_plan_visibility %>
2929
```
3030

31-
This endpoint updates a service plan visibility. When `type` is `organization`, it will **replace** the existing list of organizations. See [POST service plan visibility endpoint](#apply-a-service-plan-visibility) to append to the existing list.
31+
This endpoint updates a service plan visibility. It behaves similar to the [POST service plan visibility endpoint](#apply-a-service-plan-visibility) but this endpoint will replace the existing list of organizations when the service plan is `organization` visible.
3232

3333
#### Definition
3434
`PATCH /v3/service_plans/:guid/visibility`

spec/request/service_plan_visibility_spec.rb

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,21 @@
454454
let(:service_plan) { VCAP::CloudController::ServicePlan.make(public: true) }
455455
let(:body) { { type: 'organization', organizations: [{ guid: org.guid }] } }
456456

457-
it 'returns a 422 bad request' do
457+
it 'updates the visibility type AND add the orgs' do
458458
post api_url, body.to_json, admin_headers
459459

460-
expect(last_response).to have_status_code(422)
461-
expect(parsed_response['errors'].first['detail']).to include("can only append organizations to plans with visibility type 'organization'")
460+
expect(parsed_response['type']).to eq 'organization'
461+
expect(parsed_response).not_to have_key('organizations')
462+
end
463+
464+
it 'creates an audit event' do
465+
post api_url, body.to_json, admin_headers
466+
event = VCAP::CloudController::Event.find(type: 'audit.service_plan_visibility.update')
467+
expect(event).to be
468+
expect(event.actee).to eq(service_plan.guid)
469+
expect(event.data).to include({
470+
'request' => body.with_indifferent_access
471+
})
462472
end
463473
end
464474

@@ -509,11 +519,24 @@
509519
context 'when request type is not "organization"' do
510520
let(:body) { { type: 'public' } }
511521

512-
it 'returns a 422 bad request' do
522+
it 'behaves like a PATCH' do
513523
post api_url, body.to_json, admin_headers
524+
expect(last_response).to have_status_code(200)
514525

515-
expect(last_response).to have_status_code(422)
516-
expect(parsed_response['errors'].first['detail']).to include("type must be 'organization'")
526+
get api_url, {}, admin_headers
527+
expect(parsed_response).to eq({ 'type' => 'public' })
528+
visibilities = VCAP::CloudController::ServicePlanVisibility.where(service_plan:).all
529+
expect(visibilities).to be_empty
530+
end
531+
532+
it 'creates an audit event' do
533+
post api_url, body.to_json, admin_headers
534+
event = VCAP::CloudController::Event.find(type: 'audit.service_plan_visibility.update')
535+
expect(event).to be
536+
expect(event.actee).to eq(service_plan.guid)
537+
expect(event.data).to include({
538+
'request' => body.with_indifferent_access
539+
})
517540
end
518541
end
519542

spec/support/lifecycle_tests_helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def catalog
8888
end
8989

9090
def make_plan_visible(plan_guid)
91-
patch "v3/service_plans/#{plan_guid}/visibility", { type: 'public' }.to_json, admin_headers
91+
post "v3/service_plans/#{plan_guid}/visibility", { type: 'public' }.to_json, admin_headers
9292
expect(last_response).to have_status_code(200)
9393

9494
get "v3/service_plans/#{plan_guid}/visibility", nil, admin_headers

spec/unit/actions/v3/service_plan_visibility_update_spec.rb

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,9 @@ module V3
1919
updated_plan = subject.update(service_plan, message)
2020
expect(updated_plan.reload.visibility_type).to eq 'public'
2121
end
22-
23-
context "when 'append_orgs' is set to true" do
24-
it 'raises an error' do
25-
expect do
26-
subject.update(service_plan, message, append_organizations: true)
27-
end.to raise_error(ServicePlanVisibilityUpdate::UnprocessableRequest, "type must be 'organization'")
28-
end
29-
end
3022
end
3123

32-
context 'and its being updated to "organization"' do
24+
context 'and its being updated do "organization"' do
3325
let(:org_guid) { Organization.make.guid }
3426
let(:other_org_guid) { Organization.make.guid }
3527
let(:params) do
@@ -46,14 +38,6 @@ module V3
4638

4739
expect(visible_org_guids).to contain_exactly(org_guid, other_org_guid)
4840
end
49-
50-
context "when 'append_orgs' is set to true" do
51-
it 'raises an error' do
52-
expect do
53-
subject.update(service_plan, message, append_organizations: true)
54-
end.to raise_error(ServicePlanVisibilityUpdate::UnprocessableRequest, "can only append organizations to plans with visibility type 'organization'")
55-
end
56-
end
5741
end
5842
end
5943

@@ -67,17 +51,9 @@ module V3
6751
updated_plan = subject.update(service_plan, message)
6852
expect(updated_plan.reload.visibility_type).to eq 'admin'
6953
end
70-
71-
context "when 'append_orgs' is set to true" do
72-
it 'raises an error' do
73-
expect do
74-
subject.update(service_plan, message, append_organizations: true)
75-
end.to raise_error(ServicePlanVisibilityUpdate::UnprocessableRequest, "type must be 'organization'")
76-
end
77-
end
7854
end
7955

80-
context 'and its being updated to "organization"' do
56+
context 'and its being updated do "organization"' do
8157
let(:org_guid) { Organization.make.guid }
8258
let(:other_org_guid) { Organization.make.guid }
8359
let(:params) do
@@ -94,14 +70,6 @@ module V3
9470

9571
expect(visible_org_guids).to contain_exactly(org_guid, other_org_guid)
9672
end
97-
98-
context "when 'append_orgs' is set to true" do
99-
it 'raises an error' do
100-
expect do
101-
subject.update(service_plan, message, append_organizations: true)
102-
end.to raise_error(ServicePlanVisibilityUpdate::UnprocessableRequest, "can only append organizations to plans with visibility type 'organization'")
103-
end
104-
end
10573
end
10674
end
10775

@@ -168,14 +136,6 @@ module V3
168136
expect(updated_plan.service_plan_visibilities).to be_empty
169137
expect(ServicePlanVisibility.where(service_plan:).all).to be_empty
170138
end
171-
172-
context "when 'append_orgs' is set to true" do
173-
it 'raises an error' do
174-
expect do
175-
subject.update(service_plan, message, append_organizations: true)
176-
end.to raise_error(ServicePlanVisibilityUpdate::UnprocessableRequest, "type must be 'organization'")
177-
end
178-
end
179139
end
180140

181141
context 'and its being updated to "public"' do
@@ -187,14 +147,6 @@ module V3
187147
expect(updated_plan.service_plan_visibilities).to be_empty
188148
expect(ServicePlanVisibility.where(service_plan:).all).to be_empty
189149
end
190-
191-
context "when 'append_orgs' is set to true" do
192-
it 'raises an error' do
193-
expect do
194-
subject.update(service_plan, message, append_organizations: true)
195-
end.to raise_error(ServicePlanVisibilityUpdate::UnprocessableRequest, "type must be 'organization'")
196-
end
197-
end
198150
end
199151
end
200152

0 commit comments

Comments
 (0)