Commit d20b4a6a authored by Mikołaj Wawrzyniak's avatar Mikołaj Wawrzyniak Committed by Sean McGivern

Add path to gitab managed apps logs for cluster

When presenting list of clusters we should include link to
log explorer for gitlab managed apps if user can access it
parent ea3117fc
...@@ -36,7 +36,8 @@ module EnvironmentsHelper ...@@ -36,7 +36,8 @@ module EnvironmentsHelper
"environment-name": environment.name, "environment-name": environment.name,
"environments-path": project_environments_path(project, format: :json), "environments-path": project_environments_path(project, format: :json),
"environment-id": environment.id, "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 end
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
module Clusters module Clusters
class ClusterPresenter < Gitlab::View::Presenter::Delegated class ClusterPresenter < Gitlab::View::Presenter::Delegated
include ::Gitlab::Utils::StrongMemoize
include ActionView::Helpers::SanitizeHelper include ActionView::Helpers::SanitizeHelper
include ActionView::Helpers::UrlHelper include ActionView::Helpers::UrlHelper
include IconsHelper include IconsHelper
...@@ -60,6 +61,12 @@ module Clusters ...@@ -60,6 +61,12 @@ module Clusters
end end
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? def read_only_kubernetes_platform_fields?
!cluster.provided_by_user? !cluster.provided_by_user?
end end
...@@ -85,6 +92,16 @@ module Clusters ...@@ -85,6 +92,16 @@ module Clusters
ActionController::Base.helpers.image_path(path) ActionController::Base.helpers.image_path(path)
end 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 def clusterable
if cluster.group_type? if cluster.group_type?
cluster.group cluster.group
......
...@@ -16,4 +16,8 @@ class ClusterEntity < Grape::Entity ...@@ -16,4 +16,8 @@ class ClusterEntity < Grape::Entity
expose :path do |cluster| expose :path do |cluster|
Clusters::ClusterPresenter.new(cluster).show_path # rubocop: disable CodeReuse/Presenter Clusters::ClusterPresenter.new(cluster).show_path # rubocop: disable CodeReuse/Presenter
end 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 end
...@@ -10,6 +10,7 @@ class ClusterSerializer < BaseSerializer ...@@ -10,6 +10,7 @@ class ClusterSerializer < BaseSerializer
:cluster_type, :cluster_type,
:enabled, :enabled,
:environment_scope, :environment_scope,
:gitlab_managed_apps_logs_path,
:name, :name,
:nodes, :nodes,
:path, :path,
......
...@@ -114,4 +114,18 @@ RSpec.describe EnvironmentsHelper do ...@@ -114,4 +114,18 @@ RSpec.describe EnvironmentsHelper do
expect(subject).to eq(true) expect(subject).to eq(true)
end end
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 end
...@@ -289,4 +289,74 @@ RSpec.describe Clusters::ClusterPresenter do ...@@ -289,4 +289,74 @@ RSpec.describe Clusters::ClusterPresenter do
it_behaves_like 'cluster health data' it_behaves_like 'cluster health data'
end end
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 end
...@@ -3,8 +3,13 @@ ...@@ -3,8 +3,13 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe ClusterEntity do RSpec.describe ClusterEntity do
include Gitlab::Routing.url_helpers
describe '#as_json' do 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 context 'when provider type is gcp' do
let(:cluster) { create(:cluster, :instance, provider_type: :gcp, provider_gcp: provider) } let(:cluster) { create(:cluster, :instance, provider_type: :gcp, provider_gcp: provider) }
...@@ -40,7 +45,7 @@ RSpec.describe ClusterEntity do ...@@ -40,7 +45,7 @@ RSpec.describe ClusterEntity do
context 'when no application has been installed' do context 'when no application has been installed' do
let(:cluster) { create(:cluster, :instance) } 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 it 'contains helm as not_installable' do
expect(subject).not_to be_empty expect(subject).not_to be_empty
...@@ -50,5 +55,28 @@ RSpec.describe ClusterEntity do ...@@ -50,5 +55,28 @@ RSpec.describe ClusterEntity do
expect(helm[:status]).to eq(:not_installable) expect(helm[:status]).to eq(:not_installable)
end end
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
end end
...@@ -6,13 +6,14 @@ RSpec.describe ClusterSerializer do ...@@ -6,13 +6,14 @@ RSpec.describe ClusterSerializer do
let(:cluster) { create(:cluster, :project, provider_type: :user) } let(:cluster) { create(:cluster, :project, provider_type: :user) }
describe '#represent_list' do 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 it 'serializes attrs correctly' do
is_expected.to contain_exactly( is_expected.to contain_exactly(
:cluster_type, :cluster_type,
:enabled, :enabled,
:environment_scope, :environment_scope,
:gitlab_managed_apps_logs_path,
:name, :name,
:nodes, :nodes,
:path, :path,
...@@ -22,7 +23,7 @@ RSpec.describe ClusterSerializer do ...@@ -22,7 +23,7 @@ RSpec.describe ClusterSerializer do
end end
describe '#represent_status' do 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 context 'when provider type is gcp and cluster is errored' do
let(:cluster) 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