from parallels.core import MigrationError
from parallels.core.utils.common import cached
from parallels.plesk import messages
from parallels.plesk.connections.target_connections import PleskTargetConnections
from parallels.plesk_multi_server.connections.target_server import PleskMultiServerManagementServer
from parallels.plesk_multi_server.utils.xml_rpc.plesk_multi_server.client import PleskMultiServerClient


class PleskMultiServerTargetConnections(PleskTargetConnections):
    @property
    @cached
    def plesk_multi_server_management_server(self):
        return PleskMultiServerManagementServer(self, self._plesk_settings)

    def plesk_multi_server_api(self):
        if self.panel_secret_key is None:
            if self._plesk_settings.plesk_api.auth.username is None or self.panel_admin_password is None:
                raise MigrationError(messages.MIGRATION_ERROR_PLESK_CREDENTIALS_NOT_SPECIFIED.format(
                    plesk_server=self.plesk_server.description()
                ))
        client = PleskMultiServerClient(
            self._plesk_settings.plesk_api.url,
            self._plesk_settings.plesk_api.auth.username,
            self.panel_admin_password,
            self.panel_secret_key,
            '1.6.8.0',
            pretty_print=True,
            is_windows=self.is_windows,
            host=self._plesk_settings.plesk_api.host
        )

        return client