from parallels.common.actions.base.subscription_action import SubscriptionAction
from parallels.common.actions.utils.logging_properties import LoggingProperties


class CheckTargetWebHosting(SubscriptionAction):
	"""Check that each subscription with virtual hosting has target web IP"""

	def get_description(self):
		return "Check that each subscription with virtual hosting has web IP"

	def get_failure_message(self, global_context, subscription):
		"""
		:type global_context: parallels.common.global_context.GlobalMigrationContext
		:type subscription: parallels.common.migrated_subscription.MigratedSubscription
		"""
		return "Failed to check if subscription '%s' has web IP" % subscription.name

	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(info_log=False)

	def filter_subscription(self, global_context, subscription):
		"""
		:type global_context: parallels.common.global_context.GlobalMigrationContext
		:type subscription: parallels.common.migrated_subscription.MigratedSubscription
		"""
		return subscription.raw_backup.hosting_type == 'phosting'

	def run(self, global_context, subscription):
		"""
		:type global_context: parallels.common.global_context.GlobalMigrationContext
		:type subscription: parallels.common.migrated_subscription.MigratedSubscription
		"""
		if subscription.web_target_server is None:
			raise Exception(
				u"Unable to continue migration: "
				u"web hosting of the corresponding webspace in target panel is not configured."
				u"Make sure that web hosting of the corresponding webspace is configured."
			)
