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
def validate_variables(_result)
return success unless variables
unless variables.is_a?(Array) && variables.size.even?
return error(_('Optional parameter "variables" must be an array of keys and values. Ex: [key1, value1, key2, value2]'))
unless variables.is_a?(ActionController::Parameters)
return error(_('Optional parameter "variables" must be a Hash. Ex: variables[key1]=value1'))
end
success
......@@ -88,12 +88,7 @@ module Prometheus
end
def variables_hash
# .each_slice(2) converts ['key1', 'value1', 'key2', 'value2'] into
# [['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
variables.to_h
end
def query(result)
......
......@@ -15019,7 +15019,7 @@ msgstr ""
msgid "Optional"
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 ""
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
before do
expected_params[:query] = %{up{pod_name="#{pod_name}"}}
expected_params[:variables] = ['pod_name', pod_name]
expected_params[:variables] = { 'pod_name' => pod_name }
end
it 'replaces variables with values' do
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)
......
......@@ -64,7 +64,7 @@ describe Prometheus::ProxyVariableSubstitutionService do
let(:params_keys) do
{
query: 'up{pod_name="{{pod_name}}"}',
variables: ['pod_name', pod_name]
variables: { 'pod_name' => pod_name }
}
end
......@@ -76,7 +76,7 @@ describe Prometheus::ProxyVariableSubstitutionService do
let(:params_keys) do
{
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
......@@ -95,8 +95,7 @@ describe Prometheus::ProxyVariableSubstitutionService do
}
end
it_behaves_like 'error', 'Optional parameter "variables" must be an ' \
'array of keys and values. Ex: [key1, value1, key2, value2]'
it_behaves_like 'error', 'Optional parameter "variables" must be a Hash. Ex: variables[key1]=value1'
end
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