summaryrefslogtreecommitdiff
path: root/app/graphql/mutations/alert_management/prometheus_integration/create.rb
blob: c676cde90b4bc5578b87c3d704cfedad1cc00fa7 (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 Mutations
  module AlertManagement
    module PrometheusIntegration
      class Create < PrometheusIntegrationBase
        include ResolvesProject

        graphql_name 'PrometheusIntegrationCreate'

        argument :project_path, GraphQL::ID_TYPE,
                 required: true,
                 description: 'The project to create the integration in.'

        argument :active, GraphQL::BOOLEAN_TYPE,
                 required: true,
                 description: 'Whether the integration is receiving alerts.'

        argument :api_url, GraphQL::STRING_TYPE,
                 required: true,
                 description: 'Endpoint at which prometheus can be queried.'

        def resolve(args)
          project = authorized_find!(full_path: args[:project_path])

          return integration_exists if project.prometheus_service

          result = ::Projects::Operations::UpdateService.new(
            project,
            current_user,
            **integration_attributes(args),
            **token_attributes
          ).execute

          response(project.prometheus_service, result)
        end

        private

        def find_object(full_path:)
          resolve_project(full_path: full_path)
        end

        def integration_exists
          response(nil, message: _('Multiple Prometheus integrations are not supported'))
        end
      end
    end
  end
end