summaryrefslogtreecommitdiff
path: root/lib/api/metrics
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-05-20 14:34:42 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-05-20 14:34:42 +0000
commit9f46488805e86b1bc341ea1620b866016c2ce5ed (patch)
treef9748c7e287041e37d6da49e0a29c9511dc34768 /lib/api/metrics
parentdfc92d081ea0332d69c8aca2f0e745cb48ae5e6d (diff)
downloadgitlab-ce-9f46488805e86b1bc341ea1620b866016c2ce5ed.tar.gz
Add latest changes from gitlab-org/gitlab@13-0-stable-ee
Diffstat (limited to 'lib/api/metrics')
-rw-r--r--lib/api/metrics/dashboard/annotations.rb45
-rw-r--r--lib/api/metrics/user_starred_dashboards.rb46
2 files changed, 72 insertions, 19 deletions
diff --git a/lib/api/metrics/dashboard/annotations.rb b/lib/api/metrics/dashboard/annotations.rb
index 691abac863a..c8ec4d29657 100644
--- a/lib/api/metrics/dashboard/annotations.rb
+++ b/lib/api/metrics/dashboard/annotations.rb
@@ -8,30 +8,37 @@ module API
success Entities::Metrics::Dashboard::Annotation
end
- params do
- requires :starting_at, type: DateTime,
- desc: 'Date time indicating starting moment to which the annotation relates.'
- optional :ending_at, type: DateTime,
- desc: 'Date time indicating ending moment to which the annotation relates.'
- requires :dashboard_path, type: String,
- desc: 'The path to a file defining the dashboard on which the annotation should be added'
- requires :description, type: String, desc: 'The description of the annotation'
- end
+ ANNOTATIONS_SOURCES = [
+ { class: ::Environment, resource: :environments, create_service_param_key: :environment },
+ { class: Clusters::Cluster, resource: :clusters, create_service_param_key: :cluster }
+ ].freeze
+
+ ANNOTATIONS_SOURCES.each do |annotations_source|
+ resource annotations_source[:resource] do
+ params do
+ requires :starting_at, type: DateTime,
+ desc: 'Date time indicating starting moment to which the annotation relates.'
+ optional :ending_at, type: DateTime,
+ desc: 'Date time indicating ending moment to which the annotation relates.'
+ requires :dashboard_path, type: String, coerce_with: -> (val) { CGI.unescape(val) },
+ desc: 'The path to a file defining the dashboard on which the annotation should be added'
+ requires :description, type: String, desc: 'The description of the annotation'
+ end
- resource :environments do
- post ':id/metrics_dashboard/annotations' do
- environment = ::Environment.find(params[:id])
+ post ':id/metrics_dashboard/annotations' do
+ annotations_source_object = annotations_source[:class].find(params[:id])
- not_found! unless Feature.enabled?(:metrics_dashboard_annotations, environment.project)
+ forbidden! unless can?(current_user, :create_metrics_dashboard_annotation, annotations_source_object)
- forbidden! unless can?(current_user, :create_metrics_dashboard_annotation, environment)
+ create_service_params = declared(params).merge(annotations_source[:create_service_param_key] => annotations_source_object)
- result = ::Metrics::Dashboard::Annotations::CreateService.new(current_user, declared(params).merge(environment: environment)).execute
+ result = ::Metrics::Dashboard::Annotations::CreateService.new(current_user, create_service_params).execute
- if result[:status] == :success
- present result[:annotation], with: Entities::Metrics::Dashboard::Annotation
- else
- error!(result, 400)
+ if result[:status] == :success
+ present result[:annotation], with: Entities::Metrics::Dashboard::Annotation
+ else
+ error!(result, 400)
+ end
end
end
end
diff --git a/lib/api/metrics/user_starred_dashboards.rb b/lib/api/metrics/user_starred_dashboards.rb
new file mode 100644
index 00000000000..85fc0f33ed8
--- /dev/null
+++ b/lib/api/metrics/user_starred_dashboards.rb
@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+
+module API
+ module Metrics
+ class UserStarredDashboards < Grape::API
+ resource :projects do
+ desc 'Marks selected metrics dashboard as starred' do
+ success Entities::Metrics::UserStarredDashboard
+ end
+
+ params do
+ requires :dashboard_path, type: String, allow_blank: false, coerce_with: ->(val) { CGI.unescape(val) },
+ desc: 'Url encoded path to a file defining the dashboard to which the star should be added'
+ end
+
+ post ':id/metrics/user_starred_dashboards' do
+ result = ::Metrics::UsersStarredDashboards::CreateService.new(current_user, user_project, params[:dashboard_path]).execute
+
+ if result.success?
+ present result.payload, with: Entities::Metrics::UserStarredDashboard
+ else
+ error!({ errors: result.message }, 400)
+ end
+ end
+
+ desc 'Remove star from selected metrics dashboard'
+
+ params do
+ optional :dashboard_path, type: String, allow_blank: false, coerce_with: ->(val) { CGI.unescape(val) },
+ desc: 'Url encoded path to a file defining the dashboard from which the star should be removed'
+ end
+
+ delete ':id/metrics/user_starred_dashboards' do
+ result = ::Metrics::UsersStarredDashboards::DeleteService.new(current_user, user_project, params[:dashboard_path]).execute
+
+ if result.success?
+ status :ok
+ result.payload
+ else
+ status :bad_request
+ end
+ end
+ end
+ end
+ end
+end