Commit 62c85255 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'mwaw/208790-expose-cluster-list-for-gitlab-managed-apps-logs-access' into 'master'

Expose gitlab managed apps logs path in cluster entities

See merge request gitlab-org/gitlab!36458
parents 57befefe d20b4a6a
......@@ -36,7 +36,8 @@ module EnvironmentsHelper
"environment-name": environment.name,
"environments-path": project_environments_path(project, format: :json),
"environment-id": environment.id,
"cluster-applications-documentation-path" => help_page_path('user/clusters/applications.md', anchor: 'elastic-stack')
"cluster-applications-documentation-path" => help_page_path('user/clusters/applications.md', anchor: 'elastic-stack'),
"clusters-path": project_clusters_path(project, format: :json)
}
end
......
......@@ -2,6 +2,7 @@
module Clusters
class ClusterPresenter < Gitlab::View::Presenter::Delegated
include ::Gitlab::Utils::StrongMemoize
include ActionView::Helpers::SanitizeHelper
include ActionView::Helpers::UrlHelper
include IconsHelper
......@@ -60,6 +61,12 @@ module Clusters
end
end
def gitlab_managed_apps_logs_path
return unless logs_project && can_read_cluster?
project_logs_path(logs_project, cluster_id: cluster.id)
end
def read_only_kubernetes_platform_fields?
!cluster.provided_by_user?
end
......@@ -85,6 +92,16 @@ module Clusters
ActionController::Base.helpers.image_path(path)
end
# currently log explorer is only available in the scope of the project
# for group and instance level cluster selected project does not affects
# fetching logs from gitlab managed apps namespace, therefore any project
# available to user will be sufficient.
def logs_project
strong_memoize(:logs_project) do
cluster.all_projects.first
end
end
def clusterable
if cluster.group_type?
cluster.group
......
......@@ -16,4 +16,8 @@ class ClusterEntity < Grape::Entity
expose :path do |cluster|
Clusters::ClusterPresenter.new(cluster).show_path # rubocop: disable CodeReuse/Presenter
end
expose :gitlab_managed_apps_logs_path do |cluster|
Clusters::ClusterPresenter.new(cluster, current_user: request.current_user).gitlab_managed_apps_logs_path # rubocop: disable CodeReuse/Presenter
end
end
......@@ -10,6 +10,7 @@ class ClusterSerializer < BaseSerializer
:cluster_type,
:enabled,
:environment_scope,
:gitlab_managed_apps_logs_path,
:name,
:nodes,
:path,
......
......@@ -114,4 +114,18 @@ RSpec.describe EnvironmentsHelper do
expect(subject).to eq(true)
end
end
describe '#environment_logs_data' do
it 'returns logs data' do
expected_data = {
"environment-name": environment.name,
"environments-path": project_environments_path(project, format: :json),
"environment-id": environment.id,
"cluster-applications-documentation-path" => help_page_path('user/clusters/applications.md', anchor: 'elastic-stack'),
"clusters-path": project_clusters_path(project, format: :json)
}
expect(helper.environment_logs_data(project, environment)).to eq(expected_data)
end
end
end
......@@ -289,4 +289,74 @@ RSpec.describe Clusters::ClusterPresenter do
it_behaves_like 'cluster health data'
end
end
describe '#gitlab_managed_apps_logs_path' do
context 'user can read logs' do
let(:project) { cluster.project }
before do
project.add_maintainer(user)
end
it 'returns path to logs' do
expect(presenter.gitlab_managed_apps_logs_path).to eq project_logs_path(project, cluster_id: cluster.id)
end
end
context 'group cluster' do
let(:cluster) { create(:cluster, cluster_type: :group_type, groups: [group]) }
let(:group) { create(:group, name: 'Foo') }
context 'user can read logs' do
before do
group.add_maintainer(user)
end
context 'there are projects within group' do
let!(:project) { create(:project, namespace: group) }
it 'returns path to logs' do
expect(presenter.gitlab_managed_apps_logs_path).to eq project_logs_path(project, cluster_id: cluster.id)
end
end
context 'there are no projects within group' do
it 'returns nil' do
expect(presenter.gitlab_managed_apps_logs_path).to be_nil
end
end
end
end
context 'instance cluster' do
let(:cluster) { create(:cluster, cluster_type: :instance_type) }
let!(:project) { create(:project) }
let(:user) { create(:admin) }
before do
project.add_maintainer(user)
stub_feature_flags(user_mode_in_session: false)
end
context 'user can read logs' do
it 'returns path to logs' do
expect(presenter.gitlab_managed_apps_logs_path).to eq project_logs_path(project, cluster_id: cluster.id)
end
end
end
context 'user can NOT read logs' do
let(:cluster) { create(:cluster, cluster_type: :instance_type) }
let!(:project) { create(:project) }
before do
project.add_developer(user)
stub_feature_flags(user_mode_in_session: false)
end
it 'returns nil' do
expect(presenter.gitlab_managed_apps_logs_path).to be_nil
end
end
end
end
......@@ -3,8 +3,13 @@
require 'spec_helper'
RSpec.describe ClusterEntity do
include Gitlab::Routing.url_helpers
describe '#as_json' do
subject { described_class.new(cluster).as_json }
let(:user) { nil }
let(:request) { EntityRequest.new({ current_user: user }) }
subject { described_class.new(cluster, request: request).as_json }
context 'when provider type is gcp' do
let(:cluster) { create(:cluster, :instance, provider_type: :gcp, provider_gcp: provider) }
......@@ -40,7 +45,7 @@ RSpec.describe ClusterEntity do
context 'when no application has been installed' do
let(:cluster) { create(:cluster, :instance) }
subject { described_class.new(cluster).as_json[:applications]}
subject { described_class.new(cluster, request: request).as_json[:applications]}
it 'contains helm as not_installable' do
expect(subject).not_to be_empty
......@@ -50,5 +55,28 @@ RSpec.describe ClusterEntity do
expect(helm[:status]).to eq(:not_installable)
end
end
context 'gitlab_managed_apps_logs_path' do
let(:cluster) { create(:cluster, :project) }
let(:user) { create(:user) }
subject { described_class.new(cluster, request: request).as_json }
before do
allow_next_instance_of(Clusters::ClusterPresenter) do |presenter|
allow(presenter).to receive(:show_path).and_return(nil)
end
end
it 'return projects log explorer path' do
log_explorer_path = project_logs_path(cluster.project, cluster_id: cluster.id)
expect_next_instance_of(Clusters::ClusterPresenter, cluster, current_user: user) do |presenter|
expect(presenter).to receive(:gitlab_managed_apps_logs_path).and_return(log_explorer_path)
end
expect(subject[:gitlab_managed_apps_logs_path]).to eq(log_explorer_path)
end
end
end
end
......@@ -6,13 +6,14 @@ RSpec.describe ClusterSerializer do
let(:cluster) { create(:cluster, :project, provider_type: :user) }
describe '#represent_list' do
subject { described_class.new.represent_list(cluster).keys }
subject { described_class.new(current_user: nil).represent_list(cluster).keys }
it 'serializes attrs correctly' do
is_expected.to contain_exactly(
:cluster_type,
:enabled,
:environment_scope,
:gitlab_managed_apps_logs_path,
:name,
:nodes,
:path,
......@@ -22,7 +23,7 @@ RSpec.describe ClusterSerializer do
end
describe '#represent_status' do
subject { described_class.new.represent_status(cluster).keys }
subject { described_class.new(current_user: nil).represent_status(cluster).keys }
context 'when provider type is gcp and cluster is errored' do
let(:cluster) 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