Welcome to mirror list, hosted at ThFree Co, Russian Federation.

annotations.rb « dashboard « metrics « api « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 432fa3ac0c92c0fbd71a130362da7a48ae723c1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true

module API
  module Metrics
    module Dashboard
      class Annotations < Grape::API
        desc 'Create a new monitoring dashboard annotation' do
          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
            post ':id/metrics_dashboard/annotations' do
              annotations_source_object = annotations_source[:class].find(params[:id])

              not_found! unless Feature.enabled?(:metrics_dashboard_annotations, annotations_source_object.project)

              forbidden! unless can?(current_user, :create_metrics_dashboard_annotation, annotations_source_object)

              create_service_params = declared(params).merge(annotations_source[:create_service_param_key] => annotations_source_object)

              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)
              end
            end
          end
        end
      end
    end
  end
end