def orchestration_restore(self, context, saved_artifact_info,
                              custom_params):
        """

        :param ResourceCommandContext context: ResourceCommandContext object with all Resource
            Attributes inside
        :param saved_artifact_info: OrchestrationSavedArtifactInfo json
        :param custom_params: json with custom restore parameters
        """

        logger = get_logger_with_thread_id(context)
        api = get_api(context)

        resource_config = create_networking_resource_from_context(
            self.SHELL_NAME, self.SUPPORTED_OS, context)
        cli_handler = CliHandler(self._cli, resource_config, logger, api)

        configuration_operations = ConfigurationRunner(logger, resource_config,
                                                       api, cli_handler)
        logger.info('Orchestration restore started')
        configuration_operations.orchestration_restore(
            saved_artifact_info=saved_artifact_info,
            custom_params=custom_params,
        )
        logger.info('Orchestration restore completed')
    def orchestration_save(self, context, mode, custom_params):
        """

        :param ResourceCommandContext context: ResourceCommandContext object with all Resource
            Attributes inside
        :param mode: mode
        :param custom_params: json with custom save parameters
        :return str response: response json
        """

        logger = get_logger_with_thread_id(context)
        api = get_api(context)

        resource_config = create_networking_resource_from_context(
            self.SHELL_NAME, self.SUPPORTED_OS, context)
        cli_handler = CliHandler(self._cli, resource_config, logger, api)

        mode = mode or 'shallow'

        configuration_operations = ConfigurationRunner(logger, resource_config,
                                                       api, cli_handler)
        logger.info('Orchestration save started')
        response = configuration_operations.orchestration_save(
            mode=mode,
            custom_params=custom_params,
        )
        logger.info('Orchestration save completed')
        return response
    def restore(self, context, path, configuration_type, restore_method,
                vrf_management_name):
        """Restore selected file to the provided destination

        :param ResourceCommandContext context: ResourceCommandContext object with all Resource
            Attributes inside
        :param path: source config file
        :param configuration_type: running or startup configs
        :param restore_method: append or override methods
        :param vrf_management_name: VRF management Name
        """

        logger = get_logger_with_thread_id(context)
        api = get_api(context)

        resource_config = create_networking_resource_from_context(
            self.SHELL_NAME, self.SUPPORTED_OS, context)
        cli_handler = CliHandler(self._cli, resource_config, logger, api)

        configuration_type = configuration_type or 'running'
        restore_method = restore_method or 'override'
        vrf_management_name = vrf_management_name or resource_config.vrf_management_name

        configuration_operations = ConfigurationRunner(logger, resource_config,
                                                       api, cli_handler)
        logger.info('Restore started')
        configuration_operations.restore(
            path=path,
            restore_method=restore_method,
            configuration_type=configuration_type,
            vrf_management_name=vrf_management_name,
        )
        logger.info('Restore completed')
    def save(self, context, folder_path, configuration_type,
             vrf_management_name):
        """Save selected file to the provided destination

        :param ResourceCommandContext context: ResourceCommandContext object with all Resource
            Attributes inside
        :param configuration_type: startup or running
        :param folder_path: destination path where file will be saved
        :param vrf_management_name: VRF management Name
        :return str saved configuration file name:
        """

        logger = get_logger_with_thread_id(context)
        api = get_api(context)

        resource_config = create_networking_resource_from_context(
            self.SHELL_NAME, self.SUPPORTED_OS, context)
        cli_handler = CliHandler(self._cli, resource_config, logger, api)

        configuration_type = configuration_type or 'running'
        vrf_management_name = vrf_management_name or resource_config.vrf_management_name

        configuration_operations = ConfigurationRunner(logger, resource_config,
                                                       api, cli_handler)
        logger.info('Save started')
        response = configuration_operations.save(
            folder_path=folder_path,
            configuration_type=configuration_type,
            vrf_management_name=vrf_management_name,
        )
        logger.info('Save completed')
        return response
Exemplo n.º 5
0
    def _setUp(self, attrs=None):
        attributes = {
            'Backup Location': 'folder',
        }
        attributes.update(attrs or {})

        super(TestSaveConfig, self)._setUp(attributes)
        self.runner = AlcatelConfigurationRunner(self.logger,
                                                 self.resource_config,
                                                 self.api, self.cli_handler)
Exemplo n.º 6
0
class TestSaveConfig(BaseAlcatelTestCase):
    def _setUp(self, attrs=None):
        attributes = {
            'Backup Location': 'folder',
        }
        attributes.update(attrs or {})

        super(TestSaveConfig, self)._setUp(attributes)
        self.runner = AlcatelConfigurationRunner(self.logger,
                                                 self.resource_config,
                                                 self.api, self.cli_handler)

    def setUp(self):
        super(TestSaveConfig, self).setUp()
        self._setUp()

    @patch("cloudshell.cli.session.ssh_session.paramiko")
    @patch("cloudshell.cli.session.ssh_session.SSHSession._clear_buffer",
           return_value="")
    @patch('cloudshell.cli.session.ssh_session.SSHSession._receive_all')
    @patch('cloudshell.cli.session.ssh_session.SSHSession.send_line')
    def test_save_startup(self, send_mock, recv_mock, cb_mock, paramiko_mock):
        ftp = 'ftp://test.url'
        file_pattern = r'Alcatel-startup-\d+-\d+'
        emu = CliEmulator([
            [
                '^show system information$', '{0} show system information\n\n'
                '===============================================================================\n'
                'System Information\n'
                '===============================================================================\n'
                'System Name            : test\n'
                'System Type            : 7210 SAS-M-1\n'
                'System Version         : B-8.0.R4\n\n'
                'BOF Source             : cf1:\n'
                'Image Source           : primary\n'
                'Config Source          : primary\n'
                'Last Booted Config File: cf1:\config.cfg\n'
                '{0}'
                ''.format(DEFAULT_PROMPT), 1
            ],
            [
                r'^file copy cf1:\\config.cfg {}/{}$'.format(
                    ftp, file_pattern),
                'Copying file cf1:\config.cfg ... OK\n1 file copied.\n{}'.
                format(DEFAULT_PROMPT), 1
            ],
            [
                r'^file copy cf1:\\config\.sdx {}/{}\.sdx$'.format(
                    ftp, file_pattern),
                'Copying file cf1:\config.cfg ... OK\n1 file copied.\n{}'.
                format(DEFAULT_PROMPT), 1
            ],
            [
                r'^file copy cf1:\\config\.ndx {}/{}\.ndx$'.format(
                    ftp, file_pattern),
                'Copying file cf1:\config.cfg ... OK\n1 file copied.\n{}'.
                format(DEFAULT_PROMPT), 1
            ],
        ])
        recv_mock.side_effect = emu.receive_all
        send_mock.side_effect = emu.send_line

        self.runner.save(ftp, 'startup')

        emu.check_calls()

    @patch("cloudshell.cli.session.ssh_session.paramiko", MagicMock())
    @patch("cloudshell.cli.session.ssh_session.SSHSession._clear_buffer",
           MagicMock(return_value=""))
    @patch('cloudshell.cli.session.ssh_session.SSHSession._receive_all')
    @patch('cloudshell.cli.session.ssh_session.SSHSession.send_line')
    def test_save_startup_without_additional_files(self, send_mock, recv_mock):
        ftp = 'ftp://test.url'
        file_pattern = r'Alcatel-startup-\d+-\d+'
        emu = CliEmulator([
            [
                '^show system information$', '{0} show system information\n\n'
                '===============================================================================\n'
                'System Information\n'
                '===============================================================================\n'
                'System Name            : test\n'
                'System Type            : 7210 SAS-M-1\n'
                'System Version         : B-8.0.R4\n\n'
                'BOF Source             : cf1:\n'
                'Image Source           : primary\n'
                'Config Source          : primary\n'
                'Last Booted Config File: cf1:\config.cfg\n'
                '{0}'
                ''.format(DEFAULT_PROMPT), 1
            ],
            [
                r'^file copy cf1:\\config.cfg {}/{}$'.format(
                    ftp, file_pattern),
                'Copying file cf1:\config.cfg ... OK\n1 file copied.\n{}'.
                format(DEFAULT_PROMPT), 1
            ],
            [
                r'^file copy cf1:\\config\.sdx {}/{}\.sdx$'.format(
                    ftp, file_pattern),
                Exception('Copy failed.'), 1
            ],
        ])
        recv_mock.side_effect = emu.receive_all
        send_mock.side_effect = emu.send_line

        self.runner.save(ftp, 'startup')

        emu.check_calls()

    @patch("cloudshell.cli.session.ssh_session.paramiko", MagicMock())
    @patch("cloudshell.cli.session.ssh_session.SSHSession._clear_buffer",
           MagicMock(return_value=""))
    @patch('cloudshell.cli.session.ssh_session.SSHSession._receive_all')
    @patch('cloudshell.cli.session.ssh_session.SSHSession.send_line')
    def test_save_copy_addition_files_raise_not_known_error(
            self, send_mock, recv_mock):
        ftp = 'ftp://test.url'
        file_pattern = r'Alcatel-startup-\d+-\d+'
        emu = CliEmulator([
            [
                '^show system information$', '{0} show system information\n\n'
                '===============================================================================\n'
                'System Information\n'
                '===============================================================================\n'
                'System Name            : test\n'
                'System Type            : 7210 SAS-M-1\n'
                'System Version         : B-8.0.R4\n\n'
                'BOF Source             : cf1:\n'
                'Image Source           : primary\n'
                'Config Source          : primary\n'
                'Last Booted Config File: cf1:\\config.cfg\n'
                '{0}'
                ''.format(DEFAULT_PROMPT), 1
            ],
            [
                r'^file copy cf1:\\config.cfg {}/{}$'.format(
                    ftp, file_pattern),
                'Copying file cf1:\\config.cfg ... OK\n1 file copied.\n{}'.
                format(DEFAULT_PROMPT), 1
            ],
            [
                r'^file copy cf1:\\config\.sdx {}/{}\.sdx$'.format(
                    ftp, file_pattern),
                Exception('Don\'t known exception'), 1
            ],
        ])
        recv_mock.side_effect = emu.receive_all
        send_mock.side_effect = emu.send_line

        with self.assertRaisesRegexp(Exception, 'Don\'t known exception'):
            self.runner.save(ftp, 'startup')

        emu.check_calls()

    @patch("cloudshell.cli.session.ssh_session.paramiko")
    @patch("cloudshell.cli.session.ssh_session.SSHSession._clear_buffer",
           return_value="")
    @patch('cloudshell.cli.session.ssh_session.SSHSession._receive_all')
    @patch('cloudshell.cli.session.ssh_session.SSHSession.send_line')
    def test_save_running(self, send_mock, recv_mock, cb_mock, paramiko_mock):
        ftp = 'ftp://test.url'
        file_pattern = r'Alcatel-running-\d+-\d+'
        emu = CliEmulator([
            [
                r'^admin save {}/{}$'.format(ftp, file_pattern),
                'Completed.\n{}'.format(DEFAULT_PROMPT), 1
            ],
        ])
        recv_mock.side_effect = emu.receive_all
        send_mock.side_effect = emu.send_line

        self.runner.save(ftp, 'running')

        emu.check_calls()

    @patch("cloudshell.cli.session.ssh_session.paramiko")
    @patch("cloudshell.cli.session.ssh_session.SSHSession._clear_buffer",
           return_value="")
    @patch('cloudshell.cli.session.ssh_session.SSHSession._receive_all')
    @patch('cloudshell.cli.session.ssh_session.SSHSession.send_line')
    def test_save_to_default_location(self, send_mock, recv_mock, cb_mock,
                                      paramiko_mock):
        file_pattern = r'Alcatel-running-\d+-\d+'
        emu = CliEmulator([
            [
                r'^admin save {}//folder/{}'.format(self.runner.file_system,
                                                    file_pattern),
                'Completed.\n{}'.format(DEFAULT_PROMPT), 1
            ],
        ])
        recv_mock.side_effect = emu.receive_all
        send_mock.side_effect = emu.send_line

        self.runner.save(configuration_type='running')

        emu.check_calls()
 def _setUp(self, attrs=None):
     super(TestRestoreConfig, self)._setUp(attrs)
     self.runner = AlcatelConfigurationRunner(
         self.logger, self.resource_config, self.api, self.cli_handler)
class TestRestoreConfig(BaseAlcatelTestCase):

    def _setUp(self, attrs=None):
        super(TestRestoreConfig, self)._setUp(attrs)
        self.runner = AlcatelConfigurationRunner(
            self.logger, self.resource_config, self.api, self.cli_handler)

    def setUp(self):
        super(TestRestoreConfig, self).setUp()
        self._setUp()

    def test_restore_startup_append(self):
        self.assertRaises(
            Exception,
            self.runner.restore,
            'ftp://test.url', 'startup', 'append')

    def test_restore_running_append(self):
        self.assertRaises(
            Exception,
            self.runner.restore,
            'ftp://test.url', 'running', 'append')

    @patch("cloudshell.cli.session.ssh_session.paramiko")
    @patch("cloudshell.cli.session.ssh_session.SSHSession._clear_buffer", return_value="")
    @patch('cloudshell.cli.session.ssh_session.SSHSession._receive_all')
    @patch('cloudshell.cli.session.ssh_session.SSHSession.send_line')
    def test_restore_startup_override(self, send_mock, recv_mock, cb_mock, paramiko_mock):
        ftp = 'ftp://test.url'
        file_name = 'Alcatel-startup-200218-133900'
        emu = CliEmulator([
            [r'^show bof$',
             '{0} show bof\n'
             '===============================================================================\n'
             'BOF (Memory)\n'
             '===============================================================================\n'
             'primary-image      cf1:/images/TiMOS-8.0.R4\n'
             'primary-config     cf1:/config.cfg\n'
             '===============================================================================\n'
             '{0}'''.format(DEFAULT_PROMPT),
             1],
            [r'^file copy {0}/{1} cf1:/{1}$'.format(ftp, file_name),
             'Copying file cf1:\{} ... OK\n1 file copied.\n{}'.format(file_name, DEFAULT_PROMPT),
             1],
            [r'^file copy {0}/{1}\.sdx cf1:/{1}\.sdx$'.format(ftp, file_name),
             'Copying file cf1:\{}.sdx ... OK\n1 file copied.\n{}'.format(file_name, DEFAULT_PROMPT),
             1],
            [r'^file copy {0}/{1}\.ndx cf1:/{1}\.ndx$'.format(ftp, file_name),
             'Copying file cf1:\{}.ndx ... OK\n1 file copied.\n{}'.format(file_name, DEFAULT_PROMPT),
             1],
            [r'^bof primary-config cf1:/{}$'.format(file_name),
             '{}'.format(DEFAULT_PROMPT),
             1],
            ['^bof save$',
             'Writing BOF to cf1:/bof.cfg ... OK\nCompleted.\n{}'.format(DEFAULT_PROMPT),
             1],
        ])
        recv_mock.side_effect = emu.receive_all
        send_mock.side_effect = emu.send_line

        path = '{}/{}'.format(ftp, file_name)
        self.runner.restore(path, 'startup', 'override')

        emu.check_calls()

    @patch("cloudshell.cli.session.ssh_session.paramiko", MagicMock())
    @patch("cloudshell.cli.session.ssh_session.SSHSession._clear_buffer",
           MagicMock(return_value=""))
    @patch('cloudshell.cli.session.ssh_session.SSHSession._receive_all')
    @patch('cloudshell.cli.session.ssh_session.SSHSession.send_line')
    def test_restore_startup_override_without_additional_files(self, send_mock, recv_mock):
        ftp = 'ftp://test.url'
        file_name = 'Alcatel-startup-200218-133900'
        emu = CliEmulator([
            [r'^show bof$',
             '{0} show bof\n'
             '===============================================================================\n'
             'BOF (Memory)\n'
             '===============================================================================\n'
             'primary-image      cf1:/images/TiMOS-8.0.R4\n'
             'primary-config     cf1:/config.cfg\n'
             '===============================================================================\n'
             '{0}'''.format(DEFAULT_PROMPT),
             1],
            [r'^file copy {0}/{1} cf1:/{1}$'.format(ftp, file_name),
             'Copying file cf1:\{} ... OK\n1 file copied.\n{}'.format(file_name, DEFAULT_PROMPT),
             1],
            [r'^file copy {0}/{1}\.sdx cf1:/{1}\.sdx$'.format(ftp, file_name),
             Exception('Copy failed.'),
             1],
            [r'^bof primary-config cf1:/{}$'.format(file_name),
             '{}'.format(DEFAULT_PROMPT),
             1],
            ['^bof save$',
             'Writing BOF to cf1:/bof.cfg ... OK\nCompleted.\n{}'.format(DEFAULT_PROMPT),
             1],
        ])
        recv_mock.side_effect = emu.receive_all
        send_mock.side_effect = emu.send_line

        path = '{}/{}'.format(ftp, file_name)
        self.runner.restore(path, 'startup', 'override')

        emu.check_calls()

    @patch("cloudshell.cli.session.ssh_session.paramiko", MagicMock())
    @patch("cloudshell.cli.session.ssh_session.SSHSession._clear_buffer",
           MagicMock(return_value=""))
    @patch('cloudshell.cli.session.ssh_session.SSHSession._receive_all')
    @patch('cloudshell.cli.session.ssh_session.SSHSession.send_line')
    def test_restore_to_cf3(self, send_mock, recv_mock):
        ftp = 'ftp://test.url'
        file_name = 'Alcatel-startup-200218-133900'
        emu = CliEmulator([
            [r'^show bof$',
             '{0} show bof\n'
             '===============================================================================\n'
             'BOF (Memory)\n'
             '===============================================================================\n'
             'primary-image      cf3:/images/TiMOS-8.0.R4\n'
             'primary-config     cf3:/config.cfg\n'
             '===============================================================================\n'
             '{0}'''.format(DEFAULT_PROMPT),
             1],
            [r'^file copy {0}/{1} cf3:/{1}$'.format(ftp, file_name),
             'Copying file cf3:\{} ... OK\n1 file copied.\n{}'.format(file_name, DEFAULT_PROMPT),
             1],
            [r'^file copy {0}/{1}\.sdx cf3:/{1}\.sdx$'.format(ftp, file_name),
             Exception('Copy failed.'),
             1],
            [r'^bof primary-config cf3:/{}$'.format(file_name),
             '{}'.format(DEFAULT_PROMPT),
             1],
            ['^bof save$',
             'Writing BOF to cf3:/bof.cfg ... OK\nCompleted.\n{}'.format(DEFAULT_PROMPT),
             1],
        ])
        recv_mock.side_effect = emu.receive_all
        send_mock.side_effect = emu.send_line

        path = '{}/{}'.format(ftp, file_name)
        self.runner.restore(path, 'startup', 'override')

        emu.check_calls()

    @patch("cloudshell.cli.session.ssh_session.paramiko", MagicMock())
    @patch("cloudshell.cli.session.ssh_session.SSHSession._clear_buffer",
           MagicMock(return_value=""))
    @patch('cloudshell.cli.session.ssh_session.SSHSession._receive_all')
    @patch('cloudshell.cli.session.ssh_session.SSHSession.send_line')
    def test_restore_not_known_file_system(self, send_mock, recv_mock):
        ftp = 'ftp://test.url'
        file_name = 'Alcatel-startup-200218-133900'
        emu = CliEmulator([
            [r'^show bof$',
             '{0} show bof\n'
             '===============================================================================\n'
             'BOF (Memory)\n'
             '===============================================================================\n'
             'primary-image      xx:/images/TiMOS-8.0.R4\n'
             'primary-config     xx:/config.cfg\n'
             '===============================================================================\n'
             '{0}'''.format(DEFAULT_PROMPT),
             1],
            [r'^file copy {0}/{1} cf1:/{1}$'.format(ftp, file_name),
             'Copying file cf1:\{} ... OK\n1 file copied.\n{}'.format(file_name, DEFAULT_PROMPT),
             1],
            [r'^file copy {0}/{1}\.sdx cf1:/{1}\.sdx$'.format(ftp, file_name),
             Exception('Copy failed.'),
             1],
            [r'^bof primary-config cf1:/{}$'.format(file_name),
             '{}'.format(DEFAULT_PROMPT),
             1],
            ['^bof save$',
             'Writing BOF to cf1:/bof.cfg ... OK\nCompleted.\n{}'.format(DEFAULT_PROMPT),
             1],
        ])
        recv_mock.side_effect = emu.receive_all
        send_mock.side_effect = emu.send_line

        path = '{}/{}'.format(ftp, file_name)
        self.runner.restore(path, 'startup', 'override')

        emu.check_calls()

    @patch("cloudshell.cli.session.ssh_session.paramiko")
    @patch("cloudshell.cli.session.ssh_session.SSHSession._clear_buffer", return_value="")
    @patch('cloudshell.cli.session.ssh_session.SSHSession._receive_all')
    @patch('cloudshell.cli.session.ssh_session.SSHSession.send_line')
    def test_restore_running_override(self, send_mock, recv_mock, cb_mock, paramiko_mock):
        ftp = 'ftp://test.url'
        file_name = 'Alcatel-startup-200218-133900'
        emu = CliEmulator([
            [r'^show bof$',
             '''{0} show bof\n
             ===============================================================================\n
             BOF (Memory)\n
             ===============================================================================\n
             primary-image      cf1:/images/TiMOS-8.0.R4\n
             primary-config     cf1:/config.cfg\n
             ===============================================================================\n
             {0}'''.format(DEFAULT_PROMPT),
             1],
            [r'^file copy {0}/{1} cf1:/{1}$'.format(ftp, file_name),
             'Copying file cf1:\{} ... OK\n1 file copied.\n{}'.format(file_name, DEFAULT_PROMPT),
             1],
            [r'^file copy {0}/{1}\.sdx cf1:/{1}\.sdx$'.format(ftp, file_name),
             'Copying file cf1:\{}.sdx ... OK\n1 file copied.\n{}'.format(file_name, DEFAULT_PROMPT),
             1],
            [r'^file copy {0}/{1}\.ndx cf1:/{1}\.ndx$'.format(ftp, file_name),
             'Copying file cf1:\{}.ndx ... OK\n1 file copied.\n{}'.format(file_name, DEFAULT_PROMPT),
             1],
            [r'^bof primary-config cf1:/{}$'.format(file_name),
             '{}'.format(DEFAULT_PROMPT),
             1],
            [r'^bof primary-config cf1:/config.cfg$',
             '{}'.format(DEFAULT_PROMPT),
             1],
            ['^bof save$',
             'Writing BOF to cf1:/bof.cfg ... OK\nCompleted.\n{}'.format(DEFAULT_PROMPT),
             2],
            ['^admin reboot now$',
             [Exception(),
              '{}'.format(DEFAULT_PROMPT)],
             2],  # it called one time, but next time doesn't called the send_line command
        ])
        recv_mock.side_effect = emu.receive_all
        send_mock.side_effect = emu.send_line

        path = '{}/{}'.format(ftp, file_name)
        self.runner.restore(path, 'running', 'override')

        emu.check_calls()