Commit e8c8a972 authored by Allison Browne's avatar Allison Browne Committed by Peter Leitzen

Add clusters_regex to metric urls file

This will be used to match the urls within the banzai filter
for cluster metric embeds
parent 34654cc8
# frozen_string_literal: true
# Manages url matching for metrics dashboards.
module EE
module Gitlab
module Metrics
module Dashboard
module Url
# Matches dashboard urls for a metric chart embed
# for cluster metrics
#
# EX - https://<host>/<namespace>/<project>/-/clusters/<cluster_id>/?group=Cluster%20Health&title=Memory%20Usage&y_label=Memory%20(GiB)
def clusters_regex
strong_memoize(:clusters_regex) do
regex_for_project_metrics(
%r{
/clusters
/(?<cluster_id>\d+)
[/]?
}x
)
end
end
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::Metrics::Dashboard::Url do
describe '#clusters_regex' do
let(:url) do
Gitlab::Routing.url_helpers.namespace_project_cluster_url(
'foo',
'bar',
'1',
group: 'Cluster Health',
title: 'Memory Usage',
y_label: 'Memory 20(GiB)',
anchor: 'title'
)
end
let(:expected_params) do
{
'url' => url,
'namespace' => 'foo',
'project' => 'bar',
'cluster_id' => '1',
'query' => '?group=Cluster+Health&title=Memory+Usage&y_label=Memory+20%28GiB%29',
'anchor' => '#title'
}
end
subject { described_class.clusters_regex }
it_behaves_like 'regex which matches url when expected'
end
end
......@@ -25,7 +25,7 @@ module Banzai
# Regular expression matching metrics urls
def link_pattern
Gitlab::Metrics::Dashboard::Url.regex
Gitlab::Metrics::Dashboard::Url.metrics_regex
end
private
......
......@@ -59,7 +59,7 @@ module Banzai
embed = Embed.new
url = node.attribute('data-dashboard-url').to_s
set_path_and_permission(embed, url, URL.regex, :read_environment)
set_path_and_permission(embed, url, URL.metrics_regex, :read_environment)
set_path_and_permission(embed, url, URL.grafana_regex, :read_project) unless embed.permission
embeds[node] = embed if embed.permission
......
......@@ -6,41 +6,36 @@ module Gitlab
module Dashboard
class Url
class << self
include Gitlab::Utils::StrongMemoize
# Matches urls for a metrics dashboard. This could be
# either the /metrics endpoint or the /metrics_dashboard
# endpoint.
#
# EX - https://<host>/<namespace>/<project>/environments/<env_id>/metrics
def regex
def metrics_regex
strong_memoize(:metrics_regex) do
regex_for_project_metrics(
%r{
(?<url>
#{gitlab_pattern}
#{project_pattern}
(?:\/\-)?
\/environments
\/(?<environment>\d+)
\/metrics
#{query_pattern}
#{anchor_pattern}
)
/environments
/(?<environment>\d+)
/metrics
}x
)
end
end
# Matches dashboard urls for a Grafana embed.
#
# EX - https://<host>/<namespace>/<project>/grafana/metrics_dashboard
def grafana_regex
strong_memoize(:grafana_regex) do
regex_for_project_metrics(
%r{
(?<url>
#{gitlab_pattern}
#{project_pattern}
(?:\/\-)?
\/grafana
\/metrics_dashboard
#{query_pattern}
#{anchor_pattern}
)
/grafana
/metrics_dashboard
}x
)
end
end
# Parses query params out from full url string into hash.
......@@ -62,11 +57,24 @@ module Gitlab
private
def gitlab_pattern
def regex_for_project_metrics(path_suffix_pattern)
%r{
(?<url>
#{gitlab_host_pattern}
#{project_path_pattern}
(?:/-)?
#{path_suffix_pattern}
#{query_pattern}
#{anchor_pattern}
)
}x
end
def gitlab_host_pattern
Regexp.escape(Gitlab.config.gitlab.url)
end
def project_pattern
def project_path_pattern
"\/#{Project.reference_pattern}"
end
......@@ -82,3 +90,5 @@ module Gitlab
end
end
end
Gitlab::Metrics::Dashboard::Url.extend_if_ee('::EE::Gitlab::Metrics::Dashboard::Url')
......@@ -3,38 +3,6 @@
require 'spec_helper'
describe Gitlab::Metrics::Dashboard::Url do
shared_examples_for 'a regex which matches the expected url' do
it { is_expected.to be_a Regexp }
it 'matches a metrics dashboard link with named params' do
expect(subject).to match url
subject.match(url) do |m|
expect(m.named_captures).to eq expected_params
end
end
end
shared_examples_for 'does not match non-matching urls' do
it 'does not match other gitlab urls that contain the term metrics' do
url = Gitlab::Routing.url_helpers.active_common_namespace_project_prometheus_metrics_url('foo', 'bar', :json)
expect(subject).not_to match url
end
it 'does not match other gitlab urls' do
url = Gitlab.config.gitlab.url
expect(subject).not_to match url
end
it 'does not match non-gitlab urls' do
url = 'https://www.super_awesome_site.com/'
expect(subject).not_to match url
end
end
describe '#regex' do
let(:url) do
Gitlab::Routing.url_helpers.metrics_namespace_project_environment_url(
......@@ -59,10 +27,9 @@ describe Gitlab::Metrics::Dashboard::Url do
}
end
subject { described_class.regex }
subject { described_class.metrics_regex }
it_behaves_like 'a regex which matches the expected url'
it_behaves_like 'does not match non-matching urls'
it_behaves_like 'regex which matches url when expected'
end
describe '#grafana_regex' do
......@@ -89,15 +56,14 @@ describe Gitlab::Metrics::Dashboard::Url do
subject { described_class.grafana_regex }
it_behaves_like 'a regex which matches the expected url'
it_behaves_like 'does not match non-matching urls'
it_behaves_like 'regex which matches url when expected'
end
describe '#build_dashboard_url' do
it 'builds the url for the dashboard endpoint' do
url = described_class.build_dashboard_url('foo', 'bar', 1)
expect(url).to match described_class.regex
expect(url).to match described_class.metrics_regex
end
end
end
# frozen_string_literal: true
RSpec.shared_examples 'regex which matches url when expected' do
it { is_expected.to be_a Regexp }
it 'matches a metrics dashboard link with named params' do
expect(subject).to match url
subject.match(url) do |m|
expect(m.named_captures).to eq expected_params
end
end
it 'does not match other gitlab urls that contain the term metrics' do
url = Gitlab::Routing.url_helpers.active_common_namespace_project_prometheus_metrics_url('foo', 'bar', :json)
expect(subject).not_to match url
end
it 'does not match other gitlab urls' do
url = Gitlab.config.gitlab.url
expect(subject).not_to match url
end
it 'does not match non-gitlab urls' do
url = 'https://www.super_awesome_site.com/'
expect(subject).not_to match url
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