-
Notifications
You must be signed in to change notification settings - Fork 368
v3(services): introduce service route binding endpoint #1789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
blgm
merged 1 commit into
cloudfoundry:master
from
cloudfoundry-incubator:v3-service-route-bindings
Aug 18, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| require 'messages/service_route_binding_create_message' | ||
|
|
||
| class ServiceRouteBindingsController < ApplicationController | ||
| def create | ||
| message = parse_request | ||
|
|
||
| service_instance = fetch_service_instance(message.service_instance_guid) | ||
| route = fetch_route(message.route_guid) | ||
| check_space(service_instance, route) | ||
|
|
||
| head :not_implemented | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def parse_request | ||
| message = ServiceRouteBindingCreateMessage.new(hashed_params[:body]) | ||
| unprocessable!(message.errors.full_messages) unless message.valid? | ||
| message | ||
| end | ||
|
|
||
| def fetch_service_instance(guid) | ||
| service_instance = VCAP::CloudController::ServiceInstance.first(guid: guid) | ||
| unless service_instance && can_read_space?(service_instance.space) | ||
| unprocessable_service_instance!(guid) | ||
| end | ||
| service_instance | ||
| end | ||
|
|
||
| def fetch_route(guid) | ||
| route = VCAP::CloudController::Route.first(guid: guid) | ||
| unless route && can_read_space?(route.space) | ||
| unprocessable_route!(guid) | ||
| end | ||
| route | ||
| end | ||
|
|
||
| def check_space(service_instance, route) | ||
| space = service_instance.space | ||
| space_mismatch! unless space == route.space | ||
| unauthorized! unless can_write_space?(space) | ||
| end | ||
|
|
||
| def can_read_space?(space) | ||
| permission_queryer.can_read_from_space?(space.guid, space.organization_guid) | ||
| end | ||
|
|
||
| def can_write_space?(space) | ||
| permission_queryer.can_write_to_space?(space.guid) | ||
| end | ||
|
|
||
| def unprocessable_service_instance!(guid) | ||
| unprocessable!("The service instance could not be found: #{guid}") | ||
| end | ||
|
|
||
| def unprocessable_route!(guid) | ||
| unprocessable!("The route could not be found: #{guid}") | ||
| end | ||
|
|
||
| def space_mismatch! | ||
| unprocessable!('The service instance and the route are in different spaces.') | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| require 'messages/base_message' | ||
| require 'utils/hash_utils' | ||
|
|
||
| module VCAP::CloudController | ||
| class ServiceRouteBindingCreateMessage < BaseMessage | ||
| register_allowed_keys [:relationships] | ||
|
|
||
| validates_with NoAdditionalKeysValidator, RelationshipValidator | ||
|
|
||
| delegate :route_guid, :service_instance_guid, to: :relationships_message | ||
|
|
||
| def relationships_message | ||
| @relationships_message ||= Relationships.new(relationships.deep_symbolize_keys) | ||
| end | ||
|
|
||
| class Relationships < BaseMessage | ||
| register_allowed_keys [:service_instance, :route] | ||
|
|
||
| def route_guid | ||
| HashUtils.dig(route, :data, :guid) | ||
| end | ||
|
|
||
| def service_instance_guid | ||
| HashUtils.dig(service_instance, :data, :guid) | ||
| end | ||
|
|
||
| validates_with NoAdditionalKeysValidator | ||
|
|
||
| validates :service_instance, presence: true, allow_nil: false, to_one_relationship: true | ||
| validates :route, presence: true, allow_nil: false, to_one_relationship: true | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| require 'spec_helper' | ||
| require 'request_spec_shared_examples' | ||
|
|
||
| RSpec.describe 'v3 service route bindings' do | ||
| describe 'POST /v3/service_route_bindings' do | ||
| let(:api_call) { ->(user_headers) { post '/v3/service_route_bindings', request.to_json, user_headers } } | ||
|
|
||
| let(:service_instance) { VCAP::CloudController::UserProvidedServiceInstance.make(space: space) } | ||
| let(:route) { VCAP::CloudController::Route.make(space: space) } | ||
| let(:request) do | ||
| { | ||
| relationships: { | ||
| service_instance: { | ||
| data: { | ||
| guid: service_instance.guid | ||
| } | ||
| }, | ||
| route: { | ||
| data: { | ||
| guid: route.guid | ||
| } | ||
| } | ||
| } | ||
| } | ||
| end | ||
|
|
||
| describe 'permissions' do | ||
| it_behaves_like 'permissions for single object endpoint', ALL_PERMISSIONS do | ||
| let(:expected_codes_and_responses) do | ||
| Hash.new(code: 403).tap do |h| | ||
| h['admin'] = { code: 501 } | ||
| h['space_developer'] = { code: 501 } | ||
|
|
||
| h['no_role'] = { code: 422 } | ||
| h['org_auditor'] = { code: 422 } | ||
| h['org_billing_manager'] = { code: 422 } | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'errors' do | ||
| context 'invalid body' do | ||
| let(:request) do | ||
| { foo: 'bar' } | ||
| end | ||
|
|
||
| it 'fails with a 422 unprocessable' do | ||
| api_call.call(space_dev_headers) | ||
|
|
||
| expect(last_response).to have_status_code(422) | ||
| expect(parsed_response['errors']).to include( | ||
| include({ | ||
| 'detail' => "Unknown field(s): 'foo', Relationships 'relationships' is not an object", | ||
| 'title' => 'CF-UnprocessableEntity', | ||
| 'code' => 10008, | ||
| }) | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| context 'cannot read service instance' do | ||
| let(:service_instance) { VCAP::CloudController::UserProvidedServiceInstance.make } | ||
|
|
||
| it 'fails with a 422 unprocessable' do | ||
| api_call.call(space_dev_headers) | ||
|
|
||
| expect(last_response).to have_status_code(422) | ||
| expect(parsed_response['errors']).to include( | ||
| include({ | ||
| 'detail' => "The service instance could not be found: #{service_instance.guid}", | ||
| 'title' => 'CF-UnprocessableEntity', | ||
| 'code' => 10008, | ||
| }) | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| context 'cannot read route' do | ||
| let(:route) { VCAP::CloudController::Route.make } | ||
|
|
||
| it 'fails with a 422 unprocessable' do | ||
| api_call.call(space_dev_headers) | ||
|
|
||
| expect(last_response).to have_status_code(422) | ||
| expect(parsed_response['errors']).to include( | ||
| include({ | ||
| 'detail' => "The route could not be found: #{route.guid}", | ||
| 'title' => 'CF-UnprocessableEntity', | ||
| 'code' => 10008, | ||
| }) | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| context 'route and service instance in different spaces' do | ||
| let(:route) { VCAP::CloudController::Route.make } | ||
|
|
||
| it 'fails with a 422 unprocessable' do | ||
| api_call.call(admin_headers) | ||
|
|
||
| expect(last_response).to have_status_code(422) | ||
| expect(parsed_response['errors']).to include( | ||
| include({ | ||
| 'detail' => 'The service instance and the route are in different spaces.', | ||
| 'title' => 'CF-UnprocessableEntity', | ||
| 'code' => 10008, | ||
| }) | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| let(:user) { VCAP::CloudController::User.make } | ||
| let(:org) { VCAP::CloudController::Organization.make } | ||
| let(:space) { VCAP::CloudController::Space.make(organization: org) } | ||
|
|
||
| let(:space_dev_headers) do | ||
| org.add_user(user) | ||
| space.add_developer(user) | ||
| headers_for(user) | ||
| end | ||
| end |
64 changes: 64 additions & 0 deletions
64
spec/unit/messages/service_route_binding_create_message_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| require 'lightweight_spec_helper' | ||
| require 'messages/service_route_binding_create_message' | ||
|
|
||
| module VCAP::CloudController | ||
| RSpec.describe ServiceRouteBindingCreateMessage do | ||
| let(:body) do | ||
| { | ||
| relationships: { | ||
| service_instance: { | ||
| data: { guid: 'service-instance-guid' } | ||
| }, | ||
| route: { | ||
| data: { guid: 'route-guid' } | ||
| } | ||
| } | ||
| } | ||
| end | ||
|
|
||
| let(:message) { described_class.new(body) } | ||
|
|
||
| it 'accepts the allowed keys' do | ||
| expect(message).to be_valid | ||
| expect(message.requested?(:relationships)).to be_truthy | ||
| end | ||
|
|
||
| it 'builds the right message' do | ||
| expect(message.service_instance_guid).to eq('service-instance-guid') | ||
| expect(message.route_guid).to eq('route-guid') | ||
| end | ||
|
|
||
| describe 'validations' do | ||
| it 'is invalid when there are unknown keys' do | ||
| body[:parameters] = 'foo' | ||
| expect(message).to_not be_valid | ||
| expect(message.errors.full_messages).to include("Unknown field(s): 'parameters'") | ||
| end | ||
|
|
||
| describe 'service instance relationship' do | ||
| it 'fails when not present' do | ||
| body[:relationships][:service_instance] = nil | ||
| message.valid? | ||
| expect(message).to_not be_valid | ||
| expect(message.errors[:relationships]).to include( | ||
| "Service instance can't be blank", | ||
| /Service instance must be structured like this.*/ | ||
| ) | ||
| expect(message.errors[:relationships].count).to eq(2) | ||
| end | ||
| end | ||
|
|
||
| describe 'route relationship' do | ||
| it 'fails when not present' do | ||
| body[:relationships][:route] = nil | ||
| expect(message).to_not be_valid | ||
| expect(message.errors[:relationships]).to include( | ||
| "Route can't be blank", | ||
| /Route must be structured like this.*/, | ||
| ) | ||
| expect(message.errors[:relationships].count).to eq(2) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably fine to keep since there's likely a show or update or delete in the future, but just so you know, this is the key for the resource (opposed to resources/list) endpoints which by convention is keyed off as
:idin the params.