diff options
Diffstat (limited to 'app/services/metrics/dashboard/annotations/create_service.rb')
-rw-r--r-- | app/services/metrics/dashboard/annotations/create_service.rb | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/app/services/metrics/dashboard/annotations/create_service.rb b/app/services/metrics/dashboard/annotations/create_service.rb new file mode 100644 index 00000000000..c04f4c56b51 --- /dev/null +++ b/app/services/metrics/dashboard/annotations/create_service.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +# Create Metrics::Dashboard::Annotation entry based on matched dashboard_path, environment, cluster +module Metrics + module Dashboard + module Annotations + class CreateService < ::BaseService + include Stepable + + steps :authorize_environment_access, + :authorize_cluster_access, + :parse_dashboard_path, + :create + + def initialize(user, params) + @user, @params = user, params + end + + def execute + execute_steps + end + + private + + attr_reader :user, :params + + def authorize_environment_access(options) + if environment.nil? || Ability.allowed?(user, :create_metrics_dashboard_annotation, project) + options[:environment] = environment + success(options) + else + error(s_('Metrics::Dashboard::Annotation|You are not authorized to create annotation for selected environment')) + end + end + + def authorize_cluster_access(options) + if cluster.nil? || Ability.allowed?(user, :create_metrics_dashboard_annotation, cluster) + options[:cluster] = cluster + success(options) + else + error(s_('Metrics::Dashboard::Annotation|You are not authorized to create annotation for selected cluster')) + end + end + + def parse_dashboard_path(options) + dashboard_path = params[:dashboard_path] + + Gitlab::Metrics::Dashboard::Finder.find_raw(project, dashboard_path: dashboard_path) + options[:dashboard_path] = dashboard_path + + success(options) + rescue Gitlab::Template::Finders::RepoTemplateFinder::FileNotFoundError + error(s_('Metrics::Dashboard::Annotation|Dashboard with requested path can not be found')) + end + + def create(options) + annotation = Annotation.new(options.slice(:environment, :cluster, :dashboard_path).merge(params.slice(:description, :starting_at, :ending_at))) + + if annotation.save + success(annotation: annotation) + else + error(annotation.errors) + end + end + + def environment + params[:environment] + end + + def cluster + params[:cluster] + end + + def project + (environment || cluster)&.project + end + end + end + end +end |