debian-mirror-gitlab/app/services/terraform/states/trigger_destroy_service.rb

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

44 lines
1 KiB
Ruby
Raw Normal View History

2022-07-23 23:45:48 +05:30
# 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