Commit f84f2d8b authored by rpereira2's avatar rpereira2

Accept variables parameter as Hash

- Change the format of the variables parameter in the Prometheus proxy
API from an array to a Hash.

- This is because the order of elements in an array parameter is not
guaranteed to be unchanged. Different middleware can seemingly
change the order of elements in an array query parameter.
parent 55c7d620
...@@ -32,8 +32,8 @@ module Prometheus ...@@ -32,8 +32,8 @@ module Prometheus
def validate_variables(_result) def validate_variables(_result)
return success unless variables return success unless variables
unless variables.is_a?(Array) && variables.size.even? unless variables.is_a?(ActionController::Parameters)
return error(_('Optional parameter "variables" must be an array of keys and values. Ex: [key1, value1, key2, value2]')) return error(_('Optional parameter "variables" must be a Hash. Ex: variables[key1]=value1'))
end end
success success
...@@ -88,12 +88,7 @@ module Prometheus ...@@ -88,12 +88,7 @@ module Prometheus
end end
def variables_hash def variables_hash
# .each_slice(2) converts ['key1', 'value1', 'key2', 'value2'] into variables.to_h
# [['key1', 'value1'], ['key2', 'value2']] which is then converted into
# a hash by to_h: {'key1' => 'value1', 'key2' => 'value2'}
# to_h will raise an ArgumentError if the number of elements in the original
# array is not even.
variables&.each_slice(2).to_h
end end
def query(result) def query(result)
......
...@@ -15019,7 +15019,7 @@ msgstr "" ...@@ -15019,7 +15019,7 @@ msgstr ""
msgid "Optional" msgid "Optional"
msgstr "" msgstr ""
msgid "Optional parameter \"variables\" must be an array of keys and values. Ex: [key1, value1, key2, value2]" msgid "Optional parameter \"variables\" must be a Hash. Ex: variables[key1]=value1"
msgstr "" msgstr ""
msgid "Optionally, you can %{link_to_customize} how FogBugz email addresses and usernames are imported into GitLab." msgid "Optionally, you can %{link_to_customize} how FogBugz email addresses and usernames are imported into GitLab."
......
...@@ -84,12 +84,12 @@ describe Projects::Environments::PrometheusApiController do ...@@ -84,12 +84,12 @@ describe Projects::Environments::PrometheusApiController do
before do before do
expected_params[:query] = %{up{pod_name="#{pod_name}"}} expected_params[:query] = %{up{pod_name="#{pod_name}"}}
expected_params[:variables] = ['pod_name', pod_name] expected_params[:variables] = { 'pod_name' => pod_name }
end end
it 'replaces variables with values' do it 'replaces variables with values' do
get :proxy, params: environment_params.merge( get :proxy, params: environment_params.merge(
query: 'up{pod_name="{{pod_name}}"}', variables: ['pod_name', pod_name] query: 'up{pod_name="{{pod_name}}"}', variables: { 'pod_name' => pod_name }
) )
expect(response).to have_gitlab_http_status(:success) expect(response).to have_gitlab_http_status(:success)
......
...@@ -64,7 +64,7 @@ describe Prometheus::ProxyVariableSubstitutionService do ...@@ -64,7 +64,7 @@ describe Prometheus::ProxyVariableSubstitutionService do
let(:params_keys) do let(:params_keys) do
{ {
query: 'up{pod_name="{{pod_name}}"}', query: 'up{pod_name="{{pod_name}}"}',
variables: ['pod_name', pod_name] variables: { 'pod_name' => pod_name }
} }
end end
...@@ -76,7 +76,7 @@ describe Prometheus::ProxyVariableSubstitutionService do ...@@ -76,7 +76,7 @@ describe Prometheus::ProxyVariableSubstitutionService do
let(:params_keys) do let(:params_keys) do
{ {
query: 'up{pod_name="{{pod_name}}",env="{{ci_environment_slug}}"}', query: 'up{pod_name="{{pod_name}}",env="{{ci_environment_slug}}"}',
variables: ['pod_name', pod_name, 'ci_environment_slug', 'custom_value'] variables: { 'pod_name' => pod_name, 'ci_environment_slug' => 'custom_value' }
} }
end end
...@@ -95,8 +95,7 @@ describe Prometheus::ProxyVariableSubstitutionService do ...@@ -95,8 +95,7 @@ describe Prometheus::ProxyVariableSubstitutionService do
} }
end end
it_behaves_like 'error', 'Optional parameter "variables" must be an ' \ it_behaves_like 'error', 'Optional parameter "variables" must be a Hash. Ex: variables[key1]=value1'
'array of keys and values. Ex: [key1, value1, key2, value2]'
end end
context 'with nil variables' do context 'with nil variables' do
......
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