diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-07 18:09:19 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-07 18:09:19 +0000 |
commit | 3290d46655f07d7ae3dca788d6df9f326972ffd8 (patch) | |
tree | 0d24713e1592cdd3583257f14a52d46a22539ed1 /app/services | |
parent | c6b3ec3f56fa32a0e0ed3de0d0878d25f1adaddf (diff) | |
download | gitlab-ce-3290d46655f07d7ae3dca788d6df9f326972ffd8.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/metrics/dashboard/annotations/create_service.rb | 80 | ||||
-rw-r--r-- | app/services/metrics/dashboard/annotations/delete_service.rb | 43 |
2 files changed, 123 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 diff --git a/app/services/metrics/dashboard/annotations/delete_service.rb b/app/services/metrics/dashboard/annotations/delete_service.rb new file mode 100644 index 00000000000..c6a6c4f5fbf --- /dev/null +++ b/app/services/metrics/dashboard/annotations/delete_service.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +# Delete Metrics::Dashboard::Annotation entry +module Metrics + module Dashboard + module Annotations + class DeleteService < ::BaseService + include Stepable + + steps :authorize_action, + :delete + + def initialize(user, annotation) + @user, @annotation = user, annotation + end + + def execute + execute_steps + end + + private + + attr_reader :user, :annotation + + def authorize_action(_options) + if Ability.allowed?(user, :delete_metrics_dashboard_annotation, annotation) + success + else + error(s_('Metrics::Dashboard::Annotation|You are not authorized to delete this annotation')) + end + end + + def delete(_options) + if annotation.destroy + success + else + error(s_('Metrics::Dashboard::Annotation|Annotation has not been deleted')) + end + end + end + end + end +end |