diff options
Diffstat (limited to 'app/services/terraform/states/trigger_destroy_service.rb')
-rw-r--r-- | app/services/terraform/states/trigger_destroy_service.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/app/services/terraform/states/trigger_destroy_service.rb b/app/services/terraform/states/trigger_destroy_service.rb new file mode 100644 index 00000000000..3669bdcf716 --- /dev/null +++ b/app/services/terraform/states/trigger_destroy_service.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module Terraform + module States + class TriggerDestroyService + def initialize(state, current_user:) + @state = state + @current_user = current_user + end + + def execute + return unauthorized_response unless can_destroy_state? + return state_locked_response if state.locked? + + state.update!(deleted_at: Time.current) + + Terraform::States::DestroyWorker.perform_async(state.id) + + ServiceResponse.success + end + + private + + attr_reader :state, :current_user + + def can_destroy_state? + current_user.can?(:admin_terraform_state, state.project) + end + + def unauthorized_response + error_response(s_('Terraform|You have insufficient permissions to delete this state')) + end + + def state_locked_response + error_response(s_('Terraform|Cannot remove a locked state')) + end + + def error_response(message) + ServiceResponse.error(message: message) + end + end + end +end |