debian-mirror-gitlab/app/graphql/mutations/metrics/dashboard/annotations/create.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

107 lines
3.4 KiB
Ruby
Raw Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
module Mutations
module Metrics
module Dashboard
module Annotations
class Create < BaseMutation
graphql_name 'CreateAnnotation'
ANNOTATION_SOURCE_ARGUMENT_ERROR = 'Either a cluster or environment global id is required'
INVALID_ANNOTATION_SOURCE_ERROR = 'Invalid cluster or environment id'
authorize :create_metrics_dashboard_annotation
field :annotation,
Types::Metrics::Dashboards::AnnotationType,
null: true,
2021-10-27 15:23:28 +05:30
description: 'Created annotation.'
2020-05-24 23:13:21 +05:30
argument :environment_id,
2021-01-03 14:25:43 +05:30
::Types::GlobalIDType[::Environment],
2020-05-24 23:13:21 +05:30
required: false,
2021-10-27 15:23:28 +05:30
description: 'Global ID of the environment to add an annotation to.'
2020-05-24 23:13:21 +05:30
argument :cluster_id,
2021-01-03 14:25:43 +05:30
::Types::GlobalIDType[::Clusters::Cluster],
2020-05-24 23:13:21 +05:30
required: false,
2021-10-27 15:23:28 +05:30
description: 'Global ID of the cluster to add an annotation to.'
2020-05-24 23:13:21 +05:30
argument :starting_at, Types::TimeType,
required: true,
2021-03-08 18:12:59 +05:30
description: 'Timestamp indicating starting moment to which the annotation relates.'
2020-05-24 23:13:21 +05:30
argument :ending_at, Types::TimeType,
required: false,
2021-03-08 18:12:59 +05:30
description: 'Timestamp indicating ending moment to which the annotation relates.'
2020-05-24 23:13:21 +05:30
argument :dashboard_path,
2021-10-27 15:23:28 +05:30
GraphQL::Types::String,
2020-05-24 23:13:21 +05:30
required: true,
2021-10-27 15:23:28 +05:30
description: 'Path to a file defining the dashboard on which the annotation should be added.'
2020-05-24 23:13:21 +05:30
argument :description,
2021-10-27 15:23:28 +05:30
GraphQL::Types::String,
2020-05-24 23:13:21 +05:30
required: true,
2021-10-27 15:23:28 +05:30
description: 'Description of the annotation.'
2020-05-24 23:13:21 +05:30
AnnotationSource = Struct.new(:object, keyword_init: true) do
def type_keys
{ 'Clusters::Cluster' => :cluster, 'Environment' => :environment }
end
def klass
object.class.name
end
def type
raise Gitlab::Graphql::Errors::ArgumentError, INVALID_ANNOTATION_SOURCE_ERROR unless type_keys[klass]
type_keys[klass]
end
end
def resolve(args)
annotation_response = ::Metrics::Dashboard::Annotations::CreateService.new(context[:current_user], annotation_create_params(args)).execute
annotation = annotation_response[:annotation]
{
annotation: annotation.valid? ? annotation : nil,
errors: errors_on_object(annotation)
}
end
private
def ready?(**args)
# Raise error if both cluster_id and environment_id are present or neither is present
unless args[:cluster_id].present? ^ args[:environment_id].present?
raise Gitlab::Graphql::Errors::ArgumentError, ANNOTATION_SOURCE_ARGUMENT_ERROR
end
2021-01-03 14:25:43 +05:30
super(**args)
2020-05-24 23:13:21 +05:30
end
def find_object(id:)
2021-01-03 14:25:43 +05:30
GitlabSchema.find_by_gid(id)
2020-05-24 23:13:21 +05:30
end
def annotation_create_params(args)
annotation_source = AnnotationSource.new(object: annotation_source(args))
args[annotation_source.type] = annotation_source.object
args
end
def annotation_source(args)
2022-07-16 23:28:13 +05:30
annotation_source_id = args[:cluster_id] || args[:environment_id]
2020-05-24 23:13:21 +05:30
authorized_find!(id: annotation_source_id)
end
end
end
end
end
end