Commit 397e39de authored by Vitali Tatarintev's avatar Vitali Tatarintev

Expose logs path from Environments API

Changelog: changed
parent 2268d9f2
......@@ -3,21 +3,43 @@
module API
module Entities
class Environment < Entities::EnvironmentBasic
include RequestAwareEntity
include Gitlab::Utils::StrongMemoize
expose :project, using: Entities::BasicProjectDetails
expose :last_deployment, using: Entities::Deployment, if: { last_deployment: true }
expose :state
expose :enable_advanced_logs_querying, if: ->(_, _) { can_read_pod_logs? }
expose :enable_advanced_logs_querying, if: -> (*) { can_read_pod_logs? } do |environment|
environment.elastic_stack_available?
end
expose :logs_api_path, if: -> (*) { can_read_pod_logs? } do |environment|
if environment.elastic_stack_available?
elasticsearch_project_logs_path(environment.project, environment_name: environment.name, format: :json)
else
k8s_project_logs_path(environment.project, environment_name: environment.name, format: :json)
end
end
expose :gitlab_managed_apps_logs_path, if: -> (*) { can_read_pod_logs? && cluster } do |environment|
::Clusters::ClusterPresenter.new(cluster, current_user: current_user).gitlab_managed_apps_logs_path # rubocop: disable CodeReuse/Presenter
end
private
alias_method :environment, :object
def enable_advanced_logs_querying
environment.elastic_stack_available?
def can_read_pod_logs?
strong_memoize(:can_read_pod_logs) do
current_user&.can?(:read_pod_logs, environment.project)
end
end
def can_read_pod_logs?
current_user&.can?(:read_pod_logs, environment.project)
def cluster
strong_memoize(:cluster) do
environment&.last_deployment&.cluster
end
end
def current_user
......
......@@ -19,7 +19,9 @@
]
},
"state": { "type": "string" },
"enable_advanced_logs_querying": { "type": "boolean" }
"enable_advanced_logs_querying": { "type": "boolean" },
"logs_api_path": { "type": "string" },
"gitlab_managed_apps_logs_path": { "type": "string" }
},
"additionalProperties": false
}
......@@ -12,7 +12,7 @@ RSpec.describe API::Environments do
project.add_maintainer(user)
end
describe 'GET /projects/:id/environments' do
describe 'GET /projects/:id/environments', :aggregate_failures do
context 'as member of the project' do
it 'returns project environments' do
get api("/projects/#{project.id}/environments", user)
......@@ -26,17 +26,34 @@ RSpec.describe API::Environments do
expect(json_response.first['project']).to match_schema('public_api/v4/project')
expect(json_response.first['enable_advanced_logs_querying']).to eq(false)
expect(json_response.first).not_to have_key('last_deployment')
expect(json_response.first).not_to have_key('gitlab_managed_apps_logs_path')
end
context 'when elastic stack is available' do
before do
allow_next_found_instance_of(Environment) do |env|
allow(env).to receive(:elastic_stack_available?).and_return(true)
context 'when the user can read pod logs' do
context 'with successful deployment on cluster' do
let_it_be(:deployment) { create(:deployment, :on_cluster, :success, environment: environment, project: project) }
it 'returns environment with enable_advanced_logs_querying and logs_api_path' do
get api("/projects/#{project.id}/environments", user)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.size).to eq(1)
expect(json_response.first['gitlab_managed_apps_logs_path']).to eq(
"/#{project.full_path}/-/logs/k8s.json?cluster_id=#{deployment.cluster_id}"
)
end
end
context 'when the user can read pod logs' do
it 'returns environment with enable_advanced_logs_querying' do
context 'when elastic stack is available' do
before do
allow_next_found_instance_of(Environment) do |env|
allow(env).to receive(:elastic_stack_available?).and_return(true)
end
end
it 'returns environment with enable_advanced_logs_querying and logs_api_path' do
get api("/projects/#{project.id}/environments", user)
expect(response).to have_gitlab_http_status(:ok)
......@@ -44,29 +61,55 @@ RSpec.describe API::Environments do
expect(json_response).to be_an Array
expect(json_response.size).to eq(1)
expect(json_response.first['enable_advanced_logs_querying']).to eq(true)
expect(json_response.first['logs_api_path']).to eq(
"/#{project.full_path}/-/logs/elasticsearch.json?environment_name=#{environment.name}"
)
end
end
context 'when the user cannot read pod logs' do
context 'when elastic stack is not available' do
before do
allow_next_found_instance_of(User) do |user|
allow(user).to receive(:can?).and_call_original
allow(user).to receive(:can?).with(:read_pod_logs, project).and_return(false)
allow_next_found_instance_of(Environment) do |env|
allow(env).to receive(:elastic_stack_available?).and_return(false)
end
end
it 'does not contain enable_advanced_logs_querying' do
it 'returns environment with enable_advanced_logs_querying logs_api_path' do
get api("/projects/#{project.id}/environments", user)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.size).to eq(1)
expect(json_response.first).not_to have_key('enable_advanced_logs_querying')
expect(json_response.first['enable_advanced_logs_querying']).to eq(false)
expect(json_response.first['logs_api_path']).to eq(
"/#{project.full_path}/-/logs/k8s.json?environment_name=#{environment.name}"
)
end
end
end
context 'when the user cannot read pod logs' do
before do
allow_next_found_instance_of(User) do |user|
allow(user).to receive(:can?).and_call_original
allow(user).to receive(:can?).with(:read_pod_logs, project).and_return(false)
end
end
it 'does not contain enable_advanced_logs_querying' do
get api("/projects/#{project.id}/environments", user)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.size).to eq(1)
expect(json_response.first).not_to have_key('enable_advanced_logs_querying')
expect(json_response.first).not_to have_key('logs_api_path')
expect(json_response.first).not_to have_key('gitlab_managed_apps_logs_path')
end
end
context 'when filtering' do
let_it_be(:environment2) { create(:environment, project: project) }
......
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