Commit 833206ab authored by rpereira2's avatar rpereira2 Committed by Peter Leitzen

Use hash methods to filter params

- This is so that we don't have to check that params is of type
ActionController::Parameters in ProxyService.
parent b3aba120
...@@ -15,11 +15,11 @@ module Prometheus ...@@ -15,11 +15,11 @@ module Prometheus
PROXY_SUPPORT = { PROXY_SUPPORT = {
'query' => { 'query' => {
method: ['GET'], method: ['GET'],
params: [:query, :time, :timeout] params: %w(query time timeout)
}, },
'query_range' => { 'query_range' => {
method: ['GET'], method: ['GET'],
params: [:query, :start, :end, :step, :timeout] params: %w(query start end step timeout)
} }
}.freeze }.freeze
...@@ -44,7 +44,7 @@ module Prometheus ...@@ -44,7 +44,7 @@ module Prometheus
# Convert ActionController::Parameters to hash because reactive_cache_worker # Convert ActionController::Parameters to hash because reactive_cache_worker
# does not play nice with ActionController::Parameters. # does not play nice with ActionController::Parameters.
@params = permit_params(params, path).to_hash @params = filter_params(params, path).to_hash
@method = method @method = method
end end
...@@ -102,12 +102,8 @@ module Prometheus ...@@ -102,12 +102,8 @@ module Prometheus
prometheus_adapter&.can_query? prometheus_adapter&.can_query?
end end
def permit_params(params, path) def filter_params(params, path)
if params.is_a?(ActionController::Parameters) params.slice(*PROXY_SUPPORT.dig(path, :params))
params.permit(PROXY_SUPPORT.dig(path, :params))
else
params
end
end end
def can_proxy? def can_proxy?
......
...@@ -9,15 +9,15 @@ describe Prometheus::ProxyService do ...@@ -9,15 +9,15 @@ describe Prometheus::ProxyService do
set(:environment) { create(:environment, project: project) } set(:environment) { create(:environment, project: project) }
describe '#initialize' do describe '#initialize' do
let(:params) { ActionController::Parameters.new(query: '1') } let(:params) { ActionController::Parameters.new(query: '1').permit! }
it 'initializes attributes' do it 'initializes attributes' do
result = described_class.new(environment, 'GET', 'query', { query: '1' }) result = described_class.new(environment, 'GET', 'query', params)
expect(result.proxyable).to eq(environment) expect(result.proxyable).to eq(environment)
expect(result.method).to eq('GET') expect(result.method).to eq('GET')
expect(result.path).to eq('query') expect(result.path).to eq('query')
expect(result.params).to eq(query: '1') expect(result.params).to eq('query' => '1')
end end
it 'converts ActionController::Parameters into hash' do it 'converts ActionController::Parameters into hash' do
...@@ -27,7 +27,7 @@ describe Prometheus::ProxyService do ...@@ -27,7 +27,7 @@ describe Prometheus::ProxyService do
end end
context 'with unknown params' do context 'with unknown params' do
let(:params) { ActionController::Parameters.new(query: '1', other_param: 'val') } let(:params) { ActionController::Parameters.new(query: '1', other_param: 'val').permit! }
it 'filters unknown params' do it 'filters unknown params' do
result = described_class.new(environment, 'GET', 'query', params) result = described_class.new(environment, 'GET', 'query', params)
...@@ -39,7 +39,7 @@ describe Prometheus::ProxyService do ...@@ -39,7 +39,7 @@ describe Prometheus::ProxyService do
describe '#execute' do describe '#execute' do
let(:prometheus_adapter) { instance_double(PrometheusService) } let(:prometheus_adapter) { instance_double(PrometheusService) }
let(:params) { ActionController::Parameters.new(query: '1') } let(:params) { ActionController::Parameters.new(query: '1').permit! }
subject { described_class.new(environment, 'GET', 'query', params) } subject { described_class.new(environment, 'GET', 'query', params) }
...@@ -182,14 +182,14 @@ describe Prometheus::ProxyService do ...@@ -182,14 +182,14 @@ describe Prometheus::ProxyService do
describe '.from_cache' do describe '.from_cache' do
it 'initializes an instance of ProxyService class' do it 'initializes an instance of ProxyService class' do
result = described_class.from_cache( result = described_class.from_cache(
environment.class.name, environment.id, 'GET', 'query', { query: '1' } environment.class.name, environment.id, 'GET', 'query', { 'query' => '1' }
) )
expect(result).to be_an_instance_of(described_class) expect(result).to be_an_instance_of(described_class)
expect(result.proxyable).to eq(environment) expect(result.proxyable).to eq(environment)
expect(result.method).to eq('GET') expect(result.method).to eq('GET')
expect(result.path).to eq('query') expect(result.path).to eq('query')
expect(result.params).to eq(query: '1') expect(result.params).to eq('query' => '1')
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment