2020-04-22 19:07:51 +05:30
|
|
|
# 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)
|
2021-04-29 21:17:54 +05:30
|
|
|
@user = user
|
|
|
|
@params = params
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
execute_steps
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :user, :params
|
|
|
|
|
|
|
|
def authorize_environment_access(options)
|
2023-05-27 22:25:52 +05:30
|
|
|
if environment.nil? || Ability.allowed?(user, :admin_metrics_dashboard_annotation, project)
|
2020-04-22 19:07:51 +05:30
|
|
|
options[:environment] = environment
|
|
|
|
success(options)
|
|
|
|
else
|
2021-11-18 22:05:49 +05:30
|
|
|
error(s_('MetricsDashboardAnnotation|You are not authorized to create annotation for selected environment'))
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_cluster_access(options)
|
2023-05-27 22:25:52 +05:30
|
|
|
if cluster.nil? || Ability.allowed?(user, :admin_metrics_dashboard_annotation, cluster)
|
2020-04-22 19:07:51 +05:30
|
|
|
options[:cluster] = cluster
|
|
|
|
success(options)
|
|
|
|
else
|
2021-11-18 22:05:49 +05:30
|
|
|
error(s_('MetricsDashboardAnnotation|You are not authorized to create annotation for selected cluster'))
|
2020-04-22 19:07:51 +05:30
|
|
|
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
|
2021-11-18 22:05:49 +05:30
|
|
|
error(s_('MetricsDashboardAnnotation|Dashboard with requested path can not be found'))
|
2020-04-22 19:07:51 +05:30
|
|
|
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
|