import logging

from parallels.common.actions.base.common_action import CommonAction
from parallels.common.actions.hosting_settings.import_backups import ImportedBackups
from parallels.common.actions.utils.logging_properties import LoggingProperties
from parallels.common.utils.restore_hosting_utils import get_restore_hosting_utils

logger = logging.getLogger(__name__)


class RemoveImportedBackups(CommonAction):
	"""Remove imported Plesk backup files from target panel's PMM repository

	Customers could see parts of the backup in their control panels if we don't remove it, so
	we should remove backups not bother customers. Also, there will be a useless
	garbage which wastes disk space if we won't remove them.
	"""

	def get_description(self):
		return "Remove imported backup dumps from target panel's repository"

	def get_failure_message(self, global_context):
		"""
		:type global_context: parallels.common.global_context.GlobalMigrationContext
		"""
		return "Failed to remove imported backup dumps to target panel's repository"

	def is_critical(self):
		"""If action is critical or not

		If action is critical and it failed for a subscription, migration tool
		won't run the next operations for the subscription.

		:rtype: bool
		"""
		return False

	def get_logging_properties(self):
		"""Get how action should be logged to migration tools end-user

		:rtype: parallels.common.actions.utils.logging_properties.LoggingProperties
		"""
		return LoggingProperties(compound=False)

	def run(self, global_context):
		"""
		:type global_context: parallels.common.global_context.GlobalMigrationContext
		"""
		restore_hosting_utils = get_restore_hosting_utils(global_context.conn.target.plesk_server)
		for backup in ImportedBackups.get_instance().get_all():
			restore_hosting_utils.remove_backup(backup.backup_info_file)