From bffaf3cccf150fcfeff5c8bf707b2199bee6a3f7 Mon Sep 17 00:00:00 2001 From: Charlle Dias Date: Wed, 18 Jul 2018 13:17:19 -0300 Subject: [PATCH] Add event_streams as subcollection of the physical resources --- .../api/physical_chassis_controller.rb | 2 + .../api/physical_servers_controller.rb | 2 + .../api/physical_switches_controller.rb | 1 + .../api/subcollections/event_streams.rb | 9 ++++ config/api.yml | 12 +++++ spec/requests/physical_chassis_spec.rb | 49 +++++++++++++++++++ spec/requests/physical_servers_spec.rb | 48 ++++++++++++++++++ spec/requests/physical_switches_spec.rb | 49 +++++++++++++++++++ 8 files changed, 172 insertions(+) create mode 100644 app/controllers/api/subcollections/event_streams.rb diff --git a/app/controllers/api/physical_chassis_controller.rb b/app/controllers/api/physical_chassis_controller.rb index 0eeeb593ae..54101c812b 100644 --- a/app/controllers/api/physical_chassis_controller.rb +++ b/app/controllers/api/physical_chassis_controller.rb @@ -1,5 +1,7 @@ module Api class PhysicalChassisController < BaseController + include Subcollections::EventStreams + def refresh_resource(type, id, _data = nil) raise BadRequestError, "Must specify an id for refreshing a #{type} resource" if id.blank? diff --git a/app/controllers/api/physical_servers_controller.rb b/app/controllers/api/physical_servers_controller.rb index f3fa089519..109dfe9cd1 100644 --- a/app/controllers/api/physical_servers_controller.rb +++ b/app/controllers/api/physical_servers_controller.rb @@ -1,5 +1,7 @@ module Api class PhysicalServersController < BaseController + include Subcollections::EventStreams + def blink_loc_led_resource(type, id, _data) change_resource_state(:blink_loc_led, type, id) end diff --git a/app/controllers/api/physical_switches_controller.rb b/app/controllers/api/physical_switches_controller.rb index 8ba3e1dbc8..22f267c32d 100644 --- a/app/controllers/api/physical_switches_controller.rb +++ b/app/controllers/api/physical_switches_controller.rb @@ -1,6 +1,7 @@ module Api class PhysicalSwitchesController < BaseController include Api::Mixins::Operations + include Subcollections::EventStreams def refresh_resource(type, id, _data = nil) perform_action(:refresh_ems, type, id) diff --git a/app/controllers/api/subcollections/event_streams.rb b/app/controllers/api/subcollections/event_streams.rb new file mode 100644 index 0000000000..ed1282f07a --- /dev/null +++ b/app/controllers/api/subcollections/event_streams.rb @@ -0,0 +1,9 @@ +module Api + module Subcollections + module EventStreams + def event_streams_query_resource(object) + object.respond_to?(:event_where_clause) ? EventStream.where(object.event_where_clause) : [] + end + end + end +end diff --git a/config/api.yml b/config/api.yml index 8dfa1e7d88..4f82c19877 100644 --- a/config/api.yml +++ b/config/api.yml @@ -1063,6 +1063,7 @@ :description: Event Streams :options: - :collection + - :subcollection :verbs: *gp :klass: EventStream :collection_actions: @@ -1076,6 +1077,14 @@ :get: - :name: read :identifier: event_streams_show + :subcollection_actions: + :get: + - :name: read + :identifier: event_streams_show_list + :subresource_actions: + :get: + - :name: read + :identifier: event_streams_show :events: :description: Events :identifier: event @@ -1748,6 +1757,7 @@ :verbs: *gp :klass: PhysicalChassis :subcollections: + - :event_streams :collection_actions: :get: - :name: read @@ -1794,6 +1804,7 @@ :verbs: *gp :klass: PhysicalServer :subcollections: + - :event_streams :collection_actions: :get: - :name: read @@ -1884,6 +1895,7 @@ :verbs: *gp :klass: PhysicalSwitch :subcollections: + - :event_streams :collection_actions: :get: - :name: read diff --git a/spec/requests/physical_chassis_spec.rb b/spec/requests/physical_chassis_spec.rb index 5179c7032b..4a51b2fbf7 100644 --- a/spec/requests/physical_chassis_spec.rb +++ b/spec/requests/physical_chassis_spec.rb @@ -97,4 +97,53 @@ end end end + + describe "Subcollections" do + let(:physical_chassis) { FactoryGirl.create(:physical_chassis) } + let(:event_stream) { FactoryGirl.create(:event_stream, :physical_chassis_id => physical_chassis.id, :event_type => "Some Event") } + + context 'Events subcollection' do + context 'GET /api/physical_chassis/:id/event_streams' do + it 'returns the event_streams with an appropriate role' do + api_basic_authorize(collection_action_identifier(:event_streams, :read, :get)) + + expected = { + 'resources' => [ + { 'href' => api_one_physical_chassis_event_stream_url(nil, physical_chassis, event_stream) } + ] + } + get(api_one_physical_chassis_event_streams_url(nil, physical_chassis)) + + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + + it 'does not return the event_streams without an appropriate role' do + api_basic_authorize + get(api_one_physical_chassis_event_streams_url(nil, physical_chassis)) + + expect(response).to have_http_status(:forbidden) + end + end + + context 'GET /api/physical_chassis/:id/event_streams/:id' do + it 'returns the event_stream with an appropriate role' do + api_basic_authorize(action_identifier(:event_streams, :read, :resource_actions, :get)) + url = api_one_physical_chassis_event_stream_url(nil, physical_chassis, event_stream) + expected = { 'href' => url } + get(url) + + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + + it 'does not return the event_stream without an appropriate role' do + api_basic_authorize + get(api_one_physical_chassis_event_stream_url(nil, physical_chassis, event_stream)) + + expect(response).to have_http_status(:forbidden) + end + end + end + end end diff --git a/spec/requests/physical_servers_spec.rb b/spec/requests/physical_servers_spec.rb index 60390f3abc..9cf05fa977 100644 --- a/spec/requests/physical_servers_spec.rb +++ b/spec/requests/physical_servers_spec.rb @@ -575,4 +575,52 @@ end end end + describe "Subcollections" do + let(:physical_server) { FactoryGirl.create(:physical_server) } + let(:event_stream) { FactoryGirl.create(:event_stream, :physical_server_id => physical_server.id, :event_type => "Some Event") } + + context 'Events subcollection' do + context 'GET /api/physical_servers/:id/event_streams' do + it 'returns the event_streams with an appropriate role' do + api_basic_authorize(collection_action_identifier(:event_streams, :read, :get)) + + expected = { + 'resources' => [ + { 'href' => api_physical_server_event_stream_url(nil, physical_server, event_stream) } + ] + } + get(api_physical_server_event_streams_url(nil, physical_server)) + + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + + it 'does not return the event_streams without an appropriate role' do + api_basic_authorize + get(api_physical_server_event_streams_url(nil, physical_server)) + + expect(response).to have_http_status(:forbidden) + end + end + + context 'GET /api/physical_servers/:id/event_streams/:id' do + it 'returns the event_stream with an appropriate role' do + api_basic_authorize(action_identifier(:event_streams, :read, :resource_actions, :get)) + url = api_physical_server_event_stream_url(nil, physical_server, event_stream) + expected = { 'href' => url } + get(url) + + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + + it 'does not return the event_stream without an appropriate role' do + api_basic_authorize + get(api_physical_server_event_stream_url(nil, physical_server, event_stream)) + + expect(response).to have_http_status(:forbidden) + end + end + end + end end diff --git a/spec/requests/physical_switches_spec.rb b/spec/requests/physical_switches_spec.rb index 28f26f9771..d4edc2367b 100644 --- a/spec/requests/physical_switches_spec.rb +++ b/spec/requests/physical_switches_spec.rb @@ -172,4 +172,53 @@ end end end + + describe "Subcollections" do + let(:physical_switch) { FactoryGirl.create(:physical_switch) } + let(:event_stream) { FactoryGirl.create(:event_stream, :physical_switch_id => physical_switch.id, :event_type => "Some Event") } + + context 'Events subcollection' do + context 'GET /api/physical_switches/:id/event_streams' do + it 'returns the event_streams with an appropriate role' do + api_basic_authorize(collection_action_identifier(:event_streams, :read, :get)) + + expected = { + 'resources' => [ + { 'href' => api_physical_switch_event_stream_url(nil, physical_switch, event_stream) } + ] + } + get(api_physical_switch_event_streams_url(nil, physical_switch)) + + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + + it 'does not return the event_streams without an appropriate role' do + api_basic_authorize + get(api_physical_switch_event_streams_url(nil, physical_switch)) + + expect(response).to have_http_status(:forbidden) + end + end + + context 'GET /api/physical_switches/:id/event_streams/:id' do + it 'returns the event_stream with an appropriate role' do + api_basic_authorize(action_identifier(:event_streams, :read, :resource_actions, :get)) + url = api_physical_switch_event_stream_url(nil, physical_switch, event_stream) + expected = { 'href' => url } + get(url) + + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + + it 'does not return the event_stream without an appropriate role' do + api_basic_authorize + get(api_physical_switch_event_stream_url(nil, physical_switch, event_stream)) + + expect(response).to have_http_status(:forbidden) + end + end + end + end end