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
befda73e
Commit
befda73e
authored
Apr 22, 2020
by
Kirstie Cook
Committed by
Bob Van Landuyt
Apr 22, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add cluster annotations endpoint
Add cluster annotations endpoint specs
parent
9363545a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
101 additions
and
54 deletions
+101
-54
changelogs/unreleased/211460-annotations-clusters-endpoint.yml
...elogs/unreleased/211460-annotations-clusters-endpoint.yml
+5
-0
lib/api/metrics/dashboard/annotations.rb
lib/api/metrics/dashboard/annotations.rb
+19
-10
spec/requests/api/metrics/dashboard/annotations_spec.rb
spec/requests/api/metrics/dashboard/annotations_spec.rb
+77
-44
No files found.
changelogs/unreleased/211460-annotations-clusters-endpoint.yml
0 → 100644
View file @
befda73e
---
title
:
Create cluster annotations API endpoint
merge_request
:
29502
author
:
type
:
added
lib/api/metrics/dashboard/annotations.rb
View file @
befda73e
...
@@ -18,20 +18,29 @@ module API
...
@@ -18,20 +18,29 @@ module API
requires
:description
,
type:
String
,
desc:
'The description of the annotation'
requires
:description
,
type:
String
,
desc:
'The description of the annotation'
end
end
resource
:environments
do
ANNOTATIONS_SOURCES
=
[
post
':id/metrics_dashboard/annotations'
do
{
class:
::
Environment
,
resource: :environments
,
create_service_param_key: :environment
},
environment
=
::
Environment
.
find
(
params
[
:id
])
{
class:
Clusters
::
Cluster
,
resource: :clusters
,
create_service_param_key: :cluster
}
].
freeze
not_found!
unless
Feature
.
enabled?
(
:metrics_dashboard_annotations
,
environment
.
project
)
ANNOTATIONS_SOURCES
.
each
do
|
annotations_source
|
resource
annotations_source
[
:resource
]
do
post
':id/metrics_dashboard/annotations'
do
annotations_source_object
=
annotations_source
[
:class
].
find
(
params
[
:id
])
forbidden!
unless
can?
(
current_user
,
:create_metrics_dashboard_annotation
,
environmen
t
)
not_found!
unless
Feature
.
enabled?
(
:metrics_dashboard_annotations
,
annotations_source_object
.
projec
t
)
result
=
::
Metrics
::
Dashboard
::
Annotations
::
CreateService
.
new
(
current_user
,
declared
(
params
).
merge
(
environment:
environment
)).
execute
forbidden!
unless
can?
(
current_user
,
:create_metrics_dashboard_annotation
,
annotations_source_object
)
if
result
[
:status
]
==
:success
create_service_params
=
declared
(
params
).
merge
(
annotations_source
[
:create_service_param_key
]
=>
annotations_source_object
)
present
result
[
:annotation
],
with:
Entities
::
Metrics
::
Dashboard
::
Annotation
else
result
=
::
Metrics
::
Dashboard
::
Annotations
::
CreateService
.
new
(
current_user
,
create_service_params
).
execute
error!
(
result
,
400
)
if
result
[
:status
]
==
:success
present
result
[
:annotation
],
with:
Entities
::
Metrics
::
Dashboard
::
Annotation
else
error!
(
result
,
400
)
end
end
end
end
end
end
end
...
...
spec/requests/api/metrics/dashboard/annotations_spec.rb
View file @
befda73e
...
@@ -11,77 +11,110 @@ describe API::Metrics::Dashboard::Annotations do
...
@@ -11,77 +11,110 @@ describe API::Metrics::Dashboard::Annotations do
let
(
:ending_at
)
{
1
.
hour
.
from_now
.
iso8601
}
let
(
:ending_at
)
{
1
.
hour
.
from_now
.
iso8601
}
let
(
:params
)
{
attributes_for
(
:metrics_dashboard_annotation
,
environment:
environment
,
starting_at:
starting_at
,
ending_at:
ending_at
,
dashboard_path:
dashboard
)}
let
(
:params
)
{
attributes_for
(
:metrics_dashboard_annotation
,
environment:
environment
,
starting_at:
starting_at
,
ending_at:
ending_at
,
dashboard_path:
dashboard
)}
describe
'POST /environments/:environment_id/metrics_dashboard/annotations'
do
shared_examples
'POST /:source_type/:id/metrics_dashboard/annotations'
do
|
source_type
|
before
:all
do
let
(
:url
)
{
"/
#{
source_type
.
pluralize
}
/
#{
source
.
id
}
/metrics_dashboard/annotations"
}
before
do
project
.
add_developer
(
user
)
project
.
add_developer
(
user
)
end
end
context
'feature flag metrics_dashboard_annotations'
do
context
"with :source_type ==
#{
source_type
.
pluralize
}
"
do
context
'is on'
do
context
'feature flag metrics_dashboard_annotations'
do
before
do
context
'is on'
do
stub_feature_flags
(
metrics_dashboard_annotations:
{
enabled:
true
,
thing:
project
})
before
do
end
stub_feature_flags
(
metrics_dashboard_annotations:
{
enabled:
true
,
thing:
project
})
context
'with correct permissions'
do
context
'with valid parameters'
do
it
'creates a new annotation'
,
:aggregate_failures
do
post
api
(
"/environments/
#{
environment
.
id
}
/metrics_dashboard/annotations"
,
user
),
params:
params
expect
(
response
).
to
have_gitlab_http_status
(
:created
)
expect
(
json_response
[
'environment_id'
]).
to
eq
(
environment
.
id
)
expect
(
json_response
[
'starting_at'
].
to_time
).
to
eq
(
starting_at
.
to_time
)
expect
(
json_response
[
'ending_at'
].
to_time
).
to
eq
(
ending_at
.
to_time
)
expect
(
json_response
[
'description'
]).
to
eq
(
params
[
:description
])
expect
(
json_response
[
'dashboard_path'
]).
to
eq
(
dashboard
)
end
end
end
context
'with invalid parameters'
do
context
'with correct permissions'
do
it
'returns error messsage'
do
context
'with valid parameters'
do
post
api
(
"/environments/
#{
environment
.
id
}
/metrics_dashboard/annotations"
,
user
),
it
'creates a new annotation'
,
:aggregate_failures
do
params:
{
dashboard_path:
nil
,
starting_at:
nil
,
description:
nil
}
post
api
(
url
,
user
),
params:
params
expect
(
response
).
to
have_gitlab_http_status
(
:created
)
expect
(
json_response
[
"
#{
source_type
}
_id"
]).
to
eq
(
source
.
id
)
expect
(
json_response
[
'starting_at'
].
to_time
).
to
eq
(
starting_at
.
to_time
)
expect
(
json_response
[
'ending_at'
].
to_time
).
to
eq
(
ending_at
.
to_time
)
expect
(
json_response
[
'description'
]).
to
eq
(
params
[
:description
])
expect
(
json_response
[
'dashboard_path'
]).
to
eq
(
dashboard
)
end
end
expect
(
response
).
to
have_gitlab_http_status
(
:bad_request
)
context
'with invalid parameters'
do
expect
(
json_response
[
'message'
]).
to
include
({
"starting_at"
=>
[
"can't be blank"
],
"description"
=>
[
"can't be blank"
],
"dashboard_path"
=>
[
"can't be blank"
]
})
it
'returns error messsage'
do
post
api
(
url
,
user
),
params:
{
dashboard_path:
nil
,
starting_at:
nil
,
description:
nil
}
expect
(
response
).
to
have_gitlab_http_status
(
:bad_request
)
expect
(
json_response
[
'message'
]).
to
include
({
"starting_at"
=>
[
"can't be blank"
],
"description"
=>
[
"can't be blank"
],
"dashboard_path"
=>
[
"can't be blank"
]
})
end
end
context
'with undeclared params'
do
before
do
params
[
:undeclared_param
]
=
'xyz'
end
it
'filters out undeclared params'
do
expect
(
::
Metrics
::
Dashboard
::
Annotations
::
CreateService
).
to
receive
(
:new
).
with
(
user
,
hash_excluding
(
:undeclared_param
))
post
api
(
url
,
user
),
params:
params
end
end
end
end
end
context
'with undeclared params'
do
context
'without correct permissions'
do
let_it_be
(
:guest
)
{
create
(
:user
)
}
before
do
before
do
p
arams
[
:undeclared_param
]
=
'xyz'
p
roject
.
add_guest
(
guest
)
end
end
it
'filters out undeclared params'
do
expect
(
::
Metrics
::
Dashboard
::
Annotations
::
CreateService
).
to
receive
(
:new
).
with
(
user
,
hash_excluding
(
:undeclared_param
))
post
api
(
"/environments/
#{
environment
.
id
}
/metrics_dashboard/annotations"
,
user
),
params:
params
it
'returns error messsage'
do
post
api
(
url
,
guest
),
params:
params
expect
(
response
).
to
have_gitlab_http_status
(
:forbidden
)
end
end
end
end
end
end
context
'without correct permissions'
do
context
'is off'
do
let_it_be
(
:guest
)
{
create
(
:user
)
}
before
do
before
do
project
.
add_guest
(
guest
)
stub_feature_flags
(
metrics_dashboard_annotations:
{
enabled:
false
}
)
end
end
it
'returns error messsage'
do
it
'returns error messsage'
do
post
api
(
"/environments/
#{
environment
.
id
}
/metrics_dashboard/annotations"
,
guest
),
params:
params
post
api
(
url
,
user
),
params:
params
expect
(
response
).
to
have_gitlab_http_status
(
:
forbidden
)
expect
(
response
).
to
have_gitlab_http_status
(
:
not_found
)
end
end
end
end
end
end
context
'is off'
do
end
before
do
end
stub_feature_flags
(
metrics_dashboard_annotations:
{
enabled:
false
,
thing:
project
})
end
describe
'environment'
do
it_behaves_like
'POST /:source_type/:id/metrics_dashboard/annotations'
,
'environment'
do
let
(
:source
)
{
environment
}
end
end
it
'returns error messsage'
do
describe
'group cluster'
do
post
api
(
"/environments/
#{
environment
.
id
}
/metrics_dashboard/annotations"
,
user
),
params:
params
it_behaves_like
'POST /:source_type/:id/metrics_dashboard/annotations'
,
'cluster'
do
let_it_be
(
:group
)
{
create
(
:group
)
}
let_it_be
(
:cluster
)
{
create
(
:cluster_for_group
,
groups:
[
group
])
}
expect
(
response
).
to
have_gitlab_http_status
(
:not_found
)
before
do
end
group
.
add_developer
(
user
)
end
end
let
(
:source
)
{
cluster
}
end
end
describe
'project cluster'
do
it_behaves_like
'POST /:source_type/:id/metrics_dashboard/annotations'
,
'cluster'
do
let_it_be
(
:cluster
)
{
create
(
:cluster
,
projects:
[
project
])
}
let
(
:source
)
{
cluster
}
end
end
end
end
end
end
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