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?
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
state.run_after_commit do
|
|
|
|
Terraform::States::DestroyWorker.perform_async(id)
|
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
state.update!(deleted_at: Time.current)
|
2022-07-23 23:45:48 +05:30
|
|
|
|
|
|
|
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
|