summaryrefslogtreecommitdiff
path: root/app/services/alert_management
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/alert_management')
-rw-r--r--app/services/alert_management/http_integrations/create_service.rb60
-rw-r--r--app/services/alert_management/http_integrations/destroy_service.rb44
-rw-r--r--app/services/alert_management/http_integrations/update_service.rb48
-rw-r--r--app/services/alert_management/process_prometheus_alert_service.rb15
-rw-r--r--app/services/alert_management/sync_alert_service_data_service.rb56
5 files changed, 219 insertions, 4 deletions
diff --git a/app/services/alert_management/http_integrations/create_service.rb b/app/services/alert_management/http_integrations/create_service.rb
new file mode 100644
index 00000000000..576e38c23aa
--- /dev/null
+++ b/app/services/alert_management/http_integrations/create_service.rb
@@ -0,0 +1,60 @@
+# frozen_string_literal: true
+
+module AlertManagement
+ module HttpIntegrations
+ class CreateService
+ # @param project [Project]
+ # @param current_user [User]
+ # @param params [Hash]
+ def initialize(project, current_user, params)
+ @project = project
+ @current_user = current_user
+ @params = params
+ end
+
+ def execute
+ return error_no_permissions unless allowed?
+ return error_multiple_integrations unless creation_allowed?
+
+ integration = project.alert_management_http_integrations.create(params)
+ return error_in_create(integration) unless integration.valid?
+
+ success(integration)
+ end
+
+ private
+
+ attr_reader :project, :current_user, :params
+
+ def allowed?
+ current_user&.can?(:admin_operations, project)
+ end
+
+ def creation_allowed?
+ project.alert_management_http_integrations.empty?
+ end
+
+ def error(message)
+ ServiceResponse.error(message: message)
+ end
+
+ def success(integration)
+ ServiceResponse.success(payload: { integration: integration })
+ end
+
+ def error_no_permissions
+ error(_('You have insufficient permissions to create an HTTP integration for this project'))
+ end
+
+ def error_multiple_integrations
+ error(_('Multiple HTTP integrations are not supported for this project'))
+ end
+
+ def error_in_create(integration)
+ error(integration.errors.full_messages.to_sentence)
+ end
+ end
+ end
+end
+
+::AlertManagement::HttpIntegrations::CreateService.prepend_if_ee('::EE::AlertManagement::HttpIntegrations::CreateService')
diff --git a/app/services/alert_management/http_integrations/destroy_service.rb b/app/services/alert_management/http_integrations/destroy_service.rb
new file mode 100644
index 00000000000..aeb3f6cb807
--- /dev/null
+++ b/app/services/alert_management/http_integrations/destroy_service.rb
@@ -0,0 +1,44 @@
+# frozen_string_literal: true
+
+module AlertManagement
+ module HttpIntegrations
+ class DestroyService
+ # @param integration [AlertManagement::HttpIntegration]
+ # @param current_user [User]
+ def initialize(integration, current_user)
+ @integration = integration
+ @current_user = current_user
+ end
+
+ def execute
+ return error_no_permissions unless allowed?
+
+ if integration.destroy
+ success
+ else
+ error(integration.errors.full_messages.to_sentence)
+ end
+ end
+
+ private
+
+ attr_reader :integration, :current_user
+
+ def allowed?
+ current_user&.can?(:admin_operations, integration)
+ end
+
+ def error(message)
+ ServiceResponse.error(message: message)
+ end
+
+ def success
+ ServiceResponse.success(payload: { integration: integration })
+ end
+
+ def error_no_permissions
+ error(_('You have insufficient permissions to remove this HTTP integration'))
+ end
+ end
+ end
+end
diff --git a/app/services/alert_management/http_integrations/update_service.rb b/app/services/alert_management/http_integrations/update_service.rb
new file mode 100644
index 00000000000..220c4e759f0
--- /dev/null
+++ b/app/services/alert_management/http_integrations/update_service.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+module AlertManagement
+ module HttpIntegrations
+ class UpdateService
+ # @param integration [AlertManagement::HttpIntegration]
+ # @param current_user [User]
+ # @param params [Hash]
+ def initialize(integration, current_user, params)
+ @integration = integration
+ @current_user = current_user
+ @params = params
+ end
+
+ def execute
+ return error_no_permissions unless allowed?
+
+ params[:token] = nil if params.delete(:regenerate_token)
+
+ if integration.update(params)
+ success
+ else
+ error(integration.errors.full_messages.to_sentence)
+ end
+ end
+
+ private
+
+ attr_reader :integration, :current_user, :params
+
+ def allowed?
+ current_user&.can?(:admin_operations, integration)
+ end
+
+ def error(message)
+ ServiceResponse.error(message: message)
+ end
+
+ def success
+ ServiceResponse.success(payload: { integration: integration.reset })
+ end
+
+ def error_no_permissions
+ error(_('You have insufficient permissions to update this HTTP integration'))
+ end
+ end
+ end
+end
diff --git a/app/services/alert_management/process_prometheus_alert_service.rb b/app/services/alert_management/process_prometheus_alert_service.rb
index 5c7698f724a..28ce5401a6c 100644
--- a/app/services/alert_management/process_prometheus_alert_service.rb
+++ b/app/services/alert_management/process_prometheus_alert_service.rb
@@ -9,6 +9,10 @@ module AlertManagement
return bad_request unless incoming_payload.has_required_attributes?
process_alert_management_alert
+ return bad_request unless alert.persisted?
+
+ process_incident_issues if process_issues?
+ send_alert_email if send_email?
ServiceResponse.success
end
@@ -30,8 +34,6 @@ module AlertManagement
else
create_alert_management_alert
end
-
- process_incident_issues if process_issues?
end
def reset_alert_management_alert_status
@@ -85,12 +87,17 @@ module AlertManagement
end
def process_incident_issues
- return unless alert.persisted?
- return if alert.issue
+ return if alert.issue || alert.resolved?
IncidentManagement::ProcessAlertWorker.perform_async(nil, nil, alert.id)
end
+ def send_alert_email
+ notification_service
+ .async
+ .prometheus_alerts_fired(project, [alert])
+ end
+
def logger
@logger ||= Gitlab::AppLogger
end
diff --git a/app/services/alert_management/sync_alert_service_data_service.rb b/app/services/alert_management/sync_alert_service_data_service.rb
new file mode 100644
index 00000000000..1ba197065c5
--- /dev/null
+++ b/app/services/alert_management/sync_alert_service_data_service.rb
@@ -0,0 +1,56 @@
+# frozen_string_literal: true
+
+module AlertManagement
+ class SyncAlertServiceDataService
+ # @param alert_service [AlertsService]
+ def initialize(alert_service)
+ @alert_service = alert_service
+ end
+
+ def execute
+ http_integration = find_http_integration
+
+ result = if http_integration
+ update_integration_data(http_integration)
+ else
+ create_integration
+ end
+
+ result ? ServiceResponse.success : ServiceResponse.error(message: 'Update failed')
+ end
+
+ private
+
+ attr_reader :alert_service
+
+ def find_http_integration
+ AlertManagement::HttpIntegrationsFinder.new(
+ alert_service.project,
+ endpoint_identifier: ::AlertManagement::HttpIntegration::LEGACY_IDENTIFIER
+ )
+ .execute
+ .first
+ end
+
+ def create_integration
+ new_integration = AlertManagement::HttpIntegration.create(
+ project_id: alert_service.project_id,
+ name: 'HTTP endpoint',
+ endpoint_identifier: AlertManagement::HttpIntegration::LEGACY_IDENTIFIER,
+ active: alert_service.active,
+ encrypted_token: alert_service.data.encrypted_token,
+ encrypted_token_iv: alert_service.data.encrypted_token_iv
+ )
+
+ new_integration.persisted?
+ end
+
+ def update_integration_data(http_integration)
+ http_integration.update(
+ active: alert_service.active,
+ encrypted_token: alert_service.data.encrypted_token,
+ encrypted_token_iv: alert_service.data.encrypted_token_iv
+ )
+ end
+ end
+end