Commit 5589ff35 authored by Sean McGivern's avatar Sean McGivern

Merge branch '297340-improve-lcp-for-monitor-logs' into 'master'

Expose enable_advanced_logs_querying in Environments API.

See merge request gitlab-org/gitlab!59772
parents 36069d71 09705ef5
---
title: Expose enable_advanced_logs_querying in Environments API
merge_request: 59772
author:
type: changed
...@@ -6,6 +6,23 @@ module API ...@@ -6,6 +6,23 @@ module API
expose :project, using: Entities::BasicProjectDetails expose :project, using: Entities::BasicProjectDetails
expose :last_deployment, using: Entities::Deployment, if: { last_deployment: true } expose :last_deployment, using: Entities::Deployment, if: { last_deployment: true }
expose :state expose :state
expose :enable_advanced_logs_querying, if: ->(_, _) { can_read_pod_logs? }
private
alias_method :environment, :object
def enable_advanced_logs_querying
environment.elastic_stack_available?
end
def can_read_pod_logs?
current_user&.can?(:read_pod_logs, environment.project)
end
def current_user
options[:current_user]
end
end end
end end
end end
...@@ -18,7 +18,8 @@ ...@@ -18,7 +18,8 @@
{ "$ref": "deployment.json" } { "$ref": "deployment.json" }
] ]
}, },
"state": { "type": "string" } "state": { "type": "string" },
"enable_advanced_logs_querying": { "type": "boolean" }
}, },
"additionalProperties": false "additionalProperties": false
} }
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe API::Environments do RSpec.describe API::Environments do
let(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:non_member) { create(:user) } let_it_be(:non_member) { create(:user) }
let(:project) { create(:project, :private, :repository, namespace: user.namespace) } let_it_be(:project) { create(:project, :private, :repository, namespace: user.namespace) }
let!(:environment) { create(:environment, project: project) } let_it_be_with_reload(:environment) { create(:environment, project: project) }
before do before do
project.add_maintainer(user) project.add_maintainer(user)
...@@ -34,11 +34,51 @@ RSpec.describe API::Environments do ...@@ -34,11 +34,51 @@ RSpec.describe API::Environments do
expect(json_response.first['name']).to eq(environment.name) expect(json_response.first['name']).to eq(environment.name)
expect(json_response.first['external_url']).to eq(environment.external_url) expect(json_response.first['external_url']).to eq(environment.external_url)
expect(json_response.first['project'].keys).to contain_exactly(*project_data_keys) expect(json_response.first['project'].keys).to contain_exactly(*project_data_keys)
expect(json_response.first).not_to have_key("last_deployment") expect(json_response.first['enable_advanced_logs_querying']).to eq(false)
expect(json_response.first).not_to have_key('last_deployment')
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)
end
end
context 'when the user can read pod logs' do
it 'returns environment with 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['enable_advanced_logs_querying']).to eq(true)
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')
end
end
end end
context 'when filtering' do context 'when filtering' do
let!(:environment2) { create(:environment, project: project) } let_it_be(:environment2) { create(:environment, project: project) }
it 'returns environment by name' do it 'returns environment by name' do
get api("/projects/#{project.id}/environments?name=#{environment.name}", user) get api("/projects/#{project.id}/environments?name=#{environment.name}", user)
......
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