Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
397e39de
Commit
397e39de
authored
Apr 28, 2021
by
Vitali Tatarintev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Expose logs path from Environments API
Changelog: changed
parent
2268d9f2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
19 deletions
+86
-19
lib/api/entities/environment.rb
lib/api/entities/environment.rb
+27
-5
spec/fixtures/api/schemas/public_api/v4/environment.json
spec/fixtures/api/schemas/public_api/v4/environment.json
+3
-1
spec/requests/api/environments_spec.rb
spec/requests/api/environments_spec.rb
+56
-13
No files found.
lib/api/entities/environment.rb
View file @
397e39de
...
...
@@ -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
...
...
spec/fixtures/api/schemas/public_api/v4/environment.json
View file @
397e39de
...
...
@@ -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
}
spec/requests/api/environments_spec.rb
View file @
397e39de
...
...
@@ -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
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment