def get_path(self, path=''):
        if not path:
            host = get_attribute_by_name('Backup Location')
            if ':' not in host:
                scheme = get_attribute_by_name('Backup Type')
                scheme = re.sub('(:|/+).*$', '', scheme, re.DOTALL)
                host = re.sub('^/+', '', host)
                host = '{}://{}'.format(scheme, host)
            path = host

        url = UrlParser.parse_url(path)
        if UrlParser.SCHEME not in url or not url[UrlParser.SCHEME]:
            raise Exception('ConfigurationOperations', "Backup Type is wrong or empty")

        if url[UrlParser.SCHEME].lower() in self.AUTHORIZATION_REQUIRED_STORAGES:
            if UrlParser.USERNAME not in url or not url[UrlParser.USERNAME]:
                url[UrlParser.USERNAME] = get_attribute_by_name('Backup User')
            if UrlParser.PASSWORD not in url or not url[UrlParser.PASSWORD]:
                url[UrlParser.PASSWORD] = decrypt_password(get_attribute_by_name('Backup Password'))
        try:
            result = UrlParser.build_url(url)
        except Exception as e:
            self.logger.error('Failed to build url: {}'.format(e))
            raise Exception('ConfigurationOperations', 'Failed to build path url to remote host')
        return result
    def save(self, folder_path=None, configuration_type='running', vrf_management_name=None):
        """Backup 'startup-config' or 'running-config' from device to provided file_system [ftp|tftp]
        Also possible to backup config to localhost
        :param folder_path:  tftp/ftp server where file be saved
        :param configuration_type: what file to backup
        :return: status message / exception
        """

        expected_map = dict()

        full_path = self.get_path(folder_path)
        url = UrlParser.parse_url(full_path)
        password = url.get(UrlParser.PASSWORD)
        if password:
            expected_map = {r'[Pp]assword\s*:': lambda session: session.send_line(password)}
            url.pop(UrlParser.PASSWORD)
            url[UrlParser.NETLOC] = url[UrlParser.NETLOC].replace(':{}'.format(password), '')

        if not configuration_type:
            configuration_type = 'running'
        if not re.search('startup|running', configuration_type, re.IGNORECASE):
            raise Exception('EricssonConfigurationOperations', "Source filename must be 'Running' or" +
                            " 'Startup'!")

        system_name = re.sub('\s+', '_', self.resource_name)
        if len(system_name) > 23:
            system_name = system_name[:23]

        destination_filename = '{0}-{1}-{2}'.format(system_name, configuration_type.lower(), _get_time_stamp())

        self.logger.info('destination filename is {0}'.format(destination_filename))

        url[UrlParser.FILENAME] = destination_filename

        destination_file_path = UrlParser.build_url(url)

        expected_map['overwrite'] = lambda session: session.send_line('y')
        if 'startup' in configuration_type.lower():
            # startup_config_file = self.cli.send_command('show configuration | include boot')
            # match_startup_config_file = re.search('\w+\.\w+', startup_config_file)
            # if not match_startup_config_file:
            #     raise Exception('EricssonConfigurationOperations', 'no startup/boot configuration found')
            # startup_config = match_startup_config_file.group()
            # command = 'copy {0} {1}'.format(startup_config, destination_file)
            raise Exception('EricssonConfigurationOperations',
                            'There is no startup configuration for {0}'.format(self.resource_name))
        else:
            command = 'save configuration {0}'.format(destination_file_path)
        output = self.cli.send_command(command, expected_map=expected_map)
        is_downloaded = self._check_download_from_tftp(output)
        if is_downloaded[0]:
            self.logger.info('Save configuration completed.')
            return destination_filename
        else:
            self.logger.info('Save configuration failed with errors: {0}'.format(is_downloaded[1]))
            raise Exception('EricssonConfigurationOperations', 'Save configuration failed with errors:',
                            is_downloaded[1])
 def test_url_join(self):
     correct_url = 'ftp://*****:*****@google.com/folder1/file2'
     url = '/folder1/file2'
     parsed_url = UrlParser.parse_url(url)
     parsed_url[UrlParser.HOSTNAME] = 'google.com'
     parsed_url[UrlParser.SCHEME] = 'ftp'
     parsed_url[UrlParser.USERNAME] = 'user'
     parsed_url[UrlParser.PASSWORD] = 'pwd'
     result = UrlParser.build_url(parsed_url)
     self.assertIsNotNone(result)
     self.assertEqual(correct_url, result)